Using web3 API with MetaMask

This web page shows examples using web3 methods together with MetaMask.

Information about web3 API: https://github.com/ethereum/wiki/wiki/JavaScript-API
Information about MetaMask: https://metamask.io/
Explore Ethereum Test Network Rinkeby blockchain: https://rinkeby.etherscan.io
Explore Ethereum Main Network blockchain: https://etherscan.io/
MetaMask only support the web3 asynchronous methods (there are a few exceptions), see: https://github.com/MetaMask/faq/blob/master/DEVELOPERS.md
Gitter Ethereum/web3.js, see: https://gitter.im/ethereum/web3.js

How to use

Examples


[web3.eth.defaultAccount] This default account is used for the following methods:
- web3.eth.sendTransaction()
- web3.eth.call()
Note: The defaultAccount optionally can be overwritten by specifying the from property.
Default account (rinkeby example):



[web3.sha3] The Keccak-256 SHA3 of the given data
Data to be hashed:

Encoding:




[web3.toHex] Converts any value into HEX
Data to be converted into hex:




[web3.toAscii] Converts a HEX string into a ASCII string
Hex string:




[web3.fromAscii] Converts any ASCII string to a HEX string.
ASCII string:


The number of bytes the returned HEX string should have (No paddding if value = 0). This does not work!! Bug maybe?





[web3.toDecimal] Converts a HEX string to its number representation.
An HEX string to be converted to a number:




[web3.fromDecimal] Converts a number or number string to its HEX representation.
A number to be converted to a HEX string:




[web3.fromWei] Converts a number of wei into selected ethereum unit:
Wei (in dec or hex):

Unit:




[web3.toWei] Converts an ethereum unit into wei:
Number of Ethereum units (in dec or hex):

Unit:




[web3.toBigNumber] Converts a given number into a BigNumber instance.
A number, number string or HEX string of a number:




[web3.isAddress] Checks if the given string is an address.
An address (HEX string):




[web3.eth.getCode] Get the code at the specified contract address
Contract Address (rinkeby example):

To find the contract address deployed from an address, goto https://rinkeby.etherscan.io enter the address and search for the transaction which created the contract.
In the transaction details you will find the contract address.


[web3.eth.getBlock] Get the block information at the specified block hash or block number
Block hash or block number (rinkeby example):


[web3.eth.getBlockTransactionCount] Get number of transaction at the specified block hash or block number
Block hash or block number (rinkeby example):


[web3.eth.getUncle] Get blocks uncle for given block hash or block number and uncle index position
Block hash or block number (mainnet example):

Uncle index position


[web3.eth.getTransaction] Get transactions for given transaction hash
Transaction hash (rinkeby example):


[web3.eth.getTransactionFromBlock] Get transactions for given block hash or block number and transaction index position
Block hash or block number (rinkeby example):

Transaction index position


[web3.eth.getTransactionReceipt] Get transaction receipt for given transaction hash

Use this method to get the contract address.
When a contract is created using method web3.eth.sendTransaction a transaction hash is returned.
This transaction hash can be used as input for web3.eth.getTransactionReceipt.

Note:
If you got null instead of a receipt the transaction has not been included in a block yet.
Wait for a moment and check if a miner has included the transaction in a block and retry it.

Transaction hash (rinkeby example):


[web3.eth.getTransactionCount] Get the number of transaction send for specified address
Address (rinkeby example):


[web3.eth.sendTransaction] Send a transaction (ether transfer)
From account (rinkeby example):

To account:

Amount (Ether):




[web3.eth.sendTransaction] Send a transaction (contract creation)
From account (rinkeby example):

Bytecode:





Note:
The Bytecode value above was created as follow:
[web3.eth.sign] Sign data
From a specific account (rinkeby example):

Data to sign: