@heliofi/launchpad-common 1.1.2 → 1.1.4
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/src/domain/constants/curveType.d.ts +3 -1
- package/dist/src/domain/constants/curveType.js +2 -0
- package/dist/src/domain/constants/curveType.js.map +1 -1
- package/dist/src/domain/constants/solMintAddress.d.ts +2 -0
- package/dist/src/domain/constants/solMintAddress.js +3 -1
- package/dist/src/domain/constants/solMintAddress.js.map +1 -1
- package/dist/src/domain/curves/BaseConstantProductCurveV1.d.ts +4 -18
- package/dist/src/domain/curves/BaseConstantProductCurveV1.js +12 -101
- package/dist/src/domain/curves/BaseConstantProductCurveV1.js.map +1 -1
- package/dist/src/domain/curves/ConstantProductCurve.d.ts +20 -0
- package/dist/src/domain/curves/ConstantProductCurve.js +120 -0
- package/dist/src/domain/curves/ConstantProductCurve.js.map +1 -0
- package/dist/src/domain/curves/ConstantProductCurveV1.d.ts +3 -19
- package/dist/src/domain/curves/ConstantProductCurveV1.js +8 -119
- package/dist/src/domain/curves/ConstantProductCurveV1.js.map +1 -1
- package/dist/src/domain/curves/ConstantProductCurveV2.d.ts +7 -0
- package/dist/src/domain/curves/ConstantProductCurveV2.js +31 -0
- package/dist/src/domain/curves/ConstantProductCurveV2.js.map +1 -0
- package/dist/src/domain/curves/FlatCurveV1.d.ts +15 -0
- package/dist/src/domain/curves/FlatCurveV1.js +52 -0
- package/dist/src/domain/curves/FlatCurveV1.js.map +1 -0
- package/dist/src/domain/curves/index.d.ts +1 -0
- package/dist/src/domain/curves/index.js +1 -0
- package/dist/src/domain/curves/index.js.map +1 -1
- package/dist/src/domain/curves/tests/BaseConstantProductCurveV1.spec.js +29 -0
- package/dist/src/domain/curves/tests/BaseConstantProductCurveV1.spec.js.map +1 -1
- package/dist/src/domain/curves/tests/FlatCurveV1.spec.d.ts +1 -0
- package/dist/src/domain/curves/tests/FlatCurveV1.spec.js +126 -0
- package/dist/src/domain/curves/tests/FlatCurveV1.spec.js.map +1 -0
- package/dist/src/domain/curves/utils.d.ts +3 -0
- package/dist/src/domain/curves/utils.js +16 -0
- package/dist/src/domain/curves/utils.js.map +1 -0
- package/dist/src/domain/model/airlock/entities/TotalClaimedSOLResponse.entity.d.ts +4 -0
- package/dist/src/domain/model/airlock/entities/TotalClaimedSOLResponse.entity.js +8 -0
- package/dist/src/domain/model/airlock/entities/TotalClaimedSOLResponse.entity.js.map +1 -0
- package/dist/src/domain/model/airlock/entities/TotalClaimedUSDResponse.entity.d.ts +4 -0
- package/dist/src/domain/model/airlock/entities/TotalClaimedUSDResponse.entity.js +8 -0
- package/dist/src/domain/model/airlock/entities/TotalClaimedUSDResponse.entity.js.map +1 -0
- package/dist/src/domain/model/airlock/entities/index.d.ts +2 -1
- package/dist/src/domain/model/airlock/entities/index.js +2 -1
- package/dist/src/domain/model/airlock/entities/index.js.map +1 -1
- package/dist/src/domain/model/mint/create/dtos/CreateMint.dto.d.ts +2 -1
- package/dist/src/domain/model/mint/create/dtos/CreateMint.dto.js +5 -0
- package/dist/src/domain/model/mint/create/dtos/CreateMint.dto.js.map +1 -1
- package/dist/src/domain/model/uniswap/dtos/UniswapGetPrice.dto.d.ts +4 -2
- package/dist/src/domain/model/uniswap/dtos/UniswapGetPrice.dto.js +13 -6
- package/dist/src/domain/model/uniswap/dtos/UniswapGetPrice.dto.js.map +1 -1
- package/dist/src/domain/model/uniswap/dtos/UniswapPrepare.dto.d.ts +1 -1
- package/dist/src/domain/model/uniswap/dtos/UniswapPrepare.dto.js +5 -5
- package/dist/src/domain/model/uniswap/dtos/UniswapPrepare.dto.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export declare enum CurveType {
|
|
2
2
|
LINEAR_V1 = "LINEAR_V1",
|
|
3
3
|
CONSTANT_PRODUCT_V1 = "CONSTANT_PRODUCT_V1",
|
|
4
|
-
BASE_CONSTANT_PRODUCT_V1 = "BASE_CONSTANT_PRODUCT_V1"
|
|
4
|
+
BASE_CONSTANT_PRODUCT_V1 = "BASE_CONSTANT_PRODUCT_V1",
|
|
5
|
+
CONSTANT_PRODUCT_V2 = "CONSTANT_PRODUCT_V2",
|
|
6
|
+
FLAT_V1 = "FLAT_V1"
|
|
5
7
|
}
|
|
@@ -6,5 +6,7 @@ var CurveType;
|
|
|
6
6
|
CurveType["LINEAR_V1"] = "LINEAR_V1";
|
|
7
7
|
CurveType["CONSTANT_PRODUCT_V1"] = "CONSTANT_PRODUCT_V1";
|
|
8
8
|
CurveType["BASE_CONSTANT_PRODUCT_V1"] = "BASE_CONSTANT_PRODUCT_V1";
|
|
9
|
+
CurveType["CONSTANT_PRODUCT_V2"] = "CONSTANT_PRODUCT_V2";
|
|
10
|
+
CurveType["FLAT_V1"] = "FLAT_V1";
|
|
9
11
|
})(CurveType = exports.CurveType || (exports.CurveType = {}));
|
|
10
12
|
//# sourceMappingURL=curveType.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"curveType.js","sourceRoot":"","sources":["../../../../src/domain/constants/curveType.ts"],"names":[],"mappings":";;;AAAA,IAAY,
|
|
1
|
+
{"version":3,"file":"curveType.js","sourceRoot":"","sources":["../../../../src/domain/constants/curveType.ts"],"names":[],"mappings":";;;AAAA,IAAY,SAMX;AAND,WAAY,SAAS;IACnB,oCAAuB,CAAA;IACvB,wDAA2C,CAAA;IAC3C,kEAAqD,CAAA;IACrD,wDAA2C,CAAA;IAC3C,gCAAmB,CAAA;AACrB,CAAC,EANW,SAAS,GAAT,iBAAS,KAAT,iBAAS,QAMpB"}
|
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
export declare const SOL_MINT = "11111111111111111111111111111111";
|
|
2
2
|
export declare const WRAPPED_SOL_MINT = "So11111111111111111111111111111111111111112";
|
|
3
|
+
export declare const SOLANA_USDC_MAINNET_MINT = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
|
|
4
|
+
export declare const SOLANA_USDC_DEVNET_MINT = "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU";
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WRAPPED_SOL_MINT = exports.SOL_MINT = void 0;
|
|
3
|
+
exports.SOLANA_USDC_DEVNET_MINT = exports.SOLANA_USDC_MAINNET_MINT = exports.WRAPPED_SOL_MINT = exports.SOL_MINT = void 0;
|
|
4
4
|
exports.SOL_MINT = '11111111111111111111111111111111';
|
|
5
5
|
exports.WRAPPED_SOL_MINT = 'So11111111111111111111111111111111111111112';
|
|
6
|
+
exports.SOLANA_USDC_MAINNET_MINT = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v';
|
|
7
|
+
exports.SOLANA_USDC_DEVNET_MINT = '4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU';
|
|
6
8
|
//# sourceMappingURL=solMintAddress.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"solMintAddress.js","sourceRoot":"","sources":["../../../../src/domain/constants/solMintAddress.ts"],"names":[],"mappings":";;;AAAa,QAAA,QAAQ,GAAG,kCAAkC,CAAC;AAC9C,QAAA,gBAAgB,GAAG,6CAA6C,CAAC"}
|
|
1
|
+
{"version":3,"file":"solMintAddress.js","sourceRoot":"","sources":["../../../../src/domain/constants/solMintAddress.ts"],"names":[],"mappings":";;;AAAa,QAAA,QAAQ,GAAG,kCAAkC,CAAC;AAC9C,QAAA,gBAAgB,GAAG,6CAA6C,CAAC;AAEjE,QAAA,wBAAwB,GACnC,8CAA8C,CAAC;AACpC,QAAA,uBAAuB,GAClC,8CAA8C,CAAC"}
|
|
@@ -1,23 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export declare class BaseConstantProductCurveV1 extends AbstractCurve {
|
|
5
|
-
initialVirtualTokenReserves: bigint;
|
|
6
|
-
initialVirtualCollateralReserves: bigint;
|
|
7
|
-
collateralDecimals: number;
|
|
8
|
-
constantProduct: bigint;
|
|
1
|
+
import { ConstantProductCurve } from './ConstantProductCurve';
|
|
2
|
+
import { GetMarketCapOptions } from './types';
|
|
3
|
+
export declare class BaseConstantProductCurveV1 extends ConstantProductCurve {
|
|
9
4
|
dynamicThreshold: number;
|
|
10
5
|
maxThreshold: number;
|
|
11
6
|
marketCapToMinimalTokens: Map<bigint, bigint>;
|
|
12
|
-
|
|
13
|
-
getTokensAmountFromCollateral(options: CalculationOptions): bigint;
|
|
14
|
-
getCollateralAmountFromTokens(options: CalculationOptions): bigint;
|
|
15
|
-
getCollateralPrice(options: GetPriceOptions): BigNumber;
|
|
7
|
+
constructor();
|
|
16
8
|
getMarketCap(options: GetMarketCapOptions): bigint;
|
|
17
|
-
getDynamicThresholdFromMarketCap(marketCapThreshold: bigint): bigint;
|
|
18
|
-
private buyInToken;
|
|
19
|
-
private buyInCollateral;
|
|
20
|
-
private sellInToken;
|
|
21
|
-
private sellInCollateral;
|
|
22
|
-
private getCurrentReserves;
|
|
23
9
|
}
|
|
@@ -5,21 +5,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.BaseConstantProductCurveV1 = void 0;
|
|
7
7
|
const bigNumber_1 = __importDefault(require("../../config/bigNumber"));
|
|
8
|
-
const AbstractCurve_1 = require("./AbstractCurve");
|
|
9
8
|
const constants_1 = require("../constants");
|
|
10
|
-
const
|
|
11
|
-
class BaseConstantProductCurveV1 extends
|
|
9
|
+
const ConstantProductCurve_1 = require("./ConstantProductCurve");
|
|
10
|
+
class BaseConstantProductCurveV1 extends ConstantProductCurve_1.ConstantProductCurve {
|
|
12
11
|
constructor() {
|
|
13
|
-
super(
|
|
14
|
-
this.initialVirtualTokenReserves = BigInt(1.06 * 1e27);
|
|
15
|
-
this.initialVirtualCollateralReserves = BigInt(1.6 * 1e18);
|
|
16
|
-
this.collateralDecimals = 18;
|
|
17
|
-
this.constantProduct = this.initialVirtualTokenReserves * this.initialVirtualCollateralReserves;
|
|
12
|
+
super();
|
|
18
13
|
this.dynamicThreshold = 80;
|
|
19
14
|
this.maxThreshold = 82;
|
|
20
15
|
this.marketCapToMinimalTokens = new Map([
|
|
21
|
-
[BigInt(
|
|
16
|
+
[BigInt(2.5 * 1e18), 236349588721039974800000000n],
|
|
17
|
+
[BigInt(25 * 1e18), 799538870462404697804703491n],
|
|
22
18
|
]);
|
|
19
|
+
this.initialVirtualTokenReserves = BigInt(1.06 * 1e27);
|
|
20
|
+
this.initialVirtualCollateralReserves = BigInt(1.6 * 1e18);
|
|
21
|
+
this.collateralDecimals = 18;
|
|
22
|
+
this.constantProduct =
|
|
23
|
+
this.initialVirtualTokenReserves * this.initialVirtualCollateralReserves;
|
|
23
24
|
this.curveDefaults = {
|
|
24
25
|
type: constants_1.CurveType.BASE_CONSTANT_PRODUCT_V1,
|
|
25
26
|
totalSupply: BigInt(1e27),
|
|
@@ -29,113 +30,23 @@ class BaseConstantProductCurveV1 extends AbstractCurve_1.AbstractCurve {
|
|
|
29
30
|
address: '0x0000000000000000000000000000000000000000',
|
|
30
31
|
};
|
|
31
32
|
}
|
|
32
|
-
getTokensAmountFromCollateral(options) {
|
|
33
|
-
const { amount, curvePosition, tradeDirection, platformFeeBps } = options;
|
|
34
|
-
if (tradeDirection === 'BUY') {
|
|
35
|
-
const collateralAmount = amount - (amount * BigInt(platformFeeBps)) / 10000n;
|
|
36
|
-
return this.buyInCollateral(collateralAmount, curvePosition);
|
|
37
|
-
}
|
|
38
|
-
const collateralAmount = amount + (amount * BigInt(platformFeeBps)) / 10000n;
|
|
39
|
-
return this.sellInCollateral(collateralAmount, curvePosition);
|
|
40
|
-
}
|
|
41
|
-
getCollateralAmountFromTokens(options) {
|
|
42
|
-
const { amount, curvePosition, tradeDirection, platformFeeBps } = options;
|
|
43
|
-
if (tradeDirection === 'BUY') {
|
|
44
|
-
const collateralAmount = this.buyInToken(amount, curvePosition);
|
|
45
|
-
return (collateralAmount + (collateralAmount * BigInt(platformFeeBps)) / 10000n);
|
|
46
|
-
}
|
|
47
|
-
const collateralAmount = this.sellInToken(amount, curvePosition);
|
|
48
|
-
return (collateralAmount - (collateralAmount * BigInt(platformFeeBps)) / 10000n);
|
|
49
|
-
}
|
|
50
|
-
getCollateralPrice(options) {
|
|
51
|
-
const { curvePosition, collateralDecimals } = options;
|
|
52
|
-
const finalCollateralDecimals = collateralDecimals ?? this.collateralDecimals;
|
|
53
|
-
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
54
|
-
const baseCollateral = BigInt(10 ** finalCollateralDecimals);
|
|
55
|
-
const price = new bigNumber_1.default(currentVirtualCollateralReserves.toString()).div(new bigNumber_1.default(currentVirtualTokenReserves.toString()));
|
|
56
|
-
const inBaseUnits = price.multipliedBy(new bigNumber_1.default(baseCollateral.toString()));
|
|
57
|
-
return inBaseUnits;
|
|
58
|
-
}
|
|
59
33
|
getMarketCap(options) {
|
|
60
34
|
try {
|
|
61
35
|
const { curvePosition, tokenDecimals, collateralDecimals } = options;
|
|
62
36
|
const base = new bigNumber_1.default(10);
|
|
63
37
|
const baseTokens = base.pow(tokenDecimals ?? this.curveDefaults.tokenDecimals);
|
|
64
|
-
const curvePositionDecimal = new bigNumber_1.default(curvePosition.toString());
|
|
65
38
|
const price = this.getCollateralPrice({
|
|
66
39
|
curvePosition,
|
|
67
40
|
collateralDecimals,
|
|
68
41
|
});
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
.dividedBy(baseTokens);
|
|
42
|
+
const totalSupplyDecimal = new bigNumber_1.default(this.curveDefaults.totalSupply.toString());
|
|
43
|
+
const cap = price.multipliedBy(totalSupplyDecimal).dividedBy(baseTokens);
|
|
72
44
|
return BigInt(cap.integerValue(bigNumber_1.default.ROUND_FLOOR).toString());
|
|
73
45
|
}
|
|
74
46
|
catch (error) {
|
|
75
47
|
throw new Error(`Calculation error: ${error}`);
|
|
76
48
|
}
|
|
77
49
|
}
|
|
78
|
-
getDynamicThresholdFromMarketCap(marketCapThreshold) {
|
|
79
|
-
const baseTokens = BigInt(10 ** this.curveDefaults.tokenDecimals);
|
|
80
|
-
let low = 300000000n * baseTokens;
|
|
81
|
-
let high = 1000000000n * baseTokens;
|
|
82
|
-
let mid;
|
|
83
|
-
while (low < high) {
|
|
84
|
-
mid = (low + high + 1n) / 2n;
|
|
85
|
-
const marketCap = this.getMarketCap({ curvePosition: mid });
|
|
86
|
-
if (marketCap === marketCapThreshold) {
|
|
87
|
-
low = mid;
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
else if (marketCap < marketCapThreshold) {
|
|
91
|
-
low = mid;
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
high = mid - 1n;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
while (this.getMarketCap({ curvePosition: low }) >= marketCapThreshold &&
|
|
98
|
-
low > 0n) {
|
|
99
|
-
low -= 1n;
|
|
100
|
-
}
|
|
101
|
-
return low + 1n;
|
|
102
|
-
}
|
|
103
|
-
buyInToken(tokenAmount, curvePosition) {
|
|
104
|
-
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
105
|
-
const newTokenReserves = currentVirtualTokenReserves - tokenAmount;
|
|
106
|
-
const ratio = this.constantProduct / newTokenReserves;
|
|
107
|
-
const lamportsToSpend = ratio - currentVirtualCollateralReserves;
|
|
108
|
-
return lamportsToSpend;
|
|
109
|
-
}
|
|
110
|
-
buyInCollateral(collateralAmount, curvePosition) {
|
|
111
|
-
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
112
|
-
const newCollateralReserves = currentVirtualCollateralReserves + collateralAmount;
|
|
113
|
-
const ratio = this.constantProduct / newCollateralReserves;
|
|
114
|
-
const tokensToBuy = currentVirtualTokenReserves - ratio;
|
|
115
|
-
return tokensToBuy;
|
|
116
|
-
}
|
|
117
|
-
sellInToken(tokenAmount, curvePosition) {
|
|
118
|
-
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
119
|
-
const newTokenReserves = currentVirtualTokenReserves + tokenAmount;
|
|
120
|
-
const ratio = this.constantProduct / newTokenReserves;
|
|
121
|
-
const lamportsToReceive = currentVirtualCollateralReserves - ratio;
|
|
122
|
-
return lamportsToReceive;
|
|
123
|
-
}
|
|
124
|
-
sellInCollateral(collateralAmount, curvePosition) {
|
|
125
|
-
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
126
|
-
const newCollateralReserves = currentVirtualCollateralReserves - collateralAmount;
|
|
127
|
-
const ratio = this.constantProduct / newCollateralReserves;
|
|
128
|
-
const tokensToSell = ratio - currentVirtualTokenReserves;
|
|
129
|
-
return tokensToSell;
|
|
130
|
-
}
|
|
131
|
-
getCurrentReserves(curvePosition) {
|
|
132
|
-
const currentVirtualTokenReserves = this.initialVirtualTokenReserves - curvePosition;
|
|
133
|
-
const currentVirtualCollateralReserves = (0, bigNumber_1.default)(this.constantProduct.toString()).div((0, bigNumber_1.default)(currentVirtualTokenReserves.toString()));
|
|
134
|
-
return [
|
|
135
|
-
currentVirtualTokenReserves,
|
|
136
|
-
services_1.AmountService.decimalToBigInt(currentVirtualCollateralReserves),
|
|
137
|
-
];
|
|
138
|
-
}
|
|
139
50
|
}
|
|
140
51
|
exports.BaseConstantProductCurveV1 = BaseConstantProductCurveV1;
|
|
141
52
|
//# sourceMappingURL=BaseConstantProductCurveV1.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseConstantProductCurveV1.js","sourceRoot":"","sources":["../../../../src/domain/curves/BaseConstantProductCurveV1.ts"],"names":[],"mappings":";;;;;;AAAA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;YACF,MAAM,EAAE,aAAa,EAAE,aAAa,EAAE,kBAAkB,EAAE,GAAG,OAAO,CAAC;YACrE,MAAM,IAAI,GAAG,IAAI,mBAAS,CAAC,EAAE,CAAC,CAAC;YAC/B,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CACzB,aAAa,IAAI,IAAI,CAAC,aAAa,CAAC,aAAa,CAClD,CAAC;YAEF,MAAM,KAAK,GAAG,IAAI,CAAC,kBAAkB,CAAC;gBACpC,aAAa;gBACb,kBAAkB;aACnB,CAAC,CAAC;YAEH,MAAM,kBAAkB,GAAG,IAAI,mBAAS,CACtC,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,QAAQ,EAAE,CAC1C,CAAC;YACF,MAAM,GAAG,GAAG,KAAK,CAAC,YAAY,CAAC,kBAAkB,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;YAEzE,OAAO,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,mBAAS,CAAC,WAAW,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;SACnE;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,IAAI,KAAK,CAAC,sBAAsB,KAAK,EAAE,CAAC,CAAC;SAChD;IACH,CAAC;CACF;AAtDD,gEAsDC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import BigNumber from '../../config/bigNumber';
|
|
2
|
+
import { AbstractCurve } from './AbstractCurve';
|
|
3
|
+
import { CalculationOptions, CurveDefaults, GetMarketCapOptions, GetPriceOptions } from './types';
|
|
4
|
+
export declare class ConstantProductCurve extends AbstractCurve {
|
|
5
|
+
curveDefaults: CurveDefaults;
|
|
6
|
+
protected initialVirtualCollateralReserves: bigint;
|
|
7
|
+
protected initialVirtualTokenReserves: bigint;
|
|
8
|
+
protected collateralDecimals: number;
|
|
9
|
+
protected constantProduct: bigint;
|
|
10
|
+
getTokensAmountFromCollateral(options: CalculationOptions): bigint;
|
|
11
|
+
getCollateralAmountFromTokens(options: CalculationOptions): bigint;
|
|
12
|
+
getCollateralPrice(options: GetPriceOptions): BigNumber;
|
|
13
|
+
getMarketCap(options: GetMarketCapOptions): bigint;
|
|
14
|
+
getDynamicThresholdFromMarketCap(marketCapThreshold: bigint): bigint;
|
|
15
|
+
private buyInToken;
|
|
16
|
+
private buyInCollateral;
|
|
17
|
+
private sellInToken;
|
|
18
|
+
private sellInCollateral;
|
|
19
|
+
private getCurrentReserves;
|
|
20
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ConstantProductCurve = void 0;
|
|
7
|
+
const bigNumber_1 = __importDefault(require("../../config/bigNumber"));
|
|
8
|
+
const AbstractCurve_1 = require("./AbstractCurve");
|
|
9
|
+
const services_1 = require("../services");
|
|
10
|
+
class ConstantProductCurve extends AbstractCurve_1.AbstractCurve {
|
|
11
|
+
getTokensAmountFromCollateral(options) {
|
|
12
|
+
const { amount, curvePosition, tradeDirection, platformFeeBps } = options;
|
|
13
|
+
if (tradeDirection === 'BUY') {
|
|
14
|
+
const collateralAmount = amount - (amount * BigInt(platformFeeBps)) / 10000n;
|
|
15
|
+
return this.buyInCollateral(collateralAmount, curvePosition);
|
|
16
|
+
}
|
|
17
|
+
const collateralAmount = amount + (amount * BigInt(platformFeeBps)) / 10000n;
|
|
18
|
+
return this.sellInCollateral(collateralAmount, curvePosition);
|
|
19
|
+
}
|
|
20
|
+
getCollateralAmountFromTokens(options) {
|
|
21
|
+
const { amount, curvePosition, tradeDirection, platformFeeBps } = options;
|
|
22
|
+
if (tradeDirection === 'BUY') {
|
|
23
|
+
const collateralAmount = this.buyInToken(amount, curvePosition);
|
|
24
|
+
return (collateralAmount + (collateralAmount * BigInt(platformFeeBps)) / 10000n);
|
|
25
|
+
}
|
|
26
|
+
const collateralAmount = this.sellInToken(amount, curvePosition);
|
|
27
|
+
return (collateralAmount - (collateralAmount * BigInt(platformFeeBps)) / 10000n);
|
|
28
|
+
}
|
|
29
|
+
getCollateralPrice(options) {
|
|
30
|
+
const { curvePosition, collateralDecimals } = options;
|
|
31
|
+
const finalCollateralDecimals = collateralDecimals ?? this.collateralDecimals;
|
|
32
|
+
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
33
|
+
const baseCollateral = BigInt(10 ** finalCollateralDecimals);
|
|
34
|
+
const price = new bigNumber_1.default(currentVirtualCollateralReserves.toString()).div(new bigNumber_1.default(currentVirtualTokenReserves.toString()));
|
|
35
|
+
const inBaseUnits = price.multipliedBy(new bigNumber_1.default(baseCollateral.toString()));
|
|
36
|
+
return inBaseUnits;
|
|
37
|
+
}
|
|
38
|
+
getMarketCap(options) {
|
|
39
|
+
try {
|
|
40
|
+
const { curvePosition, tokenDecimals, collateralDecimals } = options;
|
|
41
|
+
const base = new bigNumber_1.default(10);
|
|
42
|
+
const baseTokens = base.pow(tokenDecimals ?? this.curveDefaults.tokenDecimals);
|
|
43
|
+
const curvePositionDecimal = new bigNumber_1.default(curvePosition.toString());
|
|
44
|
+
const price = this.getCollateralPrice({
|
|
45
|
+
curvePosition,
|
|
46
|
+
collateralDecimals,
|
|
47
|
+
});
|
|
48
|
+
const cap = price
|
|
49
|
+
.multipliedBy(curvePositionDecimal)
|
|
50
|
+
.dividedBy(baseTokens);
|
|
51
|
+
return BigInt(cap.integerValue(bigNumber_1.default.ROUND_FLOOR).toString());
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
throw new Error(`Calculation error: ${error}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
getDynamicThresholdFromMarketCap(marketCapThreshold) {
|
|
58
|
+
const baseTokens = BigInt(10 ** this.curveDefaults.tokenDecimals);
|
|
59
|
+
let low = 300000000n * baseTokens;
|
|
60
|
+
let high = 1000000000n * baseTokens;
|
|
61
|
+
let mid;
|
|
62
|
+
while (low < high) {
|
|
63
|
+
mid = (low + high + 1n) / 2n;
|
|
64
|
+
const marketCap = this.getMarketCap({ curvePosition: mid });
|
|
65
|
+
if (marketCap === marketCapThreshold) {
|
|
66
|
+
low = mid;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
else if (marketCap < marketCapThreshold) {
|
|
70
|
+
low = mid;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
high = mid - 1n;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
while (this.getMarketCap({ curvePosition: low }) >= marketCapThreshold &&
|
|
77
|
+
low > 0n) {
|
|
78
|
+
low -= 1n;
|
|
79
|
+
}
|
|
80
|
+
return low + 1n;
|
|
81
|
+
}
|
|
82
|
+
buyInToken(tokenAmount, curvePosition) {
|
|
83
|
+
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
84
|
+
const newTokenReserves = currentVirtualTokenReserves - tokenAmount;
|
|
85
|
+
const ratio = this.constantProduct / newTokenReserves;
|
|
86
|
+
const lamportsToSpend = ratio - currentVirtualCollateralReserves;
|
|
87
|
+
return lamportsToSpend;
|
|
88
|
+
}
|
|
89
|
+
buyInCollateral(collateralAmount, curvePosition) {
|
|
90
|
+
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
91
|
+
const newCollateralReserves = currentVirtualCollateralReserves + collateralAmount;
|
|
92
|
+
const ratio = this.constantProduct / newCollateralReserves;
|
|
93
|
+
const tokensToBuy = currentVirtualTokenReserves - ratio;
|
|
94
|
+
return tokensToBuy;
|
|
95
|
+
}
|
|
96
|
+
sellInToken(tokenAmount, curvePosition) {
|
|
97
|
+
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
98
|
+
const newTokenReserves = currentVirtualTokenReserves + tokenAmount;
|
|
99
|
+
const ratio = this.constantProduct / newTokenReserves;
|
|
100
|
+
const lamportsToReceive = currentVirtualCollateralReserves - ratio;
|
|
101
|
+
return lamportsToReceive;
|
|
102
|
+
}
|
|
103
|
+
sellInCollateral(collateralAmount, curvePosition) {
|
|
104
|
+
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
105
|
+
const newCollateralReserves = currentVirtualCollateralReserves - collateralAmount;
|
|
106
|
+
const ratio = this.constantProduct / newCollateralReserves;
|
|
107
|
+
const tokensToSell = ratio - currentVirtualTokenReserves;
|
|
108
|
+
return tokensToSell;
|
|
109
|
+
}
|
|
110
|
+
getCurrentReserves(curvePosition) {
|
|
111
|
+
const currentVirtualTokenReserves = this.initialVirtualTokenReserves - curvePosition;
|
|
112
|
+
const currentVirtualCollateralReserves = (0, bigNumber_1.default)(this.constantProduct.toString()).div((0, bigNumber_1.default)(currentVirtualTokenReserves.toString()));
|
|
113
|
+
return [
|
|
114
|
+
currentVirtualTokenReserves,
|
|
115
|
+
services_1.AmountService.decimalToBigInt(currentVirtualCollateralReserves),
|
|
116
|
+
];
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
exports.ConstantProductCurve = ConstantProductCurve;
|
|
120
|
+
//# sourceMappingURL=ConstantProductCurve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConstantProductCurve.js","sourceRoot":"","sources":["../../../../src/domain/curves/ConstantProductCurve.ts"],"names":[],"mappings":";;;;;;AAAA,uEAA+C;AAC/C,mDAAgD;AAOhD,0CAA4C;AAE5C,MAAa,oBAAqB,SAAQ,6BAAa;IAWrD,6BAA6B,CAAC,OAA2B;QACvD,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC;QAC1E,IAAI,cAAc,KAAK,KAAK,EAAE;YAC5B,MAAM,gBAAgB,GACpB,MAAM,GAAG,CAAC,MAAM,GAAG,MAA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3
|
-
import { CalculationOptions, CurveDefaults, GetMarketCapOptions, GetPriceOptions } from './types';
|
|
4
|
-
export declare class ConstantProductCurveV1 extends AbstractCurve {
|
|
5
|
-
initialVirtualTokenReserves: bigint;
|
|
6
|
-
initialVirtualCollateralReserves: bigint;
|
|
7
|
-
collateralDecimals: number;
|
|
8
|
-
constantProduct: bigint;
|
|
1
|
+
import { ConstantProductCurve } from './ConstantProductCurve';
|
|
2
|
+
export declare class ConstantProductCurveV1 extends ConstantProductCurve {
|
|
9
3
|
dynamicThreshold: number;
|
|
10
4
|
maxThreshold: number;
|
|
11
5
|
marketCapToMinimalTokens: Map<bigint, bigint>;
|
|
12
|
-
|
|
13
|
-
getTokensAmountFromCollateral(options: CalculationOptions): bigint;
|
|
14
|
-
getCollateralAmountFromTokens(options: CalculationOptions): bigint;
|
|
15
|
-
getCollateralPrice(options: GetPriceOptions): BigNumber;
|
|
16
|
-
getMarketCap(options: GetMarketCapOptions): bigint;
|
|
17
|
-
getDynamicThresholdFromMarketCap(marketCapThreshold: bigint): bigint;
|
|
18
|
-
private buyInToken;
|
|
19
|
-
private buyInCollateral;
|
|
20
|
-
private sellInToken;
|
|
21
|
-
private sellInCollateral;
|
|
22
|
-
private getCurrentReserves;
|
|
6
|
+
constructor();
|
|
23
7
|
}
|
|
@@ -1,26 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.ConstantProductCurveV1 = void 0;
|
|
7
|
-
const bigNumber_1 = __importDefault(require("../../config/bigNumber"));
|
|
8
|
-
const AbstractCurve_1 = require("./AbstractCurve");
|
|
9
4
|
const constants_1 = require("../constants");
|
|
10
|
-
const
|
|
11
|
-
class ConstantProductCurveV1 extends
|
|
5
|
+
const ConstantProductCurve_1 = require("./ConstantProductCurve");
|
|
6
|
+
class ConstantProductCurveV1 extends ConstantProductCurve_1.ConstantProductCurve {
|
|
12
7
|
constructor() {
|
|
13
|
-
super(
|
|
14
|
-
this.initialVirtualTokenReserves = 1073000000000000000n;
|
|
15
|
-
this.initialVirtualCollateralReserves = 30000000000n;
|
|
16
|
-
this.collateralDecimals = 9;
|
|
17
|
-
this.constantProduct = this.initialVirtualTokenReserves * this.initialVirtualCollateralReserves;
|
|
8
|
+
super();
|
|
18
9
|
this.dynamicThreshold = 80;
|
|
19
10
|
this.maxThreshold = 82;
|
|
20
11
|
this.marketCapToMinimalTokens = new Map([
|
|
21
12
|
[345000000000n, 799820983207404442n],
|
|
22
13
|
[20000000000n, 336771070069821177n],
|
|
23
14
|
]);
|
|
15
|
+
this.initialVirtualTokenReserves = 1073000000000000000n;
|
|
16
|
+
this.initialVirtualCollateralReserves = 30000000000n;
|
|
17
|
+
this.collateralDecimals = 9;
|
|
18
|
+
this.constantProduct =
|
|
19
|
+
this.initialVirtualTokenReserves * this.initialVirtualCollateralReserves;
|
|
24
20
|
this.curveDefaults = {
|
|
25
21
|
type: constants_1.CurveType.CONSTANT_PRODUCT_V1,
|
|
26
22
|
totalSupply: BigInt(1e18),
|
|
@@ -30,113 +26,6 @@ class ConstantProductCurveV1 extends AbstractCurve_1.AbstractCurve {
|
|
|
30
26
|
address: '11111111111111111111111111111111',
|
|
31
27
|
};
|
|
32
28
|
}
|
|
33
|
-
getTokensAmountFromCollateral(options) {
|
|
34
|
-
const { amount, curvePosition, tradeDirection, platformFeeBps } = options;
|
|
35
|
-
if (tradeDirection === 'BUY') {
|
|
36
|
-
const collateralAmount = amount - (amount * BigInt(platformFeeBps)) / 10000n;
|
|
37
|
-
return this.buyInCollateral(collateralAmount, curvePosition);
|
|
38
|
-
}
|
|
39
|
-
const collateralAmount = amount + (amount * BigInt(platformFeeBps)) / 10000n;
|
|
40
|
-
return this.sellInCollateral(collateralAmount, curvePosition);
|
|
41
|
-
}
|
|
42
|
-
getCollateralAmountFromTokens(options) {
|
|
43
|
-
const { amount, curvePosition, tradeDirection, platformFeeBps } = options;
|
|
44
|
-
if (tradeDirection === 'BUY') {
|
|
45
|
-
const collateralAmount = this.buyInToken(amount, curvePosition);
|
|
46
|
-
return (collateralAmount + (collateralAmount * BigInt(platformFeeBps)) / 10000n);
|
|
47
|
-
}
|
|
48
|
-
const collateralAmount = this.sellInToken(amount, curvePosition);
|
|
49
|
-
return (collateralAmount - (collateralAmount * BigInt(platformFeeBps)) / 10000n);
|
|
50
|
-
}
|
|
51
|
-
getCollateralPrice(options) {
|
|
52
|
-
const { curvePosition, collateralDecimals } = options;
|
|
53
|
-
const finalCollateralDecimals = collateralDecimals ?? this.collateralDecimals;
|
|
54
|
-
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
55
|
-
const baseCollateral = BigInt(10 ** finalCollateralDecimals);
|
|
56
|
-
const price = new bigNumber_1.default(currentVirtualCollateralReserves.toString()).div(new bigNumber_1.default(currentVirtualTokenReserves.toString()));
|
|
57
|
-
const inBaseUnits = price.multipliedBy(new bigNumber_1.default(baseCollateral.toString()));
|
|
58
|
-
return inBaseUnits;
|
|
59
|
-
}
|
|
60
|
-
getMarketCap(options) {
|
|
61
|
-
try {
|
|
62
|
-
const { curvePosition, tokenDecimals, collateralDecimals } = options;
|
|
63
|
-
const base = new bigNumber_1.default(10);
|
|
64
|
-
const baseTokens = base.pow(tokenDecimals ?? this.curveDefaults.tokenDecimals);
|
|
65
|
-
const curvePositionDecimal = new bigNumber_1.default(curvePosition.toString());
|
|
66
|
-
const price = this.getCollateralPrice({
|
|
67
|
-
curvePosition,
|
|
68
|
-
collateralDecimals,
|
|
69
|
-
});
|
|
70
|
-
const cap = price
|
|
71
|
-
.multipliedBy(curvePositionDecimal)
|
|
72
|
-
.dividedBy(baseTokens);
|
|
73
|
-
return BigInt(cap.integerValue(bigNumber_1.default.ROUND_FLOOR).toString());
|
|
74
|
-
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
throw new Error(`Calculation error: ${error}`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
getDynamicThresholdFromMarketCap(marketCapThreshold) {
|
|
80
|
-
const baseTokens = BigInt(10 ** this.curveDefaults.tokenDecimals);
|
|
81
|
-
let low = 300000000n * baseTokens;
|
|
82
|
-
let high = 1000000000n * baseTokens;
|
|
83
|
-
let mid;
|
|
84
|
-
while (low < high) {
|
|
85
|
-
mid = (low + high + 1n) / 2n;
|
|
86
|
-
const marketCap = this.getMarketCap({ curvePosition: mid });
|
|
87
|
-
if (marketCap === marketCapThreshold) {
|
|
88
|
-
low = mid;
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
else if (marketCap < marketCapThreshold) {
|
|
92
|
-
low = mid;
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
high = mid - 1n;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
while (this.getMarketCap({ curvePosition: low }) >= marketCapThreshold &&
|
|
99
|
-
low > 0n) {
|
|
100
|
-
low -= 1n;
|
|
101
|
-
}
|
|
102
|
-
return low + 1n;
|
|
103
|
-
}
|
|
104
|
-
buyInToken(tokenAmount, curvePosition) {
|
|
105
|
-
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
106
|
-
const newTokenReserves = currentVirtualTokenReserves - tokenAmount;
|
|
107
|
-
const ratio = this.constantProduct / newTokenReserves;
|
|
108
|
-
const lamportsToSpend = ratio - currentVirtualCollateralReserves;
|
|
109
|
-
return lamportsToSpend;
|
|
110
|
-
}
|
|
111
|
-
buyInCollateral(collateralAmount, curvePosition) {
|
|
112
|
-
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
113
|
-
const newCollateralReserves = currentVirtualCollateralReserves + collateralAmount;
|
|
114
|
-
const ratio = this.constantProduct / newCollateralReserves;
|
|
115
|
-
const tokensToBuy = currentVirtualTokenReserves - ratio;
|
|
116
|
-
return tokensToBuy;
|
|
117
|
-
}
|
|
118
|
-
sellInToken(tokenAmount, curvePosition) {
|
|
119
|
-
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
120
|
-
const newTokenReserves = currentVirtualTokenReserves + tokenAmount;
|
|
121
|
-
const ratio = this.constantProduct / newTokenReserves;
|
|
122
|
-
const lamportsToReceive = currentVirtualCollateralReserves - ratio;
|
|
123
|
-
return lamportsToReceive;
|
|
124
|
-
}
|
|
125
|
-
sellInCollateral(collateralAmount, curvePosition) {
|
|
126
|
-
const [currentVirtualTokenReserves, currentVirtualCollateralReserves] = this.getCurrentReserves(curvePosition);
|
|
127
|
-
const newCollateralReserves = currentVirtualCollateralReserves - collateralAmount;
|
|
128
|
-
const ratio = this.constantProduct / newCollateralReserves;
|
|
129
|
-
const tokensToSell = ratio - currentVirtualTokenReserves;
|
|
130
|
-
return tokensToSell;
|
|
131
|
-
}
|
|
132
|
-
getCurrentReserves(curvePosition) {
|
|
133
|
-
const currentVirtualTokenReserves = this.initialVirtualTokenReserves - curvePosition;
|
|
134
|
-
const currentVirtualCollateralReserves = (0, bigNumber_1.default)(this.constantProduct.toString()).div((0, bigNumber_1.default)(currentVirtualTokenReserves.toString()));
|
|
135
|
-
return [
|
|
136
|
-
currentVirtualTokenReserves,
|
|
137
|
-
services_1.AmountService.decimalToBigInt(currentVirtualCollateralReserves),
|
|
138
|
-
];
|
|
139
|
-
}
|
|
140
29
|
}
|
|
141
30
|
exports.ConstantProductCurveV1 = ConstantProductCurveV1;
|
|
142
31
|
//# sourceMappingURL=ConstantProductCurveV1.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConstantProductCurveV1.js","sourceRoot":"","sources":["../../../../src/domain/curves/ConstantProductCurveV1.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ConstantProductCurveV1.js","sourceRoot":"","sources":["../../../../src/domain/curves/ConstantProductCurveV1.ts"],"names":[],"mappings":";;;AAAA,4CAAyC;AACzC,iEAA8D;AAE9D,MAAa,sBAAuB,SAAQ,2CAAoB;IAU9D;QACE,KAAK,EAAE,CAAC;QAVV,qBAAgB,GAAG,EAAE,CAAC;QAEtB,iBAAY,GAAG,EAAE,CAAC;QAElB,6BAAwB,GAAwB,IAAI,GAAG,CAAC;YACtD,CAAC,aAAgB,EAAE,mBAAwB,CAAC;YAC5C,CAAC,YAAe,EAAE,mBAAwB,CAAC;SAC5C,CAAC,CAAC;QAKD,IAAI,CAAC,2BAA2B,GAAG,oBAA0B,CAAC;QAC9D,IAAI,CAAC,gCAAgC,GAAG,YAAe,CAAC;QACxD,IAAI,CAAC,kBAAkB,GAAG,CAAC,CAAC;QAC5B,IAAI,CAAC,eAAe;YAClB,IAAI,CAAC,2BAA2B,GAAG,IAAI,CAAC,gCAAgC,CAAC;QAE3E,IAAI,CAAC,aAAa,GAAG;YACnB,IAAI,EAAE,qBAAS,CAAC,mBAAmB;YACnC,WAAW,EAAE,MAAM,CAAC,IAAI,CAAC;YACzB,aAAa,EAAE,CAAC;YAChB,wBAAwB,EACtB,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,GAAG,IAAI;YACvD,wBAAwB,EACtB,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,GAAG,IAAI;YACnD,OAAO,EAAE,kCAAkC;SACnC,CAAC;IACb,CAAC;CACF;AA9BD,wDA8BC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConstantProductCurveV2 = void 0;
|
|
4
|
+
const constants_1 = require("../constants");
|
|
5
|
+
const ConstantProductCurve_1 = require("./ConstantProductCurve");
|
|
6
|
+
class ConstantProductCurveV2 extends ConstantProductCurve_1.ConstantProductCurve {
|
|
7
|
+
constructor() {
|
|
8
|
+
super();
|
|
9
|
+
this.dynamicThreshold = 80;
|
|
10
|
+
this.maxThreshold = 82;
|
|
11
|
+
this.marketCapToMinimalTokens = new Map([
|
|
12
|
+
[200000000000n, 814207069725281919n],
|
|
13
|
+
[10000000000n, 344740929008610006n],
|
|
14
|
+
]);
|
|
15
|
+
this.initialVirtualTokenReserves = 1060000000000000000n;
|
|
16
|
+
this.initialVirtualCollateralReserves = 14000000000n;
|
|
17
|
+
this.collateralDecimals = 9;
|
|
18
|
+
this.constantProduct =
|
|
19
|
+
this.initialVirtualTokenReserves * this.initialVirtualCollateralReserves;
|
|
20
|
+
this.curveDefaults = {
|
|
21
|
+
type: constants_1.CurveType.CONSTANT_PRODUCT_V2,
|
|
22
|
+
totalSupply: BigInt(1e18),
|
|
23
|
+
tokenDecimals: 9,
|
|
24
|
+
minAllocationTokenAmount: (BigInt(1e18) * BigInt(this.dynamicThreshold)) / 100n,
|
|
25
|
+
maxAllocationTokenAmount: (BigInt(1e18) * BigInt(this.maxThreshold)) / 100n,
|
|
26
|
+
address: '11111111111111111111111111111111',
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.ConstantProductCurveV2 = ConstantProductCurveV2;
|
|
31
|
+
//# sourceMappingURL=ConstantProductCurveV2.js.map
|