Skip to content

Commit 5049a91

Browse files
Merge pull request #27 from multiversx/update-31
Extra examples: counted variadic parameters
2 parents d311d92 + a456ccc commit 5049a91

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

cookbook/contracts_04_interactions.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ let tx2 = interaction
5050
// ```
5151
import { AbiRegistry } from "@multiversx/sdk-core";
5252

53-
const abiRegistry = AbiRegistry.create({
53+
let abiRegistry = AbiRegistry.create({
5454
"endpoints": [
5555
{
5656
"name": "foobar",
@@ -84,6 +84,47 @@ let tx3 = contract.methods.doSomethingWithValue([1, 2, 3])
8484
.buildTransaction();
8585
// ```
8686

87+
// Now let's see an example using variadic arguments, as well:
88+
89+
// ```
90+
import { StringValue, VariadicValue } from "@multiversx/sdk-core";
91+
92+
abiRegistry = AbiRegistry.create({
93+
"endpoints": [
94+
{
95+
"name": "foobar",
96+
"inputs": [],
97+
"outputs": []
98+
},
99+
{
100+
"name": "doSomething",
101+
"inputs": [{
102+
"type": "counted-variadic<utf-8 string>"
103+
},
104+
{
105+
"type": "variadic<u64>"
106+
}],
107+
"outputs": []
108+
}
109+
]
110+
});
111+
112+
contract = new SmartContract({ address: contractAddress, abi: abiRegistry });
113+
114+
let tx4 = contract.methods.doSomething(
115+
[
116+
// Counted variadic must be explicitly typed // md-as-comment
117+
VariadicValue.fromItemsCounted(StringValue.fromUTF8("foo"), StringValue.fromUTF8("bar")),
118+
// Regular variadic can be implicitly typed // md-as-comment
119+
1, 2, 3
120+
])
121+
.withSender(addressOfAlice)
122+
.withNonce(45)
123+
.withGasLimit(20000000)
124+
.withChainID("D")
125+
.buildTransaction();
126+
// ```
127+
87128
// ### Transfer & execute
88129

89130
// Given an interaction:

cookbook/generate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pathlib import Path
22
from typing import List
33

4-
current_dir = Path(__file__).parent
4+
current_dir = Path(__file__).parent.absolute()
55

66
input_files = [
77
current_dir / "basic.js",
@@ -22,7 +22,7 @@
2222

2323
def main():
2424
for input_file in input_files:
25-
output_file = current_dir / "generated" / input_file.with_suffix(".md")
25+
output_file = current_dir / "generated" / input_file.with_suffix(".md").name
2626
render_file(input_file, output_file)
2727

2828

cookbook/generated/contracts_04_interactions.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Then, sign, broadcast `tx` and wait for its completion.
4747
```
4848
import { AbiRegistry } from "@multiversx/sdk-core";
4949
50-
const abiRegistry = AbiRegistry.create({
50+
let abiRegistry = AbiRegistry.create({
5151
"endpoints": [
5252
{
5353
"name": "foobar",
@@ -81,6 +81,47 @@ let tx3 = contract.methods.doSomethingWithValue([1, 2, 3])
8181
.buildTransaction();
8282
```
8383

84+
Now let's see an example using variadic arguments, as well:
85+
86+
```
87+
import { StringValue, VariadicValue } from "@multiversx/sdk-core";
88+
89+
abiRegistry = AbiRegistry.create({
90+
"endpoints": [
91+
{
92+
"name": "foobar",
93+
"inputs": [],
94+
"outputs": []
95+
},
96+
{
97+
"name": "doSomething",
98+
"inputs": [{
99+
"type": "counted-variadic<utf-8 string>"
100+
},
101+
{
102+
"type": "variadic<u64>"
103+
}],
104+
"outputs": []
105+
}
106+
]
107+
});
108+
109+
contract = new SmartContract({ address: contractAddress, abi: abiRegistry });
110+
111+
let tx4 = contract.methods.doSomething(
112+
[
113+
// Counted variadic must be explicitly typed
114+
VariadicValue.fromItemsCounted(StringValue.fromUTF8("foo"), StringValue.fromUTF8("bar")),
115+
// Regular variadic can be implicitly typed
116+
1, 2, 3
117+
])
118+
.withSender(addressOfAlice)
119+
.withNonce(45)
120+
.withGasLimit(20000000)
121+
.withChainID("D")
122+
.buildTransaction();
123+
```
124+
84125
### Transfer & execute
85126

86127
Given an interaction:

cookbook/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"type": "module",
33
"dependencies": {
44
"@multiversx/sdk-wallet": "4.0.0",
5-
"@multiversx/sdk-core": "12.1.0",
5+
"@multiversx/sdk-core": "12.8.0",
66
"@multiversx/sdk-network-providers": "1.3.0",
77
"axios": "1.3.4"
88
}

0 commit comments

Comments
 (0)