Step 4: Initiate transfer transaction
Initiate transaction given the prior quote.
The full API reference for our
/send
endpoint can be found in the API reference section.
Similar to our quoting system, to initiate a transaction it requires all the following required information:
- Bridge
- Source chain
- Destination chain
- Source Token on the Source chain
- Destination Token on the Destination chain
- The amount you want to send
- Source Wallet Address to pull funds from
- Contract Call Information
Making a Request
POST: https://swap.prod.swing.xyz/v0/transfer/send
Body Parameters:
Key | Example | Description |
---|---|---|
fromChain | ethereum | The blockchain where the transaction originates. |
fromTokenAddress | 0x0000000000000000000000000000000000000000 | Source Token Address |
fromUserAddress | 0x018c15DA1239B84b08283799B89045CD476BBbBb | Sender's wallet address |
tokenSymbol | ETH | Source token slug |
toTokenAddress | 0x0000000000000000000000000000000000000000 | Destination token address. |
toChain | polygon | Destination chain |
toTokenAmount | 4376081 | Amount of the destination token to be received. |
toTokenSymbol | MATIC | Destination Chain slug |
toUserAddress | 0x018c15DA1239B84b08283799B89045CD476BBbBb | Receiver's wallet address |
tokenAmount | 1000000000000000000 | Amount of the source token being sent (in wei for ETH). |
projectId | replug | Your project's ID |
route | see Get Qoute | Selected route |
contractCallInfo | see Generating Contract Calldata section | Target contract info for deposits |
Sample Request
Generating Contract Calldata
For Contract Calls, Swing's /send
endpoint gives us the option to instruct Swing's API on what to do with our funds post swap.
For example, we can instruct Swing to deposit our funds into a smart contract of our choosing. A more practical instruction would be to stake our funds into a staking contract, and then send us that staking contract's LP token. We can achieve this using the contractCallInfo
object parameter in the /send
endpoint.
To execute a contract call via Swing, we'll need three (3) parameters to be passed to our contractCallInfo
object:
Property | Example | Description | |
---|---|---|---|
toContractAddress | 0x9ee91F9f426fA633d227f7a9b000E28b9dfd8599 | required | Smart Contract address we'll deposit your funds into |
toContractCallData | 0xa9059cbb0000000000000000000000abcdef........ | required | Calldata to be passed to Swing |
outputTokenAddress | 0x018c15DA1239B84b08283799B89045CD476BBbBb | optional | Address of Liquidity Provider Token to be received by sender |
When performing a contract call transaction, you are required to provide the contract calldata for the contract to which you wish to send your funds.
Let's say we've got some USDC on the Polygon chain and want to stake some ETH into LIDO's Ethereum staking contracts via Swing. The following will take place to make this transaction to be possible:
- Swing will start the transaction by deducting the necessary funds from the
fromUserAddress
. - Swing will then bridge our USDC (
tokenSymbol
) from the Polygon chain (fromChain
) to the Ethereum Chain (toChain
). - Swing will then swap that USDC (
tokenSymbol
) to ETH (toTokenSymbol
) on the destination chain (toChain
) and send it to wallet address supplied to thetoUserAddress
. - Using the
contractCallInfo
parameter, Swing will then execute a function on a target Smart Contract and deposit your funds. - Optionally, If the target smart contract emits any token to the sender's wallet like an LP token, Swing can then send those token to the address supplied to the
toUserAddress
.
If you omit the
toUserAddress
parameter, Swing will default it's value to whatever value you supply to thefromUserAddress
.
To get started, we'll first need to generate the necessary callData
by using LIDO's staking contract ABI:
It's very important to remember what we're trying to achieve here. We want Swing to interact with LIDO's contract immediately after swapping our funds. When Swing is done swapping our assets, we want Swing to call the submit
function on LIDO's contract.
For this example, we'll be using ethers.js to generate the callData
required for our contract call example.
Using ethers to generate our callData is fairly straight forward. First we'll load up LIDO's staking smart contract using it's ABI and contract address:
Next, we'll encode the function data by using the encodeFunctionData()
method to generate the final callData for the submit()
function in LIDO's staking contract.
LIDO's submit()
function takes two parameters, namely:
_amount
— the amount of tokens to be staked_referral
— an address for incentivisation reward
Putting it all together into a function called generateCallData()
:
Next, let's generate our callData and make a request to Swing to get our transaction ready to be sent over to the network:
Since performing a contract call will change the state of a user's wallet, the next step of this process requires invoking a contract-level function on the wallet address you supply to the fromUserAddress
parameter. Swing only returns the necessary call-data that will need to be signed and executed by the local wallet (i.e. Metamask, WalletConnect, Coinbase Wallet).
Sending our transaction over the network
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:
First, we'll call the /send
endpoint via the sendTransaction()
function and retrieve our txData
from it's response:
Next, we'll create a function called getSigner()
to retrieve our Signer Object using MetaMask's Injected Provider:
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.
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()
: