Skip to content

Commit

Permalink
chore: update types
Browse files Browse the repository at this point in the history
  • Loading branch information
cjkoepke committed Jan 8, 2024
1 parent 6667a1d commit 4b2e610
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions packages/asset/src/AssetAmount.ts
Original file line number Diff line number Diff line change
@@ -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;
}

/**
Expand All @@ -16,8 +20,7 @@ export interface IAssetAmountMetadata {
* @extends {IAssetAmountMetadata}
* @implements {TFungibleToken}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class AssetAmount<T extends IAssetAmountMetadata = any>
export class AssetAmount<T extends IAssetAmountMetadata = IAssetAmountMetadata>
implements TFungibleToken
{
static readonly DEFAULT_FUNGIBLE_TOKEN_DECIMALS = 0;
Expand Down Expand Up @@ -47,10 +50,10 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
* @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<T>} - A new AssetAmount instance.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static fromValue<T extends IAssetAmountMetadata = any>(
static fromValue<T extends IAssetAmountMetadata = IAssetAmountMetadata>(
value: TFractionLike,
metadata: number | T = AssetAmount.DEFAULT_FUNGIBLE_TOKEN_DECIMALS
): AssetAmount<T> {
Expand Down Expand Up @@ -85,11 +88,11 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
return AssetAmount.fromValue<T>(value, this?.metadata ?? this.decimals);
};

withMetadata = (metadata: T): AssetAmount<T> => {
return new AssetAmount(this.amount, metadata);
};
withMetadata<U extends IAssetAmountMetadata>(metadata: U): AssetAmount<U> {
return new AssetAmount<U>(this.amount, metadata);
}

add = (rhs: AssetAmount): AssetAmount => {
add = (rhs: AssetAmount): AssetAmount<T> => {
if (this.decimals !== rhs.decimals) {
// eslint-disable-next-line no-console
console.warn(AssetAmount.INVALID_DECIMAL_WARNING);
Expand All @@ -98,7 +101,7 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
};
plus = this.add;

subtract = (rhs: AssetAmount): AssetAmount => {
subtract = (rhs: AssetAmount): AssetAmount<T> => {
if (this.decimals !== rhs.decimals) {
// eslint-disable-next-line no-console
console.warn(AssetAmount.INVALID_DECIMAL_WARNING);
Expand All @@ -108,12 +111,12 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
minus = this.subtract;
sub = this.subtract;

addValue = (value: TFractionLike): AssetAmount => {
addValue = (value: TFractionLike): AssetAmount<T> => {
return this.withValue(this.value.add(value));
};
plusValue = this.add;

subtractValue = (value: TFractionLike): AssetAmount => {
subtractValue = (value: TFractionLike): AssetAmount<T> => {
return this.withValue(this.value.sub(value));
};
minusValue = this.subtract;
Expand All @@ -128,9 +131,9 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
* Multiplies the asset amount with an asset ratio and returns a new AssetAmount.
* @param {AssetRatio<T>} 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<T>} - A new AssetAmount representing the multiplication result.
*/
exchangeMultiply(ar: AssetRatio<T>): AssetAmount {
exchangeMultiply(ar: AssetRatio<T>): AssetAmount<T> {
if (!this.metadata || !ar.denominator.metadata || !ar.numerator.metadata) {
throw new Error(AssetAmount.INVALID_METADATA);
}
Expand All @@ -145,9 +148,9 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
* Divides the asset amount by an asset ratio and returns a new AssetAmount.
* @param {AssetRatio<T>} 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<T>} - A new AssetAmount representing the division result.
*/
exchangeDivide(ar: AssetRatio<T>): AssetAmount {
exchangeDivide(ar: AssetRatio<T>): AssetAmount<T> {
if (!this.metadata || !ar.denominator.metadata || !ar.numerator.metadata) {
throw new Error(AssetAmount.INVALID_METADATA);
}
Expand All @@ -161,9 +164,9 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
/**
* Performs multiplication or division on the asset amount using an asset ratio, depending on the metadata.
* @param {AssetRatio<T>} ar - The asset ratio for the operation.
* @returns {AssetAmount} - A new AssetAmount representing the result of the operation.
* @returns {AssetAmount<T>} - A new AssetAmount representing the result of the operation.
*/
exchangeAt(ar: AssetRatio<T>): AssetAmount {
exchangeAt(ar: AssetRatio<T>): AssetAmount<T> {
if (this.metadata?.assetId === ar.denominator.metadata?.assetId) {
return this.exchangeMultiply(ar);
} else {
Expand Down

0 comments on commit 4b2e610

Please sign in to comment.