Skip to content

Commit

Permalink
Merge pull request #3386 from dfinity/update-call
Browse files Browse the repository at this point in the history
fix: call examples
  • Loading branch information
jessiemongeon1 authored Aug 23, 2024
2 parents f56918f + 70127cc commit b1f5c3b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 120 deletions.
110 changes: 14 additions & 96 deletions docs/developer-docs/smart-contracts/call/arguments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,18 @@ The following example defines an array of numbers and a function that returns th

Motoko differentiates between immutable arrays, which cannot be altered, and mutable arrays, which can be modified.

To declare an immutable array (default):

```motoko
actor {
let a : [Nat] = [0, 1, 2, 3];
}
```

To declare a mutable array, the `[var _]` syntax is used:
import Nat "mo:base/Nat";
```motoko
actor {
let a : [var Nat] = [var 0, 1, 2, 3] ;
}
```

To declare a function that returns an array:

```motoko
import Nat "mo:base/Nat"
let numbers1 : [Nat] = [1, 2, 3, 4, 5, 6, 7]; // Immutable array
let numbers2 : [var Nat] = [var 1, 2, 3, 4, 5, 6, 7] ; // Mutable array
actor {
let a : [Nat] = [0, 1, 2, 3];
public func get_numbers(a: [Nat]) : async [Nat] {
public func get_numbers(numbers2: [Nat]) : [Nat] {
return a;
}
}
```

The `Array` Motoko base library provides utility functions on arrays. To learn more about the `Array` Motoko base library, refer to the [Motoko base library reference on Array](/docs/current/motoko/main/base/Array) and the [Motoko language quick reference on arrays](/docs/current/motoko/main/reference/language-manual#arrays).
Expand All @@ -68,7 +52,7 @@ The `Array` Motoko base library provides utility functions on arrays. To learn m
Rust uses the `vec` type to represent vectors (sequences, lists, arrays).

```rust
let numbers = vec![0, 1, 2, 3 ];
let numbers = vec![1, 2, 3, 4, 5, 6, 7 ];
#[query]
fn get_numbers(numbers: Vec<u32>) -> Vec<u32> {
numbers
Expand All @@ -84,93 +68,27 @@ Azle refers to the `Vec` type to represent the equivalent of an `array` in TypeS
```typescript
import { IDL, query } from 'azle';

const Day = IDL.Variant({
Sun: IDL.Text,
Mon: IDL.Text,
Tue: IDL.Text,
Wed: IDL.Text,
Thu: IDL.Text,
Fri: IDL.Text,
Sat: IDL.Text
});
type Day =
| { Sun: string }
| { Mon: string }
| { Tue: string }
| { Wed: string }
| { Thu: string }
| { Fri: string }
| { Sat: string };

export default class {
@query([Day], IDL.Text)
getText(day: Day): string {
if ('Sun' in day) {
return day.Sun;
}

if ('Mon' in day) {
return day.Mon;
}

if ('Tue' in day) {
return day.Tue;
}

if ('Wed' in day) {
return day.Wed;
}

if ('Thu' in day) {
return day.Thu;
}

if ('Fri' in day) {
return day.Fri;
}

if ('Sat' in day) {
return day.Sat;
}

throw new Error(`Variant "Day" has unknown tag`);
@query([], IDL.Vec(IDL.Nat8))
get_numbers(): Nat {
return [1, 2, 3, 4, 5, 6, 7];
}
}
```

To learn more about variants in Typescript via Azle, refer to [the Azle code](https://github.com/demergent-labs/azle/blob/main/tests/end_to_end/candid_rpc/class_syntax/complex_types/src/candid_types.ts#L3).

</AdornedTab>

<AdornedTab value={"python"} label="Python" endAdornment={<BetaChip />}>

```python
from kybra import nat32, Variant, query
from kybra import int32, query, Vec

class Day(Variant, total=False):
Sun: str
Mon: str
Tue: str
Wed: str
Thu: str
Fri: str
Sat: str

@query
def get_text(d: Day) -> str:
if d == Day.Sun:
return "Sunday"
elif d == Day.Mon:
return "Monday"
elif d == Day.Tue:
return "Tuesday"
elif d == Day.Wed:
return "Wednesday"
elif d == Day.Thu:
return "Thursday"
elif d == Day.Fri:
return "Friday"
elif d == Day.Sat:
return "Saturday"
def get_numbers() -> Vec[int32]:
return [1, 2, 3, 4, 5, 6, 7]
```

To learn more about variants in Python in Kybra, refer to [the Kybra book reference on variants](https://demergent-labs.github.io/kybra/reference/candid/variant.html).
Expand All @@ -189,7 +107,7 @@ Assuming you have a method named `get_text` that accepts a `Variant` parameter,
```bash
dfx canister call canister_name get_text '(variant {Sun})'
```
To learn more about calling a method from a canister in bash, refer to the DFX reference on the [dfx canister call command](/docs/current/developer-docs/developer-tools/cli-tools/cli-reference/dfx-canister#dfx-canister-call).
To learn more about calling a method from a canister in bash, refer to the `dfx` reference on the [dfx canister call command](/docs/current/developer-docs/developer-tools/cli-tools/cli-reference/dfx-canister#dfx-canister-call).

For additional examples, refer to [type variant in the Candid Reference](/docs/current/references/candid-ref#type-variant--n--t--)

Expand Down
67 changes: 43 additions & 24 deletions docs/developer-docs/smart-contracts/call/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ Update calls are executed on all nodes of a <GlossaryTooltip>subnet</GlossaryToo
<TabItem value="motoko" label="Motoko" default>

```motoko
actor countCharacters {
public func test(text : Text) : async Bool {
let size = Text.size(text);
return size % 2 == 0;
};
actor Counter {
stable var counter = 0;
public func inc() : async () {
counter += 1;
};
};
```

Expand All @@ -48,9 +50,16 @@ actor countCharacters {
<TabItem value="rust" label="Rust">

```rust
use candid::types::number::Nat;
use std::cell::RefCell;

thread_local! {
static COUNTER: RefCell<Nat> = RefCell::new(Nat::from(0_u32));
}

#[ic_cdk_macros::update]
fn increment() {
COUNTER.with(|counter| *counter.borrow_mut() += 1);
fn inc() {
COUNTER.with(|counter| *counter.borrow_mut() += 1_u32);
}
```

Expand All @@ -62,13 +71,13 @@ fn increment() {
import { IDL, update } from 'azle';

export default class {
count: bigint = 0n;
counter: bigint = 0n;

@update([], IDL.Nat64)
incrementCount(): bigint {
this.count += 1n;
inc(): bigint {
this.counter += 1n;

return this.count;
return this.counter;
}
}
```
Expand All @@ -78,10 +87,19 @@ export default class {
<AdornedTab value={"python"} label="Python" endAdornment={<BetaChip />}>

```python
from kybra import nat64, query, update

counter: nat64 = 0

@query
def count() -> nat64:
return counter

@update
def set_message(new_message: str) -> void:
global message
message = new_message
def inc() -> nat64:
global counter
counter += 1
return counter
```

</AdornedTab>
Expand All @@ -102,11 +120,12 @@ The downside of query calls is that the response is not trusted since it's comin
<TabItem value="motoko" label="Motoko" default>

```motoko
actor Echo {
actor {
public query func say(phrase : Text) : async Text {
return phrase;
public func greet(name : Text) : async Text {
return "Hello, " # name # "!";
};
};
```

Expand All @@ -130,11 +149,9 @@ fn greet(name: String) -> String {
import { IDL, query } from 'azle';

export default class {
count: bigint = 0n;

@query([], IDL.Nat64)
readCount(): bigint {
return this.count;
@query([], IDL.Text)
greet(name){
return "Hello, {name}";
}
}
```
Expand All @@ -144,9 +161,11 @@ export default class {
<AdornedTab value={"python"} label="Python" endAdornment={<BetaChip />}>

```python
from kybra import query

@query
def get_message() -> str:
return message
def greet(name: str) -> str:
return f"Hello, {name}!"
```

</AdornedTab>
Expand Down

0 comments on commit b1f5c3b

Please sign in to comment.