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

To initialize the swap, we need to initialize a provider, MeshTxBuilder and MeshSwapContract.

import { BlockfrostProvider, MeshTxBuilder } from '@meshsdk/core';
import { MeshSwapContract } from '@meshsdk/contracts';
import { useWallet } from '@meshsdk/react';

const { connected, wallet } = useWallet();

const blockchainProvider = new BlockfrostProvider(APIKEY);

const meshTxBuilder = new MeshTxBuilder({
  fetcher: blockchainProvider,
  submitter: blockchainProvider,
});

const contract = new MeshSwapContract({
  mesh: meshTxBuilder,
  fetcher: blockchainProvider,
  wallet: wallet,
  networkId: 0,
});

Both on-chain and off-chain codes are open-source and available on Mesh Github Repository.

You may need to mint some Mesh Token to interact with this demo. Connect your wallet and click the button below to mint a Mesh Token.

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.

In this demo, user A is initiating a swap by providing 10 ADA to the swap for a Mesh Token.

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.

User B accept the trade by paying for the assets requested to complete the swap. This returns a transaction hash if the swap is successfully accepted.

const utxo = await contract.getUtxoByTxHash(txHashToSearchFor);
if (!utxo) {
  setResponseError('Input utxo not found');
  setLoading(false);
  return;
}
const tx = await contract.acceptSwap(utxo);
const signedTx = await wallet.signTx(tx, true);
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 accepted.

Any any time before swap is accepted, user A can cancel the swap.

const utxo = await contract.getUtxoByTxHash(txHashToSearchFor);
if (!utxo) {
  setResponseError('Input utxo not found');
  setLoading(false);
  return;
}

const tx = await contract.cancelSwap(utxo);

const signedTx = await wallet.signTx(tx, true);
const txHash = await wallet.submitTx(signedTx);