Initiating a Swap
After you have gotten a price quote from the Broker, you can use the sdk to create a swap transaction object which you can then submit to the network.
So far in the previous examples we have used only a JsonRpcProvider
for interacting with the contracts. This is fine for read-only contract calls, but in order to generate a signed transaction object you will need to instantiate the SDK with an Ethers signer.
For state changing operations (e.g. swaps) the SDK returns TransactionRequest Ethers objects populated with information from the signer. Submitting the transaction to the network is left to the consumer to give flexibility for apps integrating the SDK for handling the final transaction lifecycle.
In this example we will use a Wallet
Ethers signer object which can be instantiated with a private key:
We will follow along the previous of example of swapping 1 CELO
token into cUSD
, so we can get a price quote in the same way we previously did:
Before we can submit the swap, we will need to approve the Broker
contract to spend CELO
tokens on our behalf. The Mento class provides a convenient function for increasing the allowance for a given token by a certain amount:
You can also opt for increasing the broker allowance to a larger amount to not have to execute the above step on every single swap.
After the transaction is included in a block and the allowance is increased we can use a similar process for submitting the swap transaction to the network:
Similar to the getAmountIn
price quote method where we get the amount of tokens needed to buy a specific amount of another desired token, the Mento class also exposes a swapOut
method which takes both the desired amountOut
as well as a maxAmountIn
parameter. Try it on your own!
You can find the full runnable code for this section within the mento-sdk-examples repo:
Last updated