Getting started

Getting Started

Installing

npm install --save @thetalabs/theta-js

Importing

node.js require

const thetajs = require("@thetalabs/theta-js");

ES6

import * as thetajs from "@thetalabs/theta-js";

Common Terminology

ClassDescription
ProviderA Provider (in theta-js) is a class which provides an abstraction for a connection to the Theta Network. It provides read-only access to the Blockchain and its status.
SignerA Signer is a class which (usually) in some way directly or indirectly has access to a private key, which can sign transactions to authorize the network to charge your account TFuel to perform operations.
ContractA Contract is an abstraction which represents a connection to a specific smart contract on the Theta Network, so that applications can use it like a normal JavaScript object.

Connecting to Theta: RPC

// If you don't specify a chainId, theta-js will connect to mainnet by default.
const provider = new thetajs.providers.HttpProvider();

Creating Wallet (signer)

const privateKey = "0x0000000000000000000000000000000000000000000000000000000000000000";
const wallet = new Wallet(privateKey);

Querying the Blockchain

Once you have a Provider, you have a read-only connection to the blockchain, which you can use to query the current state, fetch historic logs, interact with contracts and so on.

// Look up the current block number
provider.getBlockNumber()
// { Promise: 11098234 }


// Get the balance of an account
account = await provider.getAccount("0x0000000000000000000000000000000000000000")

Writing to the Blockchain

const ten18 = (new BigNumber(10)).pow(18); // 10^18, 1 Theta = 10^18 ThetaWei, 1 Gamma = 10^ TFuelWei
const thetaWeiToSend = (new BigNumber(0));
const tfuelWeiToSend = (new BigNumber(0.0001)).multipliedBy(ten18);
const from =  "0x0000000000000000000000000000000000000000";
const to = "0x0000000000000000000000000000000000000000";
const txData = {
    from: from,
    outputs: [
       {
           address: to,
           thetaWei: thetaWeiToSend,
           tfuelWei: tfuelWeiToSend,
       }
   ]
}

    const transaction = new SendTransaction(txData);
    const result = await wallet.sendTransaction(transaction);

Contracts

A Contract is an abstraction of program code which lives on the Theta blockchain.

The Contract object makes it easier to use an on-chain Contract as a normal JavaScript object, with the methods mapped to encoding and decoding data for you.

If you are familiar with Databases, this is similar to an Object Relational Mapper (ORM).

In order to communicate with the Contract on-chain, this class needs to know what methods are available and how to encode and decode the data, which is what the Application Binary Interface (API) provides.

This class is a meta-class, which means its methods are constructed at runtime, and when you pass in the ABI to the constructor it uses it to determine which methods to add.

While an on-chain Contract may have many methods available, you can safely ignore any methods you don't need or use, providing a smaller subset of the ABI to the contract.

An ABI often comes from the Solidity compiler.

Connecting to the Contract

// The TNT-20 Contract ABI, which is a common contract interface
// for tokens (this is the Human-Readable ABI format)
const TNT20ContractAddress = "0x0000000000000000000000000000000000000000";
const TNT20ContractABI = "[{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_from\",\"type\":\"address\"},{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"version\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"balance\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"},{\"name\":\"_extraData\",\"type\":\"bytes\"}],\"name\":\"approveAndCall\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"},{\"name\":\"_spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"name\":\"remaining\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"fallback\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"_from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"_to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"_owner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"_spender\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"}]"


// The Contract object
const tnt20Contract = new thetajs.Contract(TNT20ContractAddress, TNT20ContractABI, provider);


Read-Only Methods

// Get the TNT-20 token name
tnt20Contract.name()
// { Promise: 'HelloWorldCoin' }

// Get the TNT-20 token symbol (for tickers and UIs)
tnt20Contract.symbol()
// { Promise: 'HWC' }

// Get the balance of an address
balance = await tnt20Contract.balanceOf("0x0000000000000000000000000000000000000000")
// { BigNumber: "15923148775162018481031" }


State Changing Methods

// The contract is currently connected to the Provider,
// which is read-only. You need to connect to a Signer, so
// that you can pay to send state-changing transactions.
const tnt20ContractWithSigner = contract.connect(wallet);

// Send 1 TNT Token to "0x0000000000000000000000000000000000000000"
const tx = tnt20ContractWithSigner.transfer("0x0000000000000000000000000000000000000000", 1);


What’s Next
Did this page help you?