Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .llms-snapshots/llms-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6562,7 +6562,7 @@ typescript/hooks/├── src/│ ├── satellite/ # TypeScript
Here’s the actual TypeScript logic from `index.ts`:

```
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 } }); }});
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 } }); }});
```

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

```
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)); }}));
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)); }}));
```

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`.
Expand Down Expand Up @@ -7437,7 +7437,7 @@ This is useful if you want to:
Here's an example that calls another canister’s method and logs the result:

```
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); }});
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); }});
```

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`.
Expand Down Expand Up @@ -12635,7 +12635,7 @@ icrc1BalanceOf(params: { account: Account}): Promise<bigint>
#### Example:

```
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);};
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);};
```

### icrc1Transfer
Expand Down Expand Up @@ -12665,7 +12665,7 @@ icrc1Transfer(params: { args: TransferArgs}): Promise<TransferResult>
#### Example:

```
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); }};
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); }};
```

### icrc2TransferFrom
Expand Down Expand Up @@ -12698,7 +12698,7 @@ icrc2TransferFrom(params: { args: TransferFromArgs}): Promise<TransferFromResul
#### Example:

```
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); }};
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); }};
```

### icrc2Approve
Expand Down Expand Up @@ -12730,7 +12730,7 @@ icrc2Approve(params: { args: ApproveArgs}): Promise<ApproveResult>
#### Example:

```
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); }};
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); }};
```

---
Expand Down Expand Up @@ -12766,7 +12766,7 @@ notifyTopUp(params: { args: NotifyTopUpArgs}): Promise<NotifyTopUpResult>
#### Example:

```
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); }};
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); }};
```

---
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/functions/typescript/mutating-docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import {
encodeDocData,
setDocStore
} from "@junobuild/functions/sdk";
import { Principal } from "@dfinity/principal";
import { Principal } from "@icp-sdk/core/principal";

export const assertSetDoc = defineAssert<AssertSetDoc>({
collections: ["demo"],
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/nodejs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Suppose you want to fetch a document and export it to a file:
```typescript
import { getDoc } from "@junobuild/core";
import { defineRun } from "@junobuild/config";
import { jsonReplacer } from "@dfinity/utils";
import { jsonReplacer } from "@junobuild/utils";
import { writeFile } from "node:fs/promises";

export const onRun = defineRun(({ mode }) => ({
Expand Down
4 changes: 2 additions & 2 deletions docs/guides/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ import Call from "./components/functions/call.md";
```typescript
import { call } from "@junobuild/functions/ic-cdk";
import { defineHook, type OnSetDoc } from "@junobuild/functions";
import { IDL } from "@dfinity/candid";
import { Principal } from "@dfinity/principal";
import { IDL } from "@icp-sdk/core/candid";
import { Principal } from "@icp-sdk/core/principal";

// Define Candid types
const SubAccount = IDL.Vec(IDL.Nat8);
Expand Down
10 changes: 5 additions & 5 deletions docs/reference/functions/typescript/canisters.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ icrc1BalanceOf(params: {

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

export const onExecute = async () => {
const ledger = new IcrcLedgerCanister({
Expand Down Expand Up @@ -184,7 +184,7 @@ icrc1Transfer(params: {

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

export const onExecute = async () => {
const ledger = new IcrcLedgerCanister({
Expand Down Expand Up @@ -242,7 +242,7 @@ icrc2TransferFrom(params: {

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

export const onExecute = async () => {
const ledger = new IcrcLedgerCanister({
Expand Down Expand Up @@ -301,7 +301,7 @@ icrc2Approve(params: {

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

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

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