Swing Points Program is Live 🚀 Learn More →

Sending a Transaction

After getting a quote or executing an approval transaction, you'll next have to send a transaction to Swing's Cross-Chain API.

The steps for sending a transaction are as followed:

The full API reference for our /send endpoint can be found in the API reference section.

Making a Request

POST: https://swap.prod.swing.xyz/v0/transfer/send

Body Parameters:

KeyExampleDescription
fromChainethereumThe blockchain where the transaction originates.
fromTokenAddress0x0000000000000000000000000000000000000000Source Token Address
fromUserAddress0x018c15DA1239B84b08283799B89045CD476BBbBbSender's wallet address
tokenSymbolETHSource token slug
toTokenAddress0x0000000000000000000000000000000000000000Destination token address.
toChainpolygonDestination chain
toTokenAmount1000000000000000000Amount of the destination token to be received.
toTokenSymbolMATICDestination Chain slug
toUserAddress0x018c15DA1239B84b08283799B89045CD476BBbBbReceiver's wallet address
tokenAmount1000000000000000000Amount of the source token being sent (in wei for ETH).
projectIdreplugYour project's ID
routesee Requesting a qouteSelected route

Sample Request

const result = await axios.post(
  'https://swap.prod.swing.xyz/v0/transfer/send',
  {
    fromChain: 'polygon',
    tokenSymbol: 'USDC',
    fromTokenAddress: '0xcbe56b00d173a26a5978ce90db2e33622fd95a28',
    fromUserAddress: '0x018c15DA1239B84b08283799B89045CD476BBbBb',
    toChain: 'ethereum',
    toTokenSymbol: 'ETH',
    toTokenAddress: '0x0000000000000000000000000000000000000000',
    toUserAddress: '0x018c15DA1239B84b08283799B89045CD476BBbBb',
    tokenAmount: '1000000000',
    projectId: 'replug',
    route: [
      {
        bridge: 'dodo',
        bridgeTokenAddress: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f',
        steps: ['allowance', 'approve', 'send'],
        name: 'USDT',
        part: 100,
      },
    ],
  },
);

Signing transaction callData

It's important to note that the /send endpoint only builds an unsigned transaction. To actually send the transaction, you'll have to give Swing the permission to do so by signing the transaction using a non-custodial wallet like MetaMask or Coinbase.

Now that we've got our txData, we next need to invoke a function in one of Swing's contracts to be able to take funds from a user's wallet and begin the transaction.

We'll be using ethers.js with MetaMask Wallet to demonstrate how you can execute our txData, but feel free to use whatever wallet provider you're comfortable using.

The txData from the /send will look something like this:

{
   ....
    "tx": {
        "from": "0x018c15DA1239B84b08283799B89045CD476BBbBb",
        "to": "0x39E3e49C99834C9573c9FC7Ff5A4B226cD7B0E63",
        "data": "0x301a3720000000000000000000000000eeeeeeeeeeee........",
        "value": "0x0e35fa931a0000",
        "gas": "0x06a02f"
    }
   ....
}

First, we'll call the /send endpoint via the sendTransaction() function and retrieve our txData from it's response:

const transaction = await sendTransaction();
 
let txData = {
  data: transaction.tx.data,
  from: transaction.tx.from,
  to: transaction.tx.to,
  value: transaction.tx.value,
  gasLimit: transaction.tx.gas,
};

Next, we'll create a function called getSigner() to retrieve our Signer Object using MetaMask's Injected Provider:

async function getSigner() {
  const provider = await new ethers.providers.Web3Provider(window.ethereum);
  return provider.getSigner();
}

Now that we've got our Signer Object, we can go ahead and execute the txData by calling the sendTransaction() function present in our signer object.

async function executeTxData(txData) {
  const signer = await getSigner();
 
  const txResponse = await signer.sendTransaction(txData);
 
  const receipt = await txResponse.wait();
  console.log('Transaction receipt:', receipt);
 
  return txResponse.txHash; // We'll need this later for checking the transaction's status
}

Finally, immediately after we get our txData from the /send Response, we'll prompt a user's MetaMask wallet to execute our txData by calling executeTxData():

const transaction = await sendTransaction();
 
let txData = {
  data: transaction.tx.data,
  from: transaction.tx.from,
  to: transaction.tx.to,
  value: transaction.tx.value,
  gasLimit: transaction.tx.gas,
};
 
const txHash = await executeTxData(txData); // This will open MetaMask and ask the user to confirm the transaction
 
console.log(txHash);