OMG Network Docs
DocsAPIsEnvironmentsProduct
  • OMG Network Documentation
  • Table of Contents
  • Network
    • Getting Started
    • Retrieve Balances
    • Deposit Funds
    • Make a Transfer
    • Make an Atomic Swap
    • Make a Fee Relay Transfer
    • Start a Standard Exit
    • Start an In-flight Exit
    • Process an Exit
    • Manage UTXO
    • Challenge an Exit
  • Use Cases
    • Community Points Engine
    • Exchange Use Case
    • Wallet Use Case
  • Wallet
    • Web Wallet Quick Start
  • Contracts
    • MoreVP Technical Overview
    • Plasma Proposition
    • Smart Contract Administrative Accounts
  • Watcher
    • How to Run a Watcher
    • How to Manage a VPS
    • How to Manage a Watcher
  • Security
    • Bug Bounty Program
    • Plasma Contracts Audits
  • 3rd-Party
    • MetaMask
    • MultiBaas by Curvegrid
  • Concepts
    • Blockchain Design
    • Byzantine Conditions
    • Network Architecture
    • Challenge Period
    • Exit Bonds
    • Fees
  • Resources
    • Glossary
    • FAQ
    • Environments
    • API References
    • Release Notes
  • Migration Guides
    • Introducing v0.3 (ODP Edition) · OMG Network
    • v0.4 · OMG Network
Powered by GitBook
On this page
  • Implementation
  • 2. Import dependencies
  • 3. Retrieve balances
  • 3.1 Retrieve child chain (OMG Network) balances
  • 3.2 Retrieve root chain (Ethereum) balances

Was this helpful?

  1. Network

Retrieve Balances

PreviousGetting StartedNextDeposit Funds

Last updated 4 years ago

Was this helpful?

Retrieving balances involves converting an array of balances into a human-readable array of balances.

Implementation

1. Install ,

To access network features from your application, use our official libraries:

Requires Node >= 8.11.3 < 13.0.0

npm install @omisego/omg-js web3

You can add omg-js to a website using a script tag:

<script src="https://unpkg.com/@omisego/browser-omg-js"></script>

You can easily integrate omg-js with React Native projects. First, add this postinstall script to your project's package.json:

"scripts": {
    "postinstall": "omgjs-nodeify"
}

Then install the react native compatible library:

npm install @omisego/react-native-omg-js

2. Import dependencies

omg-js library has 3 main objects that are used during all of the code samples. Here's an example of how to instantiate them:

import Web3 from "web3";
import { ChildChain, RootChain, OmgUtil } from "@omisego/omg-js";
const web3 = new Web3(new Web3.providers.HttpProvider(web3_provider_url));
const rootChain = new RootChain({ web3, plasmaContractAddress });
const childChain = new ChildChain({ watcherUrl });
const erc20ContractAddress = "0xd92e713d051c37ebb2561803a3b5fbabc4962431";
const aliceAddress = "0x8cb0de6206f459812525f2ba043b14155c2230c0";
  • web3_provider_url - the URL to a full Ethereum RPC node (local or from infrastructure provider, e.g. ).

  • plasmaContractAddress - CONTRACT_ADDRESS_PLASMA_FRAMEWORK for defined .

  • watcherUrl - the Watcher Info URL for defined (personal or from OMG Network).

3. Retrieve balances

The amount in balance array is defined in WEI (e.g. 429903000000000000), the smallest denomination of ether, ETH. The currency contains 0x0000000000000000000000000000000000000000 for ETH currency or a smart contract address (e.g. 0xd92e713d051c37ebb2561803a3b5fbabc4962431) for ERC20 tokens.

3.1 Retrieve child chain (OMG Network) balances

async function retrieveChildChainBalance() {
  
  const childchainBalanceArray = await childChain.getBalance(aliceAddress);
  
  const childchainBalance = childchainBalanceArray.map((i) => {
    return {
      currency:
        i.currency === OmgUtil.transaction.ETH_CURRENCY ? "ETH" : i.currency,
      amount:
        i.currency === OmgUtil.transaction.ETH_CURRENCY ?
          web3.utils.fromWei(String(i.amount), "ether") :
          web3.utils.toBN(i.amount).toString()
    };
  });
[
  {
    "currency": "ETH",
    "amount": "0.299969999999999963"
  },
  {
    "currency": "0x2d453e2a14a00f4a26714a82abfc235c2b2094d5",
    "amount": "100"
  },
  {
    "currency": "0x5592ec0cfb4dbc12d3ab100b257153436a1f0fea",
    "amount": "100000000000000000000"
  },
  {
    "currency": "0x942f123b3587ede66193aa52cf2bf9264c564f87",
    "amount": "100000000000000000000"
  },
  {
    "currency": "0xd92e713d051c37ebb2561803a3b5fbabc4962431",
    "amount": "687000000"
  }
]
  web3.utils.fromWei(String(i.amount), "mwei")

3.2 Retrieve root chain (Ethereum) balances

async function retrieveRootChainErc20Balance() {
  
  const rootchainBalance = await web3.eth.getBalance(aliceAddress);
  const rootchainBalances = [
    {
      currency: "ETH",
      amount: web3.utils.fromWei(String(rootchainBalance), "ether"),
    },
  ];  
  const rootchainERC20Balance = await OmgUtil.getErc20Balance({
    web3,
    address: aliceAddress,
    erc20Address: erc20ContractAddress,
  });
  rootchainBalances.push({
    currency: erc20ContractAddress,
    amount: web3.utils.toBN(rootchainERC20Balance).toString(),
  });
}

Note, you can return the ERC20 balance only for one token at a time.

There's no direct way to retrieve balances on both Ethereum and OMG Network. Instead, you first retrieve an array of balances and then convert it to a preferred format.

Note, the amount for ERC20 tokens will be displayed in the lowest denomination of that particular token. You can convert it into a number via . For example, the provided address has multiple tokens with different decimals (18, 0, 18, 18, 6):

For example, if you want to convert the amount for TUSDT token (), you should either create a custom converter or use the web3.utils as follows:

You can find the number of decimals for a given token on one of the blockchain explorers, such as .

RLP encoded
omg-js
web3
Infura
environment
environment
RLP encoded
BigNum
web3.utils.fromWei
0xd92e713d051c37ebb2561803a3b5fbabc4962431
Etherscan