当前位置:   article > 正文

前端VUE使用web3调用小狐狸(metamask)和合约(ERC20)交互_vue web3

vue web3

1.创建vue项目

2.安装web3

npm install web3

3.项目web3

main.js

  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import Web3 from 'web3'
  4. Vue.config.productionTip = false
  5. Vue.prototype.Web3 = Web3
  6. new Vue({
  7. render: h => h(App),
  8. }).$mount('#app')

项目结构

页面代码中引用web3,倒入ERC20代币的abi

  1. <template>
  2. <div>
  3. <p>{{msg}}</p>
  4. <button @click="get">连接钱包</button>
  5. <button @click="getETH">获取账户信息</button>
  6. <button @click="getTransfer">ETH转帐</button>
  7. <button @click="getTokenBalance">代币余额</button>
  8. <button @click="getTokenTransfer">转账代币</button>
  9. <button @click="getAllowance">查询授权金额</button>
  10. <button @click="getApprove">授权</button>
  11. </div>
  12. </template>
  13. <script>
  14. import Web3 from 'web3'
  15. import abi from '../abi/ERC20.json'
  16. export default {
  17. name: 'Connect',
  18. props: {
  19. msg: String
  20. },
  21. data() {
  22. return {};
  23. },
  24. mounted() {},
  25. methods: {
  26. getData(){
  27. console.log('first')
  28. },
  29. get() { // 唤起钱包
  30. if (window.ethereum) {
  31. window.ethereum.enable().then((res) => {
  32. alert("当前钱包地址:" + res[0]);
  33. });
  34. } else {
  35. alert("请安装MetaMask钱包");
  36. }
  37. },
  38. async getETH() {
  39. let web3 = new Web3(window.web3.currentProvider)
  40. console.log(web3)
  41. // console.log(web3.eth.getAccounts())
  42. let fromAddress = await web3.eth.getAccounts()
  43. console.log(web3.eth.getBalance(fromAddress[0]))
  44. console.log(fromAddress)
  45. web3.eth.getBalance(fromAddress[0],(err, res) => {
  46. if (!err) {
  47. alert("ETH余额=="+res/Math.pow(10,18));
  48. //console.log(res)
  49. }
  50. })
  51. },
  52. //ETH转账
  53. async getTransfer(){
  54. let web3 = new Web3(window.web3.currentProvider)
  55. let fromAddress = await web3.eth.getAccounts()
  56. let amount = 0.01*Math.pow(10,18);
  57. let toAddress = "0x17D98A1c1D4814B03d71a08a07AF4C8CCABb7E2E";
  58. web3.eth.sendTransaction({
  59. gas: 21000,
  60. gasPrice: 5000000000,
  61. from: fromAddress[0],
  62. to: toAddress,
  63. value: amount
  64. }, (err, result) => {
  65. console.log("转账Hash=",result)
  66. })
  67. },
  68. //查询代币余额
  69. async getTokenBalance(){
  70. if(window.web3) {
  71. var web3 = web3 = new Web3(window.web3.currentProvider);
  72. let fromAddress = "0x394A64e586FC05bD28783351F14dfcc426EFD230";//查询用户地址
  73. let ethContract = new web3.eth.Contract(abi.abi,"0x3d2dd604866d0ec1ddd5e8ef27848a6fc0962018") //所有代币的abi可以通用(abi,合约地址)
  74. let balance = await ethContract.methods.balanceOf(fromAddress).call()
  75. alert(balance)
  76. }
  77. },
  78. //直接转账充币地址
  79. async getTokenTransfer(){
  80. if(window.web3) {
  81. let web3 = new Web3(window.web3.currentProvider)
  82. let ethContract = new web3.eth.Contract(abi.abi,"0x3d2dd604866d0ec1ddd5e8ef27848a6fc0962018") //所有代币的abi可以通用(abi,合约地址)
  83. //转账数量
  84. let amount = 100*Math.pow(10,18);//转账100个
  85. //小狐狸账户
  86. let fromAddress = await web3.eth.getAccounts()
  87. //接收地址
  88. let toAddress = "0xcaD75EADAf24F41d6274E129d7aE54764d7cd8E7";
  89. ethContract.methods.transfer(toAddress,amount+'').send({ from: fromAddress[0] })
  90. }
  91. },
  92. //查询授权金额
  93. async getAllowance(){
  94. if(window.web3) {
  95. let web3 = new Web3(window.web3.currentProvider)
  96. let fromAddress = "0x394A64e586FC05bD28783351F14dfcc426EFD230";//查询地址
  97. let ethContract = new web3.eth.Contract(abi.abi,"0x3d2dd604866d0ec1ddd5e8ef27848a6fc0962018") //所有代币的abi可以通用(abi,合约地址)
  98. let toAddress = "0xcaD75EADAf24F41d6274E129d7aE54764d7cd8E7";//被授权地址
  99. let balance = await ethContract.methods.allowance(fromAddress,toAddress).call()
  100. alert("授权金额"+balance/Math.pow(10,18))
  101. }
  102. },
  103. //授权
  104. async getApprove(){
  105. if(window.web3) {
  106. let web3 = new Web3(window.web3.currentProvider)
  107. let ethContract = new web3.eth.Contract(abi.abi,"0x3d2dd604866d0ec1ddd5e8ef27848a6fc0962018") //所有代币的abi可以通用(abi,合约地址)
  108. //授权数量
  109. let amount = 100*Math.pow(10,18);//转账100个
  110. let toAddress = "0xcaD75EADAf24F41d6274E129d7aE54764d7cd8E7";//被授权地址
  111. //小狐狸账户
  112. let fromAddress = await web3.eth.getAccounts()
  113. ethContract.methods.approve(toAddress,amount+'').send({ from: fromAddress[0] })
  114. }
  115. }
  116. }
  117. }
  118. </script>
  119. <style>
  120. </style>

项目页面

 

调用小狐狸metamask演示

 

项目任何难题,可以加入qq群:981921011

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/445105
推荐阅读
相关标签
  

闽ICP备14008679号