QuickSwap/UniSwap Swap-Automation Web3
Tags: blockchain, web3, javascript
Published: 2022-04-09 23:08:27

Hey i want to automate in javascript my interactions with QuickSwap.
In the first step i just want to swap SAND to MATIC. (https://quickswap.exchange/#/swap)
In the secound step i want to add Liqudity to mSand-Matic Pool
(https://quickswap.exchange/#/add/ETH/0xBbba073C31bF03b8ACf7c28EF0738DeCF3695683)

How to do this?

Edit:
I trying now the following but for some reason it doesn work. PLEASE HELP :)

I get no error i get only back some information from the blockchain like you can see on the bottom.

This is my code for reproduce:

        const qs = require('quickswap-sdk')
        const ethers = require('ethers')
        Web3 = require('web3')
        const { Console } = require('console');
        const fs = require('fs')

    const alchemy = 'https://polygon-mainnet.g.alchemy.com/v2/OfATG5uHb8ZdFbaCLfD79M236-hICOPz'
    provider = new ethers.providers.JsonRpcProvider(alchemy)
    const web3 = new Web3("https://polygon-rpc.com")
    
    const contractAddressQuickSwapRouter = "0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff"
    const contractQuickSwapRouterJson = fs.readFileSync('./abiQuickSwapRouter.json')
    const abiQuickSwapRouter = JSON.parse(contractQuickSwapRouterJson)
    const quickSwapRouterContract = new web3.eth.Contract(abiQuickSwapRouter, contractAddressQuickSwapRouter)
    
    asyncCall()
    
    async function asyncCall() {
    
       //Swap Matic to Sand on Quick Swap
        getUnixCurrentTimestamp = Math.floor(Date.now() / 1000)
        getDeadlineAfter1Day = getUnixCurrentTimestamp + 86400
        console.log(getUnixCurrentTimestamp)
    
        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
    
        amountSand = 0.05
        amountMatic = amountSand * price
        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, getDeadlineAfter1Day).encodeABI()
        block = await web3.eth.getBlock("latest");
        gasLimit = Math.round(block.gasLimit / block.transactions.length);
    
        var tx = {
            gas: gasLimit,
            to: publicKey,
            data: encoded
        }
    
        web3.eth.accounts.signTransaction(tx, privateKey).then(signed => {
            web3.eth.sendSignedTransaction(signed.rawTransaction).on('receipt', console.log)
        })
    }

I get this as log in my console:

    node contract-interact.js
    1649626258
    0.05
    0.1012595
    {
      blockHash: '0xc2a10b99e045ec343251ceba3bcf6e2e0a565464f5b2569ef2733ce05a1c06ae',
      blockNumber: 26983103,
      contractAddress: null,
      cumulativeGasUsed: 10545837,
      effectiveGasPrice: '0x73ab4d2b0',
      from: '0x560cc2be59c5def53431783c3583b8f9f63b7793',
      gasUsed: 22656,
      logs: [
        {
          address: '0x0000000000000000000000000000000000001010',
          topics: [Array],
          data: '0x00000000000000000000000000000000000000000000000000027fcb82cf9f000000000000000000000000000000000000000000000000002f6238feb83b7611000000000000000000000000000000000000000000002c2dc38207aeea7fd51e0000000000000000000000000000000000000000000000002f5fb933356bd711000000000000000000000000000000000000000000002c2dc384877a6d4f741e',   
          blockNumber: 26983103,
          transactionHash: '0x61affa8dd44deb45ae3fa09d0b39468e6abd6af51e4fb9cf43eeb37ddf437d40',
          transactionIndex: 67,
          blockHash: '0xc2a10b99e045ec343251ceba3bcf6e2e0a565464f5b2569ef2733ce05a1c06ae',
          logIndex: 289,
          removed: false,
          id: 'log_20cccca0'
        }
      ],
      logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000080000000000000800000000000000000000000000000800000000000000000000100000000000100000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000010000000000000000000200000000000000000000000000000000000000000000000000000000000004000000000000000040001000000000000000000000000000000100000000000000000000000040000000000000000000000000000000000000000000000100000',
      status: true,
      to: '0x560cc2be59c5def53431783c3583b8f9f63b7793',
      transactionHash: '0x61affa8dd44deb45ae3fa09d0b39468e6abd6af51e4fb9cf43eeb37ddf437d40',
      transactionIndex: 67,
      type: '0x0'
    }