To demonstrate how a DApp can interact with the Theta blockchain via Web3.js, we put up a few quick examples in this repository. For readers that are not familiar with Web3.js, here and here are good starting points.
First, setup the Theta local privatenet and the ETH RPC adaptor following this guide. The ETH RPC adaptor running at
http://localhost:18888/rpc interacts with the Web3.js library by translating the Theta RPC interface into the ETH RPC interface.
Execute the two commands below to fund the test accounts with some TFuel:
export SEQ=`thetacli query account --address=0x2E833968E5bB786Ae419c4d13189fB081Cc43bab | grep sequence | grep -o '[[:digit:]]\+'` thetacli tx send --chain="privatenet" --from=0x2E833968E5bB786Ae419c4d13189fB081Cc43bab --to=0x19E7E376E7C213B7E7e7e46cc70A5dD086DAff2A --tfuel=1000 --password=qwertyuiop --seq=$(($SEQ+1))
Execute the command below to clone the
theta-web3-examples repository and install dependencies (please ignore errors from
npm install if any):
git clone https://github.com/thetatoken/theta-web3-examples cd theta-web3-examples npm install
Run the following scripts to interact with the Theta local privatenet through Web3.js. Note that the chainID of the Theta local privatenet is 366. In the examples, when we compose the transactions, we need to specify the chainID, otherwise the transactions will be rejected by the blockchain.
# This example shows how to query the TFuel balance of a given account node examples/account_query.js # This example shows how to inspect the latest blocks node examples/block_inspection.js # This example shows how to deploy a smart contract to the local privatenet node examples/contract_deploy.js # This example shows how to send TFuel between two accounts node examples/send_signed_transaction.js # This example shows how some extra features of the Web3.js library node examples/web3_extras.js
You can also interact with the Theta Mainnet through Web3.js. The chainID of the Theta Mainnet is 361. The Theta Lab hosts an ETH RPC endpoint for the Mainnet at
https://eth-rpc-api.thetatoken.org/rpc. The code below shows the setup for accessing the Theta Mainnet via Web3.js.
const Web3 = require('web3') const web3 = new Web3('https://eth-rpc-api.thetatoken.org/rpc') const chainID = 361 // for the Theta Mainnet
Below are some examples:
# This example shows how to inspect the latest blocks of the Theta Mainnet node examples/block_inspection_mainnet.js # This example shows how to read a smart contract deployed on the Theta Mainnet node examples/contract_read_mainnet.js # This example shows how to list and filter events emitted by smart contracts on the Theta Mainnet node examples/events_filtering_mainnet.js
If you would like to test your contracts on the Theta Testnet, the chainID would be 365, and the hosted ETH RPC endpoint is
Updated 5 months ago