自学内容网 自学内容网

-- je--tt--on批量转账(空投)

const TonClient = require('@tonclient/core').TonClient;
const { libNode } = require('@tonclient/lib-node');
const { signerKeys } = require('@tonclient/types').internal;

const TON_ENDPOINT = 'https://testnet.toncenter.com/api/v2/jsonRPC';
const WALLET_PRIVATE_KEY = 'your_private_key';
const JETTON_MASTER_ADDRESS = '0:jetton_master_address';
const JETTON_WALLET_CODE = '0xjetton_wallet_code';

const recipientAddresses = [
  '0:recipient1_address',
  '0:recipient2_address',
  // 添加更多接收者地址
];

const amountToTransfer = '1000000000'; // 1 Jetton = 1000000000 nano-Jettons

async function main() {
  const client = new TonClient({ network: { endpoints: [TON_ENDPOINT] }, useStore: false });

  // 获取钱包账户信息
  const walletAddress = '0:your_wallet_address';
  const walletAccount = await client.net.query_collection({
    collection: 'accounts',
    filter: { id: { eq: walletAddress } },
    limit: 1,
    result: 'balance last_transaction_id'
  });

  if (!walletAccount.result.length) {
    throw new Error('Wallet not found');
  }

  const walletBalance = walletAccount.result[0].balance;
  console.log(`Wallet balance: ${walletBalance}`);

  // 获取 Jetton 主合约信息
  const jettonMasterAccount = await client.net.query_collection({
    collection: 'accounts',
    filter: { id: { eq: JETTON_MASTER_ADDRESS } },
    limit: 1,
    result: 'balance last_transaction_id'
  });

  if (!jettonMasterAccount.result.length) {
    throw new Error('Jetton master contract not found');
  }

  const jettonMasterBalance = jettonMasterAccount.result[0].balance;
  console.log(`Jetton master contract balance: ${jettonMasterBalance}`);

  // 创建 Jetton 钱包地址
  const jettonWalletAddress = await createJettonWalletAddress(client, JETTON_MASTER_ADDRESS, walletAddress, JETTON_WALLET_CODE);

  // 获取 Jetton 钱包余额
  const jettonWalletAccount = await client.net.query_collection({
    collection: 'accounts',
    filter: { id: { eq: jettonWalletAddress } },
    limit: 1,
    result: 'balance last_transaction_id'
  });

  if (!jettonWalletAccount.result.length) {
    throw new Error('Jetton wallet not found');
  }

  const jettonWalletBalance = jettonWalletAccount.result[0].balance;
  console.log(`Jetton wallet balance: ${jettonWalletBalance}`);

  // 创建 Jetton 转账交易
  const keys = {
    public: 'your_public_key',
    secret: WALLET_PRIVATE_KEY
  };

  const signer = signerKeys(keys);

  for (const recipient of recipientAddresses) {
    const recipientJettonWalletAddress = await createJettonWalletAddress(client, JETTON_MASTER_ADDRESS, recipient, JETTON_WALLET_CODE);

    const transferParams = {
      destination: recipientJettonWalletAddress,
      query_id: Math.floor(Math.random() * 1000000000),
      amount: amountToTransfer,
      response_destination: walletAddress,
      response_payload: ''
    };

    const message = {
      body: JSON.stringify(transferParams),
      init: {
        code: JETTON_WALLET_CODE,
        data: ''
      }
    };

    const result = await client.net.send_message({
      address: jettonWalletAddress,
      message: message,
      signer: signer
    });

    console.log(`Transaction sent to ${recipient}: ${result}`);
  }

  console.log('All transactions sent successfully');
}

async function createJettonWalletAddress(client, jettonMasterAddress, ownerAddress, jettonWalletCode) {
  const params = {
    method: 'get_jetton_wallet_address',
    params: {
      jetton_master_address: jettonMasterAddress,
      wallet_code: jettonWalletCode,
      owner_address: ownerAddress
    }
  };

  const result = await client.run_method(params);
  return result.result.value0;
}

main().catch(console.error);

golang版本

package main

import (
    "fmt"
    "github.com/xssnick/tonutils-go"
    "github.com/xssnick/tonutils-go/ton"
    "log"
    "math/big"
)

func main() {
    // 初始化 TonClient
    client, err := ton.NewClient("https://api.ton.sh") // 选择合适的 Ton API
    if err != nil {
        log.Fatalf("Failed to create TonClient: %v", err)
    }

    // 用户钱包的私钥和地址
    privateKey := "YOUR_PRIVATE_KEY" // 替换为你的私钥
    walletAddress := "YOUR_WALLET_ADDRESS" // 替换为你的钱包地址

    // 初始化钱包
    wallet := tonutils.NewWallet(walletAddress, privateKey)

    // Jetton 合约地址
    jettonAddress := "JETTON_CONTRACT_ADDRESS" // 替换为你的 Jetton 合约地址

    // 批量转账信息
    recipients := []struct {
        Address string
        Amount  *big.Int
    }{
        {"RECIPIENT_ADDRESS_1", big.NewInt(1000000000)}, // 替换为接收者地址和金额
        {"RECIPIENT_ADDRESS_2", big.NewInt(2000000000)},
        // 添加更多接收者...
    }

    for _, recipient := range recipients {
        // 创建转账消息
        msg := tonutils.NewJettonTransfer(jettonAddress, recipient.Address, recipient.Amount)

        // 发送转账
        tx, err := wallet.SendJetton(msg)
        if err != nil {
            log.Printf("Failed to send jetton to %s: %v", recipient.Address, err)
            continue
        }

        fmt.Printf("Sent %s jettons to %s, transaction ID: %s\n", recipient.Amount, recipient.Address, tx.ID)
    }
}

原文地址:https://blog.csdn.net/qq_37106501/article/details/143727995

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!