如何通过 web3 和 infura 提供商集成智能合约
How to integrate smart contract via web3 and infura provider
我已经使用 infura 提供商创建了一个项目
const web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/07630919731949aa87a45b96c98a834d'))
然后我尝试调用智能合约的方法
{
to: addressTo,
from: addressFrom,
data: {
name: 'addWhitelisted',
inputs: [{
name: 'account',
address: '0x57e755461FF79176fC8f14B085A8CBb4AE1fC2f6'
}]
}
}
然后我需要签署交易并调用web3.eth.sendSignedTransaction
?
但是当我签名时出现错误。请帮助。我做错了什么?
- 应该是什么数据?
您需要使用 new web3.eth.Contract().methods.MyMethod().encodeABI()
为您的合约生成 data
属性 个 transaction
这是代码示例:
const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/07630919731949aa87a45b96c98a834d'))
const CONTRACT_ADDRESS = '0x3312fd1a550451beeda9fd2bd6e686af9ebabe1e'
const ADDRESS_TO_WHITELIST = '0x11c652e32b8064000a4ab34af0ae24e4966e309e'
const PRIVATE_KEY = '0x331E79A051B6D2B1F34C4195E70752D59E7E4F7E55244FA67BCC9CF476141231'
const CONTRACT_ABI = [ { constant: false, inputs: [ { name: '_address', type: 'address' } ], name: 'addWhitelisted', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [ { name: '', type: 'address' } ], name: 'whiteList', outputs: [ { name: '', type: 'bool' } ], payable: false, stateMutability: 'view', type: 'function' } ]
const sendRawTx = rawTx =>
new Promise((resolve, reject) =>
web3.eth
.sendSignedTransaction(rawTx)
.on('transactionHash', resolve)
.on('error', reject)
);
(async () => {
const { address: from } = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY)
const contract = new web3.eth.Contract(CONTRACT_ABI, CONTRACT_ADDRESS)
const query = await contract.methods.addWhitelisted(ADDRESS_TO_WHITELIST)
const transaction = {
to: CONTRACT_ADDRESS,
from,
value: '0',
data: query.encodeABI(),
gasPrice: web3.utils.toWei('20', 'gwei'),
gas: Math.round((await query.estimateGas({ from })) * 1.5), // 1.5 coefficient, just make sure that gas amount is enough
nonce: await web3.eth.getTransactionCount(from, 'pending')
}
const signed = await web3.eth.accounts.signTransaction(transaction, PRIVATE_KEY)
const hash = await sendRawTx(signed.rawTransaction)
console.log(hash)
})()
其中 Contract.sol
pragma solidity ^0.5.10;
contract Test {
mapping (address => bool) public whiteList;
function addWhitelisted(address _address) public {
whiteList[_address] = true;
}
}
我已经使用 infura 提供商创建了一个项目
const web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/07630919731949aa87a45b96c98a834d'))
然后我尝试调用智能合约的方法
{
to: addressTo,
from: addressFrom,
data: {
name: 'addWhitelisted',
inputs: [{
name: 'account',
address: '0x57e755461FF79176fC8f14B085A8CBb4AE1fC2f6'
}]
}
}
然后我需要签署交易并调用web3.eth.sendSignedTransaction
?
但是当我签名时出现错误。请帮助。我做错了什么?
- 应该是什么数据?
您需要使用 new web3.eth.Contract().methods.MyMethod().encodeABI()
为您的合约生成 data
属性 个 transaction
这是代码示例:
const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/07630919731949aa87a45b96c98a834d'))
const CONTRACT_ADDRESS = '0x3312fd1a550451beeda9fd2bd6e686af9ebabe1e'
const ADDRESS_TO_WHITELIST = '0x11c652e32b8064000a4ab34af0ae24e4966e309e'
const PRIVATE_KEY = '0x331E79A051B6D2B1F34C4195E70752D59E7E4F7E55244FA67BCC9CF476141231'
const CONTRACT_ABI = [ { constant: false, inputs: [ { name: '_address', type: 'address' } ], name: 'addWhitelisted', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [ { name: '', type: 'address' } ], name: 'whiteList', outputs: [ { name: '', type: 'bool' } ], payable: false, stateMutability: 'view', type: 'function' } ]
const sendRawTx = rawTx =>
new Promise((resolve, reject) =>
web3.eth
.sendSignedTransaction(rawTx)
.on('transactionHash', resolve)
.on('error', reject)
);
(async () => {
const { address: from } = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY)
const contract = new web3.eth.Contract(CONTRACT_ABI, CONTRACT_ADDRESS)
const query = await contract.methods.addWhitelisted(ADDRESS_TO_WHITELIST)
const transaction = {
to: CONTRACT_ADDRESS,
from,
value: '0',
data: query.encodeABI(),
gasPrice: web3.utils.toWei('20', 'gwei'),
gas: Math.round((await query.estimateGas({ from })) * 1.5), // 1.5 coefficient, just make sure that gas amount is enough
nonce: await web3.eth.getTransactionCount(from, 'pending')
}
const signed = await web3.eth.accounts.signTransaction(transaction, PRIVATE_KEY)
const hash = await sendRawTx(signed.rawTransaction)
console.log(hash)
})()
其中 Contract.sol
pragma solidity ^0.5.10;
contract Test {
mapping (address => bool) public whiteList;
function addWhitelisted(address _address) public {
whiteList[_address] = true;
}
}