1
1
import { Fraction , TFractionLike , TIntegerLike } from "@sundaeswap/fraction" ;
2
+
2
3
import { IHasStringId , stringIdEquals , TFungibleToken } from "./Asset" ;
3
4
import { AssetRatio } from "./AssetRatio" ;
4
5
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 {
6
12
id ?: string ;
7
13
assetId : string ;
8
14
decimals : number ;
9
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
- [ key : string ] : any ;
11
15
}
12
16
13
17
/**
@@ -16,8 +20,7 @@ export interface IAssetAmountMetadata {
16
20
* @extends {IAssetAmountMetadata }
17
21
* @implements {TFungibleToken}
18
22
*/
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 >
21
24
implements TFungibleToken
22
25
{
23
26
static readonly DEFAULT_FUNGIBLE_TOKEN_DECIMALS = 0 ;
@@ -47,10 +50,10 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
47
50
* @template T
48
51
* @param {TFractionLike } value - The token amount represented as a fraction.
49
52
* @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.
51
54
*/
52
55
// eslint-disable-next-line @typescript-eslint/no-explicit-any
53
- static fromValue < T extends IAssetAmountMetadata = any > (
56
+ static fromValue < T extends IAssetAmountMetadata = IAssetAmountMetadata > (
54
57
value : TFractionLike ,
55
58
metadata : number | T = AssetAmount . DEFAULT_FUNGIBLE_TOKEN_DECIMALS
56
59
) : AssetAmount < T > {
@@ -85,11 +88,11 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
85
88
return AssetAmount . fromValue < T > ( value , this ?. metadata ?? this . decimals ) ;
86
89
} ;
87
90
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
+ }
91
94
92
- add = ( rhs : AssetAmount ) : AssetAmount => {
95
+ add = ( rhs : AssetAmount ) : AssetAmount < T > => {
93
96
if ( this . decimals !== rhs . decimals ) {
94
97
// eslint-disable-next-line no-console
95
98
console . warn ( AssetAmount . INVALID_DECIMAL_WARNING ) ;
@@ -98,7 +101,7 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
98
101
} ;
99
102
plus = this . add ;
100
103
101
- subtract = ( rhs : AssetAmount ) : AssetAmount => {
104
+ subtract = ( rhs : AssetAmount ) : AssetAmount < T > => {
102
105
if ( this . decimals !== rhs . decimals ) {
103
106
// eslint-disable-next-line no-console
104
107
console . warn ( AssetAmount . INVALID_DECIMAL_WARNING ) ;
@@ -108,12 +111,12 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
108
111
minus = this . subtract ;
109
112
sub = this . subtract ;
110
113
111
- addValue = ( value : TFractionLike ) : AssetAmount => {
114
+ addValue = ( value : TFractionLike ) : AssetAmount < T > => {
112
115
return this . withValue ( this . value . add ( value ) ) ;
113
116
} ;
114
117
plusValue = this . add ;
115
118
116
- subtractValue = ( value : TFractionLike ) : AssetAmount => {
119
+ subtractValue = ( value : TFractionLike ) : AssetAmount < T > => {
117
120
return this . withValue ( this . value . sub ( value ) ) ;
118
121
} ;
119
122
minusValue = this . subtract ;
@@ -128,9 +131,9 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
128
131
* Multiplies the asset amount with an asset ratio and returns a new AssetAmount.
129
132
* @param {AssetRatio<T> } ar - The asset ratio to multiply with.
130
133
* @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.
132
135
*/
133
- exchangeMultiply ( ar : AssetRatio < T > ) : AssetAmount {
136
+ exchangeMultiply ( ar : AssetRatio < T > ) : AssetAmount < T > {
134
137
if ( ! this . metadata || ! ar . denominator . metadata || ! ar . numerator . metadata ) {
135
138
throw new Error ( AssetAmount . INVALID_METADATA ) ;
136
139
}
@@ -145,9 +148,9 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
145
148
* Divides the asset amount by an asset ratio and returns a new AssetAmount.
146
149
* @param {AssetRatio<T> } ar - The asset ratio to divide by.
147
150
* @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.
149
152
*/
150
- exchangeDivide ( ar : AssetRatio < T > ) : AssetAmount {
153
+ exchangeDivide ( ar : AssetRatio < T > ) : AssetAmount < T > {
151
154
if ( ! this . metadata || ! ar . denominator . metadata || ! ar . numerator . metadata ) {
152
155
throw new Error ( AssetAmount . INVALID_METADATA ) ;
153
156
}
@@ -161,9 +164,9 @@ export class AssetAmount<T extends IAssetAmountMetadata = any>
161
164
/**
162
165
* Performs multiplication or division on the asset amount using an asset ratio, depending on the metadata.
163
166
* @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.
165
168
*/
166
- exchangeAt ( ar : AssetRatio < T > ) : AssetAmount {
169
+ exchangeAt ( ar : AssetRatio < T > ) : AssetAmount < T > {
167
170
if ( this . metadata ?. assetId === ar . denominator . metadata ?. assetId ) {
168
171
return this . exchangeMultiply ( ar ) ;
169
172
} else {
0 commit comments