Quickstart
This section of the documentation will be focused on teaching you how to effortlessly bridge and swap your digital assets by example using the Swing API.
Our cross-chain APIs are robust and enables you to do any of the following:
- Fetch a list of chains and tokens.
- Perform same chain and cross chain swaps.
- Perform same chain and cross chain deposits/staking.
Our APIs also support cross-chain operations between multiple blockchains of different architectures, enabling developers to bridge their assets between the EVM and BTC chains, for example.
Steps: Executing a Bridge & Swap
- 1) Gather the Transaction Info:
- Gather info on the source chain and token, destination chain and token, amount, and projectId.
- See Gathering transaction info
- 2) Request a Quote:
- Endpoint: /quote
- Get a quote from one of Swing's integrated partners.
- See Requesting a Quote
- 3) Get Allowance Limit and Approve ERC20 Tokens (Optional):
- Endpoint: /allowance
- Get approval for non-native token transfers/swaps.
- See Requesting Approval Calldata
- 4) Sign Approval CallData (Optional):
- Endpoint: /approve
- Execute approval callData smart contract transaction using a wallet.
- See Requesting Approval Calldata
- 5) Send a Transaction:
- Endpoint: /send
- Intiating transaction given the prior quote on a specific bridge.
- See Sending a Transaction
- 6) Sign Transaction CallData:
- Execute send callData smart contract transaction using a wallet.
- See Signing Transaction CallData
- 7) Poll Transaction Status:
- Endpoint: /status
- Wait for transaction to complete by polling for transaction status
- See Checking Transaction Status
The full API reference for our /quote, /allowance, /approve, /send and /status endpoints can be found in the API reference section.
Due to compliance reasons, to give your users the best user experience and to prevent
failed
transactions, we advise you to notify your users whether they're connecting from a restricted jurisdiction or not. Learn more here.
Bridge and Swap by example (Quick Copy and Paste Tutorial)
Let's get you started by demonstrating how you can quickly build up a transaction and send it over to Swing using a short example!
For our example, we're going to be bridging some USDC from the Ethereum network over to the MATIC on the Polygon network. For our API request client, we will be using axios for querying the Swing API.
Step 1: Gather transaction info
Before beginning a transaction, we must gather the source and destination chain information. Below are the list of parameter definitions that the Swing API accepts:
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 Source slug |
toTokenAmount | 1000000000000000000 | Amount of the destination token being 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). |
type | swap | Type of transaction. |
projectId | replug | Your project's ID |
route | See Requesting a Quote page | Selected Route from /quote endpoint |
Sample payload:
Step 2: Get a quote
Once we have our source and destination chain details, we have to request a qoute from Swing via the /qoute
endpoint.
The quote contains a list of possible routes that can swap or bridge a user's assets. Each route includes the total amount of tokens a wallet holder must bridge, as well as the fees required for the transaction to go through.
The first item in the quotes array will always be the cheapest route, hence the term "Best Route".
The response schema for the quote endpoint can be found on the Request a Quote page.
Step 3: Get spender allowance limit (Optional)
Next, we have to verify if the wallet holder has already authorized Swing to access the transaction amount specified in the quote.
This endpoint return a property that holds the approved allownace called allowance
:
Step 4: Get & Sign approval callData (Optional)
Next, we have to determine whether an approval is required. To do that, we have to check if the allowance
from the /allowance
endpoint is less than or equal to the tokenAmount
specified in the quoteParams
. If the allowance amount is less than or equal to the tokenAmount, then we must execute an approval to continue.
The /approval
endpoint returns an unsigned callData that we must sign using a wallet like MetaMask or Coinbase Wallet.
The response schema for the quote endpoint can be found on the Approving Non-Native Tokens page.
Step 5: Send transaction to Swing using the bestRoute
via the /send
The /send
endpoint will return an unsigned transaction callData
that we will have to sign using a user's wallet:
The unsigned callData will be returned in the tx
object in the response
Step 6: Sign transaction using a wallet using ethers.js and MetaMask
Pass the tx
details to ether's sendTransaction
function to sign the transaction and send it over to the network
Executing this will bring up a MetaMask wallet prompt for the wallet holder to approve. The transaction officially begins once the wallet holder has approved the transaction.
Step 7: Check Transaction Status
After the transaction has been mined and a block created, you can check for a transaction's status via the /status
endpoint by using the id
property from the /send
endpoint and our transaction's txHash
:
Native Vs. Non-Native Bridge/Swaps
Native tokens are the fundamental currency of a blockchain. They are integral to the blockchain's protocol and are used for various purposes, including transactions and paying fees. For example, on the Bitcoin blockchain, the native token is Bitcoin (BTC), and on the Ethereum blockchain, it’s Ether (ETH).
Smart Contract tokens like ERC20 tokens, on the other hand, are not native to the blockchain but are a type of fungible Smart Contract based token standard used primarily on EVM-compatible blockchains.
Why this matters?
When sending a transaction involving an ERC20 token on the source chain, it is important to understand that if the transaction requires a smart contract to transfer tokens on behalf of the wallet holder, the wallet holder must first grant approval to that smart contract. This approval allows the smart contract to access and manage a specified amount of tokens from the user's wallet.
Native tokens, unlike ERC20 tokens, do not require any approval processes because they are the primary currency of the blockchain. This means that transactions involving native tokens can be executed directly without the need for a smart contract approval step.
Examples/Starter Projects
You can check out our examples and starter projects with our API integrated, which you can use as references to build your apps here.