The LayerTwoOracle API is used to get the current exchange rates in USD and ETH for the various stablecoin that we support. These rates are fed by the Chainlink price feeds for the stablecoin rates and the DIA oracle for the CARD token rates. As we onboard new stablecoin we'll add more exchange rates. The price oracles that we use reside in layer 2, so please supply a layer 2 web3 instance obtaining an LayerTwoOracle API from getSDK().

Example

import { getSDK } from "@cardstack/cardpay-sdk";
let web3 = new Web3(myProvider); // Layer 2 web3 instance
let layerTwoOracle = await getSDK('LayerTwoOracle', web3);

Hierarchy

  • LayerTwoOracle

Constructors

Properties

layer2Web3: default

Methods

  • This call will convert a SPEND amount into the specified token amount, where the result is a string that represents the token in units of wei. Since SPEND tokens represent $0.01 USD, it is safe to represent SPEND as a number when providing the input value.

    Example

    let weiAmount = await layerTwoOracle.convertFromSpend(daicpxdAddress, 10000); // convert 10000 SPEND into DAI
    console.log(`DAI value ${fromWei(weiAmount)}`);

    Parameters

    • token: string
    • amount: string

    Returns Promise<number>

  • This returns a function that converts an amount of a token in wei to USD. Similar to LayerTwoOracle.getUSDPrice, an exception will be thrown if we don't have the exchange rate for the token. The returned function accepts a string that represents an amount in wei and returns a number that represents the USD value of that amount of the token. Currently, we assume 18 decimals so only tokens conforming to this can be supported.

    Example

    let layerTwoOracle = await getSDK('LayerTwoOracle', web3);
    let converter = await layerTwoOracle.getUSDConverter("DAI");
    console.log(`USD value: $${converter(amountInWei)} USD`);

    Parameters

    • token: string

    Returns Promise<((amountInWei: string) => number)>

  • This call will return the USD value for the specified amount of the specified token. If we do not have an exchange rate for the token, then an exception will be thrown. This API requires that the token amount be specified in wei (1018 wei = 1 token) as a string, and will return a floating point value in units of USD. You can easily convert a token value to wei by using the Web3.utils.toWei() function. Currently, we assume 18 decimals so only tokens conforming to this can be supported.

    Example

    let layerTwoOracle = await getSDK('LayerTwoOracle', web3);
    let usdPrice = await layerTwoOracleRate.getUSDPrice("DAI", amountInWei);
    console.log(`USD value: $${usdPrice.toFixed(2)} USD`);

    Parameters

    • token: string
    • amount: string

    Returns Promise<number>

  • This call will return the ETH value for the specified amount of the specified token. If we do not have an exchange rate for the token, then an exception will be thrown. This API requires that the token amount be specified in wei (1018 wei = 1 token) as a string, and will return a string that represents the ETH value in units of wei as well. You can easily convert a token value to wei by using the Web3.utils.toWei() function. You can also easily convert units of wei back into ethers by using the Web3.utils.fromWei() function. Currently, we assume 18 decimals so only tokens conforming to this can be supported.

    Example

    let layerTwoOracle = await getSDK('LayerTwoOracle', web3);
    let ethWeiPrice = await layerTwoOracle.getETHPrice("CARD", amountInWei);
    console.log(`ETH value: ${fromWei(ethWeiPrice)} ETH`);

    Parameters

    • token: string
    • amount: string

    Returns Promise<string>

  • This call will return a Date instance that indicates the date the token rate was last updated.

    Example

    let layerTwoOracle = await getSDK('LayerTwoOracle', web3);
    let date = await layerTwoOracle.getUpdatedAt("DAI");
    console.log(`The ${token} rate was last updated at ${date.toString()}`);

    Parameters

    • token: string

    Returns Promise<Date>

Generated using TypeDoc