aftermath-ts-sdk 0.0.163 → 0.0.165
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/general/utils/caller.d.ts.map +1 -1
- package/dist/general/utils/caller.js +2 -0
- package/dist/general/utils/fixed.d.ts +5 -0
- package/dist/general/utils/fixed.d.ts.map +1 -1
- package/dist/general/utils/fixed.js +4 -0
- package/dist/packages/pools/api/poolsApiCasting.d.ts.map +1 -1
- package/dist/packages/pools/api/poolsApiCasting.js +2 -1
- package/dist/packages/pools/pool.d.ts.map +1 -1
- package/dist/packages/pools/pool.js +4 -1
- package/dist/packages/pools/poolsTypes.d.ts +4 -1
- package/dist/packages/pools/poolsTypes.d.ts.map +1 -1
- package/dist/packages/pools/utils/cmmmCalculations.d.ts.map +1 -1
- package/dist/packages/pools/utils/cmmmCalculations.js +81 -77
- package/dist/packages/router/utils/synchronous/routerGraph.js +6 -7
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"caller.d.ts","sourceRoot":"","sources":["../../../src/general/utils/caller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EACN,aAAa,EACb,gBAAgB,EAGhB,GAAG,EACH,MAAM,aAAa,CAAC;AAGrB,qBAAa,MAAM;aAQD,OAAO,CAAC;IACxB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAR3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAM;gBAOd,OAAO,CAAC,oBAAkB,EACzB,SAAS,GAAE,GAAQ;IAcrC,OAAO,CAAC,MAAM,CAAC,iBAAiB;mBAYX,mBAAmB;
|
|
1
|
+
{"version":3,"file":"caller.d.ts","sourceRoot":"","sources":["../../../src/general/utils/caller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EACN,aAAa,EACb,gBAAgB,EAGhB,GAAG,EACH,MAAM,aAAa,CAAC;AAGrB,qBAAa,MAAM;aAQD,OAAO,CAAC;IACxB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAR3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAM;gBAOd,OAAO,CAAC,oBAAkB,EACzB,SAAS,GAAE,GAAQ;IAcrC,OAAO,CAAC,MAAM,CAAC,iBAAiB;mBAYX,mBAAmB;IAUxC,OAAO,CAAC,aAAa,CAQnB;cAMc,QAAQ,CAAC,MAAM,EAAE,QAAQ,GAAG,SAAS,EACpD,GAAG,EAAE,GAAG,EACR,IAAI,CAAC,EAAE,QAAQ,EACf,MAAM,CAAC,EAAE,WAAW,GAClB,OAAO,CAAC,MAAM,CAAC;cAsBF,mBAAmB,CAAC,QAAQ,GAAG,SAAS,EACvD,GAAG,EAAE,GAAG,EACR,IAAI,CAAC,EAAE,QAAQ,EACf,MAAM,CAAC,EAAE,WAAW;cAWL,cAAc,CAAC,SAAS,EAAE,QAAQ,GAAG,aAAa,EACjE,GAAG,EAAE,GAAG,EACR,IAAI,EAAE,QAAQ,EACd,MAAM,CAAC,EAAE,WAAW;CAQrB"}
|
|
@@ -49,6 +49,8 @@ class Caller {
|
|
|
49
49
|
}
|
|
50
50
|
static fetchResponseToType(response) {
|
|
51
51
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
if (!response.ok)
|
|
53
|
+
throw new Error(yield response.text());
|
|
52
54
|
const json = JSON.stringify(yield response.json());
|
|
53
55
|
const output = helpers_1.Helpers.parseJsonWithBigint(json);
|
|
54
56
|
return output;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Balance, DecimalsScalar, NormalizedBalance } from "../../types";
|
|
1
2
|
export type OnChainNumber = bigint;
|
|
2
3
|
export type LocalNumber = number;
|
|
3
4
|
export declare class Fixed {
|
|
@@ -8,6 +9,10 @@ export declare class Fixed {
|
|
|
8
9
|
static directCast: (n: OnChainNumber) => LocalNumber;
|
|
9
10
|
static directUncast: (n: LocalNumber) => OnChainNumber;
|
|
10
11
|
static complement: (n: LocalNumber) => number;
|
|
12
|
+
static normalizeAmount: (decimalsScalar: DecimalsScalar, amount: Balance) => NormalizedBalance;
|
|
13
|
+
static unnormalizeAmount: (decimalsScalar: DecimalsScalar, normalizedAmount: NormalizedBalance) => Balance;
|
|
14
|
+
static castAndNormalize: (decimalsScalar: DecimalsScalar, amount: Balance) => LocalNumber;
|
|
15
|
+
static uncastAndUnnormalize: (decimalsScalar: DecimalsScalar, normalizedAmount: LocalNumber) => Balance;
|
|
11
16
|
}
|
|
12
17
|
export type OnChainRawInteger = OnChainNumber;
|
|
13
18
|
export type OnChainUnitaryParameter = OnChainNumber;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fixed.d.ts","sourceRoot":"","sources":["../../../src/general/utils/fixed.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC;AACnC,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC;AAEjC,qBAAa,KAAK;IACd,OAAc,SAAS,EAAE,MAAM,CAA6B;IAC5D,OAAc,SAAS,EAAE,MAAM,CAAiC;IAIhE,OAAc,cAAc,MAAO,aAAa,KAAG,WAAW,CAAc;IAC/E,OAAc,YAAY,MAAO,WAAW,KAAG,aAAa,CAA0B;IACnF,OAAc,UAAU,MAAO,aAAa,KAAG,WAAW,CAAgC;IAC1F,OAAc,YAAY,MAAO,WAAW,KAAG,aAAa,CAE1D;IAEF,OAAc,UAAU,MAAO,WAAW,YAAqC;
|
|
1
|
+
{"version":3,"file":"fixed.d.ts","sourceRoot":"","sources":["../../../src/general/utils/fixed.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEzE,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC;AACnC,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC;AAEjC,qBAAa,KAAK;IACd,OAAc,SAAS,EAAE,MAAM,CAA6B;IAC5D,OAAc,SAAS,EAAE,MAAM,CAAiC;IAIhE,OAAc,cAAc,MAAO,aAAa,KAAG,WAAW,CAAc;IAC/E,OAAc,YAAY,MAAO,WAAW,KAAG,aAAa,CAA0B;IACnF,OAAc,UAAU,MAAO,aAAa,KAAG,WAAW,CAAgC;IAC1F,OAAc,YAAY,MAAO,WAAW,KAAG,aAAa,CAE1D;IAEF,OAAc,UAAU,MAAO,WAAW,YAAqC;IAElF,OAAc,eAAe,mBACZ,cAAc,UACtB,OAAO,KACb,iBAAiB,CACK;IAEzB,OAAc,iBAAiB,mBACd,cAAc,oBACZ,iBAAiB,KAChC,OAAO,CACyB;IAEjC,OAAc,gBAAgB,mBACV,cAAc,UACtB,OAAO,KAChB,WAAW,CAKb;IAED,OAAc,oBAAoB,mBACd,cAAc,oBACZ,WAAW,KAC9B,OAAO,CAGT;CACJ;AAKD,MAAM,MAAM,iBAAiB,GAAG,aAAa,CAAC;AAG9C,MAAM,MAAM,uBAAuB,GAAG,aAAa,CAAC;AAGpD,MAAM,MAAM,aAAa,GAAG,aAAa,CAAC"}
|
|
@@ -13,3 +13,7 @@ Fixed.convertToInt = (n) => BigInt(Math.floor(n));
|
|
|
13
13
|
Fixed.directCast = (n) => Number(n) / Fixed.fixedOneN;
|
|
14
14
|
Fixed.directUncast = (n) => BigInt(Math.floor(n * Fixed.fixedOneN));
|
|
15
15
|
Fixed.complement = (n) => Math.max(0, 1 - Math.max(0, n));
|
|
16
|
+
Fixed.normalizeAmount = (decimalsScalar, amount) => amount * decimalsScalar;
|
|
17
|
+
Fixed.unnormalizeAmount = (decimalsScalar, normalizedAmount) => normalizedAmount / decimalsScalar;
|
|
18
|
+
Fixed.castAndNormalize = (decimalsScalar, amount) => Fixed.directCast(Fixed.normalizeAmount(decimalsScalar, amount));
|
|
19
|
+
Fixed.uncastAndUnnormalize = (decimalsScalar, normalizedAmount) => Fixed.unnormalizeAmount(decimalsScalar, Fixed.directUncast(normalizedAmount));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"poolsApiCasting.d.ts","sourceRoot":"","sources":["../../../../src/packages/pools/api/poolsApiCasting.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,QAAQ,EACR,iBAAiB,EAGjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAEN,gBAAgB,EAChB,UAAU,EACV,cAAc,EACd,iBAAiB,EACjB,MAAM,eAAe,CAAC;AACvB,OAAO,EACN,sBAAsB,EACtB,uBAAuB,EAEvB,qBAAqB,EACrB,wBAAwB,EACxB,MAAM,wBAAwB,CAAC;AAMhC,qBAAa,eAAe;IAK3B,OAAc,uBAAuB,cACzB,iBAAiB,KAC1B,UAAU,
|
|
1
|
+
{"version":3,"file":"poolsApiCasting.d.ts","sourceRoot":"","sources":["../../../../src/packages/pools/api/poolsApiCasting.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,QAAQ,EACR,iBAAiB,EAGjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAEN,gBAAgB,EAChB,UAAU,EACV,cAAc,EACd,iBAAiB,EACjB,MAAM,eAAe,CAAC;AACvB,OAAO,EACN,sBAAsB,EACtB,uBAAuB,EAEvB,qBAAqB,EACrB,wBAAwB,EACxB,MAAM,wBAAwB,CAAC;AAMhC,qBAAa,eAAe;IAK3B,OAAc,uBAAuB,cACzB,iBAAiB,KAC1B,UAAU,CAwDX;IAEF,OAAc,iCAAiC,SACxC,iBAAiB,KACrB,QAAQ,CAWT;IAMF,OAAc,yBAAyB,2CAEpC,cAAc,CAiBf;IAEF,OAAc,2BAA2B,6CAEtC,gBAAgB,CAejB;IAEF,OAAc,4BAA4B,8CAEvC,iBAAiB,CAclB;IAEF,OAAc,sCAAsC,4CAEjD,QAAQ,CAGT;CACF"}
|
|
@@ -23,7 +23,8 @@ PoolsApiCasting.poolObjectFromSuiObject = (suiObject) => {
|
|
|
23
23
|
tradeFeeOut: BigInt(poolFieldsOnChain.fees_swap_out[index]),
|
|
24
24
|
depositFee: BigInt(poolFieldsOnChain.fees_deposit[index]),
|
|
25
25
|
withdrawFee: BigInt(poolFieldsOnChain.fees_withdraw[index]),
|
|
26
|
-
|
|
26
|
+
normalizedBalance: BigInt(poolFieldsOnChain.normalized_balances[index]),
|
|
27
|
+
decimalsScalar: BigInt(poolFieldsOnChain.decimal_scalars[index]),
|
|
27
28
|
} });
|
|
28
29
|
}, {});
|
|
29
30
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../../../src/packages/pools/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,kBAAkB,EAClB,gBAAgB,EAChB,mBAAmB,EACnB,OAAO,EACP,QAAQ,EACR,cAAc,
|
|
1
|
+
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../../../src/packages/pools/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,kBAAkB,EAClB,gBAAgB,EAChB,mBAAmB,EACnB,OAAO,EACP,QAAQ,EACR,cAAc,EAEd,aAAa,EACb,yBAAyB,EACzB,UAAU,EACV,SAAS,EAET,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,cAAc,EAEd,0BAA0B,EAE1B,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AAIpD,qBAAa,IAAK,SAAQ,MAAM;aAoBd,IAAI,EAAE,UAAU;aAChB,OAAO,CAAC;IAhBzB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAE/B;IAMK,KAAK,EAAE,SAAS,GAAG,SAAS,CAAC;gBAOnB,IAAI,EAAE,UAAU,EAChB,OAAO,CAAC,oBAAkB;IAU9B,QAAQ;IAId,QAAQ,CAAC,KAAK,EAAE,SAAS;IAInB,aAAa,CAAC,MAAM,EAAE;QAClC,SAAS,EAAE,yBAAyB,CAAC;KACrC,GAAG,OAAO,CAAC,aAAa,EAAE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EhB,QAAQ,CAAC,EAAE,OAAO,CAAC;KACnB,KAAG,cAAc,CAehB;IAEK,2BAA2B,WAAY;QAC7C,eAAe,EAAE,MAAM,CAAC;KACxB,KAAG,MAAM,CAEsB;IAEzB,yBAAyB,WAAY;QAC3C,eAAe,EAAE,MAAM,CAAC;KACxB,KAAG,MAAM,CACuD;CACjE"}
|
|
@@ -26,7 +26,10 @@ class Pool extends caller_1.Caller {
|
|
|
26
26
|
// Calculations
|
|
27
27
|
// =========================================================================
|
|
28
28
|
this.getSpotPrice = (inputs) => {
|
|
29
|
-
|
|
29
|
+
const spotPriceWithDecimals = cmmmCalculations_1.CmmmCalculations.calcSpotPriceWithFees(utils_1.Helpers.deepCopy(this.pool), inputs.coinInType, inputs.coinOutType, !inputs.withFees);
|
|
30
|
+
return ((spotPriceWithDecimals *
|
|
31
|
+
Number(this.pool.coins[inputs.coinOutType].decimalsScalar)) /
|
|
32
|
+
Number(this.pool.coins[inputs.coinInType].decimalsScalar));
|
|
30
33
|
};
|
|
31
34
|
this.getTradeAmountOut = (inputs) => {
|
|
32
35
|
const pool = utils_1.Helpers.deepCopy(this.pool);
|
|
@@ -8,6 +8,8 @@ export type PoolTradeFee = bigint;
|
|
|
8
8
|
export type PoolDepositFee = bigint;
|
|
9
9
|
export type PoolWithdrawFee = bigint;
|
|
10
10
|
export type PoolFlatness = bigint;
|
|
11
|
+
export type NormalizedBalance = bigint;
|
|
12
|
+
export type DecimalsScalar = bigint;
|
|
11
13
|
export type PoolCoins = Record<CoinType, PoolCoin>;
|
|
12
14
|
export interface PoolCoin {
|
|
13
15
|
weight: PoolWeight;
|
|
@@ -16,7 +18,8 @@ export interface PoolCoin {
|
|
|
16
18
|
tradeFeeOut: PoolTradeFee;
|
|
17
19
|
depositFee: PoolDepositFee;
|
|
18
20
|
withdrawFee: PoolWithdrawFee;
|
|
19
|
-
|
|
21
|
+
decimalsScalar: DecimalsScalar;
|
|
22
|
+
normalizedBalance: NormalizedBalance;
|
|
20
23
|
}
|
|
21
24
|
export interface PoolObject extends Object {
|
|
22
25
|
name: PoolName;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"poolsTypes.d.ts","sourceRoot":"","sources":["../../../src/packages/pools/poolsTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,OAAO,EACP,KAAK,EACL,MAAM,EAEN,QAAQ,EACR,SAAS,EAET,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAS1E,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC;AAC9B,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC;AAChC,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC;AAClC,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC;AACpC,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC;AACrC,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"poolsTypes.d.ts","sourceRoot":"","sources":["../../../src/packages/pools/poolsTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,OAAO,EACP,KAAK,EACL,MAAM,EAEN,QAAQ,EACR,SAAS,EAET,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAS1E,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC;AAC9B,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC;AAChC,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC;AAClC,MAAM,MAA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QAAQ,CAAC;CACtB;AAED,MAAM,WAAW,gCAAgC;IAChD,UAAU,EAAE,QAAQ,CAAC;CACrB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cmmmCalculations.d.ts","sourceRoot":"","sources":["../../../../src/packages/pools/utils/cmmmCalculations.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"cmmmCalculations.d.ts","sourceRoot":"","sources":["../../../../src/packages/pools/utils/cmmmCalculations.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,OAAO,EACP,QAAQ,EACR,cAAc,EAEd,UAAU,EACV,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAEN,WAAW,EACX,aAAa,EACb,MAAM,8BAA8B,CAAC;AA0CtC,qBAAa,gBAAgB;IAC5B,OAAO,CAAC,MAAM,CAAC,SAAS,CAAqB;IAG7C,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAoB;IAMpD,OAAO,CAAC,MAAM,CAAC,UAAU,CAAoB;IAC7C,OAAO,CAAC,MAAM,CAAC,WAAW,CAAoB;IAG9C,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAoB;IAEpD,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAkB;IAElD,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAoB;IACpD,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAA8B;IAC7D,OAAO,CAAC,MAAM,CAAC,SAAS,CAAoC;IAC5D,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAA0B;IAO1D,OAAc,aAAa,SAAU,UAAU,KAAG,MAAM,CAmBtD;IAIF,OAAc,sBAAsB,SAC7B,MAAM,OACP,MAAM,YACD,MAAM,KACd,MAAM,CAON;IAUH,OAAc,uBAAuB,SAC9B,UAAU,SACT,QAAQ,oEAqCd;IAGF,OAAc,aAAa,SACpB,UAAU,cACJ,QAAQ,eACP,QAAQ,KACnB,MAAM,CAMN;IAGH,OAAc,qBAAqB,SAC5B,UAA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QAAQ,YACX,OAAO,KACf,OAAO,CAQP;IAGH,OAAc,qBAAqB,SAC5B,UAAU,cACJ,QAAQ,eACP,QAAQ,aACV,OAAO,KAChB,OAAO,CASP;IAIH,OAAc,sBAAsB,SAC7B,UAAU,aACL,cAAc,uBACJ,cAAc,KACjC,WAAW,CA0CZ;IAIF,OAAc,uBAAuB,SAC9B,UAAU,sBACI,cAAc,cACtB,cAAc,KACxB,WAAW,CA0CZ;IAIF,OAAc,8BAA8B,SACrC,UAAU,aACL,cAAc,KACvB,WAAW,CA0DZ;IAKF,OAAc,gCAAgC,SACvC,UAAU,uBACK,cAAc,WAC1B,WAAW,KAClB,WAAW,CA6DZ;CACF"}
|
|
@@ -70,7 +70,7 @@ CmmmCalculations.calcInvariant = (pool) => {
|
|
|
70
70
|
let balance;
|
|
71
71
|
let weight;
|
|
72
72
|
for (let coin of Object.values(pool.coins)) {
|
|
73
|
-
balance = fixed_1.Fixed.
|
|
73
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
74
74
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
75
75
|
sum += weight * balance;
|
|
76
76
|
prod += weight * Math.log(balance);
|
|
@@ -104,7 +104,7 @@ CmmmCalculations.calcInvariantComponents = (pool, index) => {
|
|
|
104
104
|
let p;
|
|
105
105
|
let s;
|
|
106
106
|
for (let [coinType, coin] of Object.entries(pool.coins)) {
|
|
107
|
-
balance = fixed_1.Fixed.
|
|
107
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
108
108
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
109
109
|
p = weight * Math.log(balance);
|
|
110
110
|
s = weight * balance;
|
|
@@ -133,8 +133,8 @@ CmmmCalculations.calcSpotPriceWithFees = (pool, coinTypeIn, coinTypeOut, ignoreF
|
|
|
133
133
|
let part1 = CmmmCalculations.calcSpotPriceBody(pool);
|
|
134
134
|
let coinIn = pool.coins[coinTypeIn];
|
|
135
135
|
let coinOut = pool.coins[coinTypeOut];
|
|
136
|
-
let balanceIn = fixed_1.Fixed.
|
|
137
|
-
let balanceOut = fixed_1.Fixed.
|
|
136
|
+
let balanceIn = fixed_1.Fixed.directCast(coinIn.normalizedBalance);
|
|
137
|
+
let balanceOut = fixed_1.Fixed.directCast(coinOut.normalizedBalance);
|
|
138
138
|
let weightIn = fixed_1.Fixed.directCast(coinIn.weight);
|
|
139
139
|
let weightOut = fixed_1.Fixed.directCast(coinOut.weight);
|
|
140
140
|
let swapFeeIn = ignoreFees ? 0 : fixed_1.Fixed.directCast(coinIn.tradeFeeIn);
|
|
@@ -157,7 +157,7 @@ CmmmCalculations.calcSpotPriceBody = (pool) => {
|
|
|
157
157
|
// The spot price formula requires knowing the value of the invariant. We need the prod and sum parts
|
|
158
158
|
// also later on so no need to compute them twice by calling calcInvariant, just evaluate here.
|
|
159
159
|
for (let coin of Object.values(pool.coins)) {
|
|
160
|
-
balance = fixed_1.Fixed.
|
|
160
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
161
161
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
162
162
|
prod += weight * Math.log(balance);
|
|
163
163
|
sum += weight * balance;
|
|
@@ -179,11 +179,12 @@ CmmmCalculations.calcOutGivenIn = (pool, coinTypeIn, coinTypeOut, amountIn) => {
|
|
|
179
179
|
return BigInt(0);
|
|
180
180
|
}
|
|
181
181
|
let flatness = fixed_1.Fixed.directCast(pool.flatness);
|
|
182
|
-
let oldIn = fixed_1.Fixed.
|
|
183
|
-
let oldOut = fixed_1.Fixed.
|
|
182
|
+
let oldIn = fixed_1.Fixed.directCast(coinIn.normalizedBalance);
|
|
183
|
+
let oldOut = fixed_1.Fixed.directCast(coinOut.normalizedBalance);
|
|
184
184
|
let wIn = fixed_1.Fixed.directCast(coinIn.weight);
|
|
185
185
|
let [prod, _sum, p0, s0, h] = CmmmCalculations.calcInvariantComponents(pool, coinTypeOut);
|
|
186
|
-
let feedAmountIn = (1 - swapFeeIn) *
|
|
186
|
+
let feedAmountIn = (1 - swapFeeIn) *
|
|
187
|
+
fixed_1.Fixed.castAndNormalize(coinIn.decimalsScalar, amountIn);
|
|
187
188
|
let newIn = oldIn + feedAmountIn;
|
|
188
189
|
let prodRatio = Math.pow(newIn / oldIn, wIn);
|
|
189
190
|
let newP0 = p0 * prodRatio;
|
|
@@ -195,7 +196,7 @@ CmmmCalculations.calcOutGivenIn = (pool, coinTypeIn, coinTypeOut, amountIn) => {
|
|
|
195
196
|
newP0, // P(B) / xi^(1/n) (everything but the missing part)
|
|
196
197
|
newS0 // S(B) - xi / n (everything but the missing part)
|
|
197
198
|
);
|
|
198
|
-
let amountOut = fixed_1.Fixed.
|
|
199
|
+
let amountOut = fixed_1.Fixed.uncastAndUnnormalize(coinOut.decimalsScalar, (oldOut - tokenAmountOut) * (1 - swapFeeOut));
|
|
199
200
|
if (!CmmmCalculations.checkValid1dSwap(pool, coinTypeIn, coinTypeOut, amountIn, amountOut))
|
|
200
201
|
throw Error("invalid 1d swap");
|
|
201
202
|
return amountOut;
|
|
@@ -215,11 +216,12 @@ CmmmCalculations.calcInGivenOut = (pool, coinTypeIn, coinTypeOut, amountOut) =>
|
|
|
215
216
|
throw Error("this swap is disabled");
|
|
216
217
|
}
|
|
217
218
|
let flatness = fixed_1.Fixed.directCast(pool.flatness);
|
|
218
|
-
let oldIn = fixed_1.Fixed.
|
|
219
|
-
let oldOut = fixed_1.Fixed.
|
|
219
|
+
let oldIn = fixed_1.Fixed.directCast(coinIn.normalizedBalance);
|
|
220
|
+
let oldOut = fixed_1.Fixed.directCast(coinOut.normalizedBalance);
|
|
220
221
|
let wOut = fixed_1.Fixed.directCast(coinOut.weight);
|
|
221
222
|
let [prod, _sum, p0, s0, h] = CmmmCalculations.calcInvariantComponents(pool, coinTypeIn);
|
|
222
|
-
let feedAmountOut = fixed_1.Fixed.
|
|
223
|
+
let feedAmountOut = fixed_1.Fixed.castAndNormalize(coinIn.decimalsScalar, amountOut) /
|
|
224
|
+
(1 - swapFeeOut);
|
|
223
225
|
let newOut = oldOut - feedAmountOut;
|
|
224
226
|
let prodRatio = Math.pow(newOut / oldOut, wOut);
|
|
225
227
|
let newP0 = p0 * prodRatio;
|
|
@@ -231,7 +233,7 @@ CmmmCalculations.calcInGivenOut = (pool, coinTypeIn, coinTypeOut, amountOut) =>
|
|
|
231
233
|
newP0, // P(B) / xi^(1/n) (everything but the missing part)
|
|
232
234
|
newS0 // S(B) - xi / n (everything but the missing part)
|
|
233
235
|
);
|
|
234
|
-
let amountIn = fixed_1.Fixed.
|
|
236
|
+
let amountIn = fixed_1.Fixed.uncastAndUnnormalize(coinOut.decimalsScalar, (tokenAmountIn - oldIn) / (1 - swapFeeIn));
|
|
235
237
|
if (!CmmmCalculations.checkValid1dSwap(pool, coinTypeIn, coinTypeOut, amountIn, amountOut))
|
|
236
238
|
throw Error("invalid 1d swap");
|
|
237
239
|
return amountIn;
|
|
@@ -267,7 +269,7 @@ CmmmCalculations.calcSwapFixedIn = (pool, amountsIn, amountsOutDirection) => {
|
|
|
267
269
|
let shifter = 1;
|
|
268
270
|
// make sure no disabled coin type is expected
|
|
269
271
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
270
|
-
amountOut = fixed_1.Fixed.
|
|
272
|
+
amountOut = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
|
|
271
273
|
feeOut = fixed_1.Fixed.complement(fixed_1.Fixed.directCast(coin.tradeFeeOut));
|
|
272
274
|
if (amountOut > 0) {
|
|
273
275
|
if (feeOut == 0) {
|
|
@@ -276,8 +278,8 @@ CmmmCalculations.calcSwapFixedIn = (pool, amountsIn, amountsOutDirection) => {
|
|
|
276
278
|
else {
|
|
277
279
|
// pool is drained when b + Ain * (1 - Sin) - t * Aout / (1 - Sout) = 0, or t = (b + Ain * (1 - Sin)) * (1 - So) / Aout
|
|
278
280
|
t =
|
|
279
|
-
((fixed_1.Fixed.
|
|
280
|
-
fixed_1.Fixed.
|
|
281
|
+
((fixed_1.Fixed.directCast(coin.normalizedBalance) +
|
|
282
|
+
fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0)) *
|
|
281
283
|
fixed_1.Fixed.complement(fixed_1.Fixed.directCast(coin.tradeFeeIn))) /
|
|
282
284
|
amountOut) *
|
|
283
285
|
feeOut;
|
|
@@ -298,10 +300,10 @@ CmmmCalculations.calcSwapFixedIn = (pool, amountsIn, amountsOutDirection) => {
|
|
|
298
300
|
sum1 = 0;
|
|
299
301
|
skip = false;
|
|
300
302
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
301
|
-
balance = fixed_1.Fixed.
|
|
303
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
302
304
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
303
|
-
amountIn = fixed_1.Fixed.
|
|
304
|
-
amountOut = fixed_1.Fixed.
|
|
305
|
+
amountIn = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
|
|
306
|
+
amountOut = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
|
|
305
307
|
feeIn = fixed_1.Fixed.complement(fixed_1.Fixed.directCast(coin.tradeFeeIn));
|
|
306
308
|
feeOut = fixed_1.Fixed.complement(fixed_1.Fixed.directCast(coin.tradeFeeOut));
|
|
307
309
|
// pseudoin
|
|
@@ -377,10 +379,10 @@ CmmmCalculations.calcSwapFixedOut = (pool, amountsInDirection, amountsOut) => {
|
|
|
377
379
|
sum = 0;
|
|
378
380
|
sum1 = 0;
|
|
379
381
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
380
|
-
balance = fixed_1.Fixed.
|
|
382
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
381
383
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
382
|
-
amountIn = fixed_1.Fixed.
|
|
383
|
-
amountOut = fixed_1.Fixed.
|
|
384
|
+
amountIn = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsInDirection[coinType] || BigInt(0));
|
|
385
|
+
amountOut = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOut[coinType] || BigInt(0));
|
|
384
386
|
feeIn = 1 - fixed_1.Fixed.directCast(coin.tradeFeeIn);
|
|
385
387
|
feeOut = 1 - fixed_1.Fixed.directCast(coin.tradeFeeOut);
|
|
386
388
|
// pseudoin expected
|
|
@@ -429,8 +431,8 @@ CmmmCalculations.calcDepositFixedAmounts = (pool, amountsIn) => {
|
|
|
429
431
|
let prevR = r;
|
|
430
432
|
let fees = {};
|
|
431
433
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
432
|
-
balance = fixed_1.Fixed.
|
|
433
|
-
amount = fixed_1.Fixed.
|
|
434
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
435
|
+
amount = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
|
|
434
436
|
fees[coinType] =
|
|
435
437
|
r * (balance + amount) >= balance
|
|
436
438
|
? 1 - fixed_1.Fixed.directCast(coin.tradeFeeIn)
|
|
@@ -450,9 +452,9 @@ CmmmCalculations.calcDepositFixedAmounts = (pool, amountsIn) => {
|
|
|
450
452
|
sum = 0;
|
|
451
453
|
sum1 = 0;
|
|
452
454
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
453
|
-
balance = fixed_1.Fixed.
|
|
455
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
454
456
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
455
|
-
amount = fixed_1.Fixed.
|
|
457
|
+
amount = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
|
|
456
458
|
fee = fees[coinType];
|
|
457
459
|
part1 = balance + amount;
|
|
458
460
|
part2 = fee * r * part1 + balance - fee * balance;
|
|
@@ -497,15 +499,17 @@ CmmmCalculations.calcDepositFixedAmountsInitialEstimate = (pool, amountsIn) => {
|
|
|
497
499
|
let part1;
|
|
498
500
|
// start cf_max as corresponding to r = 1
|
|
499
501
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
500
|
-
balance = fixed_1.Fixed.
|
|
501
|
-
amount = fixed_1.Fixed.
|
|
502
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
503
|
+
amount = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
|
|
502
504
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
503
505
|
// this is all in so use fees in
|
|
504
506
|
part1 = balance + (1 - fixed_1.Fixed.directCast(coin.tradeFeeIn)) * amount;
|
|
505
507
|
prod += weight * Math.log(part1);
|
|
506
508
|
sum += weight * part1;
|
|
507
509
|
// r_min portion of the loop
|
|
508
|
-
r =
|
|
510
|
+
r =
|
|
511
|
+
(fixed_1.Fixed.directCast(coin.tradeFeeOut) * balance) /
|
|
512
|
+
(balance + amount);
|
|
509
513
|
rMin = Math.max(r, rMin);
|
|
510
514
|
}
|
|
511
515
|
prod = Math.exp(prod);
|
|
@@ -513,18 +517,18 @@ CmmmCalculations.calcDepositFixedAmountsInitialEstimate = (pool, amountsIn) => {
|
|
|
513
517
|
let rMax = 1;
|
|
514
518
|
let cf;
|
|
515
519
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
516
|
-
balance = fixed_1.Fixed.
|
|
520
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
517
521
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
518
|
-
amount = fixed_1.Fixed.
|
|
522
|
+
amount = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
|
|
519
523
|
r = balance / (balance + amount);
|
|
520
524
|
if (r <= rMin)
|
|
521
525
|
continue;
|
|
522
526
|
prod = 0;
|
|
523
527
|
sum = 0;
|
|
524
528
|
for (let [coinType2, coin2] of Object.entries(coins)) {
|
|
525
|
-
balance = fixed_1.Fixed.
|
|
529
|
+
balance = fixed_1.Fixed.directCast(coin2.normalizedBalance);
|
|
526
530
|
weight = fixed_1.Fixed.directCast(coin2.weight);
|
|
527
|
-
amount = fixed_1.Fixed.
|
|
531
|
+
amount = fixed_1.Fixed.castAndNormalize(coin2.decimalsScalar, amountsIn[coinType2] || BigInt(0));
|
|
528
532
|
part1 = r * (balance + amount);
|
|
529
533
|
if (part1 >= balance) {
|
|
530
534
|
// r * (B0 + Din) >= B0 so use fees in
|
|
@@ -536,8 +540,9 @@ CmmmCalculations.calcDepositFixedAmountsInitialEstimate = (pool, amountsIn) => {
|
|
|
536
540
|
else {
|
|
537
541
|
// r * (B0 + Din) < B0 so use fees out
|
|
538
542
|
part1 =
|
|
539
|
-
balance -
|
|
540
|
-
|
|
543
|
+
balance -
|
|
544
|
+
(balance - part1) /
|
|
545
|
+
fixed_1.Fixed.complement(fixed_1.Fixed.directCast(coin.tradeFeeOut));
|
|
541
546
|
}
|
|
542
547
|
prod += weight * Math.log(part1);
|
|
543
548
|
sum += weight * part1;
|
|
@@ -596,8 +601,8 @@ CmmmCalculations.calcWithdrawFlpAmountsOut = (pool, amountsOutDirection, lpRatio
|
|
|
596
601
|
shrinker /= 2;
|
|
597
602
|
let fees = {};
|
|
598
603
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
599
|
-
balance = fixed_1.Fixed.
|
|
600
|
-
amountOut = fixed_1.Fixed.
|
|
604
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
605
|
+
amountOut = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
|
|
601
606
|
fees[coinType] =
|
|
602
607
|
balance * lpc >= r * amountOut
|
|
603
608
|
? 1 - fixed_1.Fixed.directCast(coin.tradeFeeIn)
|
|
@@ -611,9 +616,9 @@ CmmmCalculations.calcWithdrawFlpAmountsOut = (pool, amountsOutDirection, lpRatio
|
|
|
611
616
|
sum1 = 0;
|
|
612
617
|
skip = false;
|
|
613
618
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
614
|
-
balance = fixed_1.Fixed.
|
|
619
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
615
620
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
616
|
-
amountOut = fixed_1.Fixed.
|
|
621
|
+
amountOut = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
|
|
617
622
|
fee = fees[coinType];
|
|
618
623
|
part1 = balance * (lpr + lpc * fee);
|
|
619
624
|
part2 = fee * r * amountOut;
|
|
@@ -651,8 +656,8 @@ CmmmCalculations.calcWithdrawFlpAmountsOut = (pool, amountsOutDirection, lpRatio
|
|
|
651
656
|
if (utils_1.Helpers.closeEnough(r, prevR, CmmmCalculations.convergenceBound)) {
|
|
652
657
|
let returner = {};
|
|
653
658
|
for (let coinType of Object.keys(coins)) {
|
|
654
|
-
returner[coinType] = fixed_1.Fixed.
|
|
655
|
-
fixed_1.Fixed.
|
|
659
|
+
returner[coinType] = fixed_1.Fixed.directUncast(r *
|
|
660
|
+
fixed_1.Fixed.directCast(amountsOutDirection[coinType] || BigInt(0)));
|
|
656
661
|
}
|
|
657
662
|
if (!CmmmCalculations.checkValidWithdraw(pool, returner, lpRatio))
|
|
658
663
|
throw Error("invalid withdraw");
|
|
@@ -693,7 +698,7 @@ CmmmCalculations.calcWithdrawFlpAmountsOutInitialEstimate = (pool, amountsOutDir
|
|
|
693
698
|
prod = 0;
|
|
694
699
|
sum = 0;
|
|
695
700
|
for (let coin of Object.values(coins)) {
|
|
696
|
-
balance = fixed_1.Fixed.
|
|
701
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
697
702
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
698
703
|
fee = fixed_1.Fixed.directCast(coin.tradeFeeIn);
|
|
699
704
|
part1 = balance * (1 + lpr * fee - fee);
|
|
@@ -706,11 +711,11 @@ CmmmCalculations.calcWithdrawFlpAmountsOutInitialEstimate = (pool, amountsOutDir
|
|
|
706
711
|
cfMin = 0;
|
|
707
712
|
tMin = Number.POSITIVE_INFINITY;
|
|
708
713
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
709
|
-
amountOut = fixed_1.Fixed.
|
|
714
|
+
amountOut = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
|
|
710
715
|
if (amountOut == 0)
|
|
711
716
|
continue;
|
|
712
717
|
t =
|
|
713
|
-
(fixed_1.Fixed.
|
|
718
|
+
(fixed_1.Fixed.directCast(coin.normalizedBalance) *
|
|
714
719
|
fixed_1.Fixed.complement(fixed_1.Fixed.directCast(coin.tradeFeeOut) * lpRatio)) /
|
|
715
720
|
amountOut;
|
|
716
721
|
if (t < tMin)
|
|
@@ -719,18 +724,18 @@ CmmmCalculations.calcWithdrawFlpAmountsOutInitialEstimate = (pool, amountsOutDir
|
|
|
719
724
|
tDrain = tMin;
|
|
720
725
|
// remaining test points are the CF discontinuities: where B0 - t*D = R*B0
|
|
721
726
|
for (let [coinTypeT, coinT] of Object.entries(coins)) {
|
|
722
|
-
amountOut = fixed_1.Fixed.
|
|
727
|
+
amountOut = fixed_1.Fixed.castAndNormalize(coinT.decimalsScalar, amountsOutDirection[coinTypeT] || BigInt(0));
|
|
723
728
|
if (amountOut == 0)
|
|
724
729
|
continue;
|
|
725
|
-
balance = fixed_1.Fixed.
|
|
730
|
+
balance = fixed_1.Fixed.directCast(coinT.normalizedBalance);
|
|
726
731
|
t = (balance * lpc) / amountOut;
|
|
727
732
|
prod = 0;
|
|
728
733
|
sum = 0;
|
|
729
734
|
keepT = true;
|
|
730
735
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
731
|
-
balance = fixed_1.Fixed.
|
|
736
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
732
737
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
733
|
-
amountOut = fixed_1.Fixed.
|
|
738
|
+
amountOut = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
|
|
734
739
|
part1 = t * amountOut;
|
|
735
740
|
if (part1 >= balance) {
|
|
736
741
|
// this t is too large to be a bound because B0 - t*D overdraws the pool
|
|
@@ -792,8 +797,8 @@ CmmmCalculations.calcAllCoinDeposit = (pool, amountsIn) => {
|
|
|
792
797
|
let s;
|
|
793
798
|
let sMin = Number.POSITIVE_INFINITY;
|
|
794
799
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
795
|
-
balance = fixed_1.Fixed.
|
|
796
|
-
amountIn = fixed_1.Fixed.
|
|
800
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
801
|
+
amountIn = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
|
|
797
802
|
s = amountIn / balance;
|
|
798
803
|
if (s < sMin)
|
|
799
804
|
sMin = s;
|
|
@@ -815,8 +820,8 @@ CmmmCalculations.calcAllCoinWithdraw = (pool, amountsOut) => {
|
|
|
815
820
|
let s;
|
|
816
821
|
let sMax = 0;
|
|
817
822
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
818
|
-
balance = fixed_1.Fixed.
|
|
819
|
-
amountOut = fixed_1.Fixed.
|
|
823
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
824
|
+
amountOut = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOut[coinType] || BigInt(0));
|
|
820
825
|
s = amountOut / balance;
|
|
821
826
|
if (s > sMax)
|
|
822
827
|
sMax = s;
|
|
@@ -934,14 +939,12 @@ CmmmCalculations.checkValidSwap = (pool, amountsIn, amountsInScalar, amountsOut,
|
|
|
934
939
|
let postprod = 0;
|
|
935
940
|
let postsum = 0;
|
|
936
941
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
937
|
-
balance = fixed_1.Fixed.
|
|
942
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
938
943
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
939
944
|
amountIn =
|
|
940
|
-
fixed_1.Fixed.
|
|
941
|
-
amountsInScalar;
|
|
945
|
+
fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0)) * amountsInScalar;
|
|
942
946
|
amountOut =
|
|
943
|
-
fixed_1.Fixed.
|
|
944
|
-
amountsOutScalar;
|
|
947
|
+
fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOut[coinType] || BigInt(0)) * amountsOutScalar;
|
|
945
948
|
if (amountIn > 0 && amountOut > 0)
|
|
946
949
|
return false;
|
|
947
950
|
feedAmountIn = amountIn * (1 - fixed_1.Fixed.directCast(coin.tradeFeeIn));
|
|
@@ -981,6 +984,8 @@ CmmmCalculations.checkValid1dSwap = (pool, coinTypeIn, coinTypeOut, amountInB, a
|
|
|
981
984
|
if (coinTypeIn == coinTypeOut)
|
|
982
985
|
return false;
|
|
983
986
|
let coins = pool.coins;
|
|
987
|
+
let coinIn = coins[coinTypeIn];
|
|
988
|
+
let coinOut = coins[coinTypeOut];
|
|
984
989
|
let flatness = fixed_1.Fixed.directCast(pool.flatness);
|
|
985
990
|
// balance = balances[i]
|
|
986
991
|
let balance;
|
|
@@ -989,13 +994,12 @@ CmmmCalculations.checkValid1dSwap = (pool, coinTypeIn, coinTypeOut, amountInB, a
|
|
|
989
994
|
// postbalance = balance + amount in - amount out
|
|
990
995
|
let postbalance;
|
|
991
996
|
let weight;
|
|
992
|
-
let amountIn = fixed_1.Fixed.
|
|
993
|
-
let amountOut = fixed_1.Fixed.
|
|
994
|
-
let feedAmountIn = amountIn * (1 - fixed_1.Fixed.directCast(
|
|
997
|
+
let amountIn = fixed_1.Fixed.castAndNormalize(coinIn.decimalsScalar, amountInB);
|
|
998
|
+
let amountOut = fixed_1.Fixed.castAndNormalize(coinOut.decimalsScalar, amountOutB);
|
|
999
|
+
let feedAmountIn = amountIn * (1 - fixed_1.Fixed.directCast(coinIn.tradeFeeIn));
|
|
995
1000
|
let feedAmountOut = amountOut == 0
|
|
996
1001
|
? 0
|
|
997
|
-
: amountOut /
|
|
998
|
-
(1 - fixed_1.Fixed.directCast(coins[coinTypeOut].tradeFeeOut));
|
|
1002
|
+
: amountOut / (1 - fixed_1.Fixed.directCast(coinOut.tradeFeeOut));
|
|
999
1003
|
let preprod = 0;
|
|
1000
1004
|
let presum = 0;
|
|
1001
1005
|
let pseudoprod = 0;
|
|
@@ -1005,7 +1009,7 @@ CmmmCalculations.checkValid1dSwap = (pool, coinTypeIn, coinTypeOut, amountInB, a
|
|
|
1005
1009
|
let p;
|
|
1006
1010
|
let s;
|
|
1007
1011
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
1008
|
-
balance = fixed_1.Fixed.
|
|
1012
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
1009
1013
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
1010
1014
|
p = weight * Math.log(balance);
|
|
1011
1015
|
s = weight * balance;
|
|
@@ -1078,9 +1082,9 @@ CmmmCalculations.checkValidDeposit = (pool, amountsIn, lpRatioRaw) => {
|
|
|
1078
1082
|
let postprod = 0;
|
|
1079
1083
|
let postsum = 0;
|
|
1080
1084
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
1081
|
-
balance = fixed_1.Fixed.
|
|
1085
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
1082
1086
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
1083
|
-
amount = fixed_1.Fixed.
|
|
1087
|
+
amount = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
|
|
1084
1088
|
postbalance = lpRatio * (balance + amount);
|
|
1085
1089
|
if (postbalance >= balance) {
|
|
1086
1090
|
// use fee in
|
|
@@ -1144,10 +1148,10 @@ CmmmCalculations.checkValidWithdraw = (pool, amountsOutSrc, lpRatio) => {
|
|
|
1144
1148
|
let postprod = 0;
|
|
1145
1149
|
let postsum = 0;
|
|
1146
1150
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
1147
|
-
balance = fixed_1.Fixed.
|
|
1151
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
1148
1152
|
scaledBalance = lpRatio * balance;
|
|
1149
1153
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
1150
|
-
amount = fixed_1.Fixed.
|
|
1154
|
+
amount = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOutSrc[coinType] || BigInt(0));
|
|
1151
1155
|
if (amount > scaledBalance + 1)
|
|
1152
1156
|
return false;
|
|
1153
1157
|
postbalance = balance - amount;
|
|
@@ -1210,9 +1214,9 @@ CmmmCalculations.getEstimateSwapFixedIn = (pool, amountsIn, amountsOutDirection)
|
|
|
1210
1214
|
let inDotGrad = 0;
|
|
1211
1215
|
let outDotGrad = 0;
|
|
1212
1216
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
1213
|
-
balance = fixed_1.Fixed.
|
|
1214
|
-
amountIn = fixed_1.Fixed.
|
|
1215
|
-
amountOut = fixed_1.Fixed.
|
|
1217
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
1218
|
+
amountIn = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
|
|
1219
|
+
amountOut = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
|
|
1216
1220
|
grad =
|
|
1217
1221
|
amountIn == 0
|
|
1218
1222
|
? (fixed_1.Fixed.directCast(coin.weight) *
|
|
@@ -1245,9 +1249,9 @@ CmmmCalculations.getEstimateSwapFixedOut = (pool, amountsInDirection, amountsOut
|
|
|
1245
1249
|
let inDotGrad = 0;
|
|
1246
1250
|
let outDotGrad = 0;
|
|
1247
1251
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
1248
|
-
balance = fixed_1.Fixed.
|
|
1249
|
-
amountIn = fixed_1.Fixed.
|
|
1250
|
-
amountOut = fixed_1.Fixed.
|
|
1252
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
1253
|
+
amountIn = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsInDirection[coinType] || BigInt(0));
|
|
1254
|
+
amountOut = fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOut[coinType] || BigInt(0));
|
|
1251
1255
|
grad =
|
|
1252
1256
|
amountIn == 0
|
|
1253
1257
|
? (fixed_1.Fixed.directCast(coin.weight) *
|
|
@@ -1287,11 +1291,11 @@ CmmmCalculations.getEstimateDepositFixedAmounts = (pool, amountsIn) => {
|
|
|
1287
1291
|
let grad;
|
|
1288
1292
|
let scaledAmount;
|
|
1289
1293
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
1290
|
-
balance = fixed_1.Fixed.
|
|
1294
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
1291
1295
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
1292
1296
|
amount =
|
|
1293
1297
|
balance +
|
|
1294
|
-
fixed_1.Fixed.
|
|
1298
|
+
fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
|
|
1295
1299
|
scaledAmount = amount * r0;
|
|
1296
1300
|
grad =
|
|
1297
1301
|
scaledAmount < balance
|
|
@@ -1335,11 +1339,11 @@ CmmmCalculations.getEstimateWithdrawFlpAmountsOut = (pool, amountsOutDirection,
|
|
|
1335
1339
|
let amount;
|
|
1336
1340
|
let grad;
|
|
1337
1341
|
for (let [coinType, coin] of Object.entries(coins)) {
|
|
1338
|
-
balance = fixed_1.Fixed.
|
|
1342
|
+
balance = fixed_1.Fixed.directCast(coin.normalizedBalance);
|
|
1339
1343
|
weight = fixed_1.Fixed.directCast(coin.weight);
|
|
1340
1344
|
amount =
|
|
1341
1345
|
balance +
|
|
1342
|
-
fixed_1.Fixed.
|
|
1346
|
+
fixed_1.Fixed.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
|
|
1343
1347
|
scaledAmount = amount * r0;
|
|
1344
1348
|
grad =
|
|
1345
1349
|
scaledAmount < balance
|
|
@@ -495,13 +495,12 @@ RouterGraph.findCompleteRoutes = (graph, routes, coinOut, maxRouteLength, isGive
|
|
|
495
495
|
for (const [coinOut, throughPools] of Object.entries(graph.coinNodes[lastPath.coinOut.type]
|
|
496
496
|
.coinOutThroughPoolEdges)) {
|
|
497
497
|
for (const poolUid of throughPools) {
|
|
498
|
-
if (
|
|
499
|
-
//
|
|
500
|
-
//
|
|
501
|
-
|
|
502
|
-
//
|
|
503
|
-
|
|
504
|
-
lastPath.poolUid === poolUid)
|
|
498
|
+
if (route.paths.some(
|
|
499
|
+
// NOTE: would it ever make sense to go back into a pool ?
|
|
500
|
+
// (could relax this restriction)
|
|
501
|
+
(path) => path.poolUid === poolUid)
|
|
502
|
+
// lastPath.poolUid === poolUid
|
|
503
|
+
)
|
|
505
504
|
continue;
|
|
506
505
|
if (!(poolUid in graph.pools))
|
|
507
506
|
continue;
|