Swap
Swap contract facilitates the exchange of assets between two parties
Swap contract facilitates the exchange of assets between two parties. This contract is designed to be used in a peer-to-peer exchange scenario where two parties agree to exchange assets. The contract ensures that the assets are locked up until it is accepted by the other party. At any point before it is accepted, one can cancel the swap to retrieve the assets.
There are 2 actions (or endpoints) available to interact with this smart contract:
- initiate swap
- accept asset
- cancel swap
Install package
First you can to install the @meshsdk/contracts
package:
Initialize the contract
To initialize the swap, we need to initialize a provider, MeshTxBuilder
and MeshSwapContract
.
Both on-chain and off-chain codes are open-source and available on Mesh Github Repository.
Mint a Mesh Token to try demos
Initiate Swap
User A can initiate a swap by providing assets to the swap contract.
initiateSwap()
initiate a swap. The function accepts the following parameters:
- toProvide (Asset[]) - a list of assets user A is trading
- toReceive (Asset[]) - a list of assets user A is expecting to receive from another user
Note that the parameters are arrays, so you can provide multiple assets to the swap, and these assets can be tokens and lovelace.
Initiate a swap by defining the assets for the swap contract
Connect wallet to run this demo
Accept Swap
User B can accept a swap by providing the swap transaction hash to the contract.
acceptSwap()
accept a swap. The function accepts the following parameters:
- swapUtxo (UTxO) - the utxo of the transaction in the script for the swap
The function accepts a swap transaction hash and returns a transaction hash if the swap is successfully accepted.
A successful transaction will send the assets to the wallet that signed the transaction to accept the swap.
Accept a swap by providing the assets to the swap contract
Connect wallet to run this demo
Cancel Swap
Any any time before swap is accepted, user A can cancel the swap.
cancelSwap()
cancel a swap. The function accepts the following parameters:
- swapUtxo (UTxO) - the utxo of the transaction in the script for the swap
The function accepts a swap transaction hash and returns a transaction hash if the swap is successfully canceled.
Cancel a swap to get your funds back
Connect wallet to run this demo