首页 > 解决方案 > BEGINNER JavaScript 我需要一些帮助来计算不同行的函数

问题描述

我试图为 bsc 链制作一个小型机器人,但我坚持使用 nonce 部分。我想让Nonce每一行的计数都增加+1。这是我目前拥有的一个例子。

    amountIn,
    Slippage,
   [Spend, Receive],
   recipientaddress,
     Date.now() + 1000 * 60 * 10, 
     { gasLimit: Limit, gasPrice: transaction.gasPrice, nonce: 1}
     ),
     
    router.swapExactTokensForTokens(
      amountIn1,
      Slippage1,
      [Spend, Receive],
      recipientaddress,
      Date.now() + 1000 * 60 * 10, 
      { gasLimit: Limit, gasPrice: transaction.gasPrice, nonce: 1}
      ),
      router.swapExactTokensForTokens(
      amountIn2,
      Slippage2,
      [Spend, Receive],
      recipientaddress,
      Date.now() + 1000 * 60 * 10, 
      { gasLimit: Limit, gasPrice: transaction.gasPrice, nonce: 1}
      ),

我如何确保所有nonce 数字 1 每次都会计数。所以它将是:

    amountIn,
    Slippage,
   [Spend, Receive],
   recipientaddress,
     Date.now() + 1000 * 60 * 10, 
     { gasLimit: Limit, gasPrice: transaction.gasPrice, nonce: 1}
     ),
     
    router.swapExactTokensForTokens(
      amountIn1,
      Slippage1,
      [Spend, Receive],
      recipientaddress,
      Date.now() + 1000 * 60 * 10, 
      { gasLimit: Limit, gasPrice: transaction.gasPrice, nonce: 2}
      ),
      router.swapExactTokensForTokens(
      amountIn2,
      Slippage2,
      [Spend, Receive],
      recipientaddress,
      Date.now() + 1000 * 60 * 10, 
      { gasLimit: Limit, gasPrice: transaction.gasPrice, nonce: 3}
      ),


抱歉英语不好和代码不好。我对此很陌生。我在这里先向您的帮助表示感谢

这就是我现在所拥有的

const nonce = (() => {
  let nonce = 5;
  return () => ++nonce;
})();

console.log(nonce());
console.log(nonce());
console.log(nonce());
console.log(nonce());


const MethodID = "0xf305d719" 
const MethodID2 = "0xe8e33700" 
const provider = new ethers.providers.WebSocketProvider(WSS);
const wallet = ethers.Wallet.fromMnemonic(Seed);
const account = wallet.connect(provider);
provider.removeAllListeners()
const router = new ethers.Contract(
  routeraddress,
  ['function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'],
  account
);
console.log(`Connecting to the blockchain`),
console.log(`Starting to scan the network for a matching transaction based on the config entered`),
console.log(`As soon as a matching transaction has been found, it will be displayed here`),
provider.on("pending", async (tx) => {
  provider.getTransaction(tx).then(function (transaction){
    if (transaction != null && transaction['data'].includes(MethodID2) && transaction['data'].includes(SnipeID) || transaction != null && transaction['data'].includes(MethodID) && transaction['data'].includes(SnipeID))
  console.log(transaction),
  console.log(`Matching liquidity add transaction found!`),


  console.log(`Buying tokens 1`),



  router.swapExactTokensForTokens(
    amountIn,
    Slippage,
   [Spend, Receive],
   recipientaddress,
     Date.now() + 1000 * 60 * 10, 
     { gasLimit: Limit, gasPrice: transaction.gasPrice, nonce:  }
     ),
     
    router.swapExactTokensForTokens(
      amountIn1,
      Slippage1,
      [Spend, Receive],
      recipientaddress,
      Date.now() + 1000 * 60 * 10, 
      { gasLimit: Limit, gasPrice: transaction.gasPrice, nonce:"" }
      ),

标签: javascript

解决方案


有多种方法可以做到这一点,但在不改变也不了解整体设计的情况下,您可以简单地创建一个函数来访问顺序递增的 nonce。

  1. 使用 IIFE 在nonce变量上创建闭包并返回一个函数,该函数递增并返回下一个随机数。

const nonce = (() => {
  let nonce = 0;
  return () => ++nonce;
})();

console.log(nonce());
console.log(nonce());
console.log(nonce());
console.log(nonce());

  1. 使用发电机。我真的不明白在这里诚实地使用发电机有什么好处。

function *newNonceSequence() {
  let nonce = 0;
  
  while (true) {
    yield ++nonce;
  }
}

const nonceSequence = newNonceSequence();
const nonce = () => nonceSequence.next().value;

console.log(nonce());
console.log(nonce());
console.log(nonce());
console.log(nonce());

您可能还想找到一种方法来抽象使用,而不是让调用者承担责任。

例如,

const swapTx1 = newTokenSwapTx(...); // sets the nonce


推荐阅读