Skip to content

Commit 0f2f96f

Browse files
Merge branch 'main' into docs/e2e-no-internet-identity
2 parents e17fcb8 + 192b301 commit 0f2f96f

5 files changed

Lines changed: 17 additions & 17 deletions

File tree

.llms-snapshots/llms-full.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6562,7 +6562,7 @@ typescript/hooks/├── src/│ ├── satellite/ # TypeScript
65626562
Here’s the actual TypeScript logic from `index.ts`:
65636563

65646564
```
6565-
import { type AssertSetDoc, defineAssert, defineHook, type OnSetDoc} from "@junobuild/functions";import { PersonData, PersonDataSchema } from "../types";import { decodeDocData, encodeDocData, setDocStore} from "@junobuild/functions/sdk";import { Principal } from "@dfinity/principal";export const assertSetDoc = defineAssert<AssertSetDoc>({ collections: ["demo"], assert: (context) => { // We validate that the data submitted for create or update matches the expected schema. const person = decodeDocData<PersonData>(context.data.data.proposed.data); PersonDataSchema.parse(person); }});export const onSetDoc = defineHook<OnSetDoc>({ collections: ["demo"], run: async (context) => { const { caller, data: { key, collection, data: { after: currentDoc } } } = context; // We decode the new data saved in the Datastore because it holds those as blob. const person = decodeDocData<PersonData>(currentDoc.data); // Some console.log for demo purpose console.log( "[on_set_doc] Caller:", Principal.fromUint8Array(caller).toText() ); console.log("[on_set_doc] Collection:", collection); console.log("[on_set_doc] Data:", person.principal, person.value); // We update the document's data that was saved in the Datastore with the call from the frontend dapp. const { hello, ...rest } = person; const updatePerson = { ...rest, hello: `${hello} checked`, yolo: false }; // We encode the data back to blob. const updateData = encodeDocData(updatePerson); // We save the document for the same caller as the one who triggered the original on_set_doc, in the same collection with the same key as well. setDocStore({ caller: caller, collection, key, doc: { version: currentDoc.version, data: updateData } }); }});
6565+
import { type AssertSetDoc, defineAssert, defineHook, type OnSetDoc} from "@junobuild/functions";import { PersonData, PersonDataSchema } from "../types";import { decodeDocData, encodeDocData, setDocStore} from "@junobuild/functions/sdk";import { Principal } from "@icp-sdk/core/principal";export const assertSetDoc = defineAssert<AssertSetDoc>({ collections: ["demo"], assert: (context) => { // We validate that the data submitted for create or update matches the expected schema. const person = decodeDocData<PersonData>(context.data.data.proposed.data); PersonDataSchema.parse(person); }});export const onSetDoc = defineHook<OnSetDoc>({ collections: ["demo"], run: async (context) => { const { caller, data: { key, collection, data: { after: currentDoc } } } = context; // We decode the new data saved in the Datastore because it holds those as blob. const person = decodeDocData<PersonData>(currentDoc.data); // Some console.log for demo purpose console.log( "[on_set_doc] Caller:", Principal.fromUint8Array(caller).toText() ); console.log("[on_set_doc] Collection:", collection); console.log("[on_set_doc] Data:", person.principal, person.value); // We update the document's data that was saved in the Datastore with the call from the frontend dapp. const { hello, ...rest } = person; const updatePerson = { ...rest, hello: `${hello} checked`, yolo: false }; // We encode the data back to blob. const updateData = encodeDocData(updatePerson); // We save the document for the same caller as the one who triggered the original on_set_doc, in the same collection with the same key as well. setDocStore({ caller: caller, collection, key, doc: { version: currentDoc.version, data: updateData } }); }});
65666566
```
65676567

65686568
**Explanation:**
@@ -7077,7 +7077,7 @@ juno run --src ./my-task.ts
70777077
Suppose you want to fetch a document and export it to a file:
70787078

70797079
```
7080-
import { getDoc } from "@junobuild/core";import { defineRun } from "@junobuild/config";import { jsonReplacer } from "@dfinity/utils";import { writeFile } from "node:fs/promises";export const onRun = defineRun(({ mode }) => ({ run: async (context) => { const key = mode === "staging" ? "123" : "456"; const doc = await getDoc({ collection: "demo", key, satellite: context }); await writeFile("./mydoc.json", JSON.stringify(doc, jsonReplacer, 2)); }}));
7080+
import { getDoc } from "@junobuild/core";import { defineRun } from "@junobuild/config";import { jsonReplacer } from "@junobuild/utils";import { writeFile } from "node:fs/promises";export const onRun = defineRun(({ mode }) => ({ run: async (context) => { const key = mode === "staging" ? "123" : "456"; const doc = await getDoc({ collection: "demo", key, satellite: context }); await writeFile("./mydoc.json", JSON.stringify(doc, jsonReplacer, 2)); }}));
70817081
```
70827082

70837083
This script retrieves a document using `getDoc`, selects the document ID based on the current `mode` (for example staging or production), and writes the result to `mydoc.json`, using `jsonReplacer` to handle types not supported by JSON such as `BigInt`.
@@ -7437,7 +7437,7 @@ This is useful if you want to:
74377437
Here's an example that calls another canister’s method and logs the result:
74387438

74397439
```
7440-
import { call } from "@junobuild/functions/ic-cdk";import { defineHook, type OnSetDoc } from "@junobuild/functions";import { IDL } from "@dfinity/candid";import { Principal } from "@dfinity/principal";// Define Candid typesconst SubAccount = IDL.Vec(IDL.Nat8);const Account = IDL.Record({ owner: IDL.Principal, subaccount: IDL.Opt(SubAccount)});const Icrc1Tokens = IDL.Nat;// Define TypeScript interfacesexport type SubAccountType = Uint8Array | number[];export interface AccountType { owner: Principal; subaccount: [] | [SubAccountType];}export type Icrc1TokensType = bigint;// Define the onSetDoc hookexport const onSetDoc = defineHook<OnSetDoc>({ collections: ["notes"], run: async (context) => { const account: AccountType = { owner: Principal.from(context.caller), subaccount: [] }; const icpLedgerId = Principal.fromText("ryjl3-tyaaa-aaaaa-aaaba-cai"); const balance = await call<Icrc1TokensType>({ canisterId: icpLedgerId, method: "icrc1_balance_of", args: [[Account, account]], result: Icrc1Tokens }); console.log("Balance:", balance); }});
7440+
import { call } from "@junobuild/functions/ic-cdk";import { defineHook, type OnSetDoc } from "@junobuild/functions";import { IDL } from "@icp-sdk/core/candid";import { Principal } from "@icp-sdk/core/principal";// Define Candid typesconst SubAccount = IDL.Vec(IDL.Nat8);const Account = IDL.Record({ owner: IDL.Principal, subaccount: IDL.Opt(SubAccount)});const Icrc1Tokens = IDL.Nat;// Define TypeScript interfacesexport type SubAccountType = Uint8Array | number[];export interface AccountType { owner: Principal; subaccount: [] | [SubAccountType];}export type Icrc1TokensType = bigint;// Define the onSetDoc hookexport const onSetDoc = defineHook<OnSetDoc>({ collections: ["notes"], run: async (context) => { const account: AccountType = { owner: Principal.from(context.caller), subaccount: [] }; const icpLedgerId = Principal.fromText("ryjl3-tyaaa-aaaaa-aaaba-cai"); const balance = await call<Icrc1TokensType>({ canisterId: icpLedgerId, method: "icrc1_balance_of", args: [[Account, account]], result: Icrc1Tokens }); console.log("Balance:", balance); }});
74417441
```
74427442

74437443
This example performs a call to the ICP Ledger canister using the `icrc1_balance_of` method to retrieve the token balance for the calling user. The result is printed to the log using `console.log`.
@@ -12635,7 +12635,7 @@ icrc1BalanceOf(params: { account: Account}): Promise<bigint>
1263512635
#### Example:
1263612636

1263712637
```
12638-
import { IcrcLedgerCanister } from "@junobuild/functions/canisters/ledger/icrc";import { Principal } from "@dfinity/principal";export const onExecute = async () => { const ledger = new IcrcLedgerCanister({ canisterId: "your-icrc-ledger-canister-id" }); const balance = await ledger.icrc1BalanceOf({ account: { owner: Principal.fromText("user-principal") } }); console.log("Balance:", balance);};
12638+
import { IcrcLedgerCanister } from "@junobuild/functions/canisters/ledger/icrc";import { Principal } from "@icp-sdk/core/principal";export const onExecute = async () => { const ledger = new IcrcLedgerCanister({ canisterId: "your-icrc-ledger-canister-id" }); const balance = await ledger.icrc1BalanceOf({ account: { owner: Principal.fromText("user-principal") } }); console.log("Balance:", balance);};
1263912639
```
1264012640

1264112641
### icrc1Transfer
@@ -12665,7 +12665,7 @@ icrc1Transfer(params: { args: TransferArgs}): Promise<TransferResult>
1266512665
#### Example:
1266612666

1266712667
```
12668-
import { IcrcLedgerCanister } from "@junobuild/functions/canisters/ledger/icrc";import { Principal } from "@dfinity/principal";export const onExecute = async () => { const ledger = new IcrcLedgerCanister({ canisterId: "your-icrc-ledger-canister-id" }); const result = await ledger.icrc1Transfer({ args: { to: { owner: Principal.fromText("recipient-principal") }, amount: 1_000_000n, fee: 10_000n } }); if ("Ok" in result) { console.log("Transfer successful, block index:", result.Ok); } else { console.error("Transfer failed:", result.Err); }};
12668+
import { IcrcLedgerCanister } from "@junobuild/functions/canisters/ledger/icrc";import { Principal } from "@icp-sdk/core/principal";export const onExecute = async () => { const ledger = new IcrcLedgerCanister({ canisterId: "your-icrc-ledger-canister-id" }); const result = await ledger.icrc1Transfer({ args: { to: { owner: Principal.fromText("recipient-principal") }, amount: 1_000_000n, fee: 10_000n } }); if ("Ok" in result) { console.log("Transfer successful, block index:", result.Ok); } else { console.error("Transfer failed:", result.Err); }};
1266912669
```
1267012670

1267112671
### icrc2TransferFrom
@@ -12698,7 +12698,7 @@ icrc2TransferFrom(params: { args: TransferFromArgs}): Promise<TransferFromResul
1269812698
#### Example:
1269912699

1270012700
```
12701-
import { IcrcLedgerCanister } from "@junobuild/functions/canisters/ledger/icrc";import { Principal } from "@dfinity/principal";export const onExecute = async () => { const ledger = new IcrcLedgerCanister({ canisterId: "your-icrc-ledger-canister-id" }); const result = await ledger.icrc2TransferFrom({ args: { from: { owner: Principal.fromText("source-principal") }, to: { owner: Principal.fromText("destination-principal") }, amount: 500_000n } }); if ("Ok" in result) { console.log("Transfer from successful, block index:", result.Ok); } else { console.error("Transfer from failed:", result.Err); }};
12701+
import { IcrcLedgerCanister } from "@junobuild/functions/canisters/ledger/icrc";import { Principal } from "@icp-sdk/core/principal";export const onExecute = async () => { const ledger = new IcrcLedgerCanister({ canisterId: "your-icrc-ledger-canister-id" }); const result = await ledger.icrc2TransferFrom({ args: { from: { owner: Principal.fromText("source-principal") }, to: { owner: Principal.fromText("destination-principal") }, amount: 500_000n } }); if ("Ok" in result) { console.log("Transfer from successful, block index:", result.Ok); } else { console.error("Transfer from failed:", result.Err); }};
1270212702
```
1270312703

1270412704
### icrc2Approve
@@ -12730,7 +12730,7 @@ icrc2Approve(params: { args: ApproveArgs}): Promise<ApproveResult>
1273012730
#### Example:
1273112731

1273212732
```
12733-
import { IcrcLedgerCanister } from "@junobuild/functions/canisters/ledger/icrc";import { Principal } from "@dfinity/principal";export const onExecute = async () => { const ledger = new IcrcLedgerCanister({ canisterId: "your-icrc-ledger-canister-id" }); const result = await ledger.icrc2Approve({ args: { spender: { owner: Principal.fromText("spender-principal") }, amount: 1_000_000n } }); if ("Ok" in result) { console.log("Approval successful, block index:", result.Ok); } else { console.error("Approval failed:", result.Err); }};
12733+
import { IcrcLedgerCanister } from "@junobuild/functions/canisters/ledger/icrc";import { Principal } from "@icp-sdk/core/principal";export const onExecute = async () => { const ledger = new IcrcLedgerCanister({ canisterId: "your-icrc-ledger-canister-id" }); const result = await ledger.icrc2Approve({ args: { spender: { owner: Principal.fromText("spender-principal") }, amount: 1_000_000n } }); if ("Ok" in result) { console.log("Approval successful, block index:", result.Ok); } else { console.error("Approval failed:", result.Err); }};
1273412734
```
1273512735

1273612736
---
@@ -12766,7 +12766,7 @@ notifyTopUp(params: { args: NotifyTopUpArgs}): Promise<NotifyTopUpResult>
1276612766
#### Example:
1276712767

1276812768
```
12769-
import { CMCCanister } from "@junobuild/functions/canisters/cmc";import { IcpLedgerCanister } from "@junobuild/functions/canisters/ledger/icp";import { Principal } from "@dfinity/principal";export const onExecute = async () => { // Step 1: Send ICP to the CMC top-up account const ledger = new IcpLedgerCanister(); const transferResult = await ledger.transfer({ args: { to: cmcTopUpAccountIdentifier, // Uint8Array amount: { e8s: 100_000_000n }, // 1 ICP fee: { e8s: 10_000n }, memo: 0n } }); if ("Err" in transferResult) { console.error("Transfer failed:", transferResult.Err); return; } const blockIndex = transferResult.Ok; // Step 2: Notify the CMC to convert the ICP to cycles const cmc = new CMCCanister(); const notifyResult = await cmc.notifyTopUp({ args: { block_index: blockIndex, canister_id: Principal.fromText("your-canister-id") } }); if ("Ok" in notifyResult) { console.log("Cycles deposited:", notifyResult.Ok); } else { console.error("Notify failed:", notifyResult.Err); }};
12769+
import { CMCCanister } from "@junobuild/functions/canisters/cmc";import { IcpLedgerCanister } from "@junobuild/functions/canisters/ledger/icp";import { Principal } from "@icp-sdk/core/principal";export const onExecute = async () => { // Step 1: Send ICP to the CMC top-up account const ledger = new IcpLedgerCanister(); const transferResult = await ledger.transfer({ args: { to: cmcTopUpAccountIdentifier, // Uint8Array amount: { e8s: 100_000_000n }, // 1 ICP fee: { e8s: 10_000n }, memo: 0n } }); if ("Err" in transferResult) { console.error("Transfer failed:", transferResult.Err); return; } const blockIndex = transferResult.Ok; // Step 2: Notify the CMC to convert the ICP to cycles const cmc = new CMCCanister(); const notifyResult = await cmc.notifyTopUp({ args: { block_index: blockIndex, canister_id: Principal.fromText("your-canister-id") } }); if ("Ok" in notifyResult) { console.log("Cycles deposited:", notifyResult.Ok); } else { console.error("Notify failed:", notifyResult.Err); }};
1277012770
```
1277112771

1277212772
---

docs/examples/functions/typescript/mutating-docs.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import {
7171
encodeDocData,
7272
setDocStore
7373
} from "@junobuild/functions/sdk";
74-
import { Principal } from "@dfinity/principal";
74+
import { Principal } from "@icp-sdk/core/principal";
7575

7676
export const assertSetDoc = defineAssert<AssertSetDoc>({
7777
collections: ["demo"],

docs/guides/nodejs.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Suppose you want to fetch a document and export it to a file:
6262
```typescript
6363
import { getDoc } from "@junobuild/core";
6464
import { defineRun } from "@junobuild/config";
65-
import { jsonReplacer } from "@dfinity/utils";
65+
import { jsonReplacer } from "@junobuild/utils";
6666
import { writeFile } from "node:fs/promises";
6767

6868
export const onRun = defineRun(({ mode }) => ({

docs/guides/typescript.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ import Call from "./components/functions/call.md";
252252
```typescript
253253
import { call } from "@junobuild/functions/ic-cdk";
254254
import { defineHook, type OnSetDoc } from "@junobuild/functions";
255-
import { IDL } from "@dfinity/candid";
256-
import { Principal } from "@dfinity/principal";
255+
import { IDL } from "@icp-sdk/core/candid";
256+
import { Principal } from "@icp-sdk/core/principal";
257257

258258
// Define Candid types
259259
const SubAccount = IDL.Vec(IDL.Nat8);

docs/reference/functions/typescript/canisters.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ icrc1BalanceOf(params: {
137137

138138
```typescript
139139
import { IcrcLedgerCanister } from "@junobuild/functions/canisters/ledger/icrc";
140-
import { Principal } from "@dfinity/principal";
140+
import { Principal } from "@icp-sdk/core/principal";
141141

142142
export const onExecute = async () => {
143143
const ledger = new IcrcLedgerCanister({
@@ -184,7 +184,7 @@ icrc1Transfer(params: {
184184

185185
```typescript
186186
import { IcrcLedgerCanister } from "@junobuild/functions/canisters/ledger/icrc";
187-
import { Principal } from "@dfinity/principal";
187+
import { Principal } from "@icp-sdk/core/principal";
188188

189189
export const onExecute = async () => {
190190
const ledger = new IcrcLedgerCanister({
@@ -242,7 +242,7 @@ icrc2TransferFrom(params: {
242242

243243
```typescript
244244
import { IcrcLedgerCanister } from "@junobuild/functions/canisters/ledger/icrc";
245-
import { Principal } from "@dfinity/principal";
245+
import { Principal } from "@icp-sdk/core/principal";
246246

247247
export const onExecute = async () => {
248248
const ledger = new IcrcLedgerCanister({
@@ -301,7 +301,7 @@ icrc2Approve(params: {
301301

302302
```typescript
303303
import { IcrcLedgerCanister } from "@junobuild/functions/canisters/ledger/icrc";
304-
import { Principal } from "@dfinity/principal";
304+
import { Principal } from "@icp-sdk/core/principal";
305305

306306
export const onExecute = async () => {
307307
const ledger = new IcrcLedgerCanister({
@@ -362,7 +362,7 @@ notifyTopUp(params: {
362362
```typescript
363363
import { CMCCanister } from "@junobuild/functions/canisters/cmc";
364364
import { IcpLedgerCanister } from "@junobuild/functions/canisters/ledger/icp";
365-
import { Principal } from "@dfinity/principal";
365+
import { Principal } from "@icp-sdk/core/principal";
366366

367367
export const onExecute = async () => {
368368
// Step 1: Send ICP to the CMC top-up account

0 commit comments

Comments
 (0)