Skip to content

Commit

Permalink
feat: new sdk method to get usd prices (#290)
Browse files Browse the repository at this point in the history
* feat: new sdk method to get usd prices

* chore: handle possible undefined price

* chore: refactor to only 2 usd price methods

* Fix code style issues with Prettier

---------

Co-authored-by: Lint Action <[email protected]>
Co-authored-by: Juan Manuel Villarraza <[email protected]>
  • Loading branch information
3 people authored Mar 1, 2024
1 parent 230b73a commit 7ca1047
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,33 @@ export class Squid extends TokensChains {
chainId,
}: {
tokenAddress: string;
chainId: string | number;
}) {
const response = await this.httpInstance.axios.get("/v2/token-price", {
params: { tokenAddress, chainId },
chainId: string;
}): Promise<number> {
const response = await this.httpInstance.axios.get("/v2/tokens", {
params: { address: tokenAddress, chainId, usdPrice: true },
});

return response.data.token.usdPrice;
}

/**
* Return tokens with USD price
* If chainId is provided, it will return only tokens for that chain
* if not, it will return all tokens
* @param {chainId?: string}
* @returns {Promise<Token[]>}
*/
public async getMultipleTokensPrice({ chainId }: { chainId?: string }): Promise<Token[]> {
const response = await this.httpInstance.axios.get("/v2/tokens", {
params: {
...(chainId && { chainId }), // only add chainId to params if it's defined
usdPrice: true,
},
});

return response.data.tokens;
}

public async getFromAmount({
fromToken,
toAmount,
Expand All @@ -237,7 +255,7 @@ export class Squid extends TokensChains {
}): Promise<string> {
// if there is an error getting real-time prices,
// use the price at the time of initialization
const [fromTokenPrice = fromToken.usdPrice, toTokenPrice = toToken.usdPrice] =
const [fromTokenPrice = fromToken.usdPrice ?? 0, toTokenPrice = toToken.usdPrice ?? 0] =
await Promise.all([
this.getTokenPrice({
chainId: fromToken.chainId,
Expand Down

0 comments on commit 7ca1047

Please sign in to comment.