Web3js常用方法

727 阅读2分钟
// const web3 = new Web3(new Web3.providers.HttpProvider(process.env.RINKEBY_HTTPS));

//打印版本号
// console.log(web3.version);
//返回当前所连接节点的链 ID
// web3.eth.getChainId().then(console.log);

//使用大数据,js中默认数字精度比较小,无法表示以太坊的精度,1wei等于18次方。
//所以要私用BigNUmber来做这种操作
// var number = BigNumber.from("12345678901234567890123456789")
// console.log(number.toString(10)); //按照十进制显示
//检查是否位BigNumber
// console.log(web3.utils.isBigNumber(number))

/*=================================== 单位转换 ============================== */
//1wei单位转换方法
// console.log(web3.utils.fromWei('1','ether'));
// console.log(web3.utils.fromWei('1','finney'));
// console.log(web3.utils.fromWei('1','szabo'));
// console.log(web3.utils.fromWei('1','shannon'));

//给定的以太转换为wei
// console.log(web3.utils.toWei('1','ether'))
// console.log(web3.utils.toWei('1','finney'))
// console.log(web3.utils.toWei('1','szabo'))
// console.log(web3.utils.toWei('1','shannon'))

/*=================================== 数值转换 ============================== */
//转换为16进制
// console.log(web3.utils.toHex("234"));
// console.log(web3.utils.toHex(234));
// console.log(web3.utils.toHex("张三"));
// console.log(web3.utils.toHex("abc"));

//16进制转换
// const str1 = web3.utils.toHex("张三");
// console.log(web3.utils.hexToUtf8(str1));

// const num1 = web3.utils.toHex("123");
// console.log(web3.utils.hexToNumber(num1));
// console.log(web3.utils.utf8ToHex("李四"));

/*=================================== 地址相关 ============================== */

//判断是否为地址
// console.log(web3.utils.isAddress("0x4BeB80232Ce2Aa5e4304e94b95fD559C4f0383AA"));

/*=================================== 区块相关 ============================== */

//查询最新区块
// web3.eth.getBlockNumber().then(console.log)

//获取制定编号或哈希对应的块,第一个传参有三种格式
//1. 区块号
//2. 区块的哈希值
//3. 字符串:earliest(创世区块)、latest(最新区块)、pending(当前开采的区块)
// web3.eth.getBlock("11143971").then(console.log);
// web3.eth.getBlock("0x2766040280fc6c73f5eaf1e43485ce9815530899d575013322687f5b0c2a263e").then(console.log);
// web3.eth.getBlock("latest").then(console.log);

//获取指定区块中特定索引号的交易对象
// web3.eth.getTransactionFromBlock("11143971",1).then(console.log);
// web3.eth.getTransactionFromBlock("latest",0).then(console.log);

//返回区块中包含的交易数量。
// web3.eth.getBlockTransactionCount("11143971").then(console.log);

/*=================================== 交易相关 ============================== */

//获取账户的ETH数量
// web3.eth.getBalance("0x4BeB80232Ce2Aa5e4304e94b95fD559C4f0383AA",function(err,result){
//   const balance = result.toString();
//   console.log(balance);
//   console.log(web3.utils.fromWei(balance,"ether"));
// });

//根据最近几个区块,计算平均Gas价格
// web3.eth.getGasPrice().then((result)=>{
//   console.log("wei:"+result);
//   console.log("ether:"+web3.utils.fromWei(result,"ether"));
// });

//发送一个交易, 怎么设置私钥?
//详细说明:https://learnblockchain.cn/docs/web3.js/web3-eth.html?highlight=sendtransaction#eth-sendtransaction
// const transactionObject = {
//   from : "0x4BeB80232Ce2Aa5e4304e94b95fD559C4f0383AA",
//   to : "0xEE0d473F19DC0f580e534F951a3f48c43df56D28",
//   value : web3.utils.toWei("0.001","ether"),
//   data : ""
// }
// web3.eth.sendTransaction(transactionObject).then(console.log);

//返回制定哈希值的交易对象
// web3.eth.getTransaction("0x2766040280fc6c73f5eaf1e43485ce9815530899d575013322687f5b0c2a263e").then(console.log);