Basickz
Basickz

Reputation: 61

BEGINNER JavaScript I need some assistance with a function that counts up for different lines

im trying to make a small bot for the bsc chain but im stuck with the nonce part. i want to let the Nonce count up +1 on every single line. here is a example of what i have at the moment.

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

How can i make sure that all the nonce that are the number 1 will count up every single time. so it will be:

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


Sorry for bad english and bad code. im new to this. thanks for the help in advance

this is what i have now

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:"" }
      ),

Upvotes: 0

Views: 428

Answers (1)

plalx
plalx

Reputation: 43718

There's multiple ways to do that, but without changing nor knowing much about the overall design you could simply create a function for accessing a sequentially incrementing nonce.

  1. Use an IIFE creating a closure over a nonce variable and which returns a function which increments & returns the next nonce.

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

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

  1. Use a generator. I don't really see what would be the advantage of using a generator here honestly.

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());

You may also want to find a way to abstract the usage in a way that doesn't put the responsibility on the caller.

For instance,

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

Upvotes: 1

Related Questions