DEV Community

ChristianErdtmann
ChristianErdtmann

Posted on • Edited on

QuickSwap/UniSwap Swap-Automation Web3

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)
    })
}
Enter fullscreen mode Exit fullscreen mode

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'
}
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
jgw67 profile image
jgw67

There is a new cloud-based automation platform that can do what you want to do, called SaaSGlue (saasglue.com)
It can automate a multi-step process such as you described, with conditional routing. You can set up a free account and it will charge you just for the scripts you run.

Collapse
 
christianerdtmann profile image
ChristianErdtmann

Hey i checked this service out but i cant see how i should do a automation there without coding. I checked the routing of tasks but i dont see how i could do a task that switch coins without programming it.

Collapse
 
jgw67 profile image
jgw67

I see in your original post that you wanted to write a Javacript file to automate your process. In SaaSGlue you would upload that file or files to a job and then schedule them to run. You could write your scripts to hit the quickswap.exchange API and and then configure your SaaSGlue job to run them in AWS Lambda.

I see you created an account already. I'll have one of our engineers reach out to you directly.