当前位置: 首页 > article >正文

web3.js + Ganache 模拟以太坊账户间转账

转账前:

转账后:

 

 

async function interact() {
  const web3 = new Web3(
    new Web3.providers.HttpProvider('http://127.0.0.1:7545')
  )
  web3.eth.Contract.handleRevert = true
  const accounts = await web3.eth.getAccounts()

  console.log(accounts)

  let balance1, balance2

  balance1 = await web3.eth.getBalance(accounts[0])

  balance2 = await web3.eth.getBalance(accounts[1])

  console.log(balance1, web3.utils.fromWei(balance1, 'ether'))
  console.log(balance2, web3.utils.fromWei(balance2, 'ether'))

  const transaction = {
    from: accounts[0],

    to: accounts[1],

    value: web3.utils.toWei('1', 'ether'),
  }

  const transactionHash = await web3.eth.sendTransaction(transaction)

  console.log('transactionHash', transactionHash)

  balance1 = await web3.eth.getBalance(accounts[0])

  balance2 = await web3.eth.getBalance(accounts[1])

  console.log(balance1, web3.utils.fromWei(balance1, 'ether'))
  console.log(balance2, web3.utils.fromWei(balance2, 'ether'))

  // const gasPrice = await web3.eth.getGasPrice()

  // console.log('gasUsed', transactionHash.gasUsed)
  // console.log('价格', web3.utils.fromWei(gasPrice, 'ether'))
  let total =  transactionHash.effectiveGasPrice * transactionHash.gasUsed + balance1 + balance2
  console.log('total', total)

  const block = await web3.eth.getBlockNumber()

  console.log('Last block:', block)
}

人工智能学习网站

https://chat.xutongbao.top


http://www.kler.cn/a/413749.html

相关文章:

  • Ubuntu 硬盘分区并挂载
  • 【项目日记】仿mudou的高并发服务器 --- 实现基础高并发服务器基础模块
  • 【LeetCode面试150】——56合并区间
  • RabbitMQ5:Fanout交换机、Direct交换机、Topic交换机
  • YOLOv11融合PIDNet中的PagFM模块及相关改进思路
  • Samba服务器常见问题处理
  • Jmeter后置处理器
  • 代码美学2:MATLAB制作渐变色
  • DVWA靶场通过——文件上传漏洞
  • 预测未来 | MATLAB实现Transformer时间序列预测未来
  • 【方案库】从单张照片快速重建3D场景:Flash3D详解
  • 【Ubuntu24.04】服务部署(Docker)
  • 实验二 系统响应及系统稳定性
  • 【11-20期】Java面试进阶:深入解析核心问题与实战案例
  • Linux基础学习--vi与vim
  • 读《Effective Java》笔记 - 条目10
  • 化工行业 FMEA 与安全生产的关系
  • Java使用反射记录操作日志
  • 23省赛区块链应用与维护(房屋租凭【下】)
  • json object转x-www-form-urlencoded