@nervos/web3
is a high-order function takes provider
and Web3 Class
(optional) as inputs, returns nervos-supported web3 instance.
@nervos/web3
strictly abides by Semver, and is compatible with CITA by MAJOR
and MINOR
version, e.g. @nervos/[email protected]
will work perfectly with [email protected]
To use `@nervos/web3', you can add it via npm
yarn add @nervos/web3
or to link it in browser directly with
<script src="node_modules/@nervos/web3/lib/bundle.js" />
import Nervos from '@nervos/web3'
const nervos = Nervos('http://localhost:1337')
nervos.appchain
allows you to interact with an Nervos Appchain and Nervos Smart Contract.
/**
* @method peerCount
* @desc inquire peer count of appchain
* @param null
* @return {Promise<string>} Promise returns peer count
*/
nervos.appchain.peerCount()
/**
* @method getMetaData
* @desc inquire metadata of appchain, including `chainId`, `chainName`, `operator`, `website`, `genesisTimestamp`, `validators`, `blockInterval`, `tokenName`, `tokenSymbol`, `tokenAvatar`
* @param null
* @return {Promise<object>} Promise returns metadata
*/
nervos.appchain.getMetaData()
/**
* @method getAbi
* @param {string} - contract address
* @param {string} - block number
* @return {Promise<string>} Promise returns abi
*/
nervos.appchain.getAbi('0xb3f940e3b5F0AA26dB9f86F0824B3581fE18E9D7', 'latest')
/**
* @method getAbi
* @param {string} - contract address
* @param {string} - block number
* @return {Promise<string>} Promise returns code
*/
nervos.appchain.getCode('0xb3f940e3b5F0AA26dB9f86F0824B3581fE18E9D7', 'latest')
/**
* @method getBalance
* @param {string} - account address
* @return {Promise<BN>} Promise returns balance
*/
nervos.appchain.getBalance('0xb3f940e3b5F0AA26dB9f86F0824B3581fE18E9D7')
// TODO:
/**
* @method getBlock
* @param {string|number} - block hash or block number
* @return {Promise<Block>} Promise returns block
*/
nervos.appchain.getBlock(0)
/**
* @method getBlockByNumber
* @param {number} - block number
* @return {Promise<Block>} Promise returns block
*/
nervos.appchain.getBlockByNumber(0)
/**
* @method getBlockByHash
* @param {string} - block hash
* @return {Promise<Block>} Promise returns block
*/
nervos.appchain.getBlockByHash('0x0c56def738d15d9dfaad64ad246e8b5fe39e175ce3da308ea1018869522a1a4d')
/**
* @method getBlockNumber
* @param null
* @return {Promise<number>} Promise returns current block number
*/
nervos.appchain.getBlockNumber()
// TODO:
/**
* @method getTransactionCount
* @param {string} - account address
* @return {Promise<number>} Promise returns transaction count of account address
*/
nervos.appchain.getTransactionCount('0xb3f940e3b5F0AA26dB9f86F0824B3581fE18E9D7')
/**
* @method newMessageFilter
* @param {object} - topics object
* @return {Promise<string>} Promise returns filter id
*/
const topics = {
topics: ['0x8fb1356be6b2a4e49ee94447eb9dcb8783f51c41dcddfe7919f945017d163bf3'],
}
nervos.appchain.newMessageFilter(topics)
/**
* @method newBlockFilter
* @param null
* @return {Promise<string>} Promise returns filter id
*/
nervos.appchain.newBlockFilter()
/**
* @method getFilterChanges
* @param {string} - filter id
* @return {Promise<Array<string>} Promise returns filter logs
*/
nervos.appchain.getFilterChanges('0x1')
/**
* @method getFitlerLogs
* @param {string} - filter id
* @return {Promise<Array<string>} Promise returns filter logs
*/
nervos.appchain.getFitlerLogs('0x1')
/**
* @method deleteMessageFilter
* @param {string} - filter id
* @return {Promise<boolean>} Promise returns success
*/
nervos.appchain.deleteMessageFilter('0x1')
/**
* @method sign
* @desc sign message in wallet
* @param {string} - message to sign
* @param {string} - address of specified account
* @return {Promise<string>} Promise returns signed message
*/
nervos.appchain.sign(message, address)
/**
* @method personal.sign
* @desc sign message in wallet
* @param {string} - message to sign
* @param {string} - address of specified account
* @param {string} - password of specified account
* @return {Promise<string>} Promise returns signed message
*/
nervos.appchain.personal.sign(message, address, password)
/**
* @method sendTransaction
* @desc send transaction to appchain, if transaction including `privateKey`, it will sign the transaction locally, otherwise the transaction will be send to node and signed remote.
* @param {object} - transaction object
* @return {Promise<object>} Promise returns transaction hash
*/
nervos.appchain.sendTransaction(transaction)
/**
* @method sendSignedTransaction
* @desc send signed transaction to appchain
* @param {string} - signed transaction message
* @return {Promise<object>} Promise returns transaction hash
*/
nervos.appchain.sendSignedTransaction(signedTransaction)
/**
* @method getTransactionReceipt
* @desc inquire transaction receipt
* @param {string} - transaction hash
* @return {Promise<object>} Promise returns transaction receipt
*/
nervos.appchain.getTransactionReceipt(transactionHash)
/**
* @method getTransaction
* @desc inquire transaction
* @param {string} - transaction hash
* @return {Promise<object>} Promise returns transaction
*/
nervos.appchain.getTransaction(transactionHash)
const bytecode =
'6060604052341561000f57600080fd5b60d38061001d6000396000f3006060604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c14606e575b600080fd5b3415605857600080fd5b606c60048080359060200190919050506094565b005b3415607857600080fd5b607e609e565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a723058202d9a0979adf6bf48461f24200e635bc19cd1786efbcfc0608eb1d76114d405860029'
const privateKey = '0x...........'
const transaction = {
from: '0xb4061fA8E18654a7d51FEF3866d45bB1DC688717',
privateKey,
nonce: 999999,
quota: 1000000,
chainId: 1,
version: 0,
validUntilBlock: 999999,
value: '0x0',
}
web3.appchain.deploy(bytecode, tx)
// or
// contract = await new web3.appchain.Contract(abi).deploy({data: bytecode}).send(tx)
const abi = JSON.parse(
'[{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]',
)
const receipt = nervos.appchain.storeAbi(contractAddress, abiString, transaction)
const abi = JSON.parse(
'[{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]',
)
const contract = new nervos.appchain.Contract(abi, contractAddress)
// call method
contract.methods.get().call()
// send method
contract.methods.set(5).send(transaction)
nervos.appchain.signer({
privateKey: '0x7cc34429d268cdf33e1595d9aa3c56bfcb785c24b7f6dd031fe059d93d8e92d9',
data:
'6060604052341561000f57600080fd5b60d38061001d6000396000f3006060604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c14606e575b600080fd5b3415605857600080fd5b606c60048080359060200190919050506094565b005b3415607857600080fd5b607e609e565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a723058202d9a0979adf6bf48461f24200e635bc19cd1786efbcfc0608eb1d76114d405860029',
nonce: '47',
quota: 999999,
validUntilBlock: 114930,
version: 0,
chainId: 1,
value: '0',
})
nervos.appchain.unsigner(transactionContent)
nervos.listeners.listenToTransactionReceipt(result.hash).then(console.log)