赞
踩
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初始化
- cd web3jTest
- npm init
下载web3到项目中
npm install web3 --save
交互代码示例:
Nodejs版本:
- var Web3 = require('web3');
- var provider = new Web3.providers.HttpProvider("http://localhost:7545");
-
- var web3 = new Web3(provider);
-
- var json = require("./contracts/MetaCoin.json");
- var contractAddr = '0x9C334Fb3287dA1E6cc063B084c5432985a708999';
-
- var metacoin = new web3.eth.Contract(json['abi'],contractAddr);
-
- var account = "0x060f4e095Eeb77ad987412578652a5201436E3c3";
-
- metacoin.methods.getBalance(account).call({from: account}, function(error, result){
- console.log(error);
- console.log(result);
- });
-
- //内网测试
- metacoin.methods.buyNow(10000, 1).send({from: account, gas: "300000"}, function(error, transactionHash){
- console.log(error)
- console.log(transactionHash)
- });
-
- //公网测试
- var functionEncode = metacoin.methods.buyNow(10000, 1).encodeABI();
- console.log(functionEncode)
- var sign = web3.eth.accounts.signTransaction({
- from: account,
- gasPrice: 20000000000,
- gas: 300000,
- to: contractAddr,
- data: functionEncode,
- }, privateKey, function(error, result){
- console.log(error)
- console.log("signTransaction:", result)
- var result = web3.eth.sendSignedTransaction(result.rawTransaction, function(error, result){
- console.log(error)
- console.log("sendSignedTransaction:", result)
- });
- });
测试执行
node test.js
执行结果:
null
9500
H5版本:
- <script src="./dist/web3.min.js"></script>
- <script>
- const tokenABI = [...];
- var ethereum
- var localAccount
- if (typeof window.ethereum !== 'undefined') {
- console.log('MetaMask is installed!')
- ethereum = window.ethereum
- }
- const contractAddr = '0x28e784AB8FA34321cb78F7F92B8b374646111111';
-
- //获取当前钱包地址
- ethereumButton.addEventListener('click', () => {
- event.preventDefault()
- ethereum.request({
- method: 'eth_requestAccounts'
- }).then((ret) => {
- localAccount = ret[0]
- console.log('accounts: ', ret)
- })
- })
-
- getNonceButton.addEventListener('click', (event) => {
- event.preventDefault()
- getBalanceTx()
- })
-
- buyNowButton.addEventListener('click', (event) => {
- event.preventDefault()
- buyNowTx(10000, 1)
- })
- </script>
-
-
- <script type="text/javascript">
- const buyNowTx = async (artworkId, num) => {
- var web3 = new Web3(ethereum)
- var tokenContract = new web3.eth.Contract(tokenABI, contractAddr)
- var nonceVal = await web3.eth.getTransactionCount(localAccount);
- var ret = await tokenContract.methods.buyNow(artworkId, num).send({
- from: localAccount,
- nonce: nonceVal,
- gas: 800000
- })
- console.log(ret.transactionHash)
- return ret
- }
-
- const getBalanceTx = async () => {
- var web3 = new Web3(ethereum)
- var tokenContract = new web3.eth.Contract(tokenABI, contractAddr)
- var balance = await tokenContract.methods.getBalance(localAccount).call()
- console.log("balance:", balance)
- return balance
- }
- </script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。