web3j發交易和查詢余額


var Web3 = require('web3');
var fs   = require('fs');
var Tx = require('ethereumjs-tx');
var store = require('./data.js')
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8866"));
//發布合約得到的合約地址
var contract_addr ="";
var data ="";
//編譯合約源碼得到abi和data let abi= JSON.parse(fs.readFileSync("./abi.txt")); var USDT = new web3.eth.Contract(abi,contract_addr);

//發布合約 function deploy(){ web3.eth.getTransactionCount(add_form,"pending",function(err,nonce){ if(err){ console.log("獲取nonce出錯",err); return; } console.log("獲得nonce:",nonce); var rawTx = { nonce:web3.utils.toHex(nonce), from: add_form, gasLimit: web3.utils.toHex(21000), gasPrice: web3.utils.toHex(web3.utils.toWei("15", "gwei")), data:data
} var tx = new Tx(rawTx); tx.sign(privateKey); var serializedTx = tx.serialize(); web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'),function(err, hash){ if (!err) { console.log(hash); } else { console.error(err); } }); }); } //發送合約交易 function contractTransfer(amount,targetAddr){ var transfer_code = USDT.methods.transfer(targetAddr, web3.utils.toWei(amount,'mwei')).encodeABI(); web3.eth.getTransactionCount(add_form,"pending",function(err,nonce){ if(err){ console.log("獲取nonce出錯",err); return; } console.log("獲得nonce:",nonce); var transfer_rawTx = { nonce:web3.utils.toHex(nonce), from: add_form, to: contract_addr, value: "0x0", gasLimit: web3.utils.toHex(60000), gasPrice: web3.utils.toHex(web3.utils.toWei("15", "gwei")), data:transfer_code } var tx = new Tx(transfer_rawTx); tx.sign(privateKey); var serializedTx = tx.serialize(); console.log("開始發送交易...") web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'),function(err, hash){ if (!err) { console.log("發送成功,等待回執:",hash); } else { console.error("發送失敗",err); } }).then(function(data) { console.log("交易結果 data:"+JSON.stringify(data)); }); }) } //查詢交易狀態 function getTransactionReceipt(hash){ var receipt = web3.eth.getTransactionReceipt(hash).then(console.log); console.log("交易信息:",receipt); } //根據交易hash查詢交易信息 function getTransactionHash(hash){ var result = web3.eth.getTransaction(hash).then(console.log); console.log("交易信息:",result); } //查詢代幣余額 function balanceOf(addr){ USDT.methods.balanceOf(addr).call(function(error, result){ if(!error) { console.log(addr,":",web3.utils.fromWei(result, 'mwei')) } else { console.log(addr,":",error); } }); } //查詢主幣余額
function balance(addr){ web3.eth.getBalance(addr).then(console.log); } //發主幣交易 function mainTransfer(item){ web3.eth.getTransactionCount(item.from,function(error,nonce){ let tx = new Tx({ nonce : web3.utils.toHex(nonce), to: item.to, value: web3.utils.toHex(web3.utils.toWei(item.val, "ether") ), gasLimit: web3.utils.toHex(600000), gasPrice: web3.utils.toHex(web3.utils.toWei("15", "gwei")), data:data
}); let privateKey = Buffer.from(item.key, 'hex') tx.sign(privateKey); let serializedTx = tx.serialize(); web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'),function(err, hash){ console.log("交易結果預覽:{error:"+err+",hash:"+hash+"}"); }).then(function(data) { console.log("交易結果 data:"+JSON.stringify(data)); }); }) }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM