Initiate a Swap

The first step is to create a swap in the smart contract

Setting up a swap in the target chain's smart contract involves a single call to the smart contract but it requires some preparation.

Calling the Smart Contract

Get values for SWAP_CONTRACT_ADDRESS here

Example

const SWAP_CONTRACT_ADDRESS = "0x..."; // get this from the docs for your network
const ABI = '{
  "constant": false,
  "inputs": [
    {
      "internalType": "address",
      "name": "_receiver",
      "type": "address"
    },
    {
      "internalType": "bytes32",
      "name": "_hashlock",
      "type": "bytes32"
    },
    {
      "internalType": "uint256",
      "name": "_timelock",
      "type": "uint256"
    },
    {
      "internalType": "bool",
      "name": "_allowLateWithdrawal",
      "type": "bool"
    },
    {
      "internalType": "address",
      "name": "_tokenContract",
      "type": "address"
    },
    {
      "internalType": "uint256",
      "name": "_amount",
      "type": "uint256"
    }
  ],
  "name": "newContract",
  "outputs": [
    {
      "internalType": "bytes32",
      "name": "contractId",
      "type": "bytes32"
    }
  ],
  "payable": false,
  "stateMutability": "nonpayable",
  "type": "function"
}';

erc20ApproveABI = '{
    "constant": false,
    "inputs": [
        {
            "name": "_spender",
            "type": "address"
        },
        {
            "name": "_value",
            "type": "uint256"
        }
    ],
    "name": "approve",
    "outputs": [
        {
            "name": "",
            "type": "bool"
        }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}';

const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();

/**
* This MUST be our SafeSwap receiver address if you want Safeswap to complete the swap.
* It indicates that this receiver will ultimately receive the original coins on the original network. 
* It is NOT the receiver on the destination chain, which will be identified in a future step.
* https://docs.safeswap.io/builders/api-integration-guide/our-swap-addresses
*/
const receiver = "0x..."

const secret = crypto.randomBytes(32); // You will need this secret later to unlock the funds on the destination chain.
const hash = crypto.createHash('sha256').update(secret).digest();
const hashlock = '0x' + hash.toString('hex');

// The timelock is the timestamp at which the swap can be refunded.
// For Safeswap, it has to be atleast 4 hours in the future.
const timelock = (Date.now() / 1000) + (4 * 60 * 60); // 4 hours in the future, in seconds.

// Late withdrawals are a safety feature and should be set to true.
// This allows the swap to continue even if the time period has passed.
const allowLateWithdrawal = true;

// This is the contract address for the ERC20 token that is being swapped away from.
const tokenAddress = "0x...";

// Amount in Wei of tokens the user wishes to swap.
// NOTE: This amount needs to be approved ahead of time by the user for the SafeSwap contract address.
const amount = ethers.utils.BigNumber.of("30000000000000000000"); // 30 tokens, with 18 decimals, in Wei.


// First a token approval must be made for this token to the SWAP_CONTRACT_ADDRESS
const token = new ethers.Contract(tokenAddress, erc20ApproveABI, signer);
await token.approve(SWAP_CONTRACT_ADDRESS, amount);

const safeSwap = new ethers.Contract(SWAP_CONTRACT_ADDRESS, ABI, signer);
const response = await safeSwap.newContract(receiver, hashlock, timelock, allowLateWithdrawal, tokenAddress, amount, { gasLimit: 300000 });

Last updated