Layer-2 SDK Samples

Code examples that demonstrate usage of Umbrella SDK for getting L2D Data.

Before using the Layer-2 sample codes, there are two important things one should do. The first one should be to read our Intro to Layer-2 Data guide which gives a visual introduction to Layer-2 SDK data in Umbrella Network. That is essential to build intuition around what's happening while you use the Umbrella Network. The second one will be to set up the proper environment.

To make this sample work, you must first install the Umbrella SDK via NPM, sign up for Infura or another node provider to get a provider URL for ethereum, and generate an API-key.

It must be noted that the current example is using the Infura testnet, soon this guide will be updated to reflect on the Ethereum mainnet once the Umbrella contract launches on the mainnet.

The provider key is masked in the sample, Ethereum developers are expected to already have this key from their previous endeavors. The new ones can sign for one on Infura website.

The contract registry address is of the Umbrella contract currently deployed on Ethereum's Kovan testnet.

After we define the contractRegistryAddress and the provider, we instantiate the ContractRegistry constructor by providing both of them to the constructor.

Later in the code we invoke the APIClient with the base URL of the Umbrella network API, provide it with the chainContract we create above and provide the API key which we generated earlier.

It must be noted that the keys in the Umbrella SDK as of the time of writing this documentation are of case-sensitive nature. ETH-USD != eth-usd.

Finally, we call our verifyProofForNewestBlock function for the 'ETH-USD' pair which verifies whether the merkle-proof for the latest block has the correct, authentic value for the pair and returns success : true if that is or success: false if the proof fails to match up.

import { ContractRegistry, ChainContract, APIClient } from '@umb-network/toolbox';
import { ethers } from 'ethers';

const { BLOCKCHAIN_PROVIDER_URL, REGISTRY_CONTRACT_ADDRESS, API_BASE_URL, API_KEY } = process.env;

async function main() {
  if (BLOCKCHAIN_PROVIDER_URL && REGISTRY_CONTRACT_ADDRESS && API_KEY) {
    const provider = new ethers.providers.JsonRpcProvider(BLOCKCHAIN_PROVIDER_URL);

    const contractRegistry = new ContractRegistry(provider, REGISTRY_CONTRACT_ADDRESS);
    const chainContractAddress = await contractRegistry.getAddress('Chain');
    const chainContract = new ChainContract(provider, chainContractAddress);
    const apiClient = new APIClient({
      baseURL: API_BASE_URL as string,
      chainContract,
      apiKey: API_KEY,
    });

    const verificationResult = await apiClient.verifyProofForNewestBlock('ETH-USD');

    console.log(verificationResult);
  }
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Result:

{
  success: true,
  value: 1945.42,
  dataTimestamp: 2021-05-23T14:36:58.000Z
}