Developer Guide

Developers can easily integrate with the Theta Network by connecting their Dapps to their user's Theta Wallets.

Here are a few examples:

  • User sign up/login (i.e. Facebook Login for Theta Network)
  • Send Theta/TFuel/TNT-20 Token transactions
  • Request Theta/TFuel (i.e. Paypal checkout for Theta Network)
  • Deploy smart contracts
  • Interact with smart contracts (i.e. Uniswap on Theta Network)
  • One-click staking to Guardian Pool
  • and more!

Getting started

For increased security and a fast browsing experience, the Theta Wallet browser extension does not inject scripts into every webpage like Metamask. Dapps will include two of our libraries in order to connect and interact with their user's accounts.

  • ThetaWalletConnect - slim library to interact with the user's accounts securely stored in the extension.
  • Theta Blockchain SDK - library to interact with the blockchain, build transactions, etc.

Installing ThetaWalletConnect

Dapps must use the ThetaWalletConnect library to interact with the user's accounts.

npm install --save @thetalabs/theta-wallet-connect

Importing

node.js require

const ThetaWalletConnect = require("@thetalabs/theta-wallet-connect");

ES6

import ThetaWalletConnect from "@thetalabs/theta-wallet-connect";

Request Accounts

You must request access to the user's accounts. This will only give you their addresses, no private keys, etc. This function will throw an error if the user hasn't initialized their account or they reject your request.

const accounts = await ThetaWalletConnect.requestAccounts();

Request Payment

You may request a payment from the user.

const ten18 = (new BigNumber(10)).pow(18); 
const thetaWeiToSend = (new BigNumber(0));
const tfuelWeiToSend = (new BigNumber(10000)).multipliedBy(ten18);
const receiverAddr = "0x000...0000"; // Your address
const txData = {
      outputs: [
        {
          address: receiverAddr,
          thetaWei: thetaWeiToSend,
          tfuelWei: tfuelWeiToSend,
        }
      ]
    };

let tx = new thetajs.transactions.SendTransaction(txData);
const txResult = await ThetaWalletConnect.sendTransaction(tx);
const hash = txResult.hash;

Deploy Contract

You may have a contract builder which can build and deploy contracts for the user.

const contractToDeploy = new thetajs.ContractFactory(ABIToDeploy, byteCodeToDeploy, null);
// string memory name, string memory symbol, uint8 decimals, uint256 amount
const deployTransaction = await contractToDeploy.populateDeployTransaction('Playground Tokens', 'PLAY', 18, '1000000000', false);
const txResult = await ThetaWalletConnect.sendTransaction(deployTransaction);
const hash = txResult.hash;

Interact with a Contract

You may have a complex contract (i.e. Dex) and need to transact on behalf of the user.

const contract = new thetajs.Contract(My_Contract_Address, My_Contract_ABI, null);
// Transaction: Transfer TNT-20 tokens (write)
const transaction = await contract.populateTransaction.transfer("0x000...000", "1000");
const txResult = await ThetaWalletConnect.sendTransaction(transaction);
const hash = txResult.hash;