Skip to content

Commit 4b2e610

Browse files
committed
chore: update types
1 parent 6667a1d commit 4b2e610

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

packages/asset/src/AssetAmount.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { Fraction, TFractionLike, TIntegerLike } from "@sundaeswap/fraction";
2+
23
import { IHasStringId, stringIdEquals, TFungibleToken } from "./Asset";
34
import { AssetRatio } from "./AssetRatio";
45

5-
export interface IAssetAmountMetadata {
6+
export interface IAssetAmountExtraMetadata {
7+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8+
[key: string]: any;
9+
}
10+
11+
export interface IAssetAmountMetadata extends IAssetAmountExtraMetadata {
612
id?: string;
713
assetId: string;
814
decimals: number;
9-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10-
[key: string]: any;
1115
}
1216

1317
/**
@@ -16,8 +20,7 @@ export interface IAssetAmountMetadata {
1620
* @extends {IAssetAmountMetadata}
1721
* @implements {TFungibleToken}
1822
*/
19-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20-
export class AssetAmount<T extends IAssetAmountMetadata = any>
23+
export class AssetAmount<T extends IAssetAmountMetadata = IAssetAmountMetadata>
2124
implements TFungibleToken
2225
{
2326
static readonly DEFAULT_FUNGIBLE_TOKEN_DECIMALS = 0;
@@ -47,10 +50,10 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
4750
* @template T
4851
* @param {TFractionLike} value - The token amount represented as a fraction.
4952
* @param {number | T} metadata - The metadata associated with the asset amount.
50-
* @returns {AssetAmount} - A new AssetAmount instance.
53+
* @returns {AssetAmount<T>} - A new AssetAmount instance.
5154
*/
5255
// eslint-disable-next-line @typescript-eslint/no-explicit-any
53-
static fromValue<T extends IAssetAmountMetadata = any>(
56+
static fromValue<T extends IAssetAmountMetadata = IAssetAmountMetadata>(
5457
value: TFractionLike,
5558
metadata: number | T = AssetAmount.DEFAULT_FUNGIBLE_TOKEN_DECIMALS
5659
): AssetAmount<T> {
@@ -85,11 +88,11 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
8588
return AssetAmount.fromValue<T>(value, this?.metadata ?? this.decimals);
8689
};
8790

88-
withMetadata = (metadata: T): AssetAmount<T> => {
89-
return new AssetAmount(this.amount, metadata);
90-
};
91+
withMetadata<U extends IAssetAmountMetadata>(metadata: U): AssetAmount<U> {
92+
return new AssetAmount<U>(this.amount, metadata);
93+
}
9194

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

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

111-
addValue = (value: TFractionLike): AssetAmount => {
114+
addValue = (value: TFractionLike): AssetAmount<T> => {
112115
return this.withValue(this.value.add(value));
113116
};
114117
plusValue = this.add;
115118

116-
subtractValue = (value: TFractionLike): AssetAmount => {
119+
subtractValue = (value: TFractionLike): AssetAmount<T> => {
117120
return this.withValue(this.value.sub(value));
118121
};
119122
minusValue = this.subtract;
@@ -128,9 +131,9 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
128131
* Multiplies the asset amount with an asset ratio and returns a new AssetAmount.
129132
* @param {AssetRatio<T>} ar - The asset ratio to multiply with.
130133
* @throws {Error} - Throws an error if the metadata is invalid or if the metadata does not match with the denominator's metadata.
131-
* @returns {AssetAmount} - A new AssetAmount representing the multiplication result.
134+
* @returns {AssetAmount<T>} - A new AssetAmount representing the multiplication result.
132135
*/
133-
exchangeMultiply(ar: AssetRatio<T>): AssetAmount {
136+
exchangeMultiply(ar: AssetRatio<T>): AssetAmount<T> {
134137
if (!this.metadata || !ar.denominator.metadata || !ar.numerator.metadata) {
135138
throw new Error(AssetAmount.INVALID_METADATA);
136139
}
@@ -145,9 +148,9 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
145148
* Divides the asset amount by an asset ratio and returns a new AssetAmount.
146149
* @param {AssetRatio<T>} ar - The asset ratio to divide by.
147150
* @throws {Error} - Throws an error if the metadata is invalid or if the metadata does not match with the numerator's metadata.
148-
* @returns {AssetAmount} - A new AssetAmount representing the division result.
151+
* @returns {AssetAmount<T>} - A new AssetAmount representing the division result.
149152
*/
150-
exchangeDivide(ar: AssetRatio<T>): AssetAmount {
153+
exchangeDivide(ar: AssetRatio<T>): AssetAmount<T> {
151154
if (!this.metadata || !ar.denominator.metadata || !ar.numerator.metadata) {
152155
throw new Error(AssetAmount.INVALID_METADATA);
153156
}
@@ -161,9 +164,9 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
161164
/**
162165
* Performs multiplication or division on the asset amount using an asset ratio, depending on the metadata.
163166
* @param {AssetRatio<T>} ar - The asset ratio for the operation.
164-
* @returns {AssetAmount} - A new AssetAmount representing the result of the operation.
167+
* @returns {AssetAmount<T>} - A new AssetAmount representing the result of the operation.
165168
*/
166-
exchangeAt(ar: AssetRatio<T>): AssetAmount {
169+
exchangeAt(ar: AssetRatio<T>): AssetAmount<T> {
167170
if (this.metadata?.assetId === ar.denominator.metadata?.assetId) {
168171
return this.exchangeMultiply(ar);
169172
} else {

0 commit comments

Comments
 (0)