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:
npm install @meshsdk/contract
Initialize the contract
To initialize the payment splitter, we need to initialize a provider, MeshTxBuilder
and MeshSwapContract
.
import { MeshSwapContract } from "@meshsdk/contract";
import { MeshTxBuilder } from "@meshsdk/core";
const provider = new BlockfrostProvider('<Your-API-Key>');
const meshTxBuilder = new MeshTxBuilder({
fetcher: provider,
submitter: provider,
});
const contract = new MeshSwapContract({
mesh: meshTxBuilder,
fetcher: provider,
wallet: wallet,
networkId: 0,
});
Both on-chain and off-chain codes are open-source and available on Mesh Github Repository.
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 Swap
Initiate a swap by defining the assets for the swap contract
Amount lovelace to give
10000000
Asset to receive
d9312da562da182b02322fd8acb536f37eb9d29fba7c49dc172555274d657368546f6b656e
const assetToProvide: Asset = {
unit: "lovelace",
quantity: '10000000',
};
const assetToReceive: Asset = {
unit: 'd9312da562da182b02322fd8acb536f37eb9d29fba7c49dc172555274d657368546f6b656e',
quantity: "1",
};
const tx = await contract.initiateSwap([assetToProvide], [assetToReceive]);
const signedTx = await wallet.signTx(tx);
const txHash = await wallet.submitTx(signedTx);
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 Swap
Accept a swap by providing the assets to the swap contract
Tx hash
Tx hash
const utxo = await contract.getUtxoByTxHash('');
const tx = await contract.acceptSwap(utxo);
const signedTx = await wallet.signTx(tx);
const txHash = await wallet.submitTx(signedTx);
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 Swap
Cancel a swap to get your funds back
Tx hash
Tx hash
const utxo = await contract.getUtxoByTxHash('');
const tx = await contract.cancelSwap(utxo);
const signedTx = await wallet.signTx(tx);
const txHash = await wallet.submitTx(signedTx);