Skip to content

Commit

Permalink
Merge pull request #2030 from aeternity/spends-via-contract
Browse files Browse the repository at this point in the history
docs(chain): compare spend tx vs spend via contract
  • Loading branch information
davidyuk authored Nov 18, 2024
2 parents 2d6ec81 + f9e2c4a commit ec40d77
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/guides/batch-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,46 @@ This way, SDK would make a single request to get info about the sender account a

Additionally, you may want to set `gasPrice` and `fee` to have predictable expenses. By default, SDK sets them based on the current network demand.

## Multiple spends via contract call

If you need to transfer a fixed AE amount from the same account to more than 24 accounts, it is more efficient to do it via a smart contract.

Let's define and deploy a contract

```
include "List.aes"
contract MultipleSpends =
payable stateful entrypoint spend(addresses : list(address), amount: int) =
List.foreach(addresses, (address) => Chain.spend(address, amount))
```

after deployment this contract, it can be called as follows

```js
await contract.spend(addresses, amount, { amount: amount * addresses.length });
```

Pros of this approach:

- less fees if more than 24 recipients;
- faster to execute because it needs to mine fewer transactions.

Cons:

- the amount of recipients in one transaction limited by the maximum gas in a block (about 800 recipients);
- harder to implement.

### Fees difference

One spend tx takes approximately 0.0000167ae. The above contract deployment is 0.0000815ae. The base contract call price is 0.000182ae. Adding an address to the list costs about 0.0000058ae. So, it is more efficient to use a contract if you have more than 24 recipients. And more than 17 recipients if you use a pre-deployed contract.

| Recipients | Fee savings for batch spending via contract |
| ---------- | ------------------------------------------- |
| 1 000 | 0.0107ae |
| 100 000 | 1.07ae |
| 10 000 000 | 107ae |

## Multiple contract static calls

Basically, the dry-run endpoint of the node is used to run them. Doing requests one by one, like
Expand Down

0 comments on commit ec40d77

Please sign in to comment.