@pioneer-platform/helpers 4.2.0 → 4.3.0
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/.turbo/turbo-build.log +2 -0
- package/CHANGELOG.md +11 -0
- package/build.sh +22 -0
- package/lib/helpers/asset.d.ts +36 -0
- package/lib/helpers/asset.js +208 -0
- package/lib/helpers/liquidity.d.ts +58 -0
- package/lib/helpers/liquidity.js +91 -0
- package/lib/helpers/memo.d.ts +46 -0
- package/lib/helpers/memo.js +43 -0
- package/lib/helpers/others.d.ts +3 -0
- package/lib/helpers/others.js +23 -0
- package/lib/helpers/request.d.ts +5 -0
- package/lib/helpers/request.js +79 -0
- package/lib/helpers/validators.d.ts +1 -0
- package/lib/helpers/validators.js +16 -0
- package/lib/index.d.ts +15 -0
- package/lib/index.js +31 -0
- package/lib/index.js.map +33 -0
- package/lib/modules/assetValue.d.ts +58 -0
- package/lib/modules/assetValue.js +355 -0
- package/lib/modules/bigIntArithmetics.d.ts +54 -0
- package/lib/modules/bigIntArithmetics.js +598 -0
- package/lib/modules/swapKitError.d.ts +64 -0
- package/lib/modules/swapKitError.js +71 -0
- package/lib/modules/swapKitNumber.d.ts +6 -0
- package/lib/modules/swapKitNumber.js +17 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
package/build.sh
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
# Fast Bun build (replaces slow tsc)
|
3
|
+
|
4
|
+
OUTDIR="${1:-lib}"
|
5
|
+
ENTRY="${2:-src/index.ts}"
|
6
|
+
|
7
|
+
# Clean output
|
8
|
+
rm -rf "$OUTDIR"
|
9
|
+
mkdir -p "$OUTDIR"
|
10
|
+
|
11
|
+
# Build with Bun (10x faster than tsc)
|
12
|
+
bun build "$ENTRY" \
|
13
|
+
--outdir "$OUTDIR" \
|
14
|
+
--target node \
|
15
|
+
--format cjs \
|
16
|
+
--sourcemap \
|
17
|
+
--minify
|
18
|
+
|
19
|
+
# Generate TypeScript declarations
|
20
|
+
bunx tsc --emitDeclarationOnly --outDir "$OUTDIR" --project .
|
21
|
+
|
22
|
+
echo "✅ Built $OUTDIR"
|
@@ -0,0 +1,36 @@
|
|
1
|
+
import { Chain, FeeOption } from '@coinmasters/types';
|
2
|
+
export type CommonAssetString = 'MAYA.MAYA' | 'ETH.THOR' | 'ETH.vTHOR' | Chain;
|
3
|
+
export declare const getDecimal: ({ chain, symbol }: {
|
4
|
+
chain: Chain;
|
5
|
+
symbol: string;
|
6
|
+
}) => Promise<number>;
|
7
|
+
export declare const gasFeeMultiplier: Record<FeeOption, number>;
|
8
|
+
export declare const isGasAsset: ({ chain, symbol }: {
|
9
|
+
chain: Chain;
|
10
|
+
symbol: string;
|
11
|
+
}) => boolean | undefined;
|
12
|
+
export declare const getCommonAssetInfo: (assetString: CommonAssetString) => {
|
13
|
+
identifier: string;
|
14
|
+
decimal: number;
|
15
|
+
};
|
16
|
+
export declare const getAssetType: ({ chain, symbol }: {
|
17
|
+
chain: Chain;
|
18
|
+
symbol: string;
|
19
|
+
}) => Chain.Avalanche | Chain.Cosmos | Chain.Kujira | "Synth" | "Native" | "ERC20" | "POLYGON" | "ARBITRUM" | "OPTIMISM" | undefined;
|
20
|
+
export declare const assetFromString: (assetString: string) => {
|
21
|
+
chain: Chain;
|
22
|
+
symbol: string;
|
23
|
+
ticker: string;
|
24
|
+
synth: boolean;
|
25
|
+
};
|
26
|
+
export declare const filterAssets: (tokens: {
|
27
|
+
value: string;
|
28
|
+
decimal: number;
|
29
|
+
chain: Chain;
|
30
|
+
symbol: string;
|
31
|
+
}[]) => {
|
32
|
+
value: string;
|
33
|
+
decimal: number;
|
34
|
+
chain: Chain;
|
35
|
+
symbol: string;
|
36
|
+
}[];
|
@@ -0,0 +1,208 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
exports.filterAssets = exports.assetFromString = exports.getAssetType = exports.getCommonAssetInfo = exports.isGasAsset = exports.gasFeeMultiplier = exports.getDecimal = void 0;
|
13
|
+
const types_1 = require("@coinmasters/types");
|
14
|
+
const index_1 = require("../index");
|
15
|
+
const getDecimalMethodHex = '0x313ce567';
|
16
|
+
const getContractDecimals = (_a) => __awaiter(void 0, [_a], void 0, function* ({ chain, to }) {
|
17
|
+
try {
|
18
|
+
const { result } = yield index_1.RequestClient.post(types_1.ChainToRPC[chain], {
|
19
|
+
headers: {
|
20
|
+
accept: '*/*',
|
21
|
+
'content-type': 'application/json',
|
22
|
+
'cache-control': 'no-cache',
|
23
|
+
},
|
24
|
+
body: JSON.stringify({
|
25
|
+
id: 44,
|
26
|
+
jsonrpc: '2.0',
|
27
|
+
method: 'eth_call',
|
28
|
+
params: [{ to: to.toLowerCase(), data: getDecimalMethodHex }, 'latest'],
|
29
|
+
}),
|
30
|
+
});
|
31
|
+
return parseInt(BigInt(result).toString());
|
32
|
+
}
|
33
|
+
catch (error) {
|
34
|
+
console.error(error);
|
35
|
+
// @ts-ignore
|
36
|
+
return types_1.BaseDecimal[chain];
|
37
|
+
}
|
38
|
+
});
|
39
|
+
const getETHAssetDecimal = (symbol) => __awaiter(void 0, void 0, void 0, function* () {
|
40
|
+
if (symbol === types_1.Chain.Ethereum)
|
41
|
+
return types_1.BaseDecimal.ETH;
|
42
|
+
const [, address] = symbol.split('-');
|
43
|
+
return (address === null || address === void 0 ? void 0 : address.startsWith('0x'))
|
44
|
+
? getContractDecimals({ chain: types_1.Chain.Ethereum, to: address })
|
45
|
+
: types_1.BaseDecimal.ETH;
|
46
|
+
});
|
47
|
+
const getAVAXAssetDecimal = (symbol) => __awaiter(void 0, void 0, void 0, function* () {
|
48
|
+
const [, address] = symbol.split('-');
|
49
|
+
return (address === null || address === void 0 ? void 0 : address.startsWith('0x'))
|
50
|
+
? getContractDecimals({ chain: types_1.Chain.Avalanche, to: address.toLowerCase() })
|
51
|
+
: types_1.BaseDecimal.AVAX;
|
52
|
+
});
|
53
|
+
const getBSCAssetDecimal = (symbol) => __awaiter(void 0, void 0, void 0, function* () {
|
54
|
+
if (symbol === types_1.Chain.BinanceSmartChain)
|
55
|
+
return types_1.BaseDecimal.BSC;
|
56
|
+
return types_1.BaseDecimal.BSC;
|
57
|
+
});
|
58
|
+
const getDecimal = (_a) => __awaiter(void 0, [_a], void 0, function* ({ chain, symbol }) {
|
59
|
+
switch (chain) {
|
60
|
+
case types_1.Chain.Ethereum:
|
61
|
+
return getETHAssetDecimal(symbol);
|
62
|
+
case types_1.Chain.Avalanche:
|
63
|
+
return getAVAXAssetDecimal(symbol);
|
64
|
+
case types_1.Chain.BinanceSmartChain:
|
65
|
+
return getBSCAssetDecimal(symbol);
|
66
|
+
default:
|
67
|
+
// @ts-ignore
|
68
|
+
return types_1.BaseDecimal[chain];
|
69
|
+
}
|
70
|
+
});
|
71
|
+
exports.getDecimal = getDecimal;
|
72
|
+
exports.gasFeeMultiplier = {
|
73
|
+
[types_1.FeeOption.Average]: 1.2,
|
74
|
+
[types_1.FeeOption.Fast]: 1.5,
|
75
|
+
[types_1.FeeOption.Fastest]: 2,
|
76
|
+
};
|
77
|
+
const isGasAsset = ({ chain, symbol }) => {
|
78
|
+
switch (chain) {
|
79
|
+
case types_1.Chain.Bitcoin:
|
80
|
+
case types_1.Chain.BitcoinCash:
|
81
|
+
case types_1.Chain.Dash:
|
82
|
+
case types_1.Chain.Digibyte:
|
83
|
+
case types_1.Chain.Zcash:
|
84
|
+
case types_1.Chain.Ripple:
|
85
|
+
case types_1.Chain.Litecoin:
|
86
|
+
case types_1.Chain.Dogecoin:
|
87
|
+
case types_1.Chain.Ethereum:
|
88
|
+
case types_1.Chain.Avalanche:
|
89
|
+
return symbol === chain;
|
90
|
+
case types_1.Chain.Arbitrum:
|
91
|
+
case types_1.Chain.Optimism:
|
92
|
+
case types_1.Chain.Base:
|
93
|
+
return 'ETH' === symbol;
|
94
|
+
// @ts-ignore
|
95
|
+
case types_1.Chain.Mayachain:
|
96
|
+
return symbol === 'CACAO';
|
97
|
+
case types_1.Chain.Kujira:
|
98
|
+
return symbol === 'KUJI';
|
99
|
+
case types_1.Chain.Cosmos:
|
100
|
+
return symbol === 'ATOM';
|
101
|
+
case types_1.Chain.Polygon:
|
102
|
+
return symbol === 'MATIC';
|
103
|
+
case types_1.Chain.BinanceSmartChain:
|
104
|
+
return symbol === 'BNB';
|
105
|
+
case types_1.Chain.THORChain:
|
106
|
+
return symbol === 'RUNE';
|
107
|
+
}
|
108
|
+
};
|
109
|
+
exports.isGasAsset = isGasAsset;
|
110
|
+
const getCommonAssetInfo = (assetString) => {
|
111
|
+
switch (assetString) {
|
112
|
+
case 'ETH.THOR':
|
113
|
+
return { identifier: 'ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044', decimal: 18 };
|
114
|
+
case 'ETH.vTHOR':
|
115
|
+
return { identifier: 'ETH.vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d', decimal: 18 };
|
116
|
+
case types_1.Chain.Cosmos:
|
117
|
+
return { identifier: 'GAIA.ATOM', decimal: types_1.BaseDecimal[assetString] };
|
118
|
+
case types_1.Chain.THORChain:
|
119
|
+
return { identifier: 'THOR.RUNE', decimal: types_1.BaseDecimal[assetString] };
|
120
|
+
case types_1.Chain.BinanceSmartChain:
|
121
|
+
return { identifier: 'BSC.BNB', decimal: types_1.BaseDecimal[assetString] };
|
122
|
+
// @ts-ignore
|
123
|
+
case types_1.Chain.Mayachain:
|
124
|
+
return { identifier: 'MAYA.CACAO', decimal: types_1.BaseDecimal.MAYA };
|
125
|
+
case 'MAYA.MAYA':
|
126
|
+
return { identifier: 'MAYA.MAYA', decimal: 4 };
|
127
|
+
case types_1.Chain.Arbitrum:
|
128
|
+
return { identifier: 'ARB.ETH', decimal: 18 };
|
129
|
+
case types_1.Chain.Base:
|
130
|
+
return { identifier: 'BASE.ETH', decimal: 18 };
|
131
|
+
case types_1.Chain.Optimism:
|
132
|
+
return { identifier: 'OP.ETH', decimal: 18 };
|
133
|
+
case types_1.Chain.Avalanche:
|
134
|
+
return { identifier: 'AVAX.ETH', decimal: 18 };
|
135
|
+
case types_1.Chain.Ripple:
|
136
|
+
case types_1.Chain.Kujira:
|
137
|
+
case types_1.Chain.BitcoinCash:
|
138
|
+
case types_1.Chain.Zcash:
|
139
|
+
case types_1.Chain.Dash:
|
140
|
+
case types_1.Chain.Litecoin:
|
141
|
+
case types_1.Chain.Dogecoin:
|
142
|
+
case types_1.Chain.Polygon:
|
143
|
+
case types_1.Chain.Bitcoin:
|
144
|
+
case types_1.Chain.Ethereum:
|
145
|
+
return { identifier: `${assetString}.${assetString}`, decimal: types_1.BaseDecimal[assetString] };
|
146
|
+
}
|
147
|
+
};
|
148
|
+
exports.getCommonAssetInfo = getCommonAssetInfo;
|
149
|
+
const getAssetType = ({ chain, symbol }) => {
|
150
|
+
if (symbol.includes('/'))
|
151
|
+
return 'Synth';
|
152
|
+
switch (chain) {
|
153
|
+
case types_1.Chain.Bitcoin:
|
154
|
+
case types_1.Chain.BitcoinCash:
|
155
|
+
case types_1.Chain.Dogecoin:
|
156
|
+
case types_1.Chain.Dash:
|
157
|
+
case types_1.Chain.Zcash:
|
158
|
+
case types_1.Chain.Litecoin:
|
159
|
+
// @ts-ignore
|
160
|
+
case types_1.Chain.Mayachain:
|
161
|
+
case types_1.Chain.THORChain:
|
162
|
+
return 'Native';
|
163
|
+
case types_1.Chain.Osmosis:
|
164
|
+
return 'Native';
|
165
|
+
case types_1.Chain.Cosmos:
|
166
|
+
return symbol === 'ATOM' ? 'Native' : types_1.Chain.Cosmos;
|
167
|
+
case types_1.Chain.Kujira:
|
168
|
+
return symbol === types_1.Chain.Kujira ? 'Native' : types_1.Chain.Kujira;
|
169
|
+
case types_1.Chain.BinanceSmartChain:
|
170
|
+
case types_1.Chain.Ethereum:
|
171
|
+
return symbol === types_1.Chain.Ethereum ? 'Native' : 'ERC20';
|
172
|
+
case types_1.Chain.Avalanche:
|
173
|
+
return symbol === types_1.Chain.Avalanche ? 'Native' : types_1.Chain.Avalanche;
|
174
|
+
case types_1.Chain.Polygon:
|
175
|
+
return symbol === types_1.Chain.Polygon ? 'Native' : 'POLYGON';
|
176
|
+
case types_1.Chain.Base:
|
177
|
+
return symbol === types_1.Chain.Ethereum ? 'Native' : 'ERC20';
|
178
|
+
case types_1.Chain.Arbitrum:
|
179
|
+
return [types_1.Chain.Ethereum, types_1.Chain.Arbitrum].includes(symbol) ? 'Native' : 'ARBITRUM';
|
180
|
+
case types_1.Chain.Optimism:
|
181
|
+
return [types_1.Chain.Ethereum, types_1.Chain.Optimism].includes(symbol) ? 'Native' : 'OPTIMISM';
|
182
|
+
}
|
183
|
+
};
|
184
|
+
exports.getAssetType = getAssetType;
|
185
|
+
const assetFromString = (assetString) => {
|
186
|
+
var _a;
|
187
|
+
const [chain, ...symbolArray] = assetString.split('.');
|
188
|
+
const synth = assetString.includes('/');
|
189
|
+
const symbol = symbolArray.join('.');
|
190
|
+
const ticker = (_a = symbol === null || symbol === void 0 ? void 0 : symbol.split('-')) === null || _a === void 0 ? void 0 : _a[0];
|
191
|
+
return { chain, symbol, ticker, synth };
|
192
|
+
};
|
193
|
+
exports.assetFromString = assetFromString;
|
194
|
+
const potentialScamRegex = new RegExp(/(.)\1{6}|\.ORG|\.NET|\.FINANCE|\.COM|WWW|HTTP|\\\\|\/\/|[\s$%:[\]]/, 'gmi');
|
195
|
+
const evmAssetHasAddress = (assetString) => {
|
196
|
+
const [chain, symbol] = assetString.split('.');
|
197
|
+
if (!types_1.EVMChainList.includes(chain))
|
198
|
+
return true;
|
199
|
+
const [, address] = symbol.split('-');
|
200
|
+
return (0, exports.isGasAsset)({ chain: chain, symbol }) || !!address;
|
201
|
+
};
|
202
|
+
const filterAssets = (tokens) => tokens.filter((token) => {
|
203
|
+
const assetString = `${token.chain}.${token.symbol}`;
|
204
|
+
return (!potentialScamRegex.test(assetString) &&
|
205
|
+
evmAssetHasAddress(assetString) &&
|
206
|
+
token.value !== '0');
|
207
|
+
});
|
208
|
+
exports.filterAssets = filterAssets;
|
@@ -0,0 +1,58 @@
|
|
1
|
+
import { SwapKitNumber } from '../index';
|
2
|
+
type ShareParams<T = {}> = T & {
|
3
|
+
liquidityUnits: string;
|
4
|
+
poolUnits: string;
|
5
|
+
};
|
6
|
+
type PoolParams<T = {}> = T & {
|
7
|
+
runeAmount: string;
|
8
|
+
assetAmount: string;
|
9
|
+
runeDepth: string;
|
10
|
+
assetDepth: string;
|
11
|
+
};
|
12
|
+
/**
|
13
|
+
* Ref: https://gitlab.com/thorchain/thornode/-/issues/657
|
14
|
+
* share = (s * A * (2 * T^2 - 2 * T * s + s^2))/T^3
|
15
|
+
* s = stakeUnits for member (after factoring in withdrawBasisPoints)
|
16
|
+
* T = totalPoolUnits for pool
|
17
|
+
* A = assetDepth to be withdrawn
|
18
|
+
*
|
19
|
+
* Formula:
|
20
|
+
* share = (s * A * (2 * T^2 - 2 * T * s + s^2))/T^3
|
21
|
+
* (part1 * (part2 - part3 + part4)) / part5
|
22
|
+
*/
|
23
|
+
export declare const getAsymmetricRuneShare: ({ liquidityUnits, poolUnits, runeDepth, }: ShareParams<{
|
24
|
+
runeDepth: string;
|
25
|
+
}>) => SwapKitNumber;
|
26
|
+
export declare const getAsymmetricAssetShare: ({ liquidityUnits, poolUnits, assetDepth, }: ShareParams<{
|
27
|
+
assetDepth: string;
|
28
|
+
}>) => SwapKitNumber;
|
29
|
+
export declare const getAsymmetricRuneWithdrawAmount: ({ percent, runeDepth, liquidityUnits, poolUnits, }: ShareParams<{
|
30
|
+
percent: number;
|
31
|
+
runeDepth: string;
|
32
|
+
}>) => SwapKitNumber;
|
33
|
+
export declare const getAsymmetricAssetWithdrawAmount: ({ percent, assetDepth, liquidityUnits, poolUnits, }: ShareParams<{
|
34
|
+
percent: number;
|
35
|
+
assetDepth: string;
|
36
|
+
}>) => SwapKitNumber;
|
37
|
+
export declare const getSymmetricPoolShare: ({ liquidityUnits, poolUnits, runeDepth, assetDepth, }: ShareParams<{
|
38
|
+
runeDepth: string;
|
39
|
+
assetDepth: string;
|
40
|
+
}>) => {
|
41
|
+
assetAmount: SwapKitNumber;
|
42
|
+
runeAmount: SwapKitNumber;
|
43
|
+
};
|
44
|
+
export declare const getSymmetricWithdraw: ({ liquidityUnits, poolUnits, runeDepth, assetDepth, percent, }: ShareParams<{
|
45
|
+
runeDepth: string;
|
46
|
+
assetDepth: string;
|
47
|
+
percent: number;
|
48
|
+
}>) => {
|
49
|
+
[k: string]: SwapKitNumber;
|
50
|
+
};
|
51
|
+
export declare const getEstimatedPoolShare: ({ runeDepth, poolUnits, assetDepth, liquidityUnits, runeAmount, assetAmount, }: ShareParams<{
|
52
|
+
runeAmount: string;
|
53
|
+
assetAmount: string;
|
54
|
+
runeDepth: string;
|
55
|
+
assetDepth: string;
|
56
|
+
}>) => number;
|
57
|
+
export declare const getLiquiditySlippage: ({ runeAmount, assetAmount, runeDepth, assetDepth, }: PoolParams) => number;
|
58
|
+
export {};
|
@@ -0,0 +1,91 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getLiquiditySlippage = exports.getEstimatedPoolShare = exports.getSymmetricWithdraw = exports.getSymmetricPoolShare = exports.getAsymmetricAssetWithdrawAmount = exports.getAsymmetricRuneWithdrawAmount = exports.getAsymmetricAssetShare = exports.getAsymmetricRuneShare = void 0;
|
4
|
+
const types_1 = require("@coinmasters/types");
|
5
|
+
const index_1 = require("../index");
|
6
|
+
/**
|
7
|
+
* Ref: https://gitlab.com/thorchain/thornode/-/issues/657
|
8
|
+
* share = (s * A * (2 * T^2 - 2 * T * s + s^2))/T^3
|
9
|
+
* s = stakeUnits for member (after factoring in withdrawBasisPoints)
|
10
|
+
* T = totalPoolUnits for pool
|
11
|
+
* A = assetDepth to be withdrawn
|
12
|
+
*
|
13
|
+
* Formula:
|
14
|
+
* share = (s * A * (2 * T^2 - 2 * T * s + s^2))/T^3
|
15
|
+
* (part1 * (part2 - part3 + part4)) / part5
|
16
|
+
*/
|
17
|
+
const getAsymmetricRuneShare = ({ liquidityUnits, poolUnits, runeDepth, }) => {
|
18
|
+
const s = toTCSwapKitNumber(liquidityUnits);
|
19
|
+
const T = toTCSwapKitNumber(poolUnits);
|
20
|
+
const A = toTCSwapKitNumber(runeDepth);
|
21
|
+
const part1 = s.mul(A);
|
22
|
+
const part2 = T.mul(T).mul(2);
|
23
|
+
const part3 = T.mul(s).mul(2);
|
24
|
+
const part4 = s.mul(s);
|
25
|
+
const part5 = T.mul(T).mul(T);
|
26
|
+
const numerator = part1.mul(part2.sub(part3).add(part4));
|
27
|
+
return numerator.div(part5);
|
28
|
+
};
|
29
|
+
exports.getAsymmetricRuneShare = getAsymmetricRuneShare;
|
30
|
+
const getAsymmetricAssetShare = ({ liquidityUnits, poolUnits, assetDepth, }) => {
|
31
|
+
const s = toTCSwapKitNumber(liquidityUnits);
|
32
|
+
const T = toTCSwapKitNumber(poolUnits);
|
33
|
+
const A = toTCSwapKitNumber(assetDepth);
|
34
|
+
const part1 = s.mul(A);
|
35
|
+
const part2 = T.mul(T).mul(2);
|
36
|
+
const part3 = T.mul(s).mul(2);
|
37
|
+
const part4 = s.mul(s);
|
38
|
+
const numerator = part1.mul(part2.sub(part3).add(part4));
|
39
|
+
const part5 = T.mul(T).mul(T);
|
40
|
+
return numerator.div(part5);
|
41
|
+
};
|
42
|
+
exports.getAsymmetricAssetShare = getAsymmetricAssetShare;
|
43
|
+
const getAsymmetricRuneWithdrawAmount = ({ percent, runeDepth, liquidityUnits, poolUnits, }) => (0, exports.getAsymmetricRuneShare)({ runeDepth, liquidityUnits, poolUnits }).mul(percent);
|
44
|
+
exports.getAsymmetricRuneWithdrawAmount = getAsymmetricRuneWithdrawAmount;
|
45
|
+
const getAsymmetricAssetWithdrawAmount = ({ percent, assetDepth, liquidityUnits, poolUnits, }) => (0, exports.getAsymmetricAssetShare)({ assetDepth, liquidityUnits, poolUnits }).mul(percent);
|
46
|
+
exports.getAsymmetricAssetWithdrawAmount = getAsymmetricAssetWithdrawAmount;
|
47
|
+
const toTCSwapKitNumber = (value) => index_1.SwapKitNumber.fromBigInt(BigInt(value), types_1.BaseDecimal.THOR);
|
48
|
+
const getSymmetricPoolShare = ({ liquidityUnits, poolUnits, runeDepth, assetDepth, }) => ({
|
49
|
+
assetAmount: toTCSwapKitNumber(assetDepth).mul(liquidityUnits).div(poolUnits),
|
50
|
+
runeAmount: toTCSwapKitNumber(runeDepth).mul(liquidityUnits).div(poolUnits),
|
51
|
+
});
|
52
|
+
exports.getSymmetricPoolShare = getSymmetricPoolShare;
|
53
|
+
const getSymmetricWithdraw = ({ liquidityUnits, poolUnits, runeDepth, assetDepth, percent, }) => Object.fromEntries(Object.entries((0, exports.getSymmetricPoolShare)({ liquidityUnits, poolUnits, runeDepth, assetDepth })).map(([name, value]) => [name, value.mul(percent)]));
|
54
|
+
exports.getSymmetricWithdraw = getSymmetricWithdraw;
|
55
|
+
const getEstimatedPoolShare = ({ runeDepth, poolUnits, assetDepth, liquidityUnits, runeAmount, assetAmount, }) => {
|
56
|
+
const R = new index_1.SwapKitNumber({ value: runeDepth, decimal: 8 });
|
57
|
+
const A = new index_1.SwapKitNumber({ value: assetDepth, decimal: 8 });
|
58
|
+
const P = new index_1.SwapKitNumber({ value: poolUnits, decimal: 8 });
|
59
|
+
const runeAddAmount = new index_1.SwapKitNumber({ value: runeAmount, decimal: 8 });
|
60
|
+
const assetAddAmount = new index_1.SwapKitNumber({ value: assetAmount, decimal: 8 });
|
61
|
+
// liquidityUnits = P * (r*A + a*R + 2*r*a) / (r*A + a*R + 2*R*A)
|
62
|
+
const rA = runeAddAmount.mul(A);
|
63
|
+
const aR = assetAddAmount.mul(R);
|
64
|
+
const ra = runeAddAmount.mul(assetAddAmount);
|
65
|
+
const RA = R.mul(A);
|
66
|
+
const numerator = P.mul(rA.add(aR.add(ra.mul(2))));
|
67
|
+
const denominator = rA.add(aR.add(RA.mul(2)));
|
68
|
+
const liquidityUnitsAfterAdd = numerator.div(denominator);
|
69
|
+
const estimatedLiquidityUnits = toTCSwapKitNumber(liquidityUnits).add(liquidityUnitsAfterAdd);
|
70
|
+
if (liquidityUnitsAfterAdd.getBaseValue('number') === 0) {
|
71
|
+
return estimatedLiquidityUnits.div(P).getBaseValue('number');
|
72
|
+
}
|
73
|
+
// get pool units after add
|
74
|
+
const newPoolUnits = P.add(estimatedLiquidityUnits);
|
75
|
+
return estimatedLiquidityUnits.div(newPoolUnits).getBaseValue('number');
|
76
|
+
};
|
77
|
+
exports.getEstimatedPoolShare = getEstimatedPoolShare;
|
78
|
+
const getLiquiditySlippage = ({ runeAmount, assetAmount, runeDepth, assetDepth, }) => {
|
79
|
+
if (runeAmount === '0' || assetAmount === '0' || runeDepth === '0' || assetDepth === '0')
|
80
|
+
return 0;
|
81
|
+
// formula: (t * R - T * r)/ (T*r + R*T)
|
82
|
+
const R = toTCSwapKitNumber(runeDepth);
|
83
|
+
const T = toTCSwapKitNumber(assetDepth);
|
84
|
+
const assetAddAmount = toTCSwapKitNumber(assetAmount);
|
85
|
+
const runeAddAmount = toTCSwapKitNumber(runeAmount);
|
86
|
+
const numerator = assetAddAmount.mul(R).sub(T.mul(runeAddAmount));
|
87
|
+
const denominator = T.mul(runeAddAmount).add(R.mul(T));
|
88
|
+
// set absolute value of percent, no negative allowed
|
89
|
+
return Math.abs(numerator.div(denominator).getBaseValue('number'));
|
90
|
+
};
|
91
|
+
exports.getLiquiditySlippage = getLiquiditySlippage;
|
@@ -0,0 +1,46 @@
|
|
1
|
+
import type { Chain } from '@coinmasters/types';
|
2
|
+
import { MemoType } from '@coinmasters/types';
|
3
|
+
export type ThornameRegisterParam = {
|
4
|
+
name: string;
|
5
|
+
chain: string;
|
6
|
+
address: string;
|
7
|
+
owner?: string;
|
8
|
+
preferredAsset?: string;
|
9
|
+
expiryBlock?: string;
|
10
|
+
};
|
11
|
+
type WithAddress<T = {}> = T & {
|
12
|
+
address: string;
|
13
|
+
};
|
14
|
+
type WithChain<T = {}> = T & {
|
15
|
+
chain: Chain;
|
16
|
+
};
|
17
|
+
export type MemoOptions<T extends MemoType> = {
|
18
|
+
[MemoType.BOND]: WithAddress;
|
19
|
+
[MemoType.LEAVE]: WithAddress;
|
20
|
+
[MemoType.CLOSE_LOAN]: WithAddress<{
|
21
|
+
asset: string;
|
22
|
+
minAmount?: string;
|
23
|
+
}>;
|
24
|
+
[MemoType.OPEN_LOAN]: WithAddress<{
|
25
|
+
asset: string;
|
26
|
+
minAmount?: string;
|
27
|
+
}>;
|
28
|
+
[MemoType.UNBOND]: WithAddress<{
|
29
|
+
unbondAmount: number;
|
30
|
+
}>;
|
31
|
+
[MemoType.DEPOSIT]: WithChain<{
|
32
|
+
symbol: string;
|
33
|
+
address?: string;
|
34
|
+
singleSide?: boolean;
|
35
|
+
}>;
|
36
|
+
[MemoType.WITHDRAW]: WithChain<{
|
37
|
+
ticker: string;
|
38
|
+
symbol: string;
|
39
|
+
basisPoints: number;
|
40
|
+
targetAssetString?: string;
|
41
|
+
singleSide?: boolean;
|
42
|
+
}>;
|
43
|
+
[MemoType.THORNAME_REGISTER]: Omit<ThornameRegisterParam, 'preferredAsset' | 'expiryBlock'>;
|
44
|
+
}[T];
|
45
|
+
export declare const getMemoFor: <T extends MemoType>(memoType: T, options: MemoOptions<T>) => string;
|
46
|
+
export {};
|
@@ -0,0 +1,43 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getMemoFor = void 0;
|
4
|
+
const types_1 = require("@coinmasters/types");
|
5
|
+
const getShortenedSymbol = ({ symbol, ticker, chain, }) => (chain === 'ETH' && ticker !== 'ETH' ? `${ticker}-${symbol.slice(-3)}` : symbol);
|
6
|
+
const getMemoFor = (memoType, options) => {
|
7
|
+
switch (memoType) {
|
8
|
+
case types_1.MemoType.LEAVE:
|
9
|
+
case types_1.MemoType.BOND: {
|
10
|
+
const { address } = options;
|
11
|
+
return `${memoType}:${address}`;
|
12
|
+
}
|
13
|
+
case types_1.MemoType.UNBOND: {
|
14
|
+
const { address, unbondAmount } = options;
|
15
|
+
return `${memoType}:${address}:${unbondAmount * Math.pow(10, 8)}`;
|
16
|
+
}
|
17
|
+
case types_1.MemoType.THORNAME_REGISTER: {
|
18
|
+
const { name, chain, address, owner } = options;
|
19
|
+
return `${memoType}:${name}:${chain}:${address}${owner ? `:${owner}` : ''}`;
|
20
|
+
}
|
21
|
+
case types_1.MemoType.DEPOSIT: {
|
22
|
+
const { chain, symbol, address, singleSide } = options;
|
23
|
+
return singleSide
|
24
|
+
? `${memoType}:${chain}/${symbol}::t:0`
|
25
|
+
: `${memoType}:${chain}.${symbol}:${address || ''}:t:0`;
|
26
|
+
}
|
27
|
+
case types_1.MemoType.WITHDRAW: {
|
28
|
+
const { chain, ticker, symbol, basisPoints, targetAssetString, singleSide } = options;
|
29
|
+
const target = !singleSide && targetAssetString ? `:${targetAssetString}` : '';
|
30
|
+
const shortenedSymbol = getShortenedSymbol({ chain, symbol, ticker });
|
31
|
+
const assetDivider = singleSide ? '/' : '.';
|
32
|
+
return `${memoType}:${chain}${assetDivider}${shortenedSymbol}:${basisPoints}${target}`;
|
33
|
+
}
|
34
|
+
case types_1.MemoType.OPEN_LOAN:
|
35
|
+
case types_1.MemoType.CLOSE_LOAN: {
|
36
|
+
const { asset, address } = options;
|
37
|
+
return `${memoType}:${asset}:${address}`; //:${minAmount ? `${minAmount}` : ''}:t:0`;
|
38
|
+
}
|
39
|
+
default:
|
40
|
+
return '';
|
41
|
+
}
|
42
|
+
};
|
43
|
+
exports.getMemoFor = getMemoFor;
|
@@ -0,0 +1,23 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.derivationPathToString = exports.validateTHORName = exports.getTHORNameCost = void 0;
|
4
|
+
// 10 rune for register, 1 rune per year
|
5
|
+
// MINIMUM_REGISTRATION_FEE = 11
|
6
|
+
const getTHORNameCost = (year) => {
|
7
|
+
if (year < 0)
|
8
|
+
throw new Error('Invalid number of year');
|
9
|
+
return 10 + year;
|
10
|
+
};
|
11
|
+
exports.getTHORNameCost = getTHORNameCost;
|
12
|
+
const validateTHORName = (name) => {
|
13
|
+
if (name.length > 30)
|
14
|
+
return false;
|
15
|
+
const regex = /^[a-zA-Z0-9+_-]+$/g;
|
16
|
+
return !!name.match(regex);
|
17
|
+
};
|
18
|
+
exports.validateTHORName = validateTHORName;
|
19
|
+
const derivationPathToString = ([network, chainId, account, change, index]) => {
|
20
|
+
const shortPath = typeof index !== 'number';
|
21
|
+
return `${network}'/${chainId}'/${account}'/${change}${shortPath ? '' : `/${index}`}`;
|
22
|
+
};
|
23
|
+
exports.derivationPathToString = derivationPathToString;
|
@@ -0,0 +1,79 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
19
|
+
var ownKeys = function(o) {
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
21
|
+
var ar = [];
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
23
|
+
return ar;
|
24
|
+
};
|
25
|
+
return ownKeys(o);
|
26
|
+
};
|
27
|
+
return function (mod) {
|
28
|
+
if (mod && mod.__esModule) return mod;
|
29
|
+
var result = {};
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
31
|
+
__setModuleDefault(result, mod);
|
32
|
+
return result;
|
33
|
+
};
|
34
|
+
})();
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
42
|
+
});
|
43
|
+
};
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
45
|
+
exports.RequestClient = void 0;
|
46
|
+
const headers = typeof window !== 'undefined'
|
47
|
+
? {}
|
48
|
+
: {
|
49
|
+
Referrer: 'https://sk.thorswap.net',
|
50
|
+
Referer: 'https://sk.thorswap.net',
|
51
|
+
};
|
52
|
+
// Define default options for the new ky instance
|
53
|
+
const defaultOptions = {
|
54
|
+
headers: headers,
|
55
|
+
timeout: 30000, // Example: 10000 milliseconds = 10 seconds
|
56
|
+
retry: {
|
57
|
+
limit: 2, // Maximum number of retry attempts
|
58
|
+
methods: ['get', 'post', 'put', 'patch', 'head', 'delete'], // HTTP methods to retry
|
59
|
+
statusCodes: [408, 500, 502, 503, 504], // HTTP status codes to retry
|
60
|
+
},
|
61
|
+
};
|
62
|
+
// Function to dynamically import ky and create an instance
|
63
|
+
function getKyInstance() {
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
65
|
+
const ky = (yield Promise.resolve().then(() => __importStar(require('ky')))).default;
|
66
|
+
return ky.create(defaultOptions);
|
67
|
+
});
|
68
|
+
}
|
69
|
+
exports.RequestClient = {
|
70
|
+
get: (url, options) => __awaiter(void 0, void 0, void 0, function* () {
|
71
|
+
const kyInstance = yield getKyInstance();
|
72
|
+
return kyInstance.get(url, options).json();
|
73
|
+
}),
|
74
|
+
post: (url, options) => __awaiter(void 0, void 0, void 0, function* () {
|
75
|
+
const kyInstance = yield getKyInstance();
|
76
|
+
return kyInstance.post(url, options).json();
|
77
|
+
}),
|
78
|
+
// Implement other methods (put, delete, patch, head) as needed
|
79
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const validateIdentifier: (identifier?: string) => boolean;
|
@@ -0,0 +1,16 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.validateIdentifier = void 0;
|
4
|
+
const types_1 = require("@coinmasters/types");
|
5
|
+
const supportedChains = Object.values(types_1.Chain);
|
6
|
+
const validateIdentifier = (identifier = '') => {
|
7
|
+
const uppercasedIdentifier = identifier.toUpperCase();
|
8
|
+
const [chain] = uppercasedIdentifier.split('.');
|
9
|
+
if (supportedChains.includes(chain))
|
10
|
+
return true;
|
11
|
+
const [synthChain] = uppercasedIdentifier.split('/');
|
12
|
+
if (supportedChains.includes(synthChain))
|
13
|
+
return true;
|
14
|
+
throw new Error(`Invalid identifier: ${identifier}. Expected format: <Chain>.<Ticker> or <Chain>.<Ticker>-<ContractAddress>`);
|
15
|
+
};
|
16
|
+
exports.validateIdentifier = validateIdentifier;
|