Skip to content

Commit

Permalink
refactor(token): merge price into Token
Browse files Browse the repository at this point in the history
BREAKING CHANGE: TokenWithPrice has been merged into Token.

The price field has been moved to Token.
The name and symbol properties are now possibly undefined.
The name no longer defaults to the symbol. The decimals property defaults to 0.
  • Loading branch information
Rubilmax committed Oct 21, 2024
1 parent b468055 commit 0f285ed
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 37 deletions.
16 changes: 7 additions & 9 deletions packages/blue-api-sdk/src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
MarketParams,
MathLib,
Position,
TokenWithPrice,
Token,
VaultConfig,
VaultMarketAllocation,
VaultMarketConfig,
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions packages/blue-sdk-ethers/src/fetch/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/blue-sdk/src/token/ConstantWrappedToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ConstantWrappedToken extends WrappedToken {
constructor(
token: InputToken,
underlying: Address,
underlyingDecimals: BigIntish = 18n,
underlyingDecimals: BigIntish = 0,
) {
super(token, underlying);

Expand Down
40 changes: 18 additions & 22 deletions packages/blue-sdk/src/token/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions packages/blue-sdk/src/vault/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 0f285ed

Please sign in to comment.