meterify.eth.Contract
The meterify.eth.Contract
object makes it easy to interact with smart contracts on the Meter blockchain. When you create a new contract object, you give it the json interface of the respective smart contract and web3 will auto convert all calls into low level ABI calls over RPC for you.
This allows you to interact with smart contracts as if they were JavaScript objects.
To use it standalone:
meterify.eth.Contract
Creates a new contract instance with all its methods and events defined in its json interface
object.
Example:
Parameters:
jsonInterface
-Array
: The json interface for the contract to instantiateaddress
-String
(optional): This address is necessary for transactions and call requests and can also be added later usingmyContract.options.address = '0x1234..'.
options
-Object
(optional): The options of the contract. Some are used as fallbacks for calls and transactions:
Returns:
Object
: The contract instance with all its methods and events.
Properties
options
The contract options object has the following properties:
data
-String
: The contract bytecode.address
-String
(deprecated usecontract.address
): The address of the contract.
address
The address used for this contract instance.
Example:
All transactions generated by web3.js from this contract will contain this address as the "to".
The address will be stored in lowercase.
Property:
address
- String
or null: The address for this contract, or null
if it is not yet set.
jsonInterface
The json interface
object derived from the ABI of this contract.
Property
jsonInterface
- AbiModel
: The json interface
for this contract. Re-setting this will regenerate the methods and events of the contract instance.
AbiModel
AbiItemModel
AbiInput
AbiOutput
Methods
clone
Clones the current contract instance.
Example:
Parameters:
none
Returns:
Object
: The new contract instance.
deploy
Call this function to deploy the contract to the blockchain.
Example:
After successful deployment the promise will resolve with a new contract instance.
Parameters:
options
-Object
: The options used for deployment.data
-String
: The byte code of the contract.arguments
-Array
(optional): The arguments which get passed to the constructor on deployment.
Returns:
Object
: The transaction object:
Array
- arguments: The arguments passed to the method before. They can be changed.Function
-send
(#contract-send): Will deploy the contract. The promise will resolve with the new contract instance, instead of the receipt!Function
-estimateGas
: Will estimate the gas used for deploying.Function
-encodeABI
: Encodes the ABI of the deployment, which is contract data + constructor parameters
For details to the methods see the documentation below.
methods
Creates a transaction object for that method, which then can be called
, send
, estimated
or ABI encoded
.
The methods of this smart contract are available through:
The name:
myContract.methods.myMethod(123)
The name with parameters:
myContract.methods['myMethod(uint256)'](123)
The signature:
myContract.methods['0x58cf5f10'](123)
This allows calling functions with same name but different parameters from the JavaScript contract object.
Example:
Parameters:
Parameters of any method depend on the smart contracts methods, defined in the json interface
.
Returns:
Object
: The Transaction Object:
Array
- arguments: The arguments passed to the method before. They can be changed.Function
-call
: Will call the "constant" method and execute its smart contract method in the EVM without sending a transaction (Can't alter the smart contract state).Function
-send
: Will send a transaction to the smart contract and execute its method (Can alter the smart contract state).Function
-estimateGas
: Will estimate the gas used when the method would be executed on chain.Function
-encodeABI
: Encodes the ABI for this method. This can be send using a transaction, call the method or passing into another smart contracts method as argument.
For details to the methods see the documentation below.
methods.myMethod.call
Will call a "constant" method and execute its smart contract method in the EVM without sending any transaction. Note calling can not alter the smart contract state.
Example:
Parameters:
options
-Object
(optional): The options used for calling.transactionObject
*from
-String
(optional): The address the call "transaction" should be made from.gasPrice
-String
(optional): The gas price in wei to use for this call "transaction". It is the wei per unit of gas.gas
-Number
(optional): The maximum gas provided for this call "transaction" (gas limit).
blockNumber
-Number
: The block number this log was created in.null
when still pending.callback
-Function
(optional): This callback will be fired with the result of the smart contract method execution as the second argument, or with an error object as the first argument.
Returns:
Promise<any>
- The return value(s) of the smart contract method. If it returns a single value, it's returned as is. If it has multiple return values they are returned as an object with properties and indices.
methods.myMethod.send
Will send a transaction to the smart contract and execute its method. Note this can alter the smart contract state.
Example:
Parameters:
options
-Object
: The options used for sending.from
-String
: The address the transaction should be sent from.gasPrice
-String
(optional): The gas price in wei to use for this transaction.It is the wei per unit of gas.gas
-Number
(optional): The maximum gas provided for this transaction (gas limit).value
-Number|String|BN|BigNumber
(optional): The value transferred for the transaction in wei.
callback
-Function
(optional): This callback will be fired first with the "transactionHash", or with an error object as the first argument.
Returns:
The callback will return the 32 bytes transaction hash.
PromiEvent
: A promise combined event emitter
. Will be resolved when the transaction receipt is available, OR if this send()
is called from a someContract.deploy()
, then the promise will resolve with the new contract instance. Additionally the following events are available:
"transactionHash"
returnsString
: is fired right after the transaction is sent and a transaction hash is available."receipt"
returnsObject
: is fired when the transaction receipt is available. Receipts from contracts will have nologs
property, but instead anevents
property with event names as keys and events as properties. SeegetPastEvents return values
for details about the returned event object."confirmation"
returnsNumber
,Object
: is fired for every confirmation up to the 24th confirmation. Receives the confirmation number as the first and the receipt as the second argument. Fired from confirmation 1 on, which is the block where it's mined."error"
returnsError
: is fired if an error occurs during sending. If an out of gas error, the second parameter is the receipt.
methods.myMethod.estimateGas
Will call estimate the gas a method execution will take when executed in the EVM without sending a transaction.
Example:
Parameters:
options
-Object
(optional): The options used for calling.from
-String
(optional): The address the call "transaction" should be made from.gas
-Number
(optional): The maximum gas provided for this call "transaction" (gas limit). Setting a specific value helps to detect out of gas errors. If all gas is used it will return the same number.value
-Number|String|BN|BigNumber
(optional): The value transferred for the call "transaction" in wei.
callback
-Function
(optional): This callback will be fired with the result of the gas estimation as the second argument, or with an error object as the first argument.
Returns:
Promise( Number
) - The gas amount estimated.
methods.myMethod.encodeABI
Encodes the ABI for this method. This can be used to send a transaction, call a method, or pass it into another smart contracts method as arguments.
Example:
Parameters:
none
Returns:
String
: The encoded ABI byte code to send via a transaction or call.
Events
once
Example:
Parameters:
event
-String
: The name of the event in the contract, or"allEvents"
to get all events.options
-Object
(optional): The options used for deployment.filter
-Object
(optional): Lets you filter events by indexed parameters, e.g.{filter: {myNumber: [12,13]}}
means all events where "myNumber" is 12 or 13.topics
-Array
(optional): This allows you to manually set the topics for the event filter. If given the filter property and event signature, (topic[0]) will not be set automatically.callback
-Function
: This callback will be fired for the first event as the second argument, or an error as the first argument. SeegetPastEvents return values
for details about the event structure.
Returns:
undefined
events
Subscribe to an event.
Example:
Parameters:
options
-Object
(optional): The options used for deployment.filter
-Object
(optional): Let you filter events by indexed parameters, e.g.{filter: {myNumber: [12,13]}}
means all events where "myNumber" is 12 or 13.fromBlock
-Number
(optional): The block number from which to get events on.topics
-Array
(optional): This allows to manually set the topics for the event filter. If given the filter property and event signature, (topic[0]) will not be set automatically.
callback
-Function
(optional): This callback will be fired for each event as the second argument, or an error as the first argument.
Returns:
EventEmitter
: The event emitter has the following events:
"data"
returnsObject
: Fires on each incoming event with the event object as argument."changed"
returnsObject
: Fires on each event which was removed from the blockchain. The event will have the additional property"removed: true"
."error"
returnsObject
: Fires when an error in the subscription occurs.
The structure of the returned event Object
looks as follows:
event
-String
: The event name.signature
-String|Null
: The event signature,null
if it's an anonymous event.address
-String
: Address this event originated from.returnValues
-Object
: The return values coming from the event, e.g.{myVar: 1, myVar2: '0x234...'}
.logIndex
-Number
: Integer of the event index position in the block.transactionIndex
-Number
: Integer of the transaction's index position the event was created in.transactionHash
32 Bytes -String
: Hash of the transaction this event was created in.blockHash
32 Bytes -String
: Hash of the block this event was created in.null
when it's still pending.blockNumber
-Number
: The block number this log was created in.null
when still pending.raw.data
-String
: The data containing non-indexed log parameter.raw.topics
-Array
: An array with max 4 32 Byte topics, topic 1-3 contains indexed parameters of the event.events.
allEvents
Same as events
but receives all events from this smart contract. Optionally the filter property can filter those events.
getPastEvents
Gets past events for this contract.
Example:
Parameters:
event
-String
: The name of the event in the contract, or"allEvents"
to get all events.options
-Object
(optional): The options used for deployment.filter
-Object
(optional): Lets you filter events by indexed parameters, e.g.{filter: {myNumber: [12,13]}}
means all events where "myNumber" is 12 or 13.fromBlock
-Number
(optional): The block number from which to get events on.toBlock
-Number
(optional): The block number to get events up to (Defaults to"latest"
).topics
-Array
(optional): This allows manually setting the topics for the event filter. If given the filter property and event signature, (topic[0]) will not be set automatically.
callback
-Function
(optional): This callback will be fired with an array of event logs as the second argument, or an error as the first argument.
Returns:
Promise
returns Array
: An array with the past event Objects
, matching the given event name and filter.
For the structure of a returned event Object
see getPastEvents return values
.
Last updated