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
We want to offer an overview on how smart contracts process payments. This includes two complementary parts: receiving tokens and sending them.
11
11
12
12
:::important important
13
-
On MultiversX it is impossible to send both EGLD and any ESDT token at the same time.
13
+
On MultiversX it is possible to send one or more tokens with any transaction. This includes EGLD, and it is also possible (though impractical) to send several payments of the same token at once.
14
+
:::
15
+
14
16
15
-
For this reason you will see no syntax for transferring both, neither when sending, nor receiving.
17
+
:::note note
18
+
Historically, it used to be impossible to send EGLD and ESDT at the same time, this is why some of the legacy APIs have this restriction. This restriction no longer applies since the [Spica release](https://multiversx.com/release/release-spica-patch-4-v1-8-12).
16
19
:::
17
20
18
21
---
@@ -29,7 +32,7 @@ There are two ways in which a smart contract can receive payments:
29
32
30
33
### Receiving payments directly
31
34
32
-
Sending EGLD and ESDT tokens directly to accounts works the same way for EOAs (extrernally owned accounts) as for smart contracts: the tokens are transferred from one account to the other without firing up the VM.
35
+
Sending EGLD and ESDT tokens directly to accounts works the same way for EOAs (externally owned accounts) as for smart contracts: the tokens are transferred from one account to the other without firing up the VM.
33
36
34
37
However, not all smart contracts are allowed to receive tokens directly. There is a flag that controls this, called "payable". This flag is part of the [code metadata](/developers/data/code-metadata), and is specified in the transaction that deploys or upgrades the smart contract.
35
38
@@ -39,13 +42,27 @@ The rationale for this is as follows: the MultiversX blockchain doesn't offer an
39
42
40
43
### Receiving payments via endpoints
41
44
42
-
The most common way for contracts to accept payments is by having endpoints annotated with the `#[payable(...)]` annotation.
45
+
The most common way for contracts to accept payments is by having endpoints annotated with the `#[payable]` annotation (or `#[payable("*")]`).
43
46
44
47
:::important important
45
48
The "payable" flag in the code metadata only refers to direct transfers. Transferring tokens via contract endpoint calls is not affected by it in any way.
46
49
:::
47
50
48
-
If an endpoint only accepts EGLD, it should be annotated with `#[payable("EGLD")]`:
51
+
To accept any kind of payment, annotate the endpoints with `#[payable]`:
52
+
53
+
```rust
54
+
#[endpoint]
55
+
#[payable]
56
+
fnaccept_any_payment(&self) {
57
+
// ...
58
+
}
59
+
```
60
+
61
+
Usually on the first line there will be an instruction that processes, interprets, and validates the received payment ([see below](#call-value-methods))
62
+
63
+
64
+
65
+
If an endpoint only accepts EGLD, it can be annotated with `#[payable("EGLD")]`, although this is slowly falling out of favor.
49
66
50
67
```rust
51
68
#[endpoint]
@@ -55,36 +72,148 @@ fn accept_egld(&self) {
55
72
}
56
73
```
57
74
58
-
When annotated like this, the contract will reject any ESDT payment. Calling this function without any payment will work.
59
75
60
-
To accept any kind of payment, do annotate the endpoints with `#[payable("*")]`:
76
+
:::note Multi-transfer note
77
+
Note that it is currently possible to send two or more EGLD payments in the same transaction. The `#[payable("EGLD")]` annotation rejects that.
78
+
:::
79
+
80
+
This snippet is equivalent to:
61
81
62
82
```rust
63
83
#[endpoint]
64
-
#[payable("*")]
65
-
fnaccept_any_payment(&self) {
66
-
// ...
84
+
#[payable]
85
+
fnaccept_egld(&self) {
86
+
letpayment_amount=self.call_value().egld();
87
+
// ...
67
88
}
68
89
```
69
90
91
+
92
+
70
93
:::note Hard-coded token identifier
71
94
It is also possible to hard-code a token identifier in the `payable`, e.g. `#[payable("MYTOKEN-123456")]`. It is rarely, if ever, used, tokens should normally be configured in storage, or at runtime.
72
95
:::
73
96
97
+
[comment]: #(mx-context-auto)
98
+
99
+
## Payment Types
100
+
101
+
The framework provides a unified approach to handling payments using the `Payment` type that treats EGLD and ESDT tokens uniformly. EGLD is represented as `EGLD-000000` token identifier, making all payment handling consistent.
102
+
103
+
**`Payment<A>`** - The primary payment type that combines:
104
+
-`token_identifier`: `TokenId<A>` - unified token identifier (EGLD serialized as "EGLD-000000")
105
+
-`token_nonce`: `u64` - token nonce for NFTs/SFTs, which is zero for all fungible tokens (incl. EGLD)
**`PaymentVec<A>`** - A managed vector of `Payment<A>` objects, representing multiple payments in a single transaction.
109
+
110
+
[comment]: #(mx-context-auto)
111
+
112
+
## Call Value Methods
113
+
74
114
Additional restrictions on the incoming tokens can be imposed in the body of the endpoint, by calling the call value API. Most of these functions retrieve data about the received payment, while also stopping execution if the payment is not of the expected type.
115
+
116
+
[comment]: #(mx-context-auto)
117
+
118
+
### `all()` - Complete Payment Collection
119
+
120
+
`self.call_value().all()` retrieves all payments sent with the transaction as a `PaymentVec<A>`. It handles all tokens uniformly, including EGLD (represented as "EGLD-000000"). Never stops execution.
`self.call_value().single()` expects exactly one payment and returns it. Will halt execution if zero or multiple payments are received. Returns a `Payment<A>` object.
`self.call_value().single_optional()` accepts either zero or one payment. Returns `Option<Payment<A>>` for graceful handling. Will halt execution if multiple payments are received.
158
+
159
+
```rust
160
+
#[payable]
161
+
#[endpoint]
162
+
pubfnexecute_with_optional_fee(&self) {
163
+
matchself.call_value().single_optional() {
164
+
Some(payment) => {
165
+
// Process the payment as fee
166
+
self.execute_premium_service(payment);
167
+
},
168
+
None=> {
169
+
// Handle no payment scenario
170
+
self.execute_basic_service();
171
+
}
172
+
}
173
+
}
174
+
```
175
+
176
+
[comment]: #(mx-context-auto)
177
+
178
+
### `array()` - Fixed-Size Payment Array
179
+
180
+
`self.call_value().array<N>()` expects exactly N payments and returns them as a fixed-size array. Will halt execution if the number of payments doesn't match exactly.
181
+
182
+
```rust
183
+
#[payable]
184
+
#[endpoint]
185
+
pubfnswap(&self) {
186
+
// Expect exactly 2 payments for the swap
187
+
let [input_payment, fee_payment] =self.call_value().array();
The following methods are available for backwards compatibility but may be deprecated in future versions:
203
+
75
204
-`self.call_value().egld_value()` retrieves the EGLD value transferred, or zero. Never stops execution.
76
205
-`self.call_value().all_esdt_transfers()` retrieves all the ESDT transfers received, or an empty list. Never stops execution.
77
206
-`self.call_value().multi_esdt<N>()` is ideal when we know exactly how many ESDT transfers we expect. It returns an array of `EsdtTokenPayment`. It knows exactly how many transfers to expect based on the return type (it is polymorphic in the length of the array). Will fail execution if the number of ESDT transfers does not match.
78
207
-`self.call_value().single_esdt()` expects a single ESDT transfer, fails otherwise. Will return the received `EsdtTokenPayment`. It is a special case of `multi_esdt`, where `N` is 1.
79
208
-`self.call_value().single_fungible_esdt()` further restricts `single_esdt` to only fungible tokens, so those with their nonce zero. Returns the token identifier and amount, as pair.
80
209
-`self.call_value().egld_or_single_esdt()` retrieves an object of type `EgldOrEsdtTokenPayment`. Will halt execution in case of ESDT multi-transfer.
81
210
-`self.call_value().egld_or_single_fungible_esdt()` further restricts `egld_or_single_esdt` to fungible ESDT tokens. It will return a pair of `EgldOrEsdtTokenIdentifier` and an amount.
82
-
-`self.call_value().any_payment()` is the most general payment retriever. Never stops execution. Returns an object of type `EgldOrMultiEsdtPayment`.
211
+
-`self.call_value().any_payment()` is the most general payment retriever. Never stops execution. Returns an object of type `EgldOrMultiEsdtPayment`.*(Deprecated since 0.64.0 - use `all()` instead)*
83
212
84
213
---
85
214
86
215
[comment]: #(mx-context-auto)
87
216
88
217
## Sending payments
89
218
90
-
We have seen how contracts can accommodate receiving tokens. Sending them is, in principle, even more straightforward, as it only involves specializing the `Payment` generic of the transaction using specific methods, or better said, attaching a payload to a regular transaction. Read more about payments [here](../transactions/tx-payment.md).
219
+
We have seen how contracts can accommodate receiving tokens. Sending them is, in principle, even more straightforward, as it only involves specializing the `Payment` generic of the transaction using specific methods, essentially attaching a payload to a regular transaction. Read more about payments [here](../transactions/tx-payment.md).
@@ -218,20 +218,28 @@ Sometimes we don't have ownership of the token identifier object, or amount, and
218
218
For brevity, instead of `payment(EsdtTokenPaymentRefs::new(&token_identifier, token_nonce, &amount))`, we can use `.single_esdt(&token_identifier, token_nonce, &amount)`.
0 commit comments