0

I try to swap coins on QuickSwap for this i use the quickswap-sdk and the ethers

In the following exapmle i want to swap 0.05 Matic to Sand I am this to get the amount of tokens i want to swap:

const qs = require('quickswap-sdk')
const ethers = require('ethers')

const alchemy = 'https://polygon-mainnet.g.alchemy.com/v2/OfATG5uHb8ZdFbaCLfD79M236-hICOPz'
provider = new ethers.providers.JsonRpcProvider(alchemy)

    wMaticAddress = '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270'
    mSandAddress = '0xBbba073C31bF03b8ACf7c28EF0738DeCF3695683'
    matic = await qs.Fetcher.fetchTokenData(137, wMaticAddress, provider)
    sand = await qs.Fetcher.fetchTokenData(137, mSandAddress, provider)
    pair = await qs.Fetcher.fetchPairData(matic, sand, provider)
    route = new qs.Route([pair], sand) //how much is sand in matic
    price = route.midPrice.toSignificant(6) //Parameter are Decimal Points

    //Swap Matic to Sand on Quick Swap
    amountSand = 0.05
    amountMatic = amountSand * price

    console.log(amountSand)
    console.log(amountMatic)

If i use this sdk i get the following back: 0.05 0.101302

But if i type 0.05 on https://quickswap.exchange/#/swap i get:

enter image description here

This is a different value given by the quickswap Interface and the quickswap-sdk

How do i solve this i dont want to swap my tokens cheaper than needed and if i try to sell them for the calculated price 0.101302 its not working because this is not correct and will not be accepted by quickswap. Any idea how why this accoures and how to solve?

1 Answer 1

0

I found now out, that this is normal and not relevant for the swap action i use now the following code and it swaps correct. But by trying to addLiquidy to the pool this is a big problem so if someone have an idea how to do this correct for the addLiquidity function please tell me:

Here is the working code for swap:

getUnixCurrentTimestamp = Math.floor(Date.now())
getDeadlineAfter1Day = getUnixCurrentTimestamp + 1000 * 60 * 10 //Add 10 Minutes
console.log(getUnixCurrentTimestamp)


matic = await qs.Fetcher.fetchTokenData(137, wMaticAddress, provider)
sand = await qs.Fetcher.fetchTokenData(137, mSandAddress, provider)
pair = await qs.Fetcher.fetchPairData(matic, sand, provider)
route = new qs.Route([pair], sand) //how much is sand in matic
price = route.midPrice.toSignificant(6) //Parameter are Decimal Points

amountSand = 0.05
amountMatic = (amountSand * price) - 0.1
amountSandWEI = Math.round(amountSand * (10 ** 18)).toString()
amountMaticWEI = Math.round(amountMatic * (10 ** 18)).toString()
console.log(amountSand)
console.log(amountMatic)


encoded = quickSwapRouterContract.methods.swapExactTokensForTokens(amountSandWEI, amountMaticWEI, [mSandAddress, wMaticAddress], publicKey, getDeadlineAfter1Day).encodeABI()
block = await web3.eth.getBlock("latest");
gasLimit = Math.round(block.gasLimit / block.transactions.length);

var tx = {
    gas: gasLimit,
    to: contractAddressQuickSwapRouter,
    data: encoded
}

web3.eth.accounts.signTransaction(tx, privateKey).then(signed => {
    web3.eth.sendSignedTransaction(signed.rawTransaction).on('receipt', console.log)
})

Your Answer

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

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