Aggregate Swap

The purpose of this interface is to provide a convenient method for you to have the ability to exchange directly within the same chain.

Query

example:

const result = await axios.get(`https://api2.chainge.finance/v1/getAggregateSwap?chain=FSN&aggregator=chaingedex&fromTokenAddress=0xaB1F7E5BF2587543FE41f268c59d35dA95f046e0&fromDecimal=18&fromAmount=1000000000000000000&toTokenAddress=0x8A20c13B42D7fE418F10F922f2cEE06246c24269&toDecimal=6&sender=0xBda05e4421bF7e682635B7B18978f2dad4eCC059&recipient=0xBda05e4421bF7e682635B7B18978f2dad4eCC059&slippage=100`)
console.log(result)
// {
//     "code":  0,
//     "msg":  "success",
//     "data":  {
//               "amountOut":  "125088",
//               "from":  "0xbda05e4421bf7e682635b7b18978f2dad4ecc059",
//               "to":  "0x62bc5327868a998e1b3d9fe6037273e642a137ae",
//               "gas":  "0",
//               "value":  "0",
//               "data":  "0x38ed17390000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000001e3be00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000bda05e4421bf7e682635b7b18978f2dad4ecc0590000000000000000000000000000000000000000000000000000000065e0309e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000ab1f7e5bf2587543fe41f268c59d35da95f046e00000000000000000000000008a20c13b42d7fe418f10f922f2cee06246c24269"
//     }
// }
const { amountOut, from, to, gas, value, data } = result.data

When you need to exchange assets on the same chain, you can consider using this method. The interface returned by this method allows you to directly perform approve and swap actions. However, please note that because chain transactions are constantly changing in real-time, the gas estimate provided by this interface may not be accurate. You may need to make a new request to ensure the accuracy of the gas estimation for the transaction to be correctly submitted.

  1. If the user selects different tokens on the same chain (fromChain and toChain are the same) and the tokens are different (fromToken and toToken), call the getAggregateQuote API method to request a quote.

  2. Check if the result indicates that the transaction occurs on the same chain (chain, fromChain, and toChain are the same).

  3. If the transaction occurs on the same chain, proceed to step 4. Otherwise, follow the regular flow for cross-chain transactions.

  4. Use the getAggregateSwap API method to request the swap result.

  5. After successfully retrieving the swap result in step 4, perform the Approve contract operation based on the result parameters.

  6. Once step 5 is completed, execute the swap by calling the wallet's sendTransaction method.

Pseudo-code example:

// The user selects ETH/USDT => ETH/USDC, with an amount of 100.
// fromChain ETH, network 1
// toChain ETH, network 1
// fromToken USDT
// toToken USDC
const quoteResult = await axios.get('https://api2.chainge.finance/v1/getAggregateQuote?fromAmount=100000000&fromTokenAddress=0xdac17f958d2ee523a2206206994597c13d831ec7&fromDecimal=6&fromChain=ETH&toTokenAddress=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48&toDecimal=6&toChain=ETH')
// quoteResult
// {
//     "code": 0,
//     "msg": "success",
//     "data": {
//         "chain": "1",
//         "chainDecimal": 6,
//         "aggregator": "OpenOcean",
//         "outAmount": "100047625",
//         "outAmountUsd": "90.03574381",
//         "minOutAmount": "",
//         "serviceFee": "0",
//         "gasFee": "10001187",
//         "priceImpact": "0.06"
//     }
// }
const quoteChain = quoteResult.data.chain
if(fromChain.network === toChain.network && fromChain.network === quoteChain && toChain.network === quoteChain) {
    // call getAggregateSwap APi
    const result = await axios.get(`https://api2.chainge.finance/v1/getAggregateSwap?chain=FSN&aggregator=chaingedex&fromTokenAddress=0xaB1F7E5BF2587543FE41f268c59d35dA95f046e0&fromDecimal=18&fromAmount=1000000000000000000&toTokenAddress=0x8A20c13B42D7fE418F10F922f2cEE06246c24269&toDecimal=6&sender=0xBda05e4421bF7e682635B7B18978f2dad4eCC059&recipient=0xBda05e4421bF7e682635B7B18978f2dad4eCC059&slippage=100`)
    console.log(result)
    // {
    //     "code":  0,
    //     "msg":  "success",
    //     "data":  {
    //               "amountOut":  "125088",
    //               "from":  "0xbda05e4421bf7e682635b7b18978f2dad4ecc059",
    //               "to":  "0x62bc5327868a998e1b3d9fe6037273e642a137ae",
    //               "gas":  "0",
    //               "value":  "0",
    //               "data":  "0x38ed17390000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000001e3be00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000bda05e4421bf7e682635b7b18978f2dad4ecc0590000000000000000000000000000000000000000000000000000000065e0309e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000ab1f7e5bf2587543fe41f268c59d35da95f046e00000000000000000000000008a20c13b42d7fe418f10f922f2cee06246c24269"
    //     }
    // }
    const { amountOut, from, to, gas, value, data } = result.data
    // Call contract method allowance to determine whether approve is required
    const allowanceResult = await wallet.readContract({
        address: fromToken.contractAddress,
        abi: inscriptionABI,
        functionName: "allowance",
        args: [userAddress, to],
   });
   // check allowanceResult
   // Invoke the contract method approve
   const hash = await walletClient?.writeContract({
        address: fromToken.contractAddress,
        abi: inscriptionABI,
        functionName: "approve",
        account: userAddress,
        gasPrice: gasPrice,
        gas: gasLimit,
        args: [to, approveAmount],
  });
  // When the approve is successful, the transaction is exchanged
    if (fromChain.network === "32659") {
     //Because of the special call of the FSN chain, GAS is fixed directly
      transactionInfo["gas"] = "500000";
    } else {
      transactionInfo["gas"] =
        transactionInfo["gas"] === "210000"
          ? "210000"
          : gasLimit.toString(); // Use the contract method estimateGas to estimate gas
    }
  const transactionInfo = {
      from,
      to,
      value,
      data
  }
  const hash = await walletClient?.sendTransaction(transactionInfo);
} else {
    // Invoke the submitOrde API
}

Under the complete contract model, no charges are incurred other than gas fees, but third-party fees can be collected through the submitOrder mode using the extra field.

Last updated