Execute a Staking and Withdrawal Transaction

Once you have selected a route, you can execute your staking transaction.

If you don't already have a route, follow these steps.

Start the transfer

Use the same transfer parameters you used when finding a route. This will handle the entire transfer process for you, including prompting the wallet for any required actions.

const transferParams = {
  type: 'deposit',
  fromChain: 'ethereum',
  toChain: 'ethereum',
  fromToken: 'SWING',
  toToken: 'aEthWETH',
  amount: '10',
  fromUserAddress: '0x018c15DA1239B84b08283799B89045CD476BBbBb',
  toUserAddress: '0x018c15DA1239B84b08283799B89045CD476BBbBb',
};
 
await swingSDK.transfer(transferRoute, transferParams);

Receive status updates

Use these events to update your UI with helpful information for your users.

swingSDK.on('TRANSFER', (transfer) => {
  switch (transfer.status) {
    case 'PENDING':
      console.log(`Creating a transaction for the ${transfer.step} step`);
      break;
    case 'CHAIN_SWITCH_REQUIRED':
      // Handle switching chains or alert the user to do it manually
      break;
    case 'ACTION_REQUIRED':
      console.log('Please complete the required action within your connected wallet');
      break;
    case 'CONFIRMING':
      console.log(`Waiting for the transaction from the ${transfer.step} step to complete`);
      break;
    case 'SUCCESS':
      console.log(`Transfer has completed the ${transfer.step} step`);
      break;
    case 'FAILED':
      console.log(`Transfer failed at the ${transfer.step} step:`, transfer.error);
      break;
  }

Withdraw Funds

You can withdraw funds from a staking position (unstake) by changing the type from deposit to withdraw in the transferParams. This will handle the entire withdrawal process for you, including prompting the wallet for any required actions.

const transferParams = {
  type: 'withdraw',
  fromChain: 'ethereum',
  toChain: 'ethereum',
  fromToken: 'SWING',
  toToken: 'SWING',
  amount: '100',
  fromUserAddress: '0x018c15DA1239B84b08283799B89045CD476BBbBb',
  toUserAddress: '0x018c15DA1239B84b08283799B89045CD476BBbBb',
};
 
await swingSDK.transfer(transferRoute, transferParams);