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
98 changes: 72 additions & 26 deletions cookbook/cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -845,9 +845,8 @@ While interactions with the contract are possible without the ABI, they are much
#### Loading the ABI from a file
```js
{
let abiJson = await promises.readFile("../src/testData/adder.abi.json", { encoding: "utf8" });
let abiObj = JSON.parse(abiJson);
let abi = Abi.create(abiObj);
let abiJson = await fs.promises.readFile("../src/testdata/adder.abi.json", { encoding: "utf8" });
const abi = Abi.create(JSON.parse(abiJson));
}
```

Expand Down Expand Up @@ -916,9 +915,12 @@ This allows arguments to be passed as native Javascript values. If the ABI is no
sender.nonce = await entrypoint.recallAccountNonce(sender.address);

// load the contract bytecode
const bytecode = await promises.readFile("../src/testData/adder.wasm");
const bytecode = await fs.promises.readFile("../src/testdata/adder.wasm");
// load the abi file
const abi = await loadAbiRegistry("../src/testdata/adder.abi.json");
const jsonContent: string = await fs.promises.readFile("../src/testdata/adder.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));

const controller = entrypoint.createSmartContractController(abi);

Expand Down Expand Up @@ -954,8 +956,10 @@ let args = [new U32Value(42), "hello", { foo: "bar" }, new TokenIdentifierValue(
```js
{
// We use the transaction hash we got when broadcasting the transaction

const abi = await loadAbiRegistry("../src/testdata/adder.abi.json");
const jsonContent: string = await fs.promises.readFile("../src/testdata/adder.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));
const entrypoint = new DevnetEntrypoint();
const controller = entrypoint.createSmartContractController(abi);
const outcome = await controller.awaitCompletedDeploy("txHash"); // waits for transaction completion and parses the result
Expand Down Expand Up @@ -988,7 +992,7 @@ Even before broadcasting, at the moment you know the sender's address and the no
{
const entrypoint = new DevnetEntrypoint();
const factory = entrypoint.createSmartContractTransactionsFactory();
const bytecode = await promises.readFile("../contracts/adder.wasm");
const bytecode = await fs.promises.readFile("../contracts/adder.wasm");

// For deploy arguments, use "TypedValue" objects if you haven't provided an ABI to the factory:
let args: any[] = [new BigUIntValue(42)];
Expand Down Expand Up @@ -1021,7 +1025,7 @@ After the transaction is created the nonce needs to be properly set and the tran
const factory = entrypoint.createSmartContractTransactionsFactory();

// load the contract bytecode
const bytecode = await promises.readFile("../src/testData/adder.wasm");
const bytecode = await fs.promises.readFile("../src/testdata/adder.wasm");

// For deploy arguments, use "TypedValue" objects if you haven't provided an ABI to the factory:
let args: any[] = [new BigUIntValue(42)];
Expand Down Expand Up @@ -1077,7 +1081,10 @@ In this section we'll see how we can call an endpoint of our previously deployed
sender.nonce = await entrypoint.recallAccountNonce(sender.address);

// load the abi file
const abi = await loadAbiRegistry("../src/testdata/adder.abi.json");
const jsonContent: string = await fs.promises.readFile("../src/testdata/adder.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));
const controller = entrypoint.createSmartContractController(abi);

const contractAddress = Address.newFromBech32("erd1qqqqqqqqqqqqqpgq7cmfueefdqkjsnnjnwydw902v8pwjqy3d8ssd4meug");
Expand Down Expand Up @@ -1129,7 +1136,10 @@ Both EGLD and ESDT tokens or a combination of both can be sent. This functionali
sender.nonce = await entrypoint.recallAccountNonce(sender.address);

// load the abi file
const abi = await loadAbiRegistry("../src/testdata/adder.abi.json");
const jsonContent: string = await fs.promises.readFile("../src/testdata/adder.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));

// get the smart contracts controller
const controller = entrypoint.createSmartContractController(abi);
Expand Down Expand Up @@ -1219,7 +1229,10 @@ As said before, the `add` endpoint we called does not return anything, but we co
{
// load the abi file
const entrypoint = new DevnetEntrypoint();
const abi = await loadAbiRegistry("../src/testdata/adder.abi.json");
const jsonContent: string = await fs.promises.readFile("../src/testdata/adder.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));
const parser = new SmartContractTransactionsOutcomeParser({ abi });
const txHash = "b3ae88ad05c464a74db73f4013de05abcfcb4fb6647c67a262a6cfdf330ef4a9";
const transactionOnNetwork = await entrypoint.getTransaction(txHash);
Expand All @@ -1238,7 +1251,10 @@ First, we load the abi file, then we fetch the transaction, we extract the event
{
// load the abi files
const entrypoint = new DevnetEntrypoint();
const abi = await loadAbiRegistry("../src/testdata/adder.abi.json");
const jsonContent: string = await fs.promises.readFile("../src/testdata/adder.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));
const parser = new TransactionEventsParser({ abi });
const txHash = "b3ae88ad05c464a74db73f4013de05abcfcb4fb6647c67a262a6cfdf330ef4a9";
const transactionOnNetwork = await entrypoint.getTransaction(txHash);
Expand All @@ -1253,7 +1269,10 @@ Whenever needed, the contract ABI can be used for manually encoding or decoding
Let's encode a struct called EsdtTokenPayment (of [multisig](https://github.com/multiversx/mx-contracts-rs/tree/main/contracts/multisig) contract) into binary data.
```js
{
const abi = await loadAbiRegistry("../src/testdata/multisig-full.abi.json");
const jsonContent: string = await fs.promises.readFile("../src/testdata/multisig-full.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));
const paymentType = abi.getStruct("EsdtTokenPayment");
const codec = new BinaryCodec();

Expand All @@ -1272,7 +1291,10 @@ Let's encode a struct called EsdtTokenPayment (of [multisig](https://github.com/
Now let's decode a struct using the ABI.
```js
{
const abi = await loadAbiRegistry("../src/testdata/multisig-full.abi.json");
const jsonContent: string = await fs.promises.readFile("../src/testdata/multisig-full.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));
const actionStructType = abi.getEnum("Action");
const data = Buffer.from(
"0500000000000000000500d006f73c4221216fa679bc559005584c4f1160e569e1000000012a0000000003616464000000010000000107",
Expand All @@ -1295,7 +1317,10 @@ In this example, we will query the **adder smart contract** by calling its `getS
{
const entrypoint = new DevnetEntrypoint();
const contractAddress = Address.newFromBech32("erd1qqqqqqqqqqqqqpgq7cmfueefdqkjsnnjnwydw902v8pwjqy3d8ssd4meug");
const abi = await loadAbiRegistry("../src/testdata/adder.abi.json");
const jsonContent: string = await fs.promises.readFile("../src/testdata/adder.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));

// create the controller
const controller = entrypoint.createSmartContractController(abi);
Expand All @@ -1313,7 +1338,10 @@ This approach achieves the same result as the previous example.
const entrypoint = new DevnetEntrypoint();

// load the abi
const abi = await loadAbiRegistry("../src/testdata/adder.abi.json");
const jsonContent: string = await fs.promises.readFile("../src/testdata/adder.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));

// the contract address we'll query
const contractAddress = Address.newFromBech32("erd1qqqqqqqqqqqqqpgq7cmfueefdqkjsnnjnwydw902v8pwjqy3d8ssd4meug");
Expand Down Expand Up @@ -1346,13 +1374,16 @@ However, in this case, the contract address is already known. Like deploying a s
sender.nonce = await entrypoint.recallAccountNonce(sender.address);

// load the abi
const abi = await loadAbiRegistry("../src/testdata/adder.abi.json");
const jsonContent: string = await fs.promises.readFile("../src/testdata/adder.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));

// create the controller
const controller = entrypoint.createSmartContractController(abi);

// load the contract bytecode; this is the new contract code, the one we want to upgrade to
const bytecode = await promises.readFile("../src/testData/adder.wasm");
const bytecode = await fs.promises.readFile("../src/testdata/adder.wasm");

// For deploy arguments, use "TypedValue" objects if you haven't provided an ABI to the factory:
let args: any[] = [new U32Value(42)];
Expand Down Expand Up @@ -2646,8 +2677,11 @@ These operations can be performed using both the **controller** and the **factor
#### Deploying a Multisig Smart Contract using the controller
```js
{
const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json");
const bytecode = await loadContractCode("src/testdata/multisig-full.wasm");
const jsonContent: string = await fs.promises.readFile("src/testdata/multisig-full.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));
const bytecode = await fs.promises.readFile("src/testdata/multisig-full.wasm");

// create the entrypoint and the multisig controller
const entrypoint = new DevnetEntrypoint();
Expand Down Expand Up @@ -2682,8 +2716,11 @@ These operations can be performed using both the **controller** and the **factor
#### Deploying a Multisig Smart Contract using the factory
```js
{
const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json");
const bytecode = await loadContractCode("src/testdata/multisig-full.wasm");
const jsonContent: string = await fs.promises.readFile("src/testdata/multisig-full.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));
const bytecode = await fs.promises.readFile("src/testdata/multisig-full.wasm");

// create the entrypoint and the multisig factory
const entrypoint = new DevnetEntrypoint();
Expand Down Expand Up @@ -2718,9 +2755,12 @@ The id can be used later for signing and performing the proposal.

```js
{
const jsonContent: string = await fs.promises.readFile("src/testdata/multisig-full.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));
// create the entrypoint and the multisig controller
const entrypoint = new DevnetEntrypoint();
const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json");
const controller = entrypoint.createMultisigController(abi);

const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
Expand Down Expand Up @@ -2755,7 +2795,10 @@ Proposing an action for a multisig contract using the MultisigFactory is very si

```js
{
const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json");
const jsonContent: string = await fs.promises.readFile("src/testdata/multisig-full.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));

// create the entrypoint and the multisig factory
const entrypoint = new DevnetEntrypoint();
Expand Down Expand Up @@ -2801,7 +2844,10 @@ Let's query the contract to get all board members.

```js
{
const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json");
const jsonContent: string = await fs.promises.readFile("src/testdata/multisig-full.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));

// create the entrypoint and the multisig controller
const entrypoint = new DevnetEntrypoint();
Expand Down
34 changes: 24 additions & 10 deletions cookbook/multisig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as fs from "fs"; // md-ignore
import path from "path"; // md-ignore
import { Account, Address, DevnetEntrypoint, TransactionWatcher } from "../src"; // md-ignore
import { MultisigTransactionsOutcomeParser } from "../src/multisig/multisigTransactionsOutcomeParser";
import { loadAbiRegistry, loadContractCode } from "../src/testutils";
import { Abi, Account, Address, DevnetEntrypoint, MultisigTransactionsOutcomeParser, TransactionWatcher } from "../src"; // md-ignore
// md-start
(async () => {
// ### Multisig
Expand All @@ -17,8 +16,11 @@ import { loadAbiRegistry, loadContractCode } from "../src/testutils";
// #### Deploying a Multisig Smart Contract using the controller
// ```js
{
const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json");
const bytecode = await loadContractCode("src/testdata/multisig-full.wasm");
const jsonContent: string = await fs.promises.readFile("src/testdata/multisig-full.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));
const bytecode = await fs.promises.readFile("src/testdata/multisig-full.wasm");

// create the entrypoint and the multisig controller // md-as-comment
const entrypoint = new DevnetEntrypoint();
Expand Down Expand Up @@ -53,8 +55,11 @@ import { loadAbiRegistry, loadContractCode } from "../src/testutils";
// #### Deploying a Multisig Smart Contract using the factory
// ```js
{
const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json");
const bytecode = await loadContractCode("src/testdata/multisig-full.wasm");
const jsonContent: string = await fs.promises.readFile("src/testdata/multisig-full.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));
const bytecode = await fs.promises.readFile("src/testdata/multisig-full.wasm");

// create the entrypoint and the multisig factory // md-as-comment
const entrypoint = new DevnetEntrypoint();
Expand Down Expand Up @@ -89,9 +94,12 @@ import { loadAbiRegistry, loadContractCode } from "../src/testutils";

// ```js
{
const jsonContent: string = await fs.promises.readFile("src/testdata/multisig-full.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));
// create the entrypoint and the multisig controller // md-as-comment
const entrypoint = new DevnetEntrypoint();
const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json");
const controller = entrypoint.createMultisigController(abi);

const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
Expand Down Expand Up @@ -126,7 +134,10 @@ import { loadAbiRegistry, loadContractCode } from "../src/testutils";

// ```js
{
const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json");
const jsonContent: string = await fs.promises.readFile("src/testdata/multisig-full.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));

// create the entrypoint and the multisig factory // md-as-comment
const entrypoint = new DevnetEntrypoint();
Expand Down Expand Up @@ -172,7 +183,10 @@ import { loadAbiRegistry, loadContractCode } from "../src/testutils";

// ```js
{
const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json");
const jsonContent: string = await fs.promises.readFile("src/testdata/multisig-full.abi.json", {
encoding: "utf8",
});
const abi = Abi.create(JSON.parse(jsonContent));

// create the entrypoint and the multisig controller // md-as-comment
const entrypoint = new DevnetEntrypoint();
Expand Down
Loading