So i made a bot that will continuously do multi hop swaps on the Uniswap V3 DAI and USDC 0.05% pool, as a result each trade the bot makes will effectively make me lose around 0.10% of my balance each time.
I want the bot to stop trading when my current balance is 90% of my original balance that i started of with but i don't know how to do that.
so far i've hard written 90% of my test balance into the if statement, but this is not ideal if i have a different balance each time i turn the bot on.
const { expect } = require("chai");
const { ethers, network} = require('hardhat')
const DAI = '0x6B175474E89094C44Da98b954EedeAC495271d0F';
const DAI_WHALE = '0xF977814e90dA44bFA03b6295A0616a897441aceC';
function waitforme(milisec) {
return new Promise(resolve => {
setTimeout(() => { resolve('') }, milisec);
})
}
describe("SwapExamples", () => {
let swapExamples
let dai
let whale
let accounts
before(async () => {
const SwapExamples = await ethers.getContractFactory("SwapExamples")
swapExamples = await SwapExamples.deploy()
await swapExamples.deployed()
dai = await ethers.getContractAt("IERC20", DAI)
whale = await ethers.getSigner(DAI_WHALE)
accounts = await ethers.getSigners()
await network.provider.request({
method: "hardhat_impersonateAccount",
params: [DAI_WHALE],
})
})
it("unlock account", async () => {
const amount = 100n * 10n ** 18n
console.log("DAI balance of whale", await dai.balanceOf(DAI_WHALE))
expect(await dai.balanceOf(DAI_WHALE)).to.gte(amount)
await dai.connect(whale).transfer(accounts[0].address, amount)
console.log(
"DAI balance of account",
await dai.balanceOf(accounts[0].address)
)
})
it("swapExactInputMultihop", async () => {
for (let i = 0; i < 111; i++) {
if ( await dai.balanceOf(accounts[0].address) <= 90000000000000000000) { break;}
const amountIn = await dai.balanceOf(accounts[0].address)
await dai.approve(swapExamples.address, amountIn)
// Swap
await swapExamples.swapExactInputMultihop(amountIn)
console.log("DAI balance", await dai.balanceOf(accounts[0].address))
await waitforme(1000);
}
}).timeout(500000)
})
instead of having the:
if ( await dai.balanceOf(accounts[0].address) <= 90000000000000000000){break;}
i want it so the loop breaks when the original balance of account[0] is down by 10%. This is to ensure any value of dai that is in a the account can use the bot.
Try this:
const balance = await dai.balanceOf(accounts[0].address);
for (let i = 0; i < 111; i++) {
const amountIn = await dai.balanceOf(accounts[0].address)
if (amountIn.mul(10).lte(balance.mul(9))) {
break;
}
// the rest of your code goes here...
}
See the comments that I left on your question for more details...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.