当前位置:   article > 正文

Web3.js与智能合约交互_web3 tokenabi

web3 tokenabi

Web3.js教程:

web3.js - 以太坊 JavaScript API — web3.js 中文文档 — 登链社区https://learnblockchain.cn/docs/web3.js/index.html

Web3js源码:

GitHub - ChainSafe/web3.js: Ethereum JavaScript APIhttps://github.com/ChainSafe/web3.js初始化

  1. cd web3jTest
  2. npm init

下载web3到项目中

npm install web3 --save

交互代码示例:

Nodejs版本:

  1. var Web3 = require('web3');
  2. var provider = new Web3.providers.HttpProvider("http://localhost:7545");
  3. var web3 = new Web3(provider);
  4. var json = require("./contracts/MetaCoin.json");
  5. var contractAddr = '0x9C334Fb3287dA1E6cc063B084c5432985a708999';
  6. var metacoin = new web3.eth.Contract(json['abi'],contractAddr);
  7. var account = "0x060f4e095Eeb77ad987412578652a5201436E3c3";
  8. metacoin.methods.getBalance(account).call({from: account}, function(error, result){
  9. console.log(error);
  10. console.log(result);
  11. });
  12. //内网测试
  13. metacoin.methods.buyNow(10000, 1).send({from: account, gas: "300000"}, function(error, transactionHash){
  14. console.log(error)
  15. console.log(transactionHash)
  16. });
  17. //公网测试
  18. var functionEncode = metacoin.methods.buyNow(10000, 1).encodeABI();
  19. console.log(functionEncode)
  20. var sign = web3.eth.accounts.signTransaction({
  21. from: account,
  22. gasPrice: 20000000000,
  23. gas: 300000,
  24. to: contractAddr,
  25. data: functionEncode,
  26. }, privateKey, function(error, result){
  27. console.log(error)
  28. console.log("signTransaction:", result)
  29. var result = web3.eth.sendSignedTransaction(result.rawTransaction, function(error, result){
  30. console.log(error)
  31. console.log("sendSignedTransaction:", result)
  32. });
  33. });

测试执行

node test.js

执行结果:

null
9500

H5版本:

  1. <script src="./dist/web3.min.js"></script>
  2. <script>
  3. const tokenABI = [...];
  4. var ethereum
  5. var localAccount
  6. if (typeof window.ethereum !== 'undefined') {
  7. console.log('MetaMask is installed!')
  8. ethereum = window.ethereum
  9. }
  10. const contractAddr = '0x28e784AB8FA34321cb78F7F92B8b374646111111';
  11. //获取当前钱包地址
  12. ethereumButton.addEventListener('click', () => {
  13. event.preventDefault()
  14. ethereum.request({
  15. method: 'eth_requestAccounts'
  16. }).then((ret) => {
  17. localAccount = ret[0]
  18. console.log('accounts: ', ret)
  19. })
  20. })
  21. getNonceButton.addEventListener('click', (event) => {
  22. event.preventDefault()
  23. getBalanceTx()
  24. })
  25. buyNowButton.addEventListener('click', (event) => {
  26. event.preventDefault()
  27. buyNowTx(10000, 1)
  28. })
  29. </script>
  30. <script type="text/javascript">
  31. const buyNowTx = async (artworkId, num) => {
  32. var web3 = new Web3(ethereum)
  33. var tokenContract = new web3.eth.Contract(tokenABI, contractAddr)
  34. var nonceVal = await web3.eth.getTransactionCount(localAccount);
  35. var ret = await tokenContract.methods.buyNow(artworkId, num).send({
  36. from: localAccount,
  37. nonce: nonceVal,
  38. gas: 800000
  39. })
  40. console.log(ret.transactionHash)
  41. return ret
  42. }
  43. const getBalanceTx = async () => {
  44. var web3 = new Web3(ethereum)
  45. var tokenContract = new web3.eth.Contract(tokenABI, contractAddr)
  46. var balance = await tokenContract.methods.getBalance(localAccount).call()
  47. console.log("balance:", balance)
  48. return balance
  49. }
  50. </script>
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号