Skip to main content

Quickstart: minimal working example

The following example demonstrates a complete transaction flow:
  1. Fetches a quote from delora API for token swap
  2. Checks and approves token allowance if needed
  3. Executes the transaction using ethers.js v5
⚠️ Security Note: Never hardcode private keys or mnemonics in production code. Use environment variables or secure key management solutions.

React Integration Example

For React applications, you can integrate the swap function as follows:

Breaking it down

1. Building the quote request

The getQuote function constructs a query string from parameters and fetches the quote from the API: senderAddress is required, while receiverAddress is optional for same-type routes like this one and defaults to senderAddress.
Key parameters:
  • originChainId / destinationChainId: Chain IDs (e.g., 1 for Ethereum, 137 for Polygon)
  • amount: Token amount in smallest unit (wei, satoshi, etc.)
  • originCurrency / destinationCurrency: Token contract addresses (0x0...0 for native tokens)
  • senderAddress: Required wallet address
  • receiverAddress: Optional for same-type routes; defaults to senderAddress. For routes across different chain types, provide it explicitly.

2. Wallet provider setup

The function accepts a Web3Provider from ethers.js, which can be obtained from various sources: Browser extension (MetaMask, etc.):
RPC endpoint:
With wallet from mnemonic (development only):
⚠️ Never hardcode mnemonics or private keys in production code.

3. Token allowance and approval

For ERC-20 tokens (non-native), the contract must be approved to spend tokens on behalf of the user:
Notes:
  • Native tokens (ETH, BNB, etc.) don’t require approval
  • The spender address (quote.calldata.to) is the contract that will execute the swap
  • Using MaxUint256 avoids repeated approvals, but you can use the exact amount for better security

4. Transaction execution

The quote response includes transaction parameters (to, data, value) and optional gas estimates: