如何在嵌套循环中订购进程?
因此,在这里,我正在构建一个脚本,该脚本将从列表中获取一个令牌,并将其从另一个列表发送到钱包。对我来说,按顺序进行操作非常重要,因此Wallet 1接收令牌1,Wallet 2接收令牌2,依此类推。我正在做一个嵌套循环,因为我需要同时从两个TXT文件中获取信息。
因此,这是代码:
const shell = require("shelljs");
const fs = require("fs");
const numberToAirdrop = 1;
const pathToTheWLFile = "./wl.txt" // list of wallets
const pathToTheTKFile = "./tk.txt" // list of tokens
const wlFile = fs.readFileSync(pathToTheWLFile).toString().replace(/\r/g, '').split("\n");
const tkFile = fs.readFileSync(pathToTheTKFile).toString().replace(/\r/g, '').split("\n");
console.log(wlFile)
console.log(tkFile)
// Here's where I struggle:
for (const [index, address] of wlFile.entries()) for (const [index2, address2] of tkFile.entries()) {
if (address.includes("(sent) ") && address2.includes("(sent) ")) {
console.log(`[${index}][${index2}] Skipping ${address} and ${address2} as has been processed before`);
continue;
}
console.log(`[${index}] [${index2}] Airdropping ${numberToAirdrop} of ${address2} to ${address}`);
console.time("Sent in")
const output = shell.exec(`spl-token transfer ${address2} ${numberToAirdrop} ${address} --allow-unfunded-recipient --fund-recipient -v`); // transfer process
if (output.stderr && output.code !== 0) {
throw new Error(
`[${index}] ${index2}] Failed to send ${address2} to the ${address} ` +
`last '${"(sent) to" + wlFile[index] && tkFile[index2]}'`
)
}
tkFile[index2] = "(sent) " + tkFile[index2]
wlFile[index] = "(sent) " + wlFile[index]
fs.writeFileSync(pathToTheWLFile, wlFile.join("\n"));
fs.writeFileSync(pathToTheTKFile, tkFile.join("\n"));
console.log(`[${index}][${index2}] Airdropping ${numberToAirdrop} of ${address2} to ${address} has been successful\n\n`);
console.timeEnd("Sent in")
}
const notDone = wlFile.find((i) => !i.includes("(sent)"));
if (notDone) {
console.log("Upload did not finish for all the addresses please re run")
return;
}
console.log("Upload is done! All the tokens have been airdropped!")
基本上是有效的,但是以嵌套的循环方式,我的意思是将令牌1发送到Wallet 1,但随后尝试将同样的令牌发送到Wallet 2,然后将令牌2发送到Wallet 1。我需要。它可以按顺序工作,因此钱包1接收令牌1,钱包2接收令牌2,依此类推,而无需重复操作。在嵌套循环中可能吗?
So here I'm building a script which will take a token from the list and send it to the wallet from another list. It's very important for me to do it in order, so wallet 1 receives token 1, wallet 2 receives token 2 and so on. I'm doing a nested loop, because I need to get info from both TXT files at the same time.
So here's the code:
const shell = require("shelljs");
const fs = require("fs");
const numberToAirdrop = 1;
const pathToTheWLFile = "./wl.txt" // list of wallets
const pathToTheTKFile = "./tk.txt" // list of tokens
const wlFile = fs.readFileSync(pathToTheWLFile).toString().replace(/\r/g, '').split("\n");
const tkFile = fs.readFileSync(pathToTheTKFile).toString().replace(/\r/g, '').split("\n");
console.log(wlFile)
console.log(tkFile)
// Here's where I struggle:
for (const [index, address] of wlFile.entries()) for (const [index2, address2] of tkFile.entries()) {
if (address.includes("(sent) ") && address2.includes("(sent) ")) {
console.log(`[${index}][${index2}] Skipping ${address} and ${address2} as has been processed before`);
continue;
}
console.log(`[${index}] [${index2}] Airdropping ${numberToAirdrop} of ${address2} to ${address}`);
console.time("Sent in")
const output = shell.exec(`spl-token transfer ${address2} ${numberToAirdrop} ${address} --allow-unfunded-recipient --fund-recipient -v`); // transfer process
if (output.stderr && output.code !== 0) {
throw new Error(
`[${index}] ${index2}] Failed to send ${address2} to the ${address} ` +
`last '${"(sent) to" + wlFile[index] && tkFile[index2]}'`
)
}
tkFile[index2] = "(sent) " + tkFile[index2]
wlFile[index] = "(sent) " + wlFile[index]
fs.writeFileSync(pathToTheWLFile, wlFile.join("\n"));
fs.writeFileSync(pathToTheTKFile, tkFile.join("\n"));
console.log(`[${index}][${index2}] Airdropping ${numberToAirdrop} of ${address2} to ${address} has been successful\n\n`);
console.timeEnd("Sent in")
}
const notDone = wlFile.find((i) => !i.includes("(sent)"));
if (notDone) {
console.log("Upload did not finish for all the addresses please re run")
return;
}
console.log("Upload is done! All the tokens have been airdropped!")
Basically it works, but in a nested loop way, I mean it sends token 1 to wallet 1, but then tries to send the same token to wallet 2, then it sends token 2 to wallet 1. And I need it to work in order, so wallet 1 receives token 1, wallet 2 receives token 2 and so on, without repeatable actions. Is it possible in a nested loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在做一个嵌套的循环。您需要的是一个循环
You are making a nested for-loop. What you need is a single for-loop instead