@proton/wrap-constants 0.2.185 → 0.2.188
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/index.d.ts +5 -6
- package/dist/index.js +34 -1489
- package/dist/index.mjs +34 -1489
- package/package.json +10 -13
- package/src/coins.ts +1 -1
- package/src/constants.ts +9 -10
- package/src/math.ts +95 -60
- package/src/parsers/auth.ts +32 -18
- package/src/parsers/pool.ts +34 -33
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
|
-
"version": "0.2.
|
|
8
|
+
"version": "0.2.188",
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"main": "dist/index.js",
|
|
11
11
|
"typings": "dist/index.d.ts",
|
|
@@ -25,20 +25,17 @@
|
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@bloks/numbers": "^27.0.0",
|
|
28
|
-
"@
|
|
29
|
-
"@proton/js": "^26.0.1",
|
|
30
|
-
"@size-limit/preset-small-lib": "^4.9.1",
|
|
28
|
+
"@size-limit/preset-small-lib": "^11.2.0",
|
|
31
29
|
"@types/lodash.clonedeep": "^4.5.6",
|
|
32
|
-
"@typescript-eslint/
|
|
33
|
-
"
|
|
34
|
-
"
|
|
30
|
+
"@typescript-eslint/eslint-plugin": "^8.36.0",
|
|
31
|
+
"@typescript-eslint/parser": "^8.36.0",
|
|
32
|
+
"eslint": "^9.30.1",
|
|
33
|
+
"size-limit": "^11.2.0",
|
|
34
|
+
"tsup": "^8.5.0"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
+
"bignumber.js": "^9.3.1",
|
|
37
38
|
"lodash.clonedeep": "^4.5.0",
|
|
38
39
|
"zod": "^3.19.1"
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
"typescript": "4.5"
|
|
42
|
-
},
|
|
43
|
-
"gitHead": "88555c5fb8a463f5e0d8af0e6976025739398ba3"
|
|
44
|
-
}
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/coins.ts
CHANGED
package/src/constants.ts
CHANGED
|
@@ -20,7 +20,6 @@ export const initializeConstants = (chain: string) => {
|
|
|
20
20
|
VEST_CONTRACT: "vest",
|
|
21
21
|
DEX_CONTRACT: "dex",
|
|
22
22
|
FEE_FACTOR,
|
|
23
|
-
GOOGLE_MAPS_KEY: "AIzaSyDb2_CrK032kSIqBXbkdwmaLKWELl0SxVs",
|
|
24
23
|
SWAP_URL:
|
|
25
24
|
chain === "proton"
|
|
26
25
|
? "https://otc.protonswap.com"
|
|
@@ -83,16 +82,16 @@ export const initializeConstants = (chain: string) => {
|
|
|
83
82
|
PROTON_ENDPOINTS:
|
|
84
83
|
chain === "proton"
|
|
85
84
|
? [
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
"https://rpc.api.mainnet.metalx.com",
|
|
86
|
+
"https://proton.greymass.com",
|
|
87
|
+
"https://proton.eoscafeblock.com",
|
|
88
|
+
"https://proton.cryptolions.io",
|
|
89
|
+
"https://proton.eosusa.news",
|
|
90
|
+
]
|
|
92
91
|
: [
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
92
|
+
"https://rpc.api.testnet.metalx.com",
|
|
93
|
+
"https://testnet.protonchain.com",
|
|
94
|
+
],
|
|
96
95
|
|
|
97
96
|
CHAIN_ID:
|
|
98
97
|
chain === "proton"
|
package/src/math.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { FEE_FACTOR } from
|
|
1
|
+
import BN from "bignumber.js";
|
|
2
|
+
import { FEE_FACTOR } from "./constants";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* XPR<>XUSDT (XPR -> XUSDT)
|
|
@@ -10,17 +10,23 @@ import { FEE_FACTOR } from './constants'
|
|
|
10
10
|
*
|
|
11
11
|
* result = term - (term * fee)
|
|
12
12
|
*/
|
|
13
|
-
export const compute_transfer = (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
export const compute_transfer = (
|
|
14
|
+
pool1: BN.Instance,
|
|
15
|
+
pool2: BN.Instance,
|
|
16
|
+
swap: BN.Instance,
|
|
17
|
+
exchange_fee: number,
|
|
18
|
+
precision: number
|
|
19
|
+
): BN => {
|
|
20
|
+
const term1 = pool2;
|
|
21
|
+
const term2over = pool1.times(pool2);
|
|
22
|
+
const term2under = pool1.plus(swap);
|
|
23
|
+
const term2 = term2over.div(term2under).integerValue(BN.ROUND_DOWN);
|
|
24
|
+
const term = term1.minus(term2);
|
|
25
|
+
const fee = new BN(exchange_fee).div(FEE_FACTOR);
|
|
26
|
+
const result = term.minus(term.times(fee));
|
|
27
|
+
|
|
28
|
+
return result.integerValue(BN.ROUND_DOWN).shiftedBy(-precision);
|
|
29
|
+
};
|
|
24
30
|
|
|
25
31
|
export const compute_transfer_curve = (
|
|
26
32
|
reserve_in: number,
|
|
@@ -31,39 +37,51 @@ export const compute_transfer_curve = (
|
|
|
31
37
|
amplifier: number
|
|
32
38
|
): BN => {
|
|
33
39
|
if (amplifier === 0) {
|
|
34
|
-
|
|
40
|
+
return compute_transfer(
|
|
41
|
+
new BN(reserve_in),
|
|
42
|
+
new BN(reserve_out),
|
|
43
|
+
new BN(amount_in),
|
|
44
|
+
exchange_fee,
|
|
45
|
+
precision
|
|
46
|
+
);
|
|
35
47
|
}
|
|
36
48
|
|
|
37
|
-
const MAX_ITERATIONS = 10
|
|
49
|
+
const MAX_ITERATIONS = 10;
|
|
38
50
|
|
|
39
|
-
const sum = reserve_in + reserve_out
|
|
51
|
+
const sum = reserve_in + reserve_out;
|
|
40
52
|
let D = sum;
|
|
41
|
-
let D_prev = 0
|
|
53
|
+
let D_prev = 0;
|
|
42
54
|
|
|
43
55
|
const F = (v: number) => {
|
|
44
|
-
|
|
45
|
-
}
|
|
56
|
+
return (v >= 0 ? 1 : -1) * Math.floor(Math.abs(v));
|
|
57
|
+
};
|
|
46
58
|
|
|
47
|
-
let i = MAX_ITERATIONS
|
|
59
|
+
let i = MAX_ITERATIONS;
|
|
48
60
|
while (D != D_prev && i--) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
61
|
+
const prod1 = F((F((D * D) / (reserve_in * 2)) * D) / (reserve_out * 2));
|
|
62
|
+
D_prev = D;
|
|
63
|
+
D = F(
|
|
64
|
+
(2 * D * (amplifier * sum + prod1)) /
|
|
65
|
+
((2 * amplifier - 1) * D + 3 * prod1)
|
|
66
|
+
);
|
|
52
67
|
}
|
|
53
68
|
|
|
54
|
-
const b =
|
|
69
|
+
const b = reserve_in + amount_in + F(D / (amplifier * 2)) - D;
|
|
55
70
|
|
|
56
|
-
const c = F(
|
|
71
|
+
const c = F(
|
|
72
|
+
(F((D * D) / ((reserve_in + amount_in) * 2)) * D) / (amplifier * 4)
|
|
73
|
+
);
|
|
57
74
|
|
|
58
|
-
let x = D,
|
|
75
|
+
let x = D,
|
|
76
|
+
x_prev = 0;
|
|
59
77
|
i = MAX_ITERATIONS;
|
|
60
|
-
while (
|
|
78
|
+
while (x != x_prev && i--) {
|
|
61
79
|
x_prev = F(x);
|
|
62
|
-
x = +new BN(x * x + c).div(2 * x + b).toFixed(0, BN.ROUND_DOWN)
|
|
80
|
+
x = +new BN(x * x + c).div(2 * x + b).toFixed(0, BN.ROUND_DOWN);
|
|
63
81
|
}
|
|
64
82
|
|
|
65
83
|
if (reserve_out <= x) {
|
|
66
|
-
|
|
84
|
+
throw new Error("curve: insufficient reserve out");
|
|
67
85
|
}
|
|
68
86
|
|
|
69
87
|
// Amount out + Fee + 1
|
|
@@ -71,42 +89,59 @@ export const compute_transfer_curve = (
|
|
|
71
89
|
|
|
72
90
|
// Fee + 1
|
|
73
91
|
// +1 covers small precision issues
|
|
74
|
-
const fee = amount_out_with_fee
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
92
|
+
const fee = amount_out_with_fee
|
|
93
|
+
.times(exchange_fee)
|
|
94
|
+
.div(FEE_FACTOR)
|
|
95
|
+
.plus(1)
|
|
96
|
+
.toFixed(0, BN.ROUND_DOWN);
|
|
78
97
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
98
|
+
// Amount out
|
|
99
|
+
const amount_out = amount_out_with_fee.minus(fee);
|
|
100
|
+
|
|
101
|
+
return amount_out.integerValue(BN.ROUND_DOWN).shiftedBy(-precision);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export const compute_liquidity = (
|
|
105
|
+
amount_lt: BN.Instance,
|
|
106
|
+
pool: BN.Instance,
|
|
107
|
+
lt_supply: BN.Instance,
|
|
108
|
+
precision: number
|
|
109
|
+
): number => {
|
|
110
|
+
const over = amount_lt.times(pool);
|
|
111
|
+
const under = lt_supply;
|
|
112
|
+
const result = over.div(under).integerValue(BN.ROUND_DOWN);
|
|
113
|
+
|
|
114
|
+
return +result.shiftedBy(-precision).toFixed(precision, BN.ROUND_DOWN);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const isInputInvalid = (
|
|
118
|
+
data: string,
|
|
119
|
+
value: string | number,
|
|
120
|
+
pool: { xtokenPrecision: number }
|
|
121
|
+
): boolean => {
|
|
122
|
+
value = String(value);
|
|
123
|
+
|
|
124
|
+
const isBackspace = data === null;
|
|
125
|
+
const isNumber = !/[0-9]|\./.test(data);
|
|
126
|
+
const isDoubleDot = data === "." && value.indexOf(".") !== value.length - 1;
|
|
127
|
+
const isPrecisionTooHigh =
|
|
128
|
+
value &&
|
|
129
|
+
value.indexOf(".") !== -1 &&
|
|
130
|
+
value.split(".")[1].length > pool.xtokenPrecision;
|
|
131
|
+
const isInvalid =
|
|
132
|
+
!isBackspace && Boolean(isNumber || isDoubleDot || isPrecisionTooHigh);
|
|
133
|
+
|
|
134
|
+
return isInvalid;
|
|
135
|
+
};
|
|
101
136
|
|
|
102
137
|
export const applyDiscountToFee = (fee: number, staked: number): number => {
|
|
103
138
|
if (staked >= 10000000) {
|
|
104
|
-
return fee * 0
|
|
139
|
+
return fee * 0;
|
|
105
140
|
} else if (staked >= 1000000) {
|
|
106
|
-
return fee * 0.33
|
|
141
|
+
return fee * 0.33;
|
|
107
142
|
} else if (staked >= 100000) {
|
|
108
|
-
return fee * 0.66
|
|
143
|
+
return fee * 0.66;
|
|
109
144
|
} else {
|
|
110
|
-
return fee
|
|
145
|
+
return fee;
|
|
111
146
|
}
|
|
112
|
-
}
|
|
147
|
+
};
|
package/src/parsers/auth.ts
CHANGED
|
@@ -1,22 +1,36 @@
|
|
|
1
|
-
import * as z from
|
|
2
|
-
import { baseActionParser, singleActionTransactionParser } from
|
|
3
|
-
import { authorizationParser } from
|
|
4
|
-
import { WRAP_CONTRACT, WRAP_AUTH_ACTION } from
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { baseActionParser, singleActionTransactionParser } from "./actions";
|
|
3
|
+
import { authorizationParser } from "./base";
|
|
4
|
+
import { WRAP_CONTRACT, WRAP_AUTH_ACTION } from "../constants";
|
|
5
5
|
|
|
6
|
-
export const createAbstractAuthParser = (
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
export const createAbstractAuthParser = (
|
|
7
|
+
contract: string,
|
|
8
|
+
action: string,
|
|
9
|
+
data: z.ZodRawShape
|
|
10
|
+
) => {
|
|
11
|
+
return z.object({
|
|
12
|
+
signer: authorizationParser,
|
|
13
|
+
signatures: z.array(z.string()).length(1),
|
|
14
|
+
transaction: singleActionTransactionParser(contract, action, data),
|
|
15
|
+
});
|
|
16
|
+
};
|
|
13
17
|
|
|
14
18
|
const generateAuthData = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
const generateAuthActionsBaseParser = baseActionParser(
|
|
19
|
-
|
|
19
|
+
time: z.string(),
|
|
20
|
+
protonAccount: z.string(),
|
|
21
|
+
};
|
|
22
|
+
const generateAuthActionsBaseParser = baseActionParser(
|
|
23
|
+
WRAP_CONTRACT,
|
|
24
|
+
WRAP_AUTH_ACTION,
|
|
25
|
+
generateAuthData
|
|
26
|
+
);
|
|
27
|
+
export type GenerateAuthBaseAction = z.infer<
|
|
28
|
+
typeof generateAuthActionsBaseParser
|
|
29
|
+
>;
|
|
20
30
|
|
|
21
|
-
export const generateAuthParser = createAbstractAuthParser(
|
|
22
|
-
|
|
31
|
+
export const generateAuthParser = createAbstractAuthParser(
|
|
32
|
+
WRAP_CONTRACT,
|
|
33
|
+
WRAP_AUTH_ACTION,
|
|
34
|
+
generateAuthData
|
|
35
|
+
);
|
|
36
|
+
export type GenerateAuth = z.infer<typeof generateAuthParser>;
|
package/src/parsers/pool.ts
CHANGED
|
@@ -1,30 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ExtendedAsset } from "@bloks/numbers";
|
|
2
|
+
import { Coin } from "../coins";
|
|
3
3
|
import { INode } from "../graph";
|
|
4
4
|
|
|
5
|
-
|
|
6
5
|
export interface RawPool {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
6
|
+
lt_symbol: string;
|
|
7
|
+
creator: string;
|
|
8
|
+
memo: string;
|
|
9
|
+
pool1: {
|
|
10
|
+
quantity: string;
|
|
11
|
+
contract: string;
|
|
12
|
+
};
|
|
13
|
+
pool2: {
|
|
14
|
+
quantity: string;
|
|
15
|
+
contract: string;
|
|
16
|
+
};
|
|
17
|
+
hash: string;
|
|
18
|
+
fee: {
|
|
19
|
+
exchange_fee: number;
|
|
20
|
+
add_liquidity_fee: number;
|
|
21
|
+
remove_liquidity_fee: number;
|
|
22
|
+
};
|
|
23
|
+
active: boolean;
|
|
24
|
+
amplifier: number;
|
|
26
25
|
}
|
|
27
|
-
export interface Pool extends Omit<RawPool,
|
|
26
|
+
export interface Pool extends Omit<RawPool, "pool1" | "pool2"> {
|
|
28
27
|
lt_symbol: string;
|
|
29
28
|
creator: string;
|
|
30
29
|
memo: string;
|
|
@@ -39,14 +38,16 @@ export interface Pool extends Omit<RawPool, 'pool1'|'pool2'> {
|
|
|
39
38
|
liquidityUsd: number;
|
|
40
39
|
liquidityApr: number;
|
|
41
40
|
}
|
|
42
|
-
|
|
43
|
-
export type ExtendedToken = Coin & ExtendedAsset & { key: string; }
|
|
44
|
-
export type ExtendedTokenWithRate = ExtendedToken & { rate: number; }
|
|
45
41
|
|
|
46
|
-
export type
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
42
|
+
export type ExtendedToken = Coin & ExtendedAsset & { key: string };
|
|
43
|
+
export type ExtendedTokenWithRate = ExtendedToken & { rate: number };
|
|
44
|
+
|
|
45
|
+
export type PoolTable =
|
|
46
|
+
| {
|
|
47
|
+
pool1: ExtendedTokenWithRate;
|
|
48
|
+
pool2: ExtendedTokenWithRate;
|
|
49
|
+
path: INode<Pool>[];
|
|
50
|
+
shortestPath: INode<Pool>[];
|
|
51
|
+
getPathString: () => string;
|
|
52
|
+
}
|
|
53
|
+
| undefined;
|