From 47f8dfd4016a2c83186044fd7c82547efbf8b749 Mon Sep 17 00:00:00 2001 From: Claudiu-Marcel Bruda Date: Mon, 28 Jun 2021 17:15:19 +0300 Subject: [PATCH 1/3] Add interaction examples --- .../examples/adder/interaction/Adder.erdjs.md | 22 ++++++ .../interaction/Crowdfunding-egld.erdjs.md | 50 +++++++++++++ .../interaction/Crowdfunding-esdt.erdjs.md | 58 +++++++++++++++ .../interaction/Multisig-adder-egld.erdjs.md | 72 +++++++++++++++++++ .../interaction/Ping-pong-egld.erdjs.md | 23 ++++++ .../interaction/esdt-FT-fungible-tokens.md | 33 +++++++++ .../esdt-NFT-non-fungible-tokens.md | 29 ++++++++ .../esdt-SFT-semi-fungible-tokens.md | 32 +++++++++ tutorial/src/interaction/interaction-basic.md | 36 ++++++++++ .../src/interaction/interaction-in-depth.md | 55 ++++++++++++++ 10 files changed, 410 insertions(+) create mode 100644 contracts/examples/adder/interaction/Adder.erdjs.md create mode 100644 contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-egld.erdjs.md create mode 100644 contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-esdt.erdjs.md create mode 100644 contracts/examples/multisig/interaction/Multisig-adder-egld.erdjs.md create mode 100644 contracts/examples/ping-pong-egld/interaction/Ping-pong-egld.erdjs.md create mode 100644 tutorial/src/interaction/esdt-FT-fungible-tokens.md create mode 100644 tutorial/src/interaction/esdt-NFT-non-fungible-tokens.md create mode 100644 tutorial/src/interaction/esdt-SFT-semi-fungible-tokens.md create mode 100644 tutorial/src/interaction/interaction-basic.md create mode 100644 tutorial/src/interaction/interaction-in-depth.md diff --git a/contracts/examples/adder/interaction/Adder.erdjs.md b/contracts/examples/adder/interaction/Adder.erdjs.md new file mode 100644 index 0000000000..775b837472 --- /dev/null +++ b/contracts/examples/adder/interaction/Adder.erdjs.md @@ -0,0 +1,22 @@ +# Adder + +First [set up a node terminal](../../../../tutorial/src/interaction/interaction-basic.md). + +```javascript +let erdjs = await require('@elrondnetwork/erdjs'); +let { erdSys, wallets: { alice } } = await erdjs.setupInteractive("local-testnet"); + +let adder = await erdSys.loadWrapper("contracts/examples/adder"); + +// Deploy the adder contract with an initial value of 42 +await adder.sender(alice).gas(20_000_000).deploy(42); + +// Check that the sum is 42 +await adder.query.getSum().then((sum) => sum.toString()); + +await adder.gas(3_000_000).call.add(30); + +// Check that the sum is 72 +await adder.query.getSum().then((sum) => sum.toString()); + +``` diff --git a/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-egld.erdjs.md b/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-egld.erdjs.md new file mode 100644 index 0000000000..c4d6e09451 --- /dev/null +++ b/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-egld.erdjs.md @@ -0,0 +1,50 @@ +# Crowdfunding ESDT - Using EGLD + +First [set up a node terminal](../../../../tutorial/src/interaction/interaction-basic.md). + +```javascript +let erdjs = await require('@elrondnetwork/erdjs'); +let { erdSys, Egld, wallets: { alice, bob, carol }} = await erdjs.setupInteractive("local-testnet"); + +let crowdfunding = await erdSys.loadWrapper("contracts/examples/crowdfunding-esdt"); + +// Set the deadline to 1 minute from now (adjust this if you want more time before claiming the rewards) +let someTimeFromNow = await erdSys.currentNonce() + erdjs.minutesToNonce(1); + +// Deploy the crowdfunding contract with a target of 2 EGLD +await crowdfunding.sender(alice).gas(50_000_000).deploy(Egld(2), someTimeFromNow, Egld); + +// Bob and carol contribute 1.5 EGLD each +await crowdfunding.sender(bob).gas(10_000_000).value(Egld(1.5)).call.fund(); +await crowdfunding.sender(carol).value(Egld(1.5)).call.fund(); + +// Get the current funds. Note the usage of Egld.raw (since the balance comes as an integer from the smart contract) +let currentFunds = Egld.raw(await crowdfunding.query.currentFunds()); + +// Should print 3 EGLD (since bob and carol added 1.5 EGLD each) +erdjs.print(currentFunds); + +// Confirming the target is 2 EGLD +erdjs.print(Egld.raw(await crowdfunding.query.get_target())); + +// Check that alice is the owner +alice.address.equals(await crowdfunding.query.get_owner()); + +// Store alice's current balance (we'll use this to check the balance difference later on) +let aliceBalanceBefore = await erdSys.getBalance(alice, Egld); +erdjs.print(aliceBalanceBefore); + +// Wait a minute first, otherwise you'll get the "cannot claim before deadline" error +// If the claim doesn't return an error - there are two possibilities: +// - the funding failed, and 1.5 EGLD are sent back to both bob and carol +// - it was succesful and alice receives 3 EGLD +// Because the target sum specified on deployment was 2 EGLD, and we have 3 EGLD, the funding should be succesful +await crowdfunding.sender(alice).call.claim(); + +// Let's check if alice received the funds +let aliceBalanceAfter = await erdSys.getBalance(alice, Egld); +erdjs.print(aliceBalanceAfter); + +// If the previous claim was successful, this prints 2.99 EGLD (because of the gas costs) +erdjs.print(aliceBalanceAfter.minus(aliceBalanceBefore)); +``` diff --git a/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-esdt.erdjs.md b/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-esdt.erdjs.md new file mode 100644 index 0000000000..8dd66bfb64 --- /dev/null +++ b/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-esdt.erdjs.md @@ -0,0 +1,58 @@ +# Crowdfunding ESDT - Using ESDT (Fungible token) + +First [set up a node terminal](../../../../tutorial/src/interaction/interaction-basic.md). + +```javascript +let erdjs = await require('@elrondnetwork/erdjs'); +let { erdSys, wallets: { alice, bob, carol } } = await erdjs.setupInteractive("local-testnet"); + +let crowdfunding = await erdSys.loadWrapper("contracts/examples/crowdfunding-esdt"); + +// Issue a new fungible token +let MyToken = await erdSys.sender(alice).issueFungible("MyFungibleToken", "MYTOKEN", 1_000_00, 2); + +// Note: wait a few seconds, otherwise the first send fails +// Send some tokens to bob and carol (so that they can call fund later on) +await erdSys.sender(alice).value(MyToken(200.0)).send(bob); +await erdSys.sender(alice).value(MyToken(200.0)).send(carol); + +// Set the deadline to 1 minute from now (adjust this if you want more time before claiming the rewards) +let someTimeFromNow = await erdSys.currentNonce() + erdjs.minutesToNonce(1); + +// Deploy the crowdfunding contract +await crowdfunding.sender(alice).gas(50_000_000).deploy(MyToken(2), someTimeFromNow, MyToken); + +// Bob and carol contribute 1.5 MYTOKEN each +await crowdfunding.sender(bob).gas(10_000_000).value(MyToken(1.5)).call.fund(); +await crowdfunding.sender(carol).value(MyToken(1.5)).call.fund(); + +// Get the current funds. Note the usage of MyToken.raw (since the balance comes as an integer from the smart contract) +let currentFunds = MyToken.raw(await crowdfunding.query.currentFunds()); + +// Should print 3 MYTOKEN (since bob and carol added 1.5 MYTOKEN each) +erdjs.print(currentFunds); + +// Confirming the target is 2 MYTOKEN +erdjs.print(MyToken.raw(await crowdfunding.query.get_target())); + +// Check that alice is the owner +alice.address.equals(await crowdfunding.query.get_owner()); + +// Store alice's current balance (we'll use this to check the balance difference later on) +let aliceBalanceBefore = await erdSys.getBalance(alice, MyToken); +erdjs.print(aliceBalanceBefore); + +// Wait a minute first, otherwise you'll get the "cannot claim before deadline" error +// If the claim doesn't return an error - there are two possibilities: +// - the funding failed, and 1.5 MYTOKEN are sent back to both bob and carol +// - it was succesful and alice receives 3 MYTOKEN +// Because the target sum specified on deployment was 2 MYTOKEN, and we have 3 MYTOKEN, the funding should be succesful +await crowdfunding.sender(alice).call.claim(); + +// Let's check if alice received the funds +let aliceBalanceAfter = await erdSys.getBalance(alice, MyToken); +erdjs.print(aliceBalanceAfter); + +// If the previous claim was successful, this prints 3 MYTOKEN +erdjs.print(aliceBalanceAfter.minus(aliceBalanceBefore)); +``` diff --git a/contracts/examples/multisig/interaction/Multisig-adder-egld.erdjs.md b/contracts/examples/multisig/interaction/Multisig-adder-egld.erdjs.md new file mode 100644 index 0000000000..ba2ee9b64c --- /dev/null +++ b/contracts/examples/multisig/interaction/Multisig-adder-egld.erdjs.md @@ -0,0 +1,72 @@ +# Multisig + +First [set up a node terminal](../../../../tutorial/src/interaction/interaction-basic.md). + +```javascript +let erdjs = await require('@elrondnetwork/erdjs'); +let { erdSys, Egld, wallets: { alice, bob, carol }} = await erdjs.setupInteractive("local-testnet"); + +let multisig = await erdSys.loadWrapper("contracts/examples/multisig"); + +// Deploy a multisig contract with a quorum of 3, but 4 possible signers: alice, bob, carol, dan +// A quorum is the minimum required number of signers in order to be able to perform a proposed action +await multisig.sender(alice).gas(150_000_000).deploy(3, alice, bob, carol, dan); + +// Deposit 10 EGLD from alice to the multisig contract, which we'll use them in the next examples +await multisig.gas(20_000_000).sender(alice).value(Egld(10)).deposit(); + +// Create a proposal to send 3 EGLD from the multisig to eve +// Note that any proposal is automatically signed by alice +var sendId = await multisig.sender(alice).proposeSendEgld(eve, Egld(3)); + +// Sign the previous proposal 2 more times +await multisig.sender(bob).sign(sendId); +await multisig.sender(carol).sign(sendId); + +// This will return 3, which means that the quorum has been reached +await multisig.getActionValidSignerCount(sendId); + +// Perform the send +await multisig.performAction(sendId); + +// Let's use the adder contract as the nested contract which is to be managed by the multisig +let adder = await erdSys.loadWrapper("contracts/examples/adder"); + +// Validate and pack the arguments into a FormattedCall object +// Note: this doesn't deploy the contract (this will be done through the proposal below) +let formattedDeploy = adder.format.deploy(42); + +// A proposal to deploy the adder smart contract. The formattedCall is automatically expanded. +var deployId = await multisig.sender(alice).gas(200_000_000).proposeSCDeploy(Egld(0), await adder.getCode(), false, false, false, formattedDeploy); + +// Sign the deployment 2 more times +await multisig.sender(bob).gas(20_000_000).sign(deployId); +await multisig.sender(carol).sign(deployId); + +// Perform the deploy. The address of the deployed adder will be returned. +var deployAddress = await multisig.performAction(deployId); + +// Check the deploy address. bech32() will output it as erd1... +deployAddress.bech32(); + +// We can also access the adder smart contract by setting its deployed address to the smart contract wrapper instance. +// Let's check that the sum is 42 (the sum provided through the constructor). +await adder.address(deployAddress).sender(alice).getSum(); + +// Create a proposal on calling the "add" method +let formattedAdd = adder.format.add(1000); +var addId = await multisig.sender(alice).proposeSCCall(adder, Egld(0), formattedAdd); + +// Sign the add proposal +await multisig.sender(bob).sign(addId); +await multisig.sender(carol).sign(addId); + +// Before performing the "add" action, the sum should still be 42 +await adder.sender(alice).getSum(); + +// Add 1000 +await multisig.gas(40_000_000).performAction(addId); + +// After adding, the sum should be 1042 +await adder.sender(alice).getSum(); +``` diff --git a/contracts/examples/ping-pong-egld/interaction/Ping-pong-egld.erdjs.md b/contracts/examples/ping-pong-egld/interaction/Ping-pong-egld.erdjs.md new file mode 100644 index 0000000000..61c8543257 --- /dev/null +++ b/contracts/examples/ping-pong-egld/interaction/Ping-pong-egld.erdjs.md @@ -0,0 +1,23 @@ +# Ping-pong + +First [set up a node terminal](../../../../tutorial/src/interaction/interaction-basic.md). + +```javascript +let erdjs = await require('@elrondnetwork/erdjs'); +let { erdSys, Egld, wallets: { alice, bob, carol, dan } } = await erdjs.setupInteractive("local-testnet"); + +let pingPong = await erdSys.loadWrapper("contracts/examples/ping-pong-egld"); + +await pingPong.sender(alice).gas(150_000_000).deploy(Egld(0.5), 2 * 60, null, Egld(1.5)); + +await pingPong.gas(20_000_000).sender(alice).value(Egld(0.5)).ping("note 1"); + +await pingPong.sender(bob).value(Egld(0.5)).ping(null); +await pingPong.sender(carol).value(Egld(0.5)).ping(null); + +// this fails because of the balance limit of 1.5 egld +await pingPong.sender(dan).value(Egld(0.5).ping(null); + +await pingPong.pongAll(); + +``` diff --git a/tutorial/src/interaction/esdt-FT-fungible-tokens.md b/tutorial/src/interaction/esdt-FT-fungible-tokens.md new file mode 100644 index 0000000000..026a995896 --- /dev/null +++ b/tutorial/src/interaction/esdt-FT-fungible-tokens.md @@ -0,0 +1,33 @@ +# Fungible Tokens (FT) + +Fungible Tokens have variable amounts, but always have nonce 0. They may be denominated. + +First [set up a node terminal](../../../../tutorial/src/interaction/interaction-basic.md). + +```javascript +let erdjs = await require('@elrondnetwork/erdjs'); +let { erdSys, wallets: { alice, bob, carol } } = await erdjs.setupInteractive("local-testnet"); + +// Issue a new fungible token +let MyToken = await erdSys.sender(alice).issueFungible("MyFungibleToken", "MYTOKEN", 1_000_00, 2); + +// Check the token's identifier +console.log(MyToken.getTokenIdentifier()); + +// Note: if you have the token identifier, you can recall the token via: +// let MyToken = await erdSys.recallToken("MYTOKEN-a4fc62"); + +// Check alice's token balance +// Note: if the balance comes up as 0, wait some time and try again +await erdSys.getBalance(alice, MyToken).then(erdjs.print); + +// Send some tokens to bob +await erdSys.sender(alice).value(MyToken(200.0)).send(bob); + +// Check alice's balance (should be 800.00 MYTOKEN) +await erdSys.getBalance(alice, MyToken).then(erdjs.print); + +// Check bob's balance (should be 200.00 MYTOKEN) +await erdSys.getBalance(bob, MyToken).then(erdjs.print); + +``` diff --git a/tutorial/src/interaction/esdt-NFT-non-fungible-tokens.md b/tutorial/src/interaction/esdt-NFT-non-fungible-tokens.md new file mode 100644 index 0000000000..950e120ec6 --- /dev/null +++ b/tutorial/src/interaction/esdt-NFT-non-fungible-tokens.md @@ -0,0 +1,29 @@ +# Non-Fungible Tokens (NFTs) + +Non-Fungible Tokens have amounts of either 0 or 1, and variable nonce. They are not denominated (the amount has 0 decimals). + +First [set up a node terminal](../../../../tutorial/src/interaction/interaction-basic.md). + +```javascript +let erdjs = await require('@elrondnetwork/erdjs'); +let { erdSys, Egld, wallets: { alice, bob, carol } } = await erdjs.setupInteractive("local-testnet"); + +// Issue a new non-fungible token +let MyToken = await erdSys.sender(alice).issueNonFungible("MyFungibleToken", "MYTOKEN"); + +// Check the token's identifier +console.log(MyToken.getTokenIdentifier()); + +await erdSys.esdtSystemContract.sender(alice).call.setSpecialRole(MyToken, alice, "ESDTRoleNFTCreate"); + +// Create 2 tokens +let MyFirstNFT = await erdSys.sender(alice).esdtNftCreate(MyToken, 1, "MyFirstNFT", 0, "", "", "https://example.com"); +let MySecondNFT = await erdSys.sender(alice).esdtNftCreate(MyToken, 1, "MySecondNFT", 0, "", "", "https://example.com"); + +// Send some tokens to bob and carol +await erdSys.sender(alice).value(MyFirstNFT.one()).send(bob); +await erdSys.sender(alice).value(MySecondNFT.one()).send(carol); + +await erdSys.getBalanceList(alice, MyToken).then(erdjs.printList); +await erdSys.getBalanceList(bob, MyToken).then(erdjs.printList); +``` diff --git a/tutorial/src/interaction/esdt-SFT-semi-fungible-tokens.md b/tutorial/src/interaction/esdt-SFT-semi-fungible-tokens.md new file mode 100644 index 0000000000..865cc8a067 --- /dev/null +++ b/tutorial/src/interaction/esdt-SFT-semi-fungible-tokens.md @@ -0,0 +1,32 @@ +# Semi-Fungible Tokens (SFTs). + +Semi-Fungible Tokens have variable amounts, and variable nonce. They are not denominated (the amount has 0 decimals). + +First [set up a node terminal](../../../../tutorial/src/interaction/interaction-basic.md). + +```javascript +let erdjs = await require('@elrondnetwork/erdjs'); +let { erdSys, Egld, wallets: { alice, bob, carol } } = await erdjs.setupInteractive("local-testnet"); + +// Issue a new semi-fungible token +let MyToken = await erdSys.sender(alice).issueSemiFungible("MySemiFungibleToken", "MYTOKEN"); + +// Check the token's identifier +console.log(MyToken.getTokenIdentifier()); + +await erdSys.esdtSystemContract.sender(alice).call.setSpecialRole(MyToken, alice, "ESDTRoleNFTCreate", "ESDTRoleNFTAddQuantity"); + +// Create a new nonce +let MyFirstSemi = await erdSys.sender(alice).esdtNftCreate(MyToken, 1_000, "MyFirstSemi", 0, "", "", "https://example.com"); + +// Check alice's token balance +// Note: if the balance comes up as 0, wait some time and try again +await erdSys.getBalance(alice, MyFirstSemi).then(erdjs.print); + +// Send some tokens to bob and carol +await erdSys.sender(alice).value(MyFirstSemi(200)).send(bob); + +await erdSys.getBalance(alice, MyFirstSemi).then(erdjs.print); +await erdSys.getBalance(bob, MyFirstSemi).then(erdjs.print); + +``` diff --git a/tutorial/src/interaction/interaction-basic.md b/tutorial/src/interaction/interaction-basic.md new file mode 100644 index 0000000000..d40564c475 --- /dev/null +++ b/tutorial/src/interaction/interaction-basic.md @@ -0,0 +1,36 @@ +# Preparations + +# Setting up the local testnet + +The following examples rely on having a (local testnet)[https://docs.elrond.com/developers/setup-local-testnet/] up and running. + +# Installing @elrondnetwork/erdjs globally + +```bash +cd ./code/elrond-sdk-erdjs +npm run compile && npm test && npm install -g +``` + +# How to start a node terminal + +By exporting `NODE_PATH`, the node terminal should have access to `erdjs`. +Open a terminal and enter the following: + +```bash +cd ./code/elrond-wasm-rs +export NODE_PATH=$HOME/.nvm/versions/node/$(node --version)/lib/node_modules +node --experimental-repl-await +``` + +# Basic ESDT usage + +- [Fungible Tokens (FTs)](esdt-FT-fungible-tokens.md) +- [Semi-Fungible Tokens (SFTs)](esdt-SFT-semi-fungible-tokens.md) +- [Non-Fungible Tokens (NFTs)](esdt-NFT-non-fungible-tokens.md) + +# Smart contract examples + +- Adder [interaction](../../../contracts/examples/adder/interaction/Adder.erdjs.md) +- Crowdfunding ESDT [EGLD interaction](../../../contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-egld.erdjs.md), [ESDT interaction](../../../contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-esdt.erdjs.md) +- Multisig [EGLD adder interaction](../../../contracts/examples/multisig/interaction/Multisig-adder-egld.erdjs.md) +- Ping-pong [EGLD interaction](../../../contracts/examples/ping-pong-egld/interaction/Ping-pong-egld.erdjs.md) diff --git a/tutorial/src/interaction/interaction-in-depth.md b/tutorial/src/interaction/interaction-in-depth.md new file mode 100644 index 0000000000..8801ea8055 --- /dev/null +++ b/tutorial/src/interaction/interaction-in-depth.md @@ -0,0 +1,55 @@ +# Basics - explained + +First [set up a node terminal](../../../../tutorial/src/interaction/interaction-basic.md). + +```javascript +let erdjs = await require('@elrondnetwork/erdjs'); +let { erdSys, Egld, wallets: { alice, bob, carol, dan, eve } } = await erdjs.setupInteractive("local-testnet"); +``` + +The `setupInteractive` call does several essential things: +- synchronizes the default `NetworkConfig` with the chosen provider +- loads the test wallets from the filesystem and *synchronizes their nonce* +- loads `erdSys`, which contains the ESDT system smart contract and builtin functions (required for ESDT issuing, transfers) +- returns `Egld` which can be used to build EGLD sums (eg. `Egld(0.5)`) + +## Choosing a provider + +For `erdjs.setupInteractive` the available providers are: +- Local Testnet proxy: `"local-testnet"` +- Elrond Testnet proxy: `"elrond-testnet"` +- Elrond Devnet proxy: `"elrond-devnet"` +- Elrond Mainnet proxy: `"elrond-mainnet"` + +# Notes + +## On working with balances + +There are two ways of thinking about a balance: +- as a denominated unit (eg. 1.5 EGLD) +- by its raw decimal representation (eg. "1500000000000000000") + +When working with examples, it makes most sense to deal with the denominated unit, both when providing and when reading such values. +However, when EGLD amounts are returned by smart contracts they are always returned as raw decimal values. + +The examples below build a `Balance` of 1.5 EGLD. +```javascript +Egld(1.5).toCurrencyString(); +Egld("1.5").toCurrencyString(); +``` + +On the other hand, if you need to build a balance from a raw non-denominated value, use `Egld.raw` instead. Note that the examples below are also 1.5 EGLD. +```javascript +Egld.raw(1_500_000_000_000_000_000).toCurrencyString(); +Egld.raw("1500000000000000000").toCurrencyString(); +``` + +### Notes + +- Javascript allows writing numerical values with the underscore separator. + +- Javascript numbers are internally floating point values and, as such, have precision issues with large values (eg. `1_500_000_000_000_000_000 + 10 == 1_500_000_000_000_000_000` is `true`). This is the reason balances are stored as integer values in smart contracts (as `BigUint`) as well as in Javascript code (through `BigNumber`, which is used by `Balance` internally). + +- The number of EGLD decimals is 18. By using `Egld` and `Egld.raw` correctly you shouldn't have to care about this. + +- When dealing with fungible or semi-fungible ESDT tokens, the number of decimals varies depending on what the token's creator chose when he made it. From 72ca3884c5009835325c27f435b4683153e5a6c3 Mon Sep 17 00:00:00 2001 From: Claudiu-Marcel Bruda Date: Tue, 29 Jun 2021 14:52:41 +0300 Subject: [PATCH 2/3] Typo fix --- tutorial/src/interaction/interaction-basic.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorial/src/interaction/interaction-basic.md b/tutorial/src/interaction/interaction-basic.md index d40564c475..84592256fa 100644 --- a/tutorial/src/interaction/interaction-basic.md +++ b/tutorial/src/interaction/interaction-basic.md @@ -2,7 +2,7 @@ # Setting up the local testnet -The following examples rely on having a (local testnet)[https://docs.elrond.com/developers/setup-local-testnet/] up and running. +The following examples rely on having a [local testnet](https://docs.elrond.com/developers/setup-local-testnet/) up and running. # Installing @elrondnetwork/erdjs globally From d496e72384a051c1218b3ec855195dd40f72d116 Mon Sep 17 00:00:00 2001 From: Claudiu-Marcel Bruda Date: Wed, 28 Jul 2021 16:55:56 +0300 Subject: [PATCH 3/3] Fix after review, update calls --- .../examples/adder/interaction/Adder.erdjs.md | 2 +- .../interaction/Crowdfunding-egld.erdjs.md | 2 +- .../interaction/Crowdfunding-esdt.erdjs.md | 2 +- .../interaction/Multisig-adder-egld.erdjs.md | 41 ++++++++++--------- .../interaction/Ping-pong-egld.erdjs.md | 2 +- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/contracts/examples/adder/interaction/Adder.erdjs.md b/contracts/examples/adder/interaction/Adder.erdjs.md index 775b837472..8df942b3b0 100644 --- a/contracts/examples/adder/interaction/Adder.erdjs.md +++ b/contracts/examples/adder/interaction/Adder.erdjs.md @@ -9,7 +9,7 @@ let { erdSys, wallets: { alice } } = await erdjs.setupInteractive("local-testnet let adder = await erdSys.loadWrapper("contracts/examples/adder"); // Deploy the adder contract with an initial value of 42 -await adder.sender(alice).gas(20_000_000).deploy(42); +await adder.sender(alice).gas(20_000_000).call.deploy(42); // Check that the sum is 42 await adder.query.getSum().then((sum) => sum.toString()); diff --git a/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-egld.erdjs.md b/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-egld.erdjs.md index c4d6e09451..2047217a89 100644 --- a/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-egld.erdjs.md +++ b/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-egld.erdjs.md @@ -12,7 +12,7 @@ let crowdfunding = await erdSys.loadWrapper("contracts/examples/crowdfunding-esd let someTimeFromNow = await erdSys.currentNonce() + erdjs.minutesToNonce(1); // Deploy the crowdfunding contract with a target of 2 EGLD -await crowdfunding.sender(alice).gas(50_000_000).deploy(Egld(2), someTimeFromNow, Egld); +await crowdfunding.sender(alice).gas(50_000_000).call.deploy(Egld(2), someTimeFromNow, Egld); // Bob and carol contribute 1.5 EGLD each await crowdfunding.sender(bob).gas(10_000_000).value(Egld(1.5)).call.fund(); diff --git a/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-esdt.erdjs.md b/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-esdt.erdjs.md index 8dd66bfb64..e590edd953 100644 --- a/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-esdt.erdjs.md +++ b/contracts/examples/crowdfunding-esdt/interaction/Crowdfunding-esdt.erdjs.md @@ -20,7 +20,7 @@ await erdSys.sender(alice).value(MyToken(200.0)).send(carol); let someTimeFromNow = await erdSys.currentNonce() + erdjs.minutesToNonce(1); // Deploy the crowdfunding contract -await crowdfunding.sender(alice).gas(50_000_000).deploy(MyToken(2), someTimeFromNow, MyToken); +await crowdfunding.sender(alice).gas(50_000_000).call.deploy(MyToken(2), someTimeFromNow, MyToken); // Bob and carol contribute 1.5 MYTOKEN each await crowdfunding.sender(bob).gas(10_000_000).value(MyToken(1.5)).call.fund(); diff --git a/contracts/examples/multisig/interaction/Multisig-adder-egld.erdjs.md b/contracts/examples/multisig/interaction/Multisig-adder-egld.erdjs.md index ba2ee9b64c..067774bbb0 100644 --- a/contracts/examples/multisig/interaction/Multisig-adder-egld.erdjs.md +++ b/contracts/examples/multisig/interaction/Multisig-adder-egld.erdjs.md @@ -1,33 +1,34 @@ # Multisig +This contract uses a few uncommon concepts (eg. _quorum_), which These are explained in detail in the [README.md](../README.md). + First [set up a node terminal](../../../../tutorial/src/interaction/interaction-basic.md). ```javascript let erdjs = await require('@elrondnetwork/erdjs'); -let { erdSys, Egld, wallets: { alice, bob, carol }} = await erdjs.setupInteractive("local-testnet"); +let { erdSys, Egld, wallets: { alice, bob, carol, dan, eve }} = await erdjs.setupInteractive("local-testnet"); let multisig = await erdSys.loadWrapper("contracts/examples/multisig"); // Deploy a multisig contract with a quorum of 3, but 4 possible signers: alice, bob, carol, dan -// A quorum is the minimum required number of signers in order to be able to perform a proposed action -await multisig.sender(alice).gas(150_000_000).deploy(3, alice, bob, carol, dan); +await multisig.sender(alice).gas(150_000_000).call.deploy(3, alice, bob, carol, dan); // Deposit 10 EGLD from alice to the multisig contract, which we'll use them in the next examples -await multisig.gas(20_000_000).sender(alice).value(Egld(10)).deposit(); +await multisig.gas(20_000_000).sender(alice).value(Egld(10)).call.deposit(); // Create a proposal to send 3 EGLD from the multisig to eve // Note that any proposal is automatically signed by alice -var sendId = await multisig.sender(alice).proposeSendEgld(eve, Egld(3)); +var sendId = await multisig.sender(alice).call.proposeSendEgld(eve, Egld(3)); // Sign the previous proposal 2 more times -await multisig.sender(bob).sign(sendId); -await multisig.sender(carol).sign(sendId); +await multisig.sender(bob).call.sign(sendId); +await multisig.sender(carol).call.sign(sendId); // This will return 3, which means that the quorum has been reached -await multisig.getActionValidSignerCount(sendId); +await multisig.query.getActionValidSignerCount(sendId); // Perform the send -await multisig.performAction(sendId); +await multisig.call.performAction(sendId); // Let's use the adder contract as the nested contract which is to be managed by the multisig let adder = await erdSys.loadWrapper("contracts/examples/adder"); @@ -37,36 +38,36 @@ let adder = await erdSys.loadWrapper("contracts/examples/adder"); let formattedDeploy = adder.format.deploy(42); // A proposal to deploy the adder smart contract. The formattedCall is automatically expanded. -var deployId = await multisig.sender(alice).gas(200_000_000).proposeSCDeploy(Egld(0), await adder.getCode(), false, false, false, formattedDeploy); +var deployId = await multisig.sender(alice).gas(200_000_000).call.proposeSCDeploy(Egld(0), await adder.getCode(), false, false, false, formattedDeploy); // Sign the deployment 2 more times -await multisig.sender(bob).gas(20_000_000).sign(deployId); -await multisig.sender(carol).sign(deployId); +await multisig.sender(bob).gas(20_000_000).call.sign(deployId); +await multisig.sender(carol).call.sign(deployId); // Perform the deploy. The address of the deployed adder will be returned. -var deployAddress = await multisig.performAction(deployId); +var deployAddress = await multisig.call.performAction(deployId); // Check the deploy address. bech32() will output it as erd1... deployAddress.bech32(); // We can also access the adder smart contract by setting its deployed address to the smart contract wrapper instance. // Let's check that the sum is 42 (the sum provided through the constructor). -await adder.address(deployAddress).sender(alice).getSum(); +await adder.address(deployAddress).sender(alice).query.getSum(); // Create a proposal on calling the "add" method let formattedAdd = adder.format.add(1000); -var addId = await multisig.sender(alice).proposeSCCall(adder, Egld(0), formattedAdd); +var addId = await multisig.sender(alice).call.proposeSCCall(adder, Egld(0), formattedAdd); // Sign the add proposal -await multisig.sender(bob).sign(addId); -await multisig.sender(carol).sign(addId); +await multisig.sender(bob).call.sign(addId); +await multisig.sender(carol).call.sign(addId); // Before performing the "add" action, the sum should still be 42 -await adder.sender(alice).getSum(); +await adder.sender(alice).query.getSum(); // Add 1000 -await multisig.gas(40_000_000).performAction(addId); +await multisig.gas(40_000_000).call.performAction(addId); // After adding, the sum should be 1042 -await adder.sender(alice).getSum(); +await adder.sender(alice).query.getSum(); ``` diff --git a/contracts/examples/ping-pong-egld/interaction/Ping-pong-egld.erdjs.md b/contracts/examples/ping-pong-egld/interaction/Ping-pong-egld.erdjs.md index 61c8543257..2fdf7d12c0 100644 --- a/contracts/examples/ping-pong-egld/interaction/Ping-pong-egld.erdjs.md +++ b/contracts/examples/ping-pong-egld/interaction/Ping-pong-egld.erdjs.md @@ -8,7 +8,7 @@ let { erdSys, Egld, wallets: { alice, bob, carol, dan } } = await erdjs.setupInt let pingPong = await erdSys.loadWrapper("contracts/examples/ping-pong-egld"); -await pingPong.sender(alice).gas(150_000_000).deploy(Egld(0.5), 2 * 60, null, Egld(1.5)); +await pingPong.sender(alice).gas(150_000_000).call.deploy(Egld(0.5), 2 * 60, null, Egld(1.5)); await pingPong.gas(20_000_000).sender(alice).value(Egld(0.5)).ping("note 1");