🛡️Javascript API
Introduction
To make your Ðapp work with on Lattice Network, you can use the web3
object provided by the web3.js library. Under the hood it communicates to a local node through RPC calls. web3.js works with AlethZero, geth and Mist, and also in an external browser if one of the former nodes are running locally.
web3
contains the eth
object - web3.eth
(for specifically Lattice Network blockchain interactions) and the shh
object - web3.shh
(for Whisper interaction). Over time we'll introduce other objects for each of the other web3 protocols.
Using callbacks
As this API is designed to work with a local RPC node and all its functions are by default use synchronous HTTP requests.
If you want to make asynchronous request, you can pass an optional callback as the last parameter to most functions. All callbacks are using an error first callback style:
A note on big numbers in JavaScript
You will always get a BigNumber object for balance values as JavaScript is not able to handle big numbers correctly. Look at the following examples:
web3.js depends on the BigNumber Library and adds it automatically.
The next example wouldn't work as we have more than 20 floating points, therefore it is recommended to keep you balance always in wei and only transform it to other units when presenting to the user:
API
register(hexString) (Not implemented yet)
unRegister(hexString) (Not implemented yet)
Usage
web3
The web3
object provides all methods.
Example
web3.version.api
Returns
String
- The Lattice Network js api version.
Example
web3.version.client
Returns
String
- The client/node version.
Example
web3.version.network
Returns
String
- The network protocol version.
Example
web3.version.Lattice Network
Returns
String
- The Lattice Network protocol version.
Example
web3.version.whisper
Returns
String
- The whisper protocol version.
Example
web3.setProvider
Should be called to set provider.
Parameters
none
Returns
undefined
Example
web3.reset
Should be called to reset state of web3. Resets everything except manager. Uninstalls all filters. Stops polling.
Parameters
none
Returns
undefined
Example
web3.sha3
Parameters
String
- The string to hash using the SHA3 algorithmFunction
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
String
- The SHA3 of the given data.
Example
web3.toHex
Converts any value into HEX.
Parameters
String|Number|Object|Array|BigNumber
- The value to parse to HEX. If its an object or array it will beJSON.stringify
first. If its a BigNumber it will make it the HEX value of a number.
Returns
String
- The hex string of mixed
.
Example
web3.toAscii
Converts a HEX string into a ASCII string.
Parameters
String
- A HEX string to be converted to ascii.
Returns
String
- An ASCII string made from the given hexString
.
Example
web3.fromAscii
Converts any ASCII string to a HEX string.
Parameters
String
- An ASCII string to be converted to HEX.Number
- The number of bytes the returned HEX string should have.
Returns
String
- The converted HEX string.
Example
web3.toDecimal
Converts a HEX string to its number representation.
Parameters
String
- An HEX string to be converted to a number.
Returns
Number
- The number representing the data hexString
.
Example
web3.fromDecimal
Converts a number or number string to its HEX representation.
Parameters
Number|String
- A number to be converted to a HEX string.
Returns
String
- The HEX string representing of the given number
.
Example
web3.fromWei
Converts a number of wei into the following lattice units:
kwei
/ada
mwei
/babbage
gwei
/shannon
szabo
finney
ether
kether
/grand
/einstein
mether
gether
tether
Parameters
Number|String|BigNumber
- A number or BigNumber instance.String
- One of the above ether units.
Returns
String|BigNumber
- Either a number string, or a BigNumber instance, depending on the given number
parameter.
Example
web3.toWei
Converts an lattice unit into wei. Possible units are:
kwei
/ada
mwei
/babbage
gwei
/shannon
szabo
finney
ether
kether
/grand
/einstein
mether
gether
tether
Parameters
Number|String|BigNumber
- A number or BigNumber instance.String
- One of the above ether units.
Returns
String|BigNumber
- Either a number string, or a BigNumber instance, depending on the given number
parameter.
Example
web3.toBigNumber
Converts a given number into a BigNumber instance.
See the note on BigNumber.
Parameters
Number|String
- A number, number string or HEX string of a number.
Returns
BigNumber
- A BigNumber instance representing the given value.
Example
web3.net.listening
This property is read only and says whether the node is actively listening for network connections or not.
Returns
Boolean
- true
if the client is actively listening for network connections, otherwise false
.
Example
web3.net.peerCount
This property is read only and returns the number of connected peers.
Returns
Number
- The number of peers currently connected to the client.
Example
web3.eth
Contains the lattice blockchain related methods.
Example
web3.eth.defaultBlock
This default block is used for the following methods (optionally you can overwrite the defaultBlock by passing it as the last parameter):
Values
Default block parameters can be one of the following:
Number
- a block numberString
-"earliest"
, the genisis blockString
-"latest"
, the latest block (current head of the blockchain)String
-"pending"
, the currently mined block (including pending transactions)
Default is latest
Returns
Number|String
- The default block number to use when querying a state.
Example
web3.eth.coinbase
This property is read only and returns the coinbase address were the mining rewards go to.
Returns
String
- The coinbase address of the client.
Example
web3.eth.mining
This property is read only and says whether the node is mining or not.
Returns
Boolean
- true
if the client is mining, otherwise false
.
Example
web3.eth.gasPrice
This property is read only and returns the current gas price. The gas price is determined by the x latest blocks median gas price.
Returns
BigNumber
- A BigNumber instance of the current gas price in wei.
See the note on BigNumber.
Example
web3.eth.accounts
This property is read only and returns a list of accounts the node controls.
Returns
Array
- An array of addresses controlled by client.
Example
web3.eth.blockNumber
This property is read only and returns the current block number.
Returns
Number
- The number of the most recent block.
Example
web3.eth.register
(Not Implemented yet) Registers the given address to be included in web3.eth.accounts
. This allows non-private-key owned accounts to be associated as an owned account (e.g., contract wallets).
Parameters
String
- The address to registerFunction
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
?
Example
web3.eth.unRegister
(Not Implemented yet) Unregisters a given address.
Parameters
String
- The address to unregister.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
?
Example
web3.eth.getBalance
Get the balance of an address at a given block.
Parameters
String
- The address to get the balance of.Number|String
- (optional) If you pass this parameter it will not use the default block set with web3.eth.defaultBlock.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
String
- A BigNumber instance of the current balance for the given address in wei.
See the note on BigNumber.
Example
web3.eth.getStorageAt
Get the storage at a specific position of an address.
Parameters
String
- The address to get the storage from.Number
- The index position of the storage.Number|String
- (optional) If you pass this parameter it will not use the default block set with web3.eth.defaultBlock.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
String
- The value in storage at the given position.
Example
web3.eth.getCode
Get the code at a specific address.
Parameters
String
- The address to get the code from.Number|String
- (optional) If you pass this parameter it will not use the default block set with web3.eth.defaultBlock.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
String
- The data at given address addressHexString
.
Example
web3.eth.getBlock
Returns a block matching the block number or block hash.
Parameters
String|Number
- The block number or hash. Or the string"earliest"
,"latest"
or"pending"
as in the default block parameter.Boolean
- (optional, defaultfalse
) Iftrue
, the returned block will contain all transactions as objects, iffalse
it will only contains the transaction hashes.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
Object
- The block object:
number
:Number
- the block number.hash
:String
, 32 Bytes - hash of the block.parentHash
:String
, 32 Bytes - hash of the parent block.nonce
:String
, 8 Bytes - hash of the generated proof-of-work.sha3Uncles
:String
, 32 Bytes - SHA3 of the uncles data in the block.logsBloom
:String
, 256 Bytes - the bloom filter for the logs of the block.transactionsRoot
:String
, 32 Bytes - the root of the transaction trie of the blockstateRoot
:String
, 32 Bytes - the root of the final state trie of the block.miner
:String
, 20 Bytes - the address of the beneficiary to whom the mining rewards were given.difficulty
:BigNumber
- integer of the difficulty for this block.totalDifficulty
:BigNumber
- integer of the total difficulty of the chain until this block.extraData
:String
- the "extra data" field of this block.size
:Number
- integer the size of this block in bytes.gasLimit
:Number
- the maximum gas allowed in this block.gasUsed
:Number
- the total used gas by all transactions in this block.timestamp
:Number
- the unix timestamp for when the block was collated.transactions
:Array
- Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter.uncles
:Array
- Array of uncle hashes.
Example
web3.eth.getBlockTransactionCount
Returns the number of transaction in a given block.
Parameters
String|Number
- The block number or hash. Or the string"earliest"
,"latest"
or"pending"
as in the default block parameter.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
Number
- The number of transactions in the given block.
Example
web3.eth.getUncle
Returns a blocks uncle by a given uncle index position.
Parameters
String|Number
- The block number or hash. Or the string"earliest"
,"latest"
or"pending"
as in the default block parameter.Number
- The index position of the uncle.Boolean
- (optional, defaultfalse
) Iftrue
, the returned block will contain all transactions as objects, iffalse
it will only contains the transaction hashes.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
Object
- the returned uncle. For a return value see web3.eth.getBlock().
Note: An uncle doesn't contain individual transactions.
Example
web3.eth.getTransaction
Returns a transaction matching the given transaction hash.
Parameters
String
- The transaction hash.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
Object
- A transaction object its hash transactionHash
:
hash
:String
, 32 Bytes - hash of the transaction.nonce
:Number
- the number of transactions made by the sender prior to this one.blockHash
:String
, 32 Bytes - hash of the block where this transaction was in.null
when the transaction is pending.blockNumber
:Number
- block number where this transaction was in.null
when the transaction is pending.transactionIndex
:Number
- integer of the transactions index position in the block.from
:String
, 20 Bytes - address of the sender.to
:String
, 20 Bytes - address of the receiver.null
when its a contract creation transaction.value
:BigNumber
- value transferred in Wei.gasPrice
:BigNumber
- price paid per gas in Wei.gas
:Number
- gas used.input
:String
- the data send along with the transaction.
Example
web3.eth.getTransactionFromBlock
Returns a transaction based on a block hash or number and the transactions index position.
Parameters
String
- A block number or hash. Or the string"earliest"
,"latest"
or"pending"
as in the default block parameter.Number
- The transactions index position.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
Object
- A transaction object, see web3.eth.getTransaction:
Example
web3.eth.getTransactionCount
Get the numbers of transactions send from this address.
Parameters
String
- The address to get the numbers of transactions from.Number|String
- (optional) If you pass this parameter it will not use the default block set with web3.eth.defaultBlock.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
Number
- The number of transactions send from the given address.
Example
web3.eth.sendTransaction
Sends a transaction to the network.
Parameters
Object
- The transaction object to send:from
:String
- The address for the sending account.to
:String
- (optional) The destination address of the message, left undefined for a contract-creation transaction.value
:Number|String|BigNumber
- (optional) The value transferred for the transaction in Wei, also the endowment if it's a contract-creation transaction.gas
:Number|String|BigNumber
- (optional, default: To-Be-Determined) The amount of gas to use for the transaction (unused gas is refunded).gasPrice
:Number|String|BigNumber
- (optional, default: To-Be-Determined) The price of gas for this transaction in wei, defaults to the mean network gas price.data
:String
- (optional) Either a byte string containing the associated data of the message, or in the case of a contract-creation transaction, the initialisation code.
Number|String
- (optional) If you pass this parameter it will not use the default block set with web3.eth.defaultBlock.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
String
- The address of the newly created contract, or the 32 Bytes transaction hash as HEX string.
Example
web3.eth.call
Executes a message call transaction, which is directly executed in the VM of the node, but never mined into the blockchain.
Parameters
Object
- A transaction object see web3.eth.sendTransaction, with the difference that for calls thefrom
property is optional as well.Number|String
- (optional) If you pass this parameter it will not use the default block set with web3.eth.defaultBlock.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
String
- The returned data of the call, e.g. a codes functions return value.
Example
web3.eth.filter
Parameters
String|Object
- The string"latest"
or"pending"
to watch for changes in the latest block or pending transactions respectively. Or a filter options object as follows:fromBlock
:Number|String
- The number of the earliest block (latest
may be given to mean the most recent andpending
currently mining, block).toBlock
:Number|String
- The number of the latest block (latest
may be given to mean the most recent andpending
currently mining, block).address
:String
- An address or a list of addresses to only get logs from particular account(s).topics
:Array of Strings
- An array of values which must each appear in the log entries. The order is important, if you want to leave topics usenull
, e.g.[null, '0x00...']
.
Returns
Object
- A filter object with the following methods:
filter.get()
: Returns all of the log entries that fit the filter.filter.watch(callback)
: Watches for state changes that fit the filter and calls the callback. See this note for details.filter.stopWatching()
: Stops the watch and uninstalls the filter in the node. Should always be called once it is done.
Callback return
Object
- A log object as follows:
logIndex
:Number
- integer of the log index position in the block.transactionIndex
:Number
- integer of the transactions index position log was created from.transactionHash
:String
, 32 Bytes - hash of the transactions this log was created from.blockHash
:String
, 32 Bytes - hash of the block where this log was in.null
when the log is pending.blockNumber
:Number
- the block number where this log was in.null
when the log is pending.address
:String
, 32 Bytes - address from which this log originated.data
:String
- contains one or more 32 Bytes non-indexed arguments of the log.topics
:Array of Strings
- Array of 0 to 4 32 BytesDATA
of indexed log arguments. (In solidity: The first topic is the hash of the signature of the event (e.g.Deposit(address,bytes32,uint256)
), except you declared the event with theanonymous
specifier.)
Note For event filter return values see Contract Events
Example
web3.eth.contract
Creates a contract object for a solidity contract, which can be used to initiate contracts on an address. You can read more about events here.
Parameters
Array
- ABI array with descriptions of functions and events of the contract.
Returns
Object
- A contract object, which can be initiated as follows:
Example
Contract Methods
The contract object exposes the contracts methods, which can be called using parameters and a transaction object.
Parameters
String|Number
- (optional) Zero or more parameters of the function.Object
- (optional) The last parameter can be a transaction object, see web3.eth.sendTransaction parameter 1 for more.
Returns
String
- If its a call the result data, if its a send transaction a created contract address, or the transaction hash, see web3.eth.sendTransaction for details.
Example
Contract Events
You can use events like filters and they have the same methods, but you pass different objects to create the event filter.
Parameters
Object
- Indexed return values you want to filter the logs by, e.g.{'valueA': 1, 'valueB': [myFirstAddress, mySecondAddress]}
.Object
- Additional filter options, see filters parameter 1 for more.
Callback return
Object
- An event object as follows:
args
:Object
- The arguments coming from the event.event
:String
- The event name.logIndex
:Number
- integer of the log index position in the block.transactionIndex
:Number
- integer of the transactions index position log was created from.transactionHash
:String
, 32 Bytes - hash of the transactions this log was created from.address
:String
, 32 Bytes - address from which this log originated.blockHash
:String
, 32 Bytes - hash of the block where this log was in.null
when the log is pending.blockNumber
:Number
- the block number where this log was in.null
when the log is pending.
Example
web3.eth.getCompilers
Gets a list of available compilers.
Parameters
Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
Array
- An array of strings of available compilers.
Example
web3.eth.compile.solidity
Compiles solidity source code.
Parameters
String
- The solidity source code.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
String
- The compiled solidity code as HEX string.
Example
web3.eth.compile.lll
Compiles LLL source code.
Parameters
String
- The LLL source code.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
String
- The compiled LLL code as HEX string.
Example
web3.eth.compile.serpent
Compiles serpent source code.
Parameters
String
- The serpent source code.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
String
- The compiled serpent code as HEX string.
web3.db.putString
This method should be called, when we want to store a string in the local leveldb database.
Parameters
String
- The database to store to.String
- The name of the store.String
- The string value to store.
Returns
Boolean
- true
if successfull, otherwise false
.
Example
param is db name, second is the key, and third is the string value.
web3.db.getString
This method should be called, when we want to get string from the local leveldb database.
Parameters
String
- The database to store to.String
- The name of the store.
Returns
String
- The stored value.
Example
param is db name and second is the key of string value.
web3.db.putHex
This method should be called, when we want to store binary data in HEX form in the local leveldb database.
Parameters
String
- The database to store to.String
- The name of the store.String
- The HEX string to store.
Returns
Boolean
- true
if successfull, otherwise false
.
Example
web3.db.getHex
This method should be called, when we want to get a binary data in HEX form from the local leveldb database.
Parameters
String
- The database to store to.String
- The name of the store.
Returns
String
- The stored HEX value.
Example
param is db name and second is the key of value.
web3.shh
Example
web3.shh.post
web3.shh.post(object [, callback])
This method should be called, when we want to post whisper message to teh network.
Parameters
Object
- The post object:from
:String
, 60 Bytes HEX - (optional) The identity of the sender.to
:String
, 60 Bytes HEX - (optional) The identity of the receiver. When present whisper will encrypt the message so that only the receiver can decrypt it.topics
:Array of Strings
- Array of topicsStrings
, for the receiver to identify messages.payload
:String|Number|Object
- The payload of the message. Will be autoconverted to a HEX string before.priority
:Number
- The integer of the priority in a rang from ... (?).ttl
:Number
- integer of the time to live in seconds.
Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
Boolean
- returns true
if the message was send, otherwise false
.
Example
web3.shh.newIdentity
Should be called to create new identity.
Parameters
Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
String
- A new identity HEX string.
Example
web3.shh.hasIdentity
Should be called, if we want to check if user has given identity.
Parameters
String
- The identity to check.Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Returns
Boolean
- returns true
if the identity exists, otherwise false
.
Example
web3.shh.newGroup
Example
web3.shh.addToGroup
Example
web3.shh.filter
Watch for incoming whisper messages.
Parameters
Object
- The filter options:topics
:Array of Strings
- Filters messages by this topic(s). You can use the following combinations:['topic1', 'topic2'] == 'topic1' && 'topic2'
['topic1', ['topic2', 'topic3']] == 'topic1' && ('topic2' || 'topic3')
[null, 'topic1', 'topic2'] == ANYTHING && 'topic1' && 'topic2'
->null
works as a wildcard
to
: Filter by identity of receiver of the message. If provided and the node has this identity, it will decrypt incoming encrypted messages.
Function
- (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.
Callback return
Object
- The incoming message:
from
:String
, 60 Bytes - The sender of the message, if a sender was specified.to
:String
, 60 Bytes - The receiver of the message, if a receiver was specified.expiry
:Number
- Integer of the time in seconds when this message should expire (?).ttl
:Number
- Integer of the time the message should float in the system in seconds (?).sent
:Number
- Integer of the unix timestamp when the message was sent.topics
:Array of String
- Array ofString
topics the message contained.payload
:String
- The payload of the message.workProved
:Number
- Integer of the work this message required before it was send (?).
Last updated