- 第 1 章 区块链
- 第 2 章 以太坊
- 第 3 章 以太坊私链入门
- 第 4 章 以太坊网络
- 第 5 章 geth v1.8.16 命令详解
- 第 6 章 Wallet
- 第 7 章 Token
- 第 8 章 智能合约语言 Solidity v0.5.0
- 第 9 章 Truffle v4.1.8 开发框架
- 第 10 章 web3.js - 1.0.0
- 第 11 章 web3j v3.4.0 - Jave Client
- 11.2. 启动以太坊
- 11.3. Maven pom.xml 文件
- 11.4. Java 与 Solidity 数据类型映射关系
- 11.5. 常量
- 11.6. 连接到服务器获取版本号
- 11.7. 获得以太坊状态信息
- 11.8. 单位转换
- 11.9. 账号管理
- 11.10. Credentials
- 11.11. 交易
- 11.12. 钱包
- 11.13. 智能合约
- 11.14. ERC20合约
- 11.15. Infura
- 11.16. 助记词
- 11.17. 过滤器 (Filter)
- 11.18. Subscription
- 11.19. 解锁账号
- 11.20. IBAN (International Bank Account Number)
- 11.21. Springboot with Ethereum (web3j)
- 第 12 章 web3.py - A python interface for interacting with the Ethereum blockchain and ecosystem.
- 第 14 章 Ethereum Developer APIs
- 第 15 章 infura
- 第 16 章 以太坊案例
- 第 17 章 FAQ
- 17.3. Error: authentication needed: password or unlock
- 17.4. 新增节点后不生效
- 17.5. Unhandled rejection Error: Returned error: The method personal_unlockAccount does not exist/is not available
- 17.6. Error: exceeds block gas limit
- 17.7. Migrations.sol:11:3: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(…) { … }" instead.
- 17.8. Exception in thread "main" rx.exceptions.OnErrorNotImplementedException: Invalid response received: okhttp3.internal.http.RealResponseBody@6c25e6c4
- 17.9. 旧版本 Remix(browser-solidity) 本地安装
- 第 18 章 Hyperledger Fabric v2.0.0
- 第 19 章 Hyperledger Fabric 运维
- 第 20 章 Chaincode 链码(智能合约)
- 第 21 章 Hyperledger Fabric Client SDK for Node.js
- 第 22 章 fabric-sdk-java
- 第 24 章 已知 Hyperledger 落地案例
- 第 25 章 Fabric Command
- 第 26 章 Fabric FAQ
- 第 27 章 IPFS(InterPlanetary File System,星际文件系统)
- 第 28 章 IPFS 命令
- 第 29 章 IPFS WebUI
- 第 30 章 IPFS 集群配置
- 第 31 章 IPFS API
- 第 32 章 IPFS Faq
- 第 33 章 EOS
- 第 34 章 EOS 安装
- 第 35 章 CLEOS
- 第 36 章 智能合约开发
- 第 37 章 EOS Dapp 开发
- 第 38 章 FAQ
- 第 39 章 BaaS (Blockchain as a Service) 平台
- 第 40 章 BitCoin
- 第 41 章 其他区块链相关
- 附录 1. 附录
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
15.4. infura.io web3.js 开发
15.4. infura.io web3.js 开发
15.4.1. Web3 通过 infura 连接到 Ropsten 测试网络
fs = require('fs'); const Web3 = require('web3'); const web3 = new Web3('https://ropsten.infura.io/CsS9shwaAab0z7B4LP2d'); console.log(web3.version) const abi = fs.readFileSync('output/TokenERC20.abi', 'utf-8'); const contractAddress = "0x70682386d0dE84B1e549DC3c4305CCB2D261b2a8"; const coinbase = "0xB94054c174995AE2A9E7fcf6c7924635FBa8ECF7"; const toAddress = "0xf56b81a2bcb964D2806071e9Be4289A5559BB0fA"; balanceWei = web3.eth.getBalance(coinbase); console.log(balanceWei); const contract = new web3.eth.Contract(JSON.parse(abi), contractAddress, { from: coinbase , gas: 100000}); contract.methods.balanceOf(coinbase).call().then(console.log).catch(console.error); contract.methods.balanceOf(toAddress).call().then(console.log).catch(console.error);
15.4.2. 使用 truffle-hdwallet-provider 连接到 https://ropsten.infura.io
fs = require('fs'); const Web3 = require('web3'); const HDWalletProvider = require("truffle-hdwallet-provider"); const mnemonic = "drill hunt food team moment mistake bird attitude tunnel ecology sister resist"; const web3 = new Web3(new HDWalletProvider(mnemonic,'https://ropsten.infura.io/CsS8shwaCab0a7B4LP2d')); console.log(web3.version) const abi = fs.readFileSync('output/TokenERC20.abi', 'utf-8'); const contractAddress = "0x70682386d0dE84B1e549DC3c4305CCB2D261b2a8"; const coinbase = "0xB94054c174995AE2A9E7fcf6c7924635FBa8ECF7"; const toAddress = "0xf56b81a2bcb964D2806071e9Be4289A5559BB0fA"; balanceWei = web3.eth.getBalance(coinbase); console.log(balanceWei); const contract = new web3.eth.Contract(JSON.parse(abi), contractAddress, { from: coinbase , gas: 100000}); contract.methods.balanceOf(coinbase).call().then(console.log).catch(console.log); contract.methods.balanceOf(toAddress).call().then(console.log).catch(console.log);
15.4.3. 转账
const coinbase = "0xB94054c174995AE2A9E7fcf6c7924635FBa8ECF7"; const toAddress = "0xf56b81a2bcb964D2806071e9Be4289A5559BB0fA"; const privateKey = "e33ea581d88e0bd2270c0fd109604039a3de59671b6d69882b4cb4688d3dcffd" var nonce = await web3.eth.getTransactionCount(coinbase); var gasPrice = await web3.eth.getGasPrice(); console.log(`gasPrice: ${gasPrice}\n`) var gasLimit = 1000000; var transferAmount = 1000; var chainId = 1; var rawTransaction = { "from": coinbase, "nonce": web3.utils.toHex(count), "gasPrice": web3.utils.toHex(gasPrice), "gasLimit": web3.utils.toHex(gasLimit), "to": toAddress, "value": "100", "data": "0x0", "chainId": web3.utils.toHex(chainId) }; var privateKey = new Buffer(privateKey, 'hex'); var tx = new Tx(rawTransaction); tx.sign(privateKey); var serializedTx = tx.serialize(); web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', console.log);
'use strict'; const Web3 = require('web3'); const wsAddress = 'wss://rinkeby.infura.io/ws'; const contractJson = '(taken from solc or remix online compiler)'; const privateKey = '0xOOOX'; const contractAddress = '0xOOOX'; const walletAddress = '0xOOOX'; const webSocketProvider = new Web3.providers.WebsocketProvider(wsAddress); const web3 = new Web3(new Web3.providers.WebsocketProvider(webSocketProvider)); const contract = new web3.eth.Contract( JSON.parse(contractJson), contractAddress ); // change this to whatever contract method you are trying to call, E.G. SimpleStore("Hello World") const query = contract.methods.SimpleStore('Hello World'); const encodedABI = query.encodeABI(); const tx = { from: walletAddress, to: contractAddress, gas: 2000000, data: encodedABI, }; const account = web3.eth.accounts.privateKeyToAccount(privateKey); console.log(account); web3.eth.getBalance(walletAddress).then(console.log); web3.eth.accounts.signTransaction(tx, privateKey).then(signed => { const tran = web3.eth .sendSignedTransaction(signed.rawTransaction) .on('confirmation', (confirmationNumber, receipt) => { console.log('=> confirmation: ' + confirmationNumber); }) .on('transactionHash', hash => { console.log('=> hash'); console.log(hash); }) .on('receipt', receipt => { console.log('=> reciept'); console.log(receipt); }) .on('error', console.error); });
15.4.4. 执行合约
const fs = require('fs'); const Web3 = require('web3'); const web3 = new Web3("https://mainnet.infura.io/CsS9shwaAab0z7B4LP2d"); const Tx = require('ethereumjs-tx'); const abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_status","type":"bool"}],"name":"setAirdropStatus","outputs":[{"name":"status","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"airdropStatus","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_mintedAmount","type":"uint256"}],"name":"mintAirdropToken","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"burn","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_lock","type":"bool"}],"name":"setLock","outputs":[{"name":"status","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_address","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"airdropCurrentTotal","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"target","type":"address"},{"name":"mintedAmount","type":"uint256"}],"name":"mintToken","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_value","type":"uint256"}],"name":"burnFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_amount","type":"uint256"}],"name":"setAirdropAmount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"frozenAccount","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"airdropTotalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"touched","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"target","type":"address"},{"name":"freeze","type":"bool"}],"name":"freezeAccount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"lock","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"airdropAmount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"},{"name":"tokenName","type":"string"},{"name":"tokenSymbol","type":"string"},{"name":"decimalUnits","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"target","type":"address"},{"indexed":false,"name":"frozen","type":"bool"}],"name":"FrozenFunds","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"target","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"AirDrop","type":"event"}]; const address = "0x3e827461Cc53CAE366175A291ed7c629F87CfF39"; const key = "19A57E4F6274AF1E0B9C3F8F7E3503876A850AFEE1912B8B9C5D9358EDEA0362" const contractAddress = "0x44cCf3d1601427Fe0B0f7588eD058216830cd13C"; const contract = new web3.eth.Contract(abi, contractAddress, { "from": address}); contract.methods.balanceOf(address).call().then(function(balance){ console.log(balance) }); contract.methods.decimals().call().then(function(decimals){ console.log(decimals) }); web3.eth.getGasPrice().then(function(gasPrice){ var price = Number(gasPrice); web3.eth.getTransactionCount(address).then(function(nonce){ var amount = "1000000"; contract.methods.mintAirdropToken(amount).estimateGas().then(function(gas){ var rawTransaction = { "nonce": web3.utils.toHex(nonce), "from": address, "to": contractAddress, "gas": web3.utils.toHex(gas), "gasPrice": web3.utils.toHex(price), // "gasLimit": this.web3.utils.toHex(gasLimit.gasLimit), "value": "0x0", "data": contract.methods.mintAirdropToken(amount).encodeABI() }; console.log(rawTransaction); var privateKey = new Buffer.from(key, 'hex'); var tx = new Tx(rawTransaction); tx.sign(privateKey); var serializedTx = tx.serialize(); web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', function(txhash){ console.log(txhash); }); }); }); }); web3.eth.getGasPrice().then(function(gasPrice){ var price = Number(gasPrice); web3.eth.getTransactionCount(address).then(function(nonce){ var amount = "10"; contract.methods.setAirdropAmount(amount).estimateGas().then(function(gas){ var rawTransaction = { "nonce": web3.utils.toHex(nonce), "from": address, "to": contractAddress, "gas": web3.utils.toHex(gas), "gasPrice": web3.utils.toHex(price), "value": "0x0", "data": contract.methods.setAirdropAmount(amount).encodeABI() }; console.log(rawTransaction); var privateKey = new Buffer.from(key, 'hex'); var tx = new Tx(rawTransaction); tx.sign(privateKey); var serializedTx = tx.serialize(); web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', function(txhash){ console.log(txhash); }); }); }); }); web3.eth.getGasPrice().then(function(gasPrice){ var price = Number(gasPrice); web3.eth.getTransactionCount(address).then(function(nonce){ var status = true; contract.methods.setAirdropStatus(status).estimateGas().then(function(gas){ var rawTransaction = { "nonce": web3.utils.toHex(nonce), "from": address, "to": contractAddress, "gas": web3.utils.toHex(gas), "gasPrice": web3.utils.toHex(price), "value": "0x0", "data": contract.methods.setAirdropStatus(status).encodeABI() }; console.log(rawTransaction); var privateKey = new Buffer.from(key, 'hex'); var tx = new Tx(rawTransaction); tx.sign(privateKey); var serializedTx = tx.serialize(); web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', function(txhash){ console.log(txhash); }); }); }); });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论