javascript - 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:"" }
),
解决方案
有多种方法可以做到这一点,但在不改变也不了解整体设计的情况下,您可以简单地创建一个函数来访问顺序递增的 nonce。
- 使用 IIFE 在
nonce
变量上创建闭包并返回一个函数,该函数递增并返回下一个随机数。
const nonce = (() => {
let nonce = 0;
return () => ++nonce;
})();
console.log(nonce());
console.log(nonce());
console.log(nonce());
console.log(nonce());
- 使用发电机。我真的不明白在这里诚实地使用发电机有什么好处。
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
推荐阅读
- java - 如何从java字符串中删除最后第六个单词
- reactjs - 我如何使 echarts 与 react-grid-layout 一起调整大小?
- python - Nvidia Jetson GST Pipeline udpsrc 使用 Python3 到 appsink
- android - 如何在 Final Android App 中的 WebView 上添加 Imageview
- entity-framework - 实体框架实体继承和自定义属性
- javascript - 没有文本时隐藏下拉值
- soap - 如何获取 SOAP 消息的“SignatureValue”和“DigestValue”值?
- python - Flask API 无法返回队列的值 - queue.get()
- google-apps-script - 创建多个函数 onEdit(e) 以在一个脚本中工作
- regex - Oracle REGEXP_SUBSTR 将不匹配点字符