繁体   English   中英

当我的余额比原始余额减少 10% 时,如何打破这个 UniswapV3 机器人的循环

[英]How do i break the loop for this UniswapV3 bot when my balance has decreased by 10% from original balance

所以我做了一个机器人,它会在 Uniswap V3 DAI 和 USDC 0.05% 池上连续进行多跳交换,因此机器人进行的每笔交易都会让我每次损失大约 0.10% 的余额。

当我的当前余额是我开始时的原始余额的 90% 时,我希望机器人停止交易,但我不知道该怎么做。

到目前为止,我已经将 90% 的测试余额硬写到 if 语句中,但如果我每次打开机器人时都有不同的余额,这并不理想。

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)
})

而不是拥有:

if ( await dai.balanceOf(accounts[0].address) <= 90000000000000000000){break;} 

我想要它,所以当 account[0] 的原始余额下降 10% 时,循环就会中断。 这是为了确保帐户中任何价值的 dai 都可以使用该机器人。

尝试这个:

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...
}

有关更多详细信息,请参阅我在您的问题上留下的评论...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM