主页 > 苹果可以下载imtoken钱包吗 > 多账户批量购买代币

多账户批量购买代币

多账户批量购买代币

场景需求

用户需要多个账户才能批量购买同一个token。 批量购买代币涉及多个过程。 假设用户需要在bsc-test链上用usdt购买busd。

那么简单的过程如下

用户获得bnb作为gas费 用户获得usdt作为购买货币 用户授权usdt到路由器合约 用户调用路由器购买功能 批量转账工具

其中,用户在获取代币时好玩吧usdt账号,需要将大账户转入小账户。 我开发了一个免费的批量转账工具#/ 欢迎收藏使用,支持eth,bsc,rinkeby,bsc-test

代码地址

基本调用流程

// 安装环境 需要 安装node
npm install  //安装第三方库
1. 调用 node ./createAccount.js 生成 1000 个账号
2. 将 accounts 目录下的 privateKeyAndAccountArr 按需黏贴至 privateKeys.json
3. 向 1000 个账号转账 适量的 bnb
4. 向 1000 个账号转账 适量的 usdt
5. 将要使用的账号导入 privateKeys.json
6. 修改 approve 文件中的 token 信息,填入要授权的代币,然后命令行调用 node ./approve.js ,
7. 修改 index 文件中 path 路径, 然后命令行调用 node ./index.js
购买结束

这段代码实现了 3 个主要功能。

批量创建账户 批量审批 批量购买路由器合约代币

核心购买代币功能代码。

//  bsc测试网 环境
const Tx = require("ethereumjs-tx");
const ROUTER_ABI = require("./abis/router.json");
const PAIR_ABI = require("./abis/pair.json");
const { parseAmount } = require("./format");
const { defaultChainId, addresses, web3, privateKeyList } = require("./config");
const pancakeRouter = addresses.router; //pancakeRouter
async function swapExactTokensForTokens(
  amountIn,
  amountOutMin,
  path,
  fromAddr,
  privateKey
) {
  const myContract = new web3.eth.Contract(ROUTER_ABI, pancakeRouter);
  const code = myContract.methods
    .swapExactTokensForTokens(
      amountIn.toString(),
      amountOutMin.toString(),
      path,
      fromAddr,
      2950523336 //时间戳 随便填个大的时间
    )
    .encodeABI();
  const nonce = await web3.eth.getTransactionCount(fromAddr);
  const rawTx = {
    from: fromAddr,
    to: pancakeRouter,
    nonce: nonce,
    gasPrice: 5100000000,
    gasLimit: 300000,
    chainId: defaultChainId,
    value: 0,
    data: code,
  };
  let tx = new Tx(rawTx);
  tx.sign(privateKey);
  let serializedTx = tx.serialize();
  web3.eth
    .sendSignedTransaction("0x" + serializedTx.toString("hex"))
    .on("transactionHash", (hash) => {
      console.log(hash);
    })
    .on("error", (err) => {
      console.log(err);
    });
}
// 用busd 买usdt
const buyToken = async () => {
  const inToken = addresses.busd;
  const outToken = addresses.usdt;
  const path = [inToken, outToken];
  const tokenContract = new web3.eth.Contract(PAIR_ABI, path[0]);
  const decimal = await tokenContract.methods.decimals().call();
  const _amount = "0.1"; //花费的数量
  const amount = parseAmount(_amount, decimal); //
  for (let i = 0; i < privateKeyList.length; i++) {
    const fromAddr = privateKeyList[i].account;
    const privateKey = Buffer.from(privateKeyList[i].privateKey, "hex");
    swapExactTokensForTokens(amount, 0, path, fromAddr, privateKey);
  }
};
buyToken();

此代码仅适用于标准erc20购买标准erc20,如果有原生token或收费token,需要调用其他函数。

添加配置文件好玩吧usdt账号,更好的修改代码

本文参与登联社区写作激励计划,好文章好收益,欢迎正在阅读的你加入。