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:
- First, we will make a request to
https://swap.prod.swing.xyz/v0/transfer/send
- Using the
txData
returned from the/send
request, sign the transaction by using a user's wallet
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:
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 | 1000000000000000000 | 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 Requesting a qoute | Selected route |
Sample Request
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:
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()
: