0.0.12 • Published 3 years ago
@huobiwallet-js/contract v0.0.12
@huobiwallet-js/contract
This package provides a collection of apis to create, deploy, and interact with smart contracts. In Huobiwallet, smart contracts all fully EVM compatible and the formats and terminologies match 1-to-1 with EVM smart contracts.
Installation
npm install @huobiwallet-js/contract
Usage
Estimate gas for contract methods
const options1 = { gasPrice: '0x3B9ACA00' }; // gas price in hex corresponds to 1 Gwei or 1000000000
contract.methods.getCount().estimateGas(options1).then(gas => {
console.log('gas required for getCount is ' + hexToNumber(gas));
});
Call contract read-only methods. Huobiwallet uses 1 Gwei gas price and gas limit of 21000 by default. Use the estimate gas api to correctly set the gas limit.
const options1 = { gasPrice: '0x3B9ACA00' }; // gas price in hex corresponds to 1 Gwei or 1000000000
let options2 = { gasPrice: 1000000000, gasLimit: 21000 }; // setting the default gas limit, but changing later based on estimate gas
contract.methods.getCount().estimateGas(options1).then(gas => {
options2 = {...options2, gasLimit: hexToNumber(gas)};
contract.methods.getCount().call(options2).then(count => {
console.log('counter value: ' + count);
});
});
Invoking contract modification methods using send
api. Need to add a signing account to the contract wallet, otherwise send
api will not work.
const options1 = { gasPrice: '0x3B9ACA00' }; // gas price in hex corresponds to 1 Gwei or 1000000000
let options2 = { gasPrice: 1000000000, gasLimit: 21000 }; // setting the default gas limit, but changing later based on estimate gas
contract.wallet.addByPrivateKey('1f054c21a0f57ebc402c00e14bd1707ddf45542d4ed9989933dbefc4ea96ca68');
contract.methods.incrementCounter().estimateGas(options1).then(gas => {
options2 = {...options2, gasLimit: hexToNumber(gas)};
contract.methods.incrementCounter().send(options2).then(response => {
console.log(response.transaction.receipt);
});
});
All the above apis can also be asynchronously executed using async
and await
.
0.0.12
3 years ago