6eer / uniswap-sushiswap-arbitrage-bot

Two bots written in JS that uses flashswaps and normal swaps to arbitrage Uniswap. Includes an automated demostration.
MIT License
584 stars 264 forks source link

Error running demo environment #7

Open cffiebigc opened 3 years ago

cffiebigc commented 3 years ago

Hi 👋 Thanks @6eer for your work. Your code looks really promising.

I followed your instructions to deploy the demo environment. I ran ganache successfully (by forking mainnet), created the .env file and also ran demo_environment.js without issues, but when I either run bot_normalswap.js or bot_flashswap.js I'm getting the following error:

UnhandledPromiseRejectionWarning: Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.

Can you point me in the right direction or help me find out what is failling?

6eer commented 3 years ago

Hey Carlos, read this thread and let me know if it helped you!

cffiebigc commented 3 years ago

Thanks @6eer I found the answer in the thread you shared. The demo environment is now working!

interzone2 commented 2 years ago

Hi - first thanks for your great work and for sharing it!

I'm having the same issue as above and have - to some extent - tried to remove gasNedeed references after commenting out line 144 (or as it is now line 143)

This led me to think this issue might have been resolved - i.e. 144 is already commented out and you have new code at 143..?

Anyway, I commented out line 143 AND 144 and tried to rewrite further statements to make sense without gasNedeed - unfortunately I still receive all of the errors above such as:

"Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.

Let me know if I should resolve this in another way. Thank you!!

interzone2 commented 2 years ago

I discovered that the gasNeeded issue has been solved and no need to comment out the lines. Line 143 fixes the issue.

The problem with my running the bot related to not replacing the below fields in the .env file with the results from running :

node ./src/demo_environment.js -a
#or
node ./src/demo_environment.js -b

.env

**ADDR_ARBITRAGE_CONTRACT** = '0xD3167f135a9e003B1bF2CB81c0738Fdbc0C9124F'

**ADDR_TOKEN0** = '0x20526CEd6B8e4601B45Ec931b6B68b72c0DB249C'
**ADDR_TOKEN1** = '0x3806F6B92F899ACD32C8285Df70cB0f379A38713'

**ADDR_UTILS** = '0x79F86fDb626533F6ed19722D7CC3784ED24876dd'
wangshenfeng commented 2 years ago

It works!

bmwmenthusiast commented 2 years ago

It works!

Does the bot still work ? Want to try and set it up, will be a big task for me.

kiidfreak commented 2 years ago

@interzone2 how does line 143 fix the issue..mine still has the same issue running

dhxmo commented 1 year ago

it's just a matter of sending the transactions from the myAccount account. copy paste this, should fix it:

const Web3 = require('web3');
//provider
const localProviderUrl = 'http://localhost:8545'
const localProvider = new Web3.providers.WebsocketProvider(localProviderUrl)
const web3 = new Web3(localProvider)
//uniswap
const IRouter = require('@uniswap/v2-periphery/build/IUniswapV2Router02.json')
const uRouter = new web3.eth.Contract(IRouter.abi, '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D')
//sushiswap
const sRouter = new web3.eth.Contract(IRouter.abi, '0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F')
//tokens
const ERC20PresetMinterPauser = require('@openzeppelin/contracts/build/contracts/ERC20PresetMinterPauser.json')
let token0 = new web3.eth.Contract(ERC20PresetMinterPauser.abi, '', { data: ERC20PresetMinterPauser.bytecode })
let token1 = new web3.eth.Contract(ERC20PresetMinterPauser.abi, '', { data: ERC20PresetMinterPauser.bytecode })
//arbitrager
const Arbitrager = require('../build/contracts/Arbitrager.json')
const arbitrager = new web3.eth.Contract(Arbitrager.abi, '', { data: Arbitrager.bytecode })
//addresses
const addr0 = '0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac'//sushiswap factory
const addr1 = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'//uniswap router
//utils
const Utils = require('../build/contracts/Utils.json')
const utils = new web3.eth.Contract(Utils.abi, '', { data: Utils.bytecode })

async function liquidity(amount0, amount1, amount2, amount3, amount4) {

    myAccount = (await web3.eth.getAccounts())[0]

    //deploying token0
    console.log("deploying token 0");
    let gasLimit0, gasLimit1, receipt, aux
    gasLimit0 = await token0.deploy({ arguments: ['Pineapple', 'PNA'] }).estimateGas() * 2
    console.log("estimating gas limit token 0");
    receipt = await token0.deploy({ arguments: ['Pineapple', 'PNA'] }).send({ from: myAccount, gas: gasLimit0 })
    token0.options.address = receipt._address

    //deploying token1
    console.log("deploying token 1");
    gasLimit1 = await token1.deploy({ arguments: ['Watermelon', 'WTM'] }).estimateGas() * 2
    receipt = await token1.deploy({ arguments: ['Watermelon', 'WTM'] }).send({ from: myAccount, gas: gasLimit1 })
    token1.options.address = receipt._address

    if (token0.options.address > token1.options.address) { aux = token0; token0 = token1; token1 = aux }

    //prints
    const token0Name = await token0.methods.name().call()
    const token0Symbol = await token0.methods.symbol().call()
    const token1Name = await token1.methods.name().call()
    const token1Symbol = await token1.methods.symbol().call()
    console.log(
        `\n${token0Name} (${token0Symbol}) {token0}\n` +
        `Deployed at ${token0.options.address}\n\n` +
        `${token1Name} (${token1Symbol}) {token1}\n` +
        `Deployed at ${token1.options.address}\n`
    )

    //minting token0
    amount4 = web3.utils.toWei(web3.utils.toBN(amount4))
    await token0.methods.mint(myAccount, amount4).send({ from: myAccount, gas: gasLimit0 })
    console.log(`${web3.utils.fromWei(amount4)} ${token0Symbol} minted`)

    //minting token1
    await token1.methods.mint(myAccount, amount4).send({ from: myAccount, gas: gasLimit1 })
    console.log(`${web3.utils.fromWei(amount4)} ${token1Symbol} minted\n`)

    //creating pair
    const deadline = Math.round(Date.now() / 1000) + 60 * 60

    //on uniswap
    amount0 = web3.utils.toWei(web3.utils.toBN(amount0), 'ether')
    amount1 = web3.utils.toWei(web3.utils.toBN(amount1), 'ether')
    await token0.methods.approve(uRouter.options.address, amount0).send({ from: myAccount, gas: gasLimit0 })
    await token1.methods.approve(uRouter.options.address, amount1).send({ from: myAccount, gas: gasLimit1 })

    let gasLimitRouter;

    console.log("estimate gas of router");
    gasLimitRouter = await uRouter.methods.addLiquidity(
        token0.options.address,
        token1.options.address,
        amount0,
        amount1,
        0,
        0,
        myAccount,
        deadline
    ).estimateGas({ from: myAccount })

    console.log("add liquidity to router");
    await uRouter.methods.addLiquidity(
        token0.options.address,
        token1.options.address,
        amount0,
        amount1,
        0,
        0,
        myAccount,
        deadline
    ).send({ from: myAccount, gas: gasLimitRouter })
    console.log(
        `Uniswap ${token0Symbol}/${token1Symbol} pair created\n` +
        `Reserves: ${web3.utils.fromWei(amount0)} ${token0Symbol} | ${web3.utils.fromWei(amount1)} ${token1Symbol}\n` +
        `Price: ${(amount0 / amount1).toFixed(2)} ${token0Symbol}/${token1Symbol}\n`
    )

    //on sushiswap
    amount2 = web3.utils.toWei(web3.utils.toBN(amount2), 'ether')
    amount3 = web3.utils.toWei(web3.utils.toBN(amount3), 'ether')

    gasLimit = await token1.methods.approve(sRouter.options.address, amount2).estimateGas({ from: myAccount })
    await token0.methods.approve(sRouter.options.address, amount2).send({ from: myAccount, gas: gasLimitRouter })

    gasLimit = await token1.methods.approve(sRouter.options.address, amount3).estimateGas({ from: myAccount })
    await token1.methods.approve(sRouter.options.address, amount3).send({ from: myAccount, gas: gasLimitRouter })

    gasLimit = await sRouter.methods.addLiquidity(
        token0.options.address,
        token1.options.address,
        amount2,
        amount3,
        0,
        0,
        myAccount,
        deadline
    ).estimateGas({ from: myAccount })
    await sRouter.methods.addLiquidity(
        token0.options.address,
        token1.options.address,
        amount2,
        amount3,
        0,
        0,
        myAccount,
        deadline
    ).send({ from: myAccount, gas: gasLimit })
    console.log(
        `Sushiswap ${token0Symbol}/${token1Symbol} pair created\n` +
        `Reserves: ${web3.utils.fromWei(amount2)} ${token0Symbol} | ${web3.utils.fromWei(amount3)} ${token1Symbol}\n` +
        `Price: ${(amount2 / amount3).toFixed(2)} ${token0Symbol}/${token1Symbol}\n`
    )

}

async function deploy(amount0, amount1, amount2, amount3, amount4) {

    await liquidity(amount0, amount1, amount2, amount3, amount4)

    myAccount = (await web3.eth.getAccounts())[0]

    //arbitrager
    let gasLimit, receipt
    gasLimit = await arbitrager.deploy({ arguments: [addr0, addr1] }).estimateGas()
    console.log("gaslimit", gasLimit);

    receipt = await arbitrager.deploy({ arguments: [addr0, addr1] }).send({ from: myAccount, gas: gasLimit })
    console.log("receipt", receipt);

    arbitrager.options.address = receipt._address

    console.log(`Arbitrager contract deployed at ${arbitrager.options.address}\n`)

    //utils
    gasLimit = await utils.deploy().estimateGas()
    receipt = await utils.deploy().send({ from: myAccount, gas: gasLimit })
    utils.options.address = receipt._address

    console.log(`Utils contract deployed at ${utils.options.address}\n`)

}

if (process.argv[2] == '-a') { //case A: token1 cheaper on sushiswap

    deploy(10e2, 5e2, 1e4, 10e4, 1e6).then(() => { process.exit(0) })

}

if (process.argv[2] == '-b') { //case B: token1 cheaper on uniswap

    deploy(1e2, 10e2, 3e4, 10e4, 1e6).then(() => { process.exit(0) })

}