From e13e823964227be9b95f8217c1719bfcbdd7c3d0 Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Fri, 8 Nov 2024 10:03:51 -0800 Subject: [PATCH 1/3] bump agent-js for counter app --- archive/motoko/hello/src/hello/main.mo | 5 +- motoko/minimal-counter-dapp/package-lock.json | 75 +++++++++---------- .../src/minimal_dapp_backend/main.mo | 2 +- .../src/minimal_dapp_frontend/package.json | 6 +- .../src/minimal_dapp_frontend/vite.config.js | 1 + 5 files changed, 44 insertions(+), 45 deletions(-) diff --git a/archive/motoko/hello/src/hello/main.mo b/archive/motoko/hello/src/hello/main.mo index 524039dce..c262cd37e 100644 --- a/archive/motoko/hello/src/hello/main.mo +++ b/archive/motoko/hello/src/hello/main.mo @@ -1,7 +1,8 @@ -actor { +import Debug "mo:base/Debug"; +actor { public func greet(name : Text) : async Text { + Debug.print("Received " # name); return "Hello, " # name # "!"; }; - }; diff --git a/motoko/minimal-counter-dapp/package-lock.json b/motoko/minimal-counter-dapp/package-lock.json index c158d129d..2773fe021 100644 --- a/motoko/minimal-counter-dapp/package-lock.json +++ b/motoko/minimal-counter-dapp/package-lock.json @@ -147,42 +147,6 @@ "node": ">=6.9.0" } }, - "node_modules/@dfinity/agent": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/@dfinity/agent/-/agent-1.4.0.tgz", - "integrity": "sha512-/zgGajZpxtbu+kLXtFx2e9V2+HbMUjrtGWx9ZEwtVwhVxKgVi/2kGQpFRPEDFJ461V7wdTwCig4OkMxVU4shTw==", - "license": "Apache-2.0", - "dependencies": { - "@noble/curves": "^1.4.0", - "@noble/hashes": "^1.3.1", - "base64-arraybuffer": "^0.2.0", - "borc": "^2.1.1", - "buffer": "^6.0.3", - "simple-cbor": "^0.4.1" - }, - "peerDependencies": { - "@dfinity/candid": "^1.4.0", - "@dfinity/principal": "^1.4.0" - } - }, - "node_modules/@dfinity/candid": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/@dfinity/candid/-/candid-1.4.0.tgz", - "integrity": "sha512-PsTJVn63ZM4A/6Xs5coI0zMFevSwJ8hcyh38LdH/92n6wi9UOTis1yc4qL5MZvvRCUAD0c3rVjELL+49E9sPyA==", - "license": "Apache-2.0", - "peerDependencies": { - "@dfinity/principal": "^1.4.0" - } - }, - "node_modules/@dfinity/principal": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-1.4.0.tgz", - "integrity": "sha512-SuTBVlc71ub89ji0WN5/T100zUG2uIMn5x4+We4vS4nJ0R3/Xt89XJsHepjd5SQTSQPOvP7eQ+S8cQKWRz/RkA==", - "license": "Apache-2.0", - "dependencies": { - "@noble/hashes": "^1.3.1" - } - }, "node_modules/@esbuild/android-arm": { "version": "0.18.20", "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.18.20.tgz", @@ -2378,9 +2342,9 @@ "src/minimal_dapp_frontend": { "version": "0.0.0", "dependencies": { - "@dfinity/agent": "^1.4.0", - "@dfinity/candid": "^1.4.0", - "@dfinity/principal": "^1.4.0", + "@dfinity/agent": "^2.1.3", + "@dfinity/candid": "^2.1.3", + "@dfinity/principal": "^2.1.3", "lit-html": "^2.8.0" }, "devDependencies": { @@ -2393,6 +2357,39 @@ "vite-plugin-environment": "^1.1.3", "vitest": "^0.32.2" } + }, + "src/minimal_dapp_frontend/node_modules/@dfinity/agent": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@dfinity/agent/-/agent-2.1.3.tgz", + "integrity": "sha512-4XmqhFR3GQSUrmx7lMFx7DyHEhFkM6nz4O9FeYJ/WpkmPe8tulKaAfgWbWdTSCjbd8meCgKVHo+QYj+JHXagcw==", + "dependencies": { + "@noble/curves": "^1.4.0", + "@noble/hashes": "^1.3.1", + "base64-arraybuffer": "^0.2.0", + "borc": "^2.1.1", + "buffer": "^6.0.3", + "simple-cbor": "^0.4.1" + }, + "peerDependencies": { + "@dfinity/candid": "^2.1.3", + "@dfinity/principal": "^2.1.3" + } + }, + "src/minimal_dapp_frontend/node_modules/@dfinity/candid": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@dfinity/candid/-/candid-2.1.3.tgz", + "integrity": "sha512-Asn7AfydLhhk7E5z9oW+5UL6ne11gxFlYTxHuhrIc7FdqYlM5Flcq1Wfg9EzRa6Btdol3w58Bcph7Brwh1bcIQ==", + "peerDependencies": { + "@dfinity/principal": "^2.1.3" + } + }, + "src/minimal_dapp_frontend/node_modules/@dfinity/principal": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-2.1.3.tgz", + "integrity": "sha512-HtiAfZcs+ToPYFepVJdFlorIfPA56KzC6J97ZuH2lGNMTAfJA+NEBzLe476B4wVCAwZ0TiGJ27J4ks9O79DFEg==", + "dependencies": { + "@noble/hashes": "^1.3.1" + } } } } diff --git a/motoko/minimal-counter-dapp/src/minimal_dapp_backend/main.mo b/motoko/minimal-counter-dapp/src/minimal_dapp_backend/main.mo index ac86d232b..7367f8bc3 100644 --- a/motoko/minimal-counter-dapp/src/minimal_dapp_backend/main.mo +++ b/motoko/minimal-counter-dapp/src/minimal_dapp_backend/main.mo @@ -1,6 +1,6 @@ actor { - var counter : Nat = 0; + stable var counter : Nat = 0; public func increment() : async Nat { counter += 1; diff --git a/motoko/minimal-counter-dapp/src/minimal_dapp_frontend/package.json b/motoko/minimal-counter-dapp/src/minimal_dapp_frontend/package.json index 7314509c0..f72f81b38 100644 --- a/motoko/minimal-counter-dapp/src/minimal_dapp_frontend/package.json +++ b/motoko/minimal-counter-dapp/src/minimal_dapp_frontend/package.json @@ -21,9 +21,9 @@ "vitest": "^0.32.2" }, "dependencies": { - "@dfinity/agent": "^1.4.0", - "@dfinity/candid": "^1.4.0", - "@dfinity/principal": "^1.4.0", + "@dfinity/agent": "^2.1.3", + "@dfinity/candid": "^2.1.3", + "@dfinity/principal": "^2.1.3", "lit-html": "^2.8.0" } } diff --git a/motoko/minimal-counter-dapp/src/minimal_dapp_frontend/vite.config.js b/motoko/minimal-counter-dapp/src/minimal_dapp_frontend/vite.config.js index 18ceb500c..63f10f4cc 100644 --- a/motoko/minimal-counter-dapp/src/minimal_dapp_frontend/vite.config.js +++ b/motoko/minimal-counter-dapp/src/minimal_dapp_frontend/vite.config.js @@ -30,6 +30,7 @@ export default defineConfig({ environment("all", { prefix: "DFX_" }), ], resolve: { + dedupe: ['@dfinity/agent'], alias: [ { find: "declarations", From c55be101081c0813bcca3f684741c784fe4a2377 Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Fri, 8 Nov 2024 10:05:52 -0800 Subject: [PATCH 2/3] fix --- archive/motoko/hello/src/hello/main.mo | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/archive/motoko/hello/src/hello/main.mo b/archive/motoko/hello/src/hello/main.mo index c262cd37e..524039dce 100644 --- a/archive/motoko/hello/src/hello/main.mo +++ b/archive/motoko/hello/src/hello/main.mo @@ -1,8 +1,7 @@ -import Debug "mo:base/Debug"; - actor { + public func greet(name : Text) : async Text { - Debug.print("Received " # name); return "Hello, " # name # "!"; }; + }; From f1916efb8e9a2ea541442759a0b017973bb5b0bd Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:13:10 -0800 Subject: [PATCH 3/3] use ordered map in superhero example --- motoko/superheroes/src/superheroes/Main.mo | 61 +++++++--------------- 1 file changed, 19 insertions(+), 42 deletions(-) diff --git a/motoko/superheroes/src/superheroes/Main.mo b/motoko/superheroes/src/superheroes/Main.mo index 31362db50..96d140641 100644 --- a/motoko/superheroes/src/superheroes/Main.mo +++ b/motoko/superheroes/src/superheroes/Main.mo @@ -1,6 +1,6 @@ import List "mo:base/List"; import Option "mo:base/Option"; -import Trie "mo:base/Trie"; +import Map "mo:base/OrderedMap"; import Nat32 "mo:base/Nat32"; actor Superheroes { @@ -15,7 +15,7 @@ actor Superheroes { // The type of a superhero. public type Superhero = { name : Text; - superpowers : List.List; + superpowers : List.List }; /** @@ -23,10 +23,11 @@ actor Superheroes { */ // The next available superhero identifier. - private stable var next : SuperheroId = 0; + stable var next : SuperheroId = 0; // The superhero data store. - private stable var superheroes : Trie.Trie = Trie.empty(); + let Ops = Map.Make(Nat32.compare); + stable var map : Map.Map = Ops.empty(); /** * High-Level API @@ -36,57 +37,33 @@ actor Superheroes { public func create(superhero : Superhero) : async SuperheroId { let superheroId = next; next += 1; - superheroes := Trie.replace( - superheroes, - key(superheroId), - Nat32.equal, - ?superhero, - ).0; - return superheroId; + map := Ops.put(map, superheroId, superhero); + return superheroId }; // Read a superhero. public query func read(superheroId : SuperheroId) : async ?Superhero { - let result = Trie.find(superheroes, key(superheroId), Nat32.equal); - return result; + let result = Ops.get(map, superheroId); + return result }; // Update a superhero. public func update(superheroId : SuperheroId, superhero : Superhero) : async Bool { - let result = Trie.find(superheroes, key(superheroId), Nat32.equal); - let exists = Option.isSome(result); + let (result, old_value) = Ops.replace(map, superheroId, superhero); + let exists = Option.isSome(old_value); if (exists) { - superheroes := Trie.replace( - superheroes, - key(superheroId), - Nat32.equal, - ?superhero, - ).0; + map := result }; - return exists; + return exists }; // Delete a superhero. public func delete(superheroId : SuperheroId) : async Bool { - let result = Trie.find(superheroes, key(superheroId), Nat32.equal); - let exists = Option.isSome(result); + let (result, old_value) = Ops.remove(map, superheroId); + let exists = Option.isSome(old_value); if (exists) { - superheroes := Trie.replace( - superheroes, - key(superheroId), - Nat32.equal, - null, - ).0; + map := result }; - return exists; - }; - - /** - * Utilities - */ - - // Create a trie key from a superhero identifier. - private func key(x : SuperheroId) : Trie.Key { - return { hash = x; key = x }; - }; -}; + return exists + } +}