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
586 stars 264 forks source link

Ganache cli fork not update getReserves() inside 'data' suscribeBlock . How to test? #4

Closed alvarheras closed 3 years ago

alvarheras commented 3 years ago

Hello

Look at your project is fine. But I see something when you debug your project. How can you debug it?

I understand that the method of subscribe ('newBlockHeaders'); you won't be able to get the data with the ganache-cli fork.

You will always receive the same block so the prices obtained within that event will be the same unless the ganache service is restarted.

Is there any way to debug this subscribe with some ropsten or fork network provider on mainnet with galache?

Greetings

6eer commented 3 years ago

If u only wanna debug the code inside the subscribe method, u can initiate ganache-cli with the '-b' option or simply without it and send transaction manually, ganache will append this new transaction in a new block, so, the event will fire.

Bellow part of the README.md of ganache-cli:

-b or --blockTime: Specify blockTime in seconds for automatic mining. If you don't specify this flag, ganache will instantly mine a new block for every transaction. Using the --blockTime flag is discouraged unless you have tests which require a specific mining interval.

In the demo I use the -b option (automining) to fire the 'newBlockHeaders' event.

alvarheras commented 3 years ago

With the option of the fork to ganache-cli

When I do not indicate or indicate the -b option the blocks change but the price obtained through the Factory is the same unless I deploy my contracts again.

Without the -b option (Manual transfers with meta mask in localhost:8545)

vfvfvfvfvfvffvfvfvf

Code:

const uFactory = new web3.eth.Contract(factoryUniV2.abi,addrUFactory); /
uPair0 = new web3.eth.Contract(pairUniV2.abi, (await uFactory.methods.getPair(addrEth, addrDai).call()) )
            // --> inside event 'data'
            let uReserves;
            uReserves = await uPair0.methods.getReserves().call();
            // console.log(uReserves);
        uReserve0 = uReserves[0]; //dai
        uReserve1 = uReserves[1]; //eth
        console.log("Reserva 1 (ether) --->" + uReserve1);
        console.log("Reserva 2 (dai) --->" + uReserve0);
        priceEth = (uReserve0/uReserve1); //dai per eth
        console.log("Dai por Eth ---> " + priceEth); // eth price in route uniswap 2

Any ideas how to change this to get the updated price?

Greetings, thank you very much PD: (Soy de España por si necesitas aclaración en español :))

6eer commented 3 years ago

but why the prices should change? you are forking locally, so the only transaction that your fork can see its your transactions, so the only way to change the prices are u withdrawing funds manually from the pools. I say withdraw because i don't remember if Uniswap allows u to provides funds that disrupt the ratio (price). And keep in mind that there is an option that allows u save the changes that u made to the chain (the deploys & tranasactions u made)

--db: Specify a path to a directory to save the chain database. If a database already exists, ganache-cli will initialize that chain instead of creating a new one.

alvarheras commented 3 years ago

You're right,

With the fork to mainnet with ganache I thought you could test this part reading from mainnet.

suscribeBlock.on('data', async function(blockHeader) {  
    console.log(blockHeader);
        try {
              // --> logic
     } 
        catch(error) {
                console.log(error)
        }
});

I am looking for is to implement the Uniswap functions of flash Swap, but through ropsten they did not work for me. Do you know if on a fork with ganache I can correctly test my token swap functions with uniswap?

Another of my questions is, do you think it is more correct to read the prices with the block event and getReserves or through the uniswap sdk in this way?

const DAI = new Token(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F', 18)
const pair = await Fetcher.fetchPairData(DAI, WETH[DAI.chainId])
const route = new Route([pair], WETH[DAI.chainId])
const trade = new Trade(route, new TokenAmount(WETH[DAI.chainId], '1000000000000000000'), TradeType.EXACT_INPUT) 
       // 1000000000000000000 --> 1 WETH full
console.log("Price Trade execute --> " + trade.executionPrice.toSignificant(6))
console.log("Price Trade next mid Pirce --> " + trade.nextMidPrice.toSignificant(6)) 
console.log("Amount input trade --> " + trade.inputAmount.toSignificant(6) + " WETH " + " OutPut Amount Token --> "  + trade.outputAmount.toSignificant(6) + " DAI")

Greetings thank you