0

I am trying to swap BEP-20 based tokens with 0x and Ethers.js.

https://bsc.api.0x.org/swap/v1/quote

I used this endpoint to get quote of the exchange.

https://bsc.api.0x.org/swap/v1/quote?sellToken=0xF8fC63200e181439823251020d691312FDcF5090&buyToken=0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee&sellAmount=10000000000000

From the above link, I can get the following JSON object.

{
    "chainId": 56,
    "price": "0.000000697723740987",
    "guaranteedPrice": "0.000000690746503577",
    "to": "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
    "data": "0xc43c9ef60000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000188a4d87c1792100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000f8fc63200e181439823251020d691312fdcf5090000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee869584cd000000000000000000000000100000000000000000000000000000000000001100000000000000000000000000000000000000000000008d4859f248618ce0f0",
    "value": "0",
    "gas": "136000",
    "estimatedGas": "136000",
    "gasPrice": "5000000000",
    "protocolFee": "0",
    "minimumProtocolFee": "0",
    "buyTokenAddress": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
    "sellTokenAddress": "0xf8fc63200e181439823251020d691312fdcf5090",
    "buyAmount": "6977237409872971",
    "sellAmount": "10000000000000",
    "sources": [
        {
            "name": "BakerySwap",
            "proportion": "0"
        },
        {
            "name": "Belt",
            "proportion": "0"
        },
        {
            "name": "DODO",
            "proportion": "0"
        },
        {
            "name": "DODO_V2",
            "proportion": "0"
        },
        {
            "name": "Ellipsis",
            "proportion": "0"
        },
        {
            "name": "Mooniswap",
            "proportion": "0"
        },
        {
            "name": "MultiHop",
            "proportion": "0"
        },
        {
            "name": "Nerve",
            "proportion": "0"
        },
        {
            "name": "PancakeSwap",
            "proportion": "0"
        },
        {
            "name": "PancakeSwap_V2",
            "proportion": "1"
        },
        {
            "name": "SushiSwap",
            "proportion": "0"
        },
        {
            "name": "Smoothy",
            "proportion": "0"
        },
        {
            "name": "ApeSwap",
            "proportion": "0"
        },
        {
            "name": "CafeSwap",
            "proportion": "0"
        },
        {
            "name": "CheeseSwap",
            "proportion": "0"
        },
        {
            "name": "JulSwap",
            "proportion": "0"
        },
        {
            "name": "LiquidityProvider",
            "proportion": "0"
        },
        {
            "name": "WaultSwap",
            "proportion": "0"
        },
        {
            "name": "FirebirdOneSwap",
            "proportion": "0"
        },
        {
            "name": "JetSwap",
            "proportion": "0"
        },
        {
            "name": "ACryptoS",
            "proportion": "0"
        }
    ],
    "orders": [
        {
            "makerToken": "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
            "takerToken": "0xf8fc63200e181439823251020d691312fdcf5090",
            "makerAmount": "6977237409872971",
            "takerAmount": "10000000000000",
            "fillData": {
                "tokenAddressPath": [
                    "0xf8fc63200e181439823251020d691312fdcf5090",
                    "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c"
                ],
                "router": "0x10ed43c718714eb63d5aa57b78b54704e256024e"
            },
            "source": "PancakeSwap_V2",
            "sourcePathId": "0x815ef5c1e45481227d1abef5571ff9f920022448699927968091f831e4cba126",
            "type": 0
        }
    ],
    "allowanceTarget": "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
    "sellTokenToEthRate": "0",
    "buyTokenToEthRate": "1"
}

I just need to confirm the transaction with Ethers.js but I am not sure how to send this JSON data with Ethers.js

1 Answer 1

0

You would use the to, data, value, gasPrice and gas fields to construct an Ethers transaction, sign it and send it.

Most javascript frameworks allow passing in the entire response and it will pluck out the needed values. I would consult the Ethers.js documentation for more info and examples.

1
  • Thanks for answering, Jacob. I sent it with Ethers.js but it was reverted. Could you check this tx history? bscscan.com/tx/…
    – Mixmaster
    Nov 13, 2021 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.