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)); }); }) }
posted @ 2020-11-06 12:36  panda's  阅读(2796)  评论(0编辑  收藏  举报