Ether

部署乙太坊合約代幣並使用node js web3轉移到其他地址

  • March 4, 2022

使用節點 js web3 部署乙太坊合約令牌並從一個地址轉移令牌到另一個地址。請幫助我。

我發現這篇文章很好地解釋瞭如何做到這一點。原始碼是可用的,您可能希望根據您的目的稍微修改它。

https://hackernoon.com/how-to-script-an-automatic-token-airdrop-for-40k-subscribers-e40c8b1a02c6

這是生成執行 ERC-20 傳輸的事務的函式:

async function TransferTokens(_TO, _AMOUNT){
   const ERC_CONTRACT_ADDR = "0x0" // your contract
   const ABI_FILE = "ABI.json"; //your abi
   const ERC_CONTRACT = new web3.eth.Contract(JSON.parse(ABI_FILE), ERC_CONTRACT_ADDR);
   const fromAddress = ""; // your address with tokens
   const fromPrivateKey = "XXX"; //your private key
   return new Promise(async function(resolve, reject){
       let params = await ERC_CONTRACT.methods.transfer(_TO, _AMOUNT).encodeABI();
       let rawTX = await generateTransaction(params, fromAddress, fromPrivateKey, ERC_CONTRACT_ADDR);
       try{
           let hash = await broadcastTransaction(rawTX);
           resolve(hash);
       }catch(e){
           reject(e);
       }
   })

}

引用自:https://ethereum.stackexchange.com/questions/24327