From 4b2e610e37e82ec5689804328e70ac9421adea48 Mon Sep 17 00:00:00 2001 From: Calvin Koepke Date: Mon, 8 Jan 2024 13:24:47 -0700 Subject: [PATCH] chore: update types --- packages/asset/src/AssetAmount.ts | 43 +++++++++++++++++-------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/asset/src/AssetAmount.ts b/packages/asset/src/AssetAmount.ts index 7b3d81d..d484001 100644 --- a/packages/asset/src/AssetAmount.ts +++ b/packages/asset/src/AssetAmount.ts @@ -1,13 +1,17 @@ import { Fraction, TFractionLike, TIntegerLike } from "@sundaeswap/fraction"; + import { IHasStringId, stringIdEquals, TFungibleToken } from "./Asset"; import { AssetRatio } from "./AssetRatio"; -export interface IAssetAmountMetadata { +export interface IAssetAmountExtraMetadata { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + [key: string]: any; +} + +export interface IAssetAmountMetadata extends IAssetAmountExtraMetadata { id?: string; assetId: string; decimals: number; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - [key: string]: any; } /** @@ -16,8 +20,7 @@ export interface IAssetAmountMetadata { * @extends {IAssetAmountMetadata} * @implements {TFungibleToken} */ -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export class AssetAmount +export class AssetAmount implements TFungibleToken { static readonly DEFAULT_FUNGIBLE_TOKEN_DECIMALS = 0; @@ -47,10 +50,10 @@ export class AssetAmount * @template T * @param {TFractionLike} value - The token amount represented as a fraction. * @param {number | T} metadata - The metadata associated with the asset amount. - * @returns {AssetAmount} - A new AssetAmount instance. + * @returns {AssetAmount} - A new AssetAmount instance. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any - static fromValue( + static fromValue( value: TFractionLike, metadata: number | T = AssetAmount.DEFAULT_FUNGIBLE_TOKEN_DECIMALS ): AssetAmount { @@ -85,11 +88,11 @@ export class AssetAmount return AssetAmount.fromValue(value, this?.metadata ?? this.decimals); }; - withMetadata = (metadata: T): AssetAmount => { - return new AssetAmount(this.amount, metadata); - }; + withMetadata(metadata: U): AssetAmount { + return new AssetAmount(this.amount, metadata); + } - add = (rhs: AssetAmount): AssetAmount => { + add = (rhs: AssetAmount): AssetAmount => { if (this.decimals !== rhs.decimals) { // eslint-disable-next-line no-console console.warn(AssetAmount.INVALID_DECIMAL_WARNING); @@ -98,7 +101,7 @@ export class AssetAmount }; plus = this.add; - subtract = (rhs: AssetAmount): AssetAmount => { + subtract = (rhs: AssetAmount): AssetAmount => { if (this.decimals !== rhs.decimals) { // eslint-disable-next-line no-console console.warn(AssetAmount.INVALID_DECIMAL_WARNING); @@ -108,12 +111,12 @@ export class AssetAmount minus = this.subtract; sub = this.subtract; - addValue = (value: TFractionLike): AssetAmount => { + addValue = (value: TFractionLike): AssetAmount => { return this.withValue(this.value.add(value)); }; plusValue = this.add; - subtractValue = (value: TFractionLike): AssetAmount => { + subtractValue = (value: TFractionLike): AssetAmount => { return this.withValue(this.value.sub(value)); }; minusValue = this.subtract; @@ -128,9 +131,9 @@ export class AssetAmount * Multiplies the asset amount with an asset ratio and returns a new AssetAmount. * @param {AssetRatio} ar - The asset ratio to multiply with. * @throws {Error} - Throws an error if the metadata is invalid or if the metadata does not match with the denominator's metadata. - * @returns {AssetAmount} - A new AssetAmount representing the multiplication result. + * @returns {AssetAmount} - A new AssetAmount representing the multiplication result. */ - exchangeMultiply(ar: AssetRatio): AssetAmount { + exchangeMultiply(ar: AssetRatio): AssetAmount { if (!this.metadata || !ar.denominator.metadata || !ar.numerator.metadata) { throw new Error(AssetAmount.INVALID_METADATA); } @@ -145,9 +148,9 @@ export class AssetAmount * Divides the asset amount by an asset ratio and returns a new AssetAmount. * @param {AssetRatio} ar - The asset ratio to divide by. * @throws {Error} - Throws an error if the metadata is invalid or if the metadata does not match with the numerator's metadata. - * @returns {AssetAmount} - A new AssetAmount representing the division result. + * @returns {AssetAmount} - A new AssetAmount representing the division result. */ - exchangeDivide(ar: AssetRatio): AssetAmount { + exchangeDivide(ar: AssetRatio): AssetAmount { if (!this.metadata || !ar.denominator.metadata || !ar.numerator.metadata) { throw new Error(AssetAmount.INVALID_METADATA); } @@ -161,9 +164,9 @@ export class AssetAmount /** * Performs multiplication or division on the asset amount using an asset ratio, depending on the metadata. * @param {AssetRatio} ar - The asset ratio for the operation. - * @returns {AssetAmount} - A new AssetAmount representing the result of the operation. + * @returns {AssetAmount} - A new AssetAmount representing the result of the operation. */ - exchangeAt(ar: AssetRatio): AssetAmount { + exchangeAt(ar: AssetRatio): AssetAmount { if (this.metadata?.assetId === ar.denominator.metadata?.assetId) { return this.exchangeMultiply(ar); } else {