You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sdk-and-tools/mxpy/smart-contract-interactions.md
+61-96Lines changed: 61 additions & 96 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,11 +21,11 @@ After each change to the interactions file, we need to repeat the source command
21
21
22
22
Let's take the following example:
23
23
24
-
1. We want to deploy a new smart contract on the Devnet.
25
-
2. We then need to upgrade the contract, to make it payable.
26
-
3. We call an endpoint without transferring any assets.
27
-
4. We make an `ESDTTransfer`, in order to call a payable endpoint.
28
-
5. We call a view function.
24
+
1. We want to **deploy** a new smart contract on the Devnet.
25
+
2. We then need to **upgrade** the contract, to make it payable.
26
+
3. We **call** an endpoint without transferring any assets.
27
+
4. We **transfer** ESDT, in order to call a payable endpoint.
28
+
5. We call a **view** function.
29
29
30
30
[comment]: #(mx-context-auto)
31
31
@@ -35,7 +35,7 @@ Before starting this tutorial, make sure you have the following:
35
35
36
36
-[`mxpy`](/sdk-and-tools/mxpy/mxpy-cli). Follow the [installation guide](/sdk-and-tools/mxpy/installing-mxpy) - make sure to use the latest version available.
37
37
-`stable`**Rust** version `≥ 1.83.0`. Follow the [installation guide](/docs/developers/toolchain-setup.md#installing-rust-and-sc-meta).
-`sc-meta`. Follow the [installation guide](/docs/developers/toolchain-setup.md#installing-rust-and-sc-meta).
39
39
40
40
[comment]: #(mx-context-auto)
41
41
@@ -58,14 +58,12 @@ Now, in order to deploy the contract, we use the special **deploy** function of
58
58
```shell
59
59
WALLET_PEM="~/my-wallet/my-wallet.pem"
60
60
PROXY="https://devnet-gateway.multiversx.com"
61
-
CHAIN_ID="D"
62
61
63
62
deploySC() {
64
63
mxpy --verbose contract deploy \
65
64
--bytecode=${WASM_PATH} \
66
65
--pem=${WALLET_PEM} \
67
-
--gas-limit=60000000 \
68
-
--proxy=${PROXY} --chain=${CHAIN_ID} \
66
+
--proxy=${PROXY} \
69
67
--arguments $1$2 \
70
68
--send ||return
71
69
}
@@ -78,11 +76,11 @@ source devnet.snippets.sh
78
76
deploySC arg1 arg2
79
77
```
80
78
81
-
Now let's look at the structure of the interaction. It receives the path of the **wasm file**, where we previously built the contract. It also receives the path of the **wallet** (the PEM file), the **proxy URL**and the **chain ID**, where the contract will be deployed. Another important parameter is the **gas limit**, where we state the maximum amount of gas we are willing to spend with this transaction. Each transaction cost depends on its complexity and the amount of data storage it handles.
79
+
Now let's look at the structure of the interaction. It receives the path of the **wasm file**, where we previously built the contract. It also receives the path of the **wallet** (the PEM file) and the **proxy URL** where the contract will be deployed.
82
80
83
81
Other than this, we also have the **arguments** keyword, that allows us to pass in the required parameters. As we previously said, deploying a smart contract means that we run the **init** function, which may or may not request some parameters. In our case, the **init** function has two different arguments, and we pass them when calling the **deploy** function. We'll come back later in this section at how we can pass parameters in function calls.
84
82
85
-
After the transaction is sent, `mxpy` will output information like the **transaction hash**, **data** and any other important information, based on the type of transaction. In case of a contract deployment, it will also output the newly deployed contract address.
83
+
After the transaction is sent, `mxpy` will output information like the **transaction hash**, **data** and any other important information, based on the type of transaction. In case of a contract deployment, it will also output the **newly deployed contract address**.
`CONTRACT_ADDRESS` is a placeholder value which value needs to be replaced with the address previously generated in the deploy action.
111
+
113
112
Here we have 2 new different elements that we need to observe:
114
113
115
114
1. We changed the **deploy** function with the **upgrade** function. This new function requires the address of the previously deployed smart contract so the system can identify which contract to update. It is important to note that this function can only be called by the smart contract's owner.
@@ -126,14 +125,13 @@ Let's suppose we want to call the following endpoint, that receives an address a
So, what happens in this interaction and how do we call it?
144
142
145
-
Besides the function and arguments parts, the snippet is more or less the same as when deploying or upgrading a contract. When calling a non payable function, we need to provide the endpoint's name as the function argument. As for the arguments, they have to be in the **same order** as in the smart contract, including when calling an endpoint that has a variable number of arguments. Now, for the sake of example, we provided the arguments in multiple ways.
143
+
Besides the function and arguments parts, the snippet is more or less the same as when deploying or upgrading a contract. When calling a **non payable** function, we need to provide the **endpoint's name** as the function argument. As for the arguments, they have to be in the **same order** as in the smart contract, including when calling an endpoint that has a variable number of arguments. Now, for the sake of example, we provided the arguments in multiple ways.
146
144
147
145
It is up to each developer to choose the layout he prefers, but a few points need to be underlined:
148
146
@@ -159,7 +157,7 @@ In our example we provide the address argument as a fixed argument. We then conv
159
157
- We can use `str:` for encoding strings. For example: `str:MYTOKEN-123456`.
160
158
- Blockchain addresses that start with `erd1` are automatically encoded, so there is no need to further hex encode them.
161
159
- The values **true** or **false** are automatically converted to **boolean** values.
162
-
- Values that are identified as **numbers** are hex encoded by default.
160
+
- Values that are identified as **numbers** are hex encoded as `BigUint` values.
163
161
- Arguments like `0x...` are left unchanged, as they are interpreted as already encoded hex values.
As we can see, the way we call a **payable endpoint** is by calling an `ESDTTransfer` function (or any other function that transfer assets and supports contract calls) and providing the name of the method as an argument. The order of the arguments differs for each transfer function. In our case, we specify in the terminal the **token type** and **the amount of tokens** we want to transfer and then we provide as a **fixed input** what smart contract endpoint we want to call.
224
+
To call a **payable endpoint**, we use the `--token-transfer` flag, which requires two values:
225
+
226
+
1. The token identifier.
227
+
2. The amount.
228
+
229
+
In our case, we specify in the terminal the **token identifier** and **the amount of tokens** we want to transfer.
230
+
231
+
:::info
232
+
When specifying the amount of tokens to transfer, the value must include the token's decimal precision.
233
+
234
+
For example EGLD use 18 decimals. This means that if you want to transfer 1.5 EGLD, the amount value will be $1.5 \times 10^{18}$.
235
+
:::
230
236
231
237
[comment]: #(mx-context-auto)
232
238
@@ -237,102 +243,61 @@ Now let's suppose we want to call an endpoint that accepts an NFT or an SFT as p
First of all, to call this type of transfer function we need to pass the receiver address the same as the sender address. So in this example, `MY_WALLET_ADDRESS` is the caller's address of the PEM wallet used.
270
-
271
-
Now, like in the case of `ESDTTransfer`, the name of the called function is `ESDTNFTTransfer`. All the other required data is passed as arguments (including the destination contract's address and the endpoint).
272
-
273
-
In case of this single NFT/SFT transfer, we first pass the **token** (identifier, nonce and amount) and then we pass the **destination address** and the **name of the endpoint**. In the end we pass whatever parameters the indicated method needs.
274
-
275
263
[comment]: #(mx-context-auto)
276
264
277
265
### Multi-ESDT transfer
278
266
279
267
In case we need to call an endpoint that accepts multiple tokens (let's say for example 2 fungible tokens and an NFT). Let's take a look at the following example:
In this example, we call `myMultiESDTPayableEndpoint` endpoint, by transferring **3 different tokens**: the first two are fungible tokens and the last one is an NFT.
333
300
334
-
The endpoint takes 2 BigUInt arguments. The layout of the snippet is almost the same as with `ESDTNFTTransfer` (including the fact that the sender is the same as the receiver) but has different arguments. We now pass the destination address first and the number of ESDT/NFT tokens that we want to sent. Then, for each sent token, we specify the identifier, the nonce (in our example 0 for the fungible tokens and a specific value for the NFT) and the amount. In the end, like with the `ESDTTransfer`, we pass the name of the method we want to call and the rest of the parameters of that specific method.
335
-
336
301
:::tip
337
302
More information about ESDT Transfers [here](/tokens/fungible-tokens/#transfers).
0 commit comments