diff --git a/packages/blue-api-sdk/src/converter.ts b/packages/blue-api-sdk/src/converter.ts index 89cdb9df..62cba186 100644 --- a/packages/blue-api-sdk/src/converter.ts +++ b/packages/blue-api-sdk/src/converter.ts @@ -6,7 +6,7 @@ import { MarketParams, MathLib, Position, - TokenWithPrice, + Token, VaultConfig, VaultMarketAllocation, VaultMarketConfig, @@ -160,17 +160,15 @@ export class BlueSdkConverter { return price; } - public getTokenWithPrice( + public getToken( dto: PartialBlueApiToken, ethPriceUsd?: BlueApiToken["priceUsd"], ) { - return new TokenWithPrice( - { - ...dto, - address: this.options.parseAddress(dto.address), - }, - this.getPriceUsd(dto, ethPriceUsd), - ); + return new Token({ + ...dto, + address: this.options.parseAddress(dto.address), + price: this.getPriceUsd(dto, ethPriceUsd), + }); } public getMarketParams(dto: PartialBlueApiMarketParams) { diff --git a/packages/blue-sdk-ethers/src/fetch/Token.ts b/packages/blue-sdk-ethers/src/fetch/Token.ts index 87fc30cf..329a69a4 100644 --- a/packages/blue-sdk-ethers/src/fetch/Token.ts +++ b/packages/blue-sdk-ethers/src/fetch/Token.ts @@ -158,16 +158,16 @@ export async function fetchToken( const erc20 = ERC20Metadata__factory.connect(address, chainId, runner); const [decimals, symbol, name] = await Promise.all([ - erc20.decimals(overrides), - erc20.symbol(overrides), - erc20.name(overrides), + erc20.decimals(overrides).catch(() => undefined), + erc20.symbol(overrides).catch(() => undefined), + erc20.name(overrides).catch(() => undefined), ]); const token = { address, - decimals: Number.parseInt(decimals.toString()), - symbol, name, + symbol, + decimals, }; const { wstEth, stEth } = getChainAddresses(chainId); diff --git a/packages/blue-sdk/src/token/ConstantWrappedToken.ts b/packages/blue-sdk/src/token/ConstantWrappedToken.ts index dd3c31cb..52d75ea4 100644 --- a/packages/blue-sdk/src/token/ConstantWrappedToken.ts +++ b/packages/blue-sdk/src/token/ConstantWrappedToken.ts @@ -10,7 +10,7 @@ export class ConstantWrappedToken extends WrappedToken { constructor( token: InputToken, underlying: Address, - underlyingDecimals: BigIntish = 18n, + underlyingDecimals: BigIntish = 0, ) { super(token, underlying); diff --git a/packages/blue-sdk/src/token/Token.ts b/packages/blue-sdk/src/token/Token.ts index c719083e..7d6694bc 100644 --- a/packages/blue-sdk/src/token/Token.ts +++ b/packages/blue-sdk/src/token/Token.ts @@ -5,9 +5,10 @@ import type { Address, BigIntish } from "../types.js"; export interface InputToken { address: Address; - decimals: BigIntish; - symbol: string; name?: string; + symbol?: string; + decimals?: BigIntish; + price?: BigIntish; } export class Token implements InputToken { @@ -23,46 +24,40 @@ export class Token implements InputToken { public readonly address: Address; /** - * The token's number of decimals. + * The token's name. */ - public readonly decimals: number; + public readonly name?: string; /** * The token's symbol. */ - public readonly symbol: string; + public readonly symbol?: string; /** - * The name of the token (defaults to the symbol). + * The token's number of decimals. Defaults to 0. */ - public readonly name: string; - - constructor({ address, decimals, symbol, name }: InputToken) { - this.address = address; - this.decimals = Number(decimals); - this.symbol = symbol; - this.name = name ?? symbol; - } -} + public readonly decimals: number; -export class TokenWithPrice extends Token { /** * Price of the token in USD (scaled by WAD). */ public price?: bigint; - constructor(token: InputToken, price?: bigint) { - super(token); - - this.price = price; + constructor({ address, decimals = 0, symbol, name, price }: InputToken) { + this.address = address; + this.name = name; + this.symbol = symbol; + this.decimals = Number(decimals); + if (price != null) this.price = BigInt(price); } /** * Quotes an amount in USD (scaled by WAD) in this token. + * Returns undefined iff the token's price is undefined. * @param amount The amount of USD to quote. */ fromUsd(amount: bigint, rounding: RoundingDirection = "Down") { - if (this.price == null) return null; + if (this.price == null) return; return MathLib.mulDiv( amount, @@ -74,10 +69,11 @@ export class TokenWithPrice extends Token { /** * Quotes an amount of tokens in USD (scaled by WAD). + * Returns undefined iff the token's price is undefined. * @param amount The amount of tokens to quote. */ toUsd(amount: bigint, rounding: RoundingDirection = "Down") { - if (this.price == null) return null; + if (this.price == null) return; return MathLib.mulDiv( amount, diff --git a/packages/blue-sdk/src/vault/Vault.ts b/packages/blue-sdk/src/vault/Vault.ts index c19077c1..9dbf6657 100644 --- a/packages/blue-sdk/src/vault/Vault.ts +++ b/packages/blue-sdk/src/vault/Vault.ts @@ -49,6 +49,16 @@ export interface InputVault extends InputVaultConfig { } export class Vault extends VaultToken implements InputVault { + /** + * The vault's share token's name. + */ + public declare readonly name: string; + + /** + * The vault's share token's symbol. + */ + public declare readonly symbol: string; + /** * The MetaMorpho vault's owner address. */