Price Feed API
This API is exported through the sbcore.feeds
object. It provides functions that can be used to get the current price of a token in USD.
interface PriceFeed {
getTokenPrice(
tokenAddress: string,
options?: {
chainId?: string;
provider?: string;
timestamp?: Date;
},
): Promise<number>;
}
By calling sbcore.feeds.getTokenPrice(tokenAddress)
, you can get the current price of a token in USD. The chain is derived from your currently active blockchain but a different chain ID can be specified as the second argument. The following example shows how to get the price of the Frax token on Arbitrum even if you are currently on Ethereum:
const price = sbcore.feeds.getTokenPrice("0x17fc002b466eec40dae837fc4be5c67993ddbd6f", { chainId: "42161" });
If no price provider is specified, we will decide based on internal logic. You can also specify a price provider as the third argument. The following example shows how to get the price of the Frax token on Arbitrum using the CoinGecko price feed:
const price = sbcore.feeds.getTokenPrice("0x17fc002b466eec40dae837fc4be5c67993ddbd6f", {
chainId: "42161",
provider: "coingecko",
});
Updated 2 months ago