@clober/v2-sdk 0.0.59 → 0.0.61
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/cjs/abis/rebalancer/minter-abi.js +262 -0
- package/dist/cjs/abis/rebalancer/minter-abi.js.map +1 -0
- package/dist/cjs/abis/rebalancer/mock-swap-abi.js +95 -0
- package/dist/cjs/abis/rebalancer/mock-swap-abi.js.map +1 -0
- package/dist/cjs/abis/rebalancer/operator-abi.js +370 -0
- package/dist/cjs/abis/rebalancer/operator-abi.js.map +1 -0
- package/dist/cjs/abis/rebalancer/rebalancer-abi.js +1235 -0
- package/dist/cjs/abis/rebalancer/rebalancer-abi.js.map +1 -0
- package/dist/cjs/abis/rebalancer/strategy-abi.js +633 -0
- package/dist/cjs/abis/rebalancer/strategy-abi.js.map +1 -0
- package/dist/cjs/apis/market.js +2 -2
- package/dist/cjs/apis/market.js.map +1 -1
- package/dist/cjs/apis/odos.js +100 -0
- package/dist/cjs/apis/odos.js.map +1 -0
- package/dist/cjs/apis/pool.js +61 -0
- package/dist/cjs/apis/pool.js.map +1 -0
- package/dist/cjs/apis/strategy.js +23 -0
- package/dist/cjs/apis/strategy.js.map +1 -0
- package/dist/cjs/call.js +359 -1
- package/dist/cjs/call.js.map +1 -1
- package/dist/cjs/constants/addresses.js +23 -3
- package/dist/cjs/constants/addresses.js.map +1 -1
- package/dist/cjs/constants/chain.js +5 -1
- package/dist/cjs/constants/chain.js.map +1 -1
- package/dist/cjs/constants/permit.js +13 -0
- package/dist/cjs/constants/permit.js.map +1 -0
- package/dist/cjs/constants/subgraph.js +1 -1
- package/dist/cjs/index.js +0 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/model/market.js +45 -0
- package/dist/cjs/model/market.js.map +1 -1
- package/dist/cjs/model/pool.js +136 -0
- package/dist/cjs/model/pool.js.map +1 -0
- package/dist/cjs/model/strategy.js +3 -0
- package/dist/cjs/model/strategy.js.map +1 -0
- package/dist/cjs/type.js.map +1 -1
- package/dist/cjs/utils/math.js +3 -1
- package/dist/cjs/utils/math.js.map +1 -1
- package/dist/cjs/utils/open.js +34 -35
- package/dist/cjs/utils/open.js.map +1 -1
- package/dist/cjs/utils/pool-key.js +17 -0
- package/dist/cjs/utils/pool-key.js.map +1 -0
- package/dist/cjs/utils/pool.js +88 -0
- package/dist/cjs/utils/pool.js.map +1 -0
- package/dist/cjs/utils/prices.js +7 -2
- package/dist/cjs/utils/prices.js.map +1 -1
- package/dist/cjs/view.js +37 -30
- package/dist/cjs/view.js.map +1 -1
- package/dist/esm/abis/rebalancer/minter-abi.js +259 -0
- package/dist/esm/abis/rebalancer/minter-abi.js.map +1 -0
- package/dist/esm/abis/rebalancer/mock-swap-abi.js +92 -0
- package/dist/esm/abis/rebalancer/mock-swap-abi.js.map +1 -0
- package/dist/esm/abis/rebalancer/operator-abi.js +367 -0
- package/dist/esm/abis/rebalancer/operator-abi.js.map +1 -0
- package/dist/esm/abis/rebalancer/rebalancer-abi.js +1232 -0
- package/dist/esm/abis/rebalancer/rebalancer-abi.js.map +1 -0
- package/dist/esm/abis/rebalancer/strategy-abi.js +630 -0
- package/dist/esm/abis/rebalancer/strategy-abi.js.map +1 -0
- package/dist/esm/apis/market.js +3 -3
- package/dist/esm/apis/market.js.map +1 -1
- package/dist/esm/apis/odos.js +94 -0
- package/dist/esm/apis/odos.js.map +1 -0
- package/dist/esm/apis/pool.js +57 -0
- package/dist/esm/apis/pool.js.map +1 -0
- package/dist/esm/apis/strategy.js +19 -0
- package/dist/esm/apis/strategy.js.map +1 -0
- package/dist/esm/call.js +390 -45
- package/dist/esm/call.js.map +1 -1
- package/dist/esm/constants/addresses.js +24 -4
- package/dist/esm/constants/addresses.js.map +1 -1
- package/dist/esm/constants/chain.js +3 -0
- package/dist/esm/constants/chain.js.map +1 -1
- package/dist/esm/constants/permit.js +10 -0
- package/dist/esm/constants/permit.js.map +1 -0
- package/dist/esm/constants/subgraph.js +1 -1
- package/dist/esm/index.js +0 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/model/market.js +46 -1
- package/dist/esm/model/market.js.map +1 -1
- package/dist/esm/model/pool.js +133 -0
- package/dist/esm/model/pool.js.map +1 -0
- package/dist/esm/model/strategy.js +2 -0
- package/dist/esm/model/strategy.js.map +1 -0
- package/dist/esm/type.js.map +1 -1
- package/dist/esm/utils/math.js +1 -0
- package/dist/esm/utils/math.js.map +1 -1
- package/dist/esm/utils/open.js +32 -33
- package/dist/esm/utils/open.js.map +1 -1
- package/dist/esm/utils/pool-key.js +12 -0
- package/dist/esm/utils/pool-key.js.map +1 -0
- package/dist/esm/utils/pool.js +84 -0
- package/dist/esm/utils/pool.js.map +1 -0
- package/dist/esm/utils/prices.js +5 -1
- package/dist/esm/utils/prices.js.map +1 -1
- package/dist/esm/view.js +62 -43
- package/dist/esm/view.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/abis/rebalancer/minter-abi.d.ts +198 -0
- package/dist/types/abis/rebalancer/minter-abi.d.ts.map +1 -0
- package/dist/types/abis/rebalancer/mock-swap-abi.d.ts +70 -0
- package/dist/types/abis/rebalancer/mock-swap-abi.d.ts.map +1 -0
- package/dist/types/abis/rebalancer/operator-abi.d.ts +283 -0
- package/dist/types/abis/rebalancer/operator-abi.d.ts.map +1 -0
- package/dist/types/abis/rebalancer/rebalancer-abi.d.ts +952 -0
- package/dist/types/abis/rebalancer/rebalancer-abi.d.ts.map +1 -0
- package/dist/types/abis/rebalancer/strategy-abi.d.ts +487 -0
- package/dist/types/abis/rebalancer/strategy-abi.d.ts.map +1 -0
- package/dist/types/apis/odos.d.ts +28 -0
- package/dist/types/apis/odos.d.ts.map +1 -0
- package/dist/types/apis/pool.d.ts +5 -0
- package/dist/types/apis/pool.d.ts.map +1 -0
- package/dist/types/apis/strategy.d.ts +5 -0
- package/dist/types/apis/strategy.d.ts.map +1 -0
- package/dist/types/call.d.ts +108 -44
- package/dist/types/call.d.ts.map +1 -1
- package/dist/types/constants/addresses.d.ts +4 -0
- package/dist/types/constants/addresses.d.ts.map +1 -1
- package/dist/types/constants/chain.d.ts +1 -0
- package/dist/types/constants/chain.d.ts.map +1 -1
- package/dist/types/constants/permit.d.ts +10 -0
- package/dist/types/constants/permit.d.ts.map +1 -0
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/model/currency.d.ts +7 -0
- package/dist/types/model/currency.d.ts.map +1 -1
- package/dist/types/model/market.d.ts +3 -0
- package/dist/types/model/market.d.ts.map +1 -1
- package/dist/types/model/pool.d.ts +38 -0
- package/dist/types/model/pool.d.ts.map +1 -0
- package/dist/types/model/strategy.d.ts +6 -0
- package/dist/types/model/strategy.d.ts.map +1 -0
- package/dist/types/type.d.ts +20 -1
- package/dist/types/type.d.ts.map +1 -1
- package/dist/types/utils/math.d.ts +1 -0
- package/dist/types/utils/math.d.ts.map +1 -1
- package/dist/types/utils/open.d.ts +1 -1
- package/dist/types/utils/open.d.ts.map +1 -1
- package/dist/types/utils/pool-key.d.ts +3 -0
- package/dist/types/utils/pool-key.d.ts.map +1 -0
- package/dist/types/utils/pool.d.ts +11 -0
- package/dist/types/utils/pool.d.ts.map +1 -0
- package/dist/types/utils/prices.d.ts +1 -0
- package/dist/types/utils/prices.d.ts.map +1 -1
- package/dist/types/view.d.ts +52 -17
- package/dist/types/view.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/cjs/signature.js +0 -149
- package/dist/cjs/signature.js.map +0 -1
- package/dist/esm/signature.js +0 -172
- package/dist/esm/signature.js.map +0 -1
- package/dist/types/signature.d.ts +0 -38
- package/dist/types/signature.d.ts.map +0 -1
package/dist/cjs/apis/market.js
CHANGED
|
@@ -21,7 +21,7 @@ const getBook = async (publicClient, chainId, quoteCurrency, baseCurrency, useSu
|
|
|
21
21
|
const bookId = (0, book_id_1.toBookId)(chainId, quoteCurrency.address, baseCurrency.address, unitSize);
|
|
22
22
|
if (useSubgraph) {
|
|
23
23
|
const { data: { book }, } = await fetchBookFromSubgraph(chainId, bookId.toString());
|
|
24
|
-
new book_1.Book({
|
|
24
|
+
return new book_1.Book({
|
|
25
25
|
chainId,
|
|
26
26
|
id: bookId,
|
|
27
27
|
base: baseCurrency,
|
|
@@ -43,7 +43,7 @@ const getBook = async (publicClient, chainId, quoteCurrency, baseCurrency, useSu
|
|
|
43
43
|
functionName: 'getLiquidity',
|
|
44
44
|
args: [bookId, Number(2n ** 19n - 1n), BigInt(n)],
|
|
45
45
|
}),
|
|
46
|
-
(0, open_1.
|
|
46
|
+
(0, open_1.fetchIsMarketOpened)(publicClient, chainId, bookId),
|
|
47
47
|
]);
|
|
48
48
|
return new book_1.Book({
|
|
49
49
|
chainId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"market.js","sourceRoot":"","sources":["../../../src/apis/market.ts"],"names":[],"mappings":";;;AAGA,4CAAwC;AACxC,wCAAoC;AACpC,4CAA6C;AAC7C,8CAA2C;AAC3C,kDAAsD;AAEtD,sDAA2D;AAC3D,kEAA8D;AAC9D,
|
|
1
|
+
{"version":3,"file":"market.js","sourceRoot":"","sources":["../../../src/apis/market.ts"],"names":[],"mappings":";;;AAGA,4CAAwC;AACxC,wCAAoC;AACpC,4CAA6C;AAC7C,8CAA2C;AAC3C,kDAAsD;AAEtD,sDAA2D;AAC3D,kEAA8D;AAC9D,wCAAmD;AACnD,gDAAiD;AACjD,oDAAgD;AAEhD,MAAM,qBAAqB,GAAG,KAAK,EAAE,OAAkB,EAAE,MAAc,EAAE,EAAE;IACzE,OAAO,mBAAQ,CAAC,GAAG,CAWjB,OAAO,EACP,SAAS,EACT,iFAAiF,EACjF;QACE,MAAM;KACP,CACF,CAAA;AACH,CAAC,CAAA;AAED,MAAM,OAAO,GAAG,KAAK,EACnB,YAA0B,EAC1B,OAAkB,EAClB,aAAuB,EACvB,YAAsB,EACtB,WAAoB,EACpB,CAAS,EACM,EAAE;IACjB,MAAM,QAAQ,GAAG,MAA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CAAC,CAAA;IAEF,OAAO,IAAI,eAAM,CAAC;QAChB,OAAO;QACP,MAAM,EAAE,CAAC,aAAa,EAAE,YAAY,CAAC;QACrC,OAAO;QACP,OAAO;KACR,CAAC,CAAA;AACJ,CAAC;AA/BD,kCA+BC"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchCallData = exports.fetchQuote = exports.fetchOdosApi = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const chain_1 = require("../constants/chain");
|
|
6
|
+
const mock_swap_abi_1 = require("../abis/rebalancer/mock-swap-abi");
|
|
7
|
+
async function fetchOdosApi(path, options) {
|
|
8
|
+
const endpoint = `https://api.odos.xyz/${path}`;
|
|
9
|
+
const response = await fetch(endpoint, options);
|
|
10
|
+
if (response.ok) {
|
|
11
|
+
return response.json();
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
const errorResponse = await response.json();
|
|
15
|
+
throw new Error(errorResponse.message || 'Unknown Error');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.fetchOdosApi = fetchOdosApi;
|
|
19
|
+
async function fetchQuote({ chainId, amountIn, tokenIn, tokenOut, slippageLimitPercent, userAddress, testnetPrice, }) {
|
|
20
|
+
if ((0, chain_1.isTestnetChain)(chainId)) {
|
|
21
|
+
if (!testnetPrice) {
|
|
22
|
+
throw new Error('Missing testnetPrice for testnet chain');
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
amountOut: (0, viem_1.parseUnits)((Number((0, viem_1.formatUnits)(amountIn, tokenIn.decimals)) * testnetPrice).toFixed(), tokenOut.decimals),
|
|
26
|
+
pathId: '0x',
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const { outAmounts, pathId, } = await fetchOdosApi('sor/quote/v2', {
|
|
30
|
+
method: 'POST',
|
|
31
|
+
headers: {
|
|
32
|
+
'Content-Type': 'application/json',
|
|
33
|
+
accept: 'application/json',
|
|
34
|
+
},
|
|
35
|
+
body: JSON.stringify({
|
|
36
|
+
chainId,
|
|
37
|
+
inputTokens: [
|
|
38
|
+
{
|
|
39
|
+
tokenAddress: tokenIn.address,
|
|
40
|
+
amount: amountIn.toString(),
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
outputTokens: [
|
|
44
|
+
{
|
|
45
|
+
tokenAddress: tokenOut.address,
|
|
46
|
+
proportion: 1,
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
userAddr: userAddress,
|
|
50
|
+
slippageLimitPercent,
|
|
51
|
+
}),
|
|
52
|
+
});
|
|
53
|
+
return {
|
|
54
|
+
amountOut: BigInt(outAmounts[0]),
|
|
55
|
+
pathId,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
exports.fetchQuote = fetchQuote;
|
|
59
|
+
async function fetchCallData({ chainId, amountIn, tokenIn, tokenOut, slippageLimitPercent, userAddress, testnetPrice, }) {
|
|
60
|
+
if ((0, chain_1.isTestnetChain)(chainId)) {
|
|
61
|
+
if (!testnetPrice) {
|
|
62
|
+
throw new Error('Missing testnetPrice for testnet chain');
|
|
63
|
+
}
|
|
64
|
+
const amountOut = (0, viem_1.parseUnits)((Number((0, viem_1.formatUnits)(amountIn, tokenIn.decimals)) * testnetPrice).toFixed(), tokenOut.decimals);
|
|
65
|
+
return {
|
|
66
|
+
amountOut,
|
|
67
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
68
|
+
abi: mock_swap_abi_1.MOCK_SWAP_ABI,
|
|
69
|
+
functionName: 'swap',
|
|
70
|
+
args: [tokenIn.address, amountIn, tokenOut.address, amountOut],
|
|
71
|
+
}),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const { pathId, amountOut } = await fetchQuote({
|
|
75
|
+
chainId,
|
|
76
|
+
amountIn,
|
|
77
|
+
tokenIn,
|
|
78
|
+
tokenOut,
|
|
79
|
+
slippageLimitPercent,
|
|
80
|
+
userAddress,
|
|
81
|
+
});
|
|
82
|
+
const { transaction } = await fetchOdosApi('sor/assemble', {
|
|
83
|
+
method: 'POST',
|
|
84
|
+
headers: {
|
|
85
|
+
'Content-Type': 'application/json',
|
|
86
|
+
accept: 'application/json',
|
|
87
|
+
},
|
|
88
|
+
body: JSON.stringify({
|
|
89
|
+
pathId,
|
|
90
|
+
simulate: false,
|
|
91
|
+
userAddr: userAddress,
|
|
92
|
+
}),
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
amountOut,
|
|
96
|
+
data: transaction.data,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
exports.fetchCallData = fetchCallData;
|
|
100
|
+
//# sourceMappingURL=odos.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"odos.js","sourceRoot":"","sources":["../../../src/apis/odos.ts"],"names":[],"mappings":";;;AAAA,+BAAkE;AAElE,8CAA8D;AAC9D,oEAAgE;AAGzD,KAAK,UAAU,YAAY,CAChC,IAAY,EACZ,OAAqB;IAErB,MAAM,QAAQ,GAAG,wBAAwB,IAAI,EAAE,CAAA;IAC/C,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;IAE/C,IAAI,QAAQ,CAAC,EAAE,EAAE,CAAC;QAChB,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAA;IACxB,CAAC;SAAM,CAAC;QACN,MAAM,aAAa,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAA;QAE3C,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,OAAO,IAAI,eAAe,CAAC,CAAA;IAC3D,CAAC;AACH,CAAC;AAdD,oCAcC;AAEM,KAAK,UAAU,UAAU,CAAC,EAC/B,OAAO,EACP,QAAQ,EACR,OAAO,EACP,QAAQ,EACR,oBAAoB,EACpB,WAAW,EACX,YAAY,GASb;IAIC,IAAI,IAAA,sBAAc,EAAC,OAAO,CAAC,E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AAC,CAAA;QAC3D,CAAC;QAED,MAAM,SAAS,GAAG,IAAA,iBAAU,EAC1B,CACE,MAAM,CAAC,IAAA,kBAAW,EAAC,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,YAAY,CAC/D,CAAC,OAAO,EAAE,EACX,QAAQ,CAAC,QAAQ,CAClB,CAAA;QAED,OAAO;YACL,SAAS;YACT,IAAI,EAAE,IAAA,yBAAkB,EAAC;gBACvB,GAAG,EAAE,6BAAa;gBAClB,YAAY,EAAE,MAAM;gBACpB,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,QAAQ,EAAE,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC;aAC/D,CAAC;SACH,CAAA;IACH,CAAC;IACD,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,UAAU,CAAC;QAC7C,OAAO;QACP,QAAQ;QACR,OAAO;QACP,QAAQ;QACR,oBAAoB;QACpB,WAAW;KACZ,CAAC,CAAA;IAEF,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,YAAY,CAUvC,cAAc,EAAE;QACjB,MAAM,EAAE,MAAM;QACd,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,MAAM,EAAE,kBAAkB;SAC3B;QACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;YACnB,MAAM;YACN,QAAQ,EAAE,KAAK;YACf,QAAQ,EAAE,WAAW;SACtB,CAAC;KACH,CAAC,CAAA;IACF,OAAO;QACL,SAAS;QACT,IAAI,EAAE,WAAW,CAAC,IAAI;KACvB,CAAA;AACH,CAAC;AA5ED,sCA4EC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchPool = void 0;
|
|
4
|
+
const pool_1 = require("../model/pool");
|
|
5
|
+
const addresses_1 = require("../constants/addresses");
|
|
6
|
+
const pool_key_1 = require("../utils/pool-key");
|
|
7
|
+
const order_1 = require("../utils/order");
|
|
8
|
+
const rebalancer_abi_1 = require("../abis/rebalancer/rebalancer-abi");
|
|
9
|
+
const market_1 = require("./market");
|
|
10
|
+
async function fetchPool(publicClient, chainId, tokenAddresses, salt, useSubgraph) {
|
|
11
|
+
if (tokenAddresses.length !== 2) {
|
|
12
|
+
throw new Error('Invalid token pair');
|
|
13
|
+
}
|
|
14
|
+
const market = await (0, market_1.fetchMarket)(publicClient, chainId, tokenAddresses, useSubgraph);
|
|
15
|
+
const poolKey = (0, pool_key_1.toPoolKey)(market.bidBook.id, market.askBook.id, salt);
|
|
16
|
+
const [{ bookIdA, bookIdB, reserveA, reserveB, orderListA, orderListB }, totalSupply, [liquidityA, liquidityB],] = await publicClient.multicall({
|
|
17
|
+
allowFailure: false,
|
|
18
|
+
contracts: [
|
|
19
|
+
{
|
|
20
|
+
address: addresses_1.CONTRACT_ADDRESSES[chainId].Rebalancer,
|
|
21
|
+
abi: rebalancer_abi_1.REBALANCER_ABI,
|
|
22
|
+
functionName: 'getPool',
|
|
23
|
+
args: [poolKey],
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
address: addresses_1.CONTRACT_ADDRESSES[chainId].Rebalancer,
|
|
27
|
+
abi: rebalancer_abi_1.REBALANCER_ABI,
|
|
28
|
+
functionName: 'totalSupply',
|
|
29
|
+
args: [BigInt(poolKey)],
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
address: addresses_1.CONTRACT_ADDRESSES[chainId].Rebalancer,
|
|
33
|
+
abi: rebalancer_abi_1.REBALANCER_ABI,
|
|
34
|
+
functionName: 'getLiquidity',
|
|
35
|
+
args: [poolKey],
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
const orders = await (0, order_1.fetchOrders)(publicClient, chainId, [...orderListA, ...orderListB], useSubgraph);
|
|
40
|
+
return new pool_1.Pool({
|
|
41
|
+
chainId,
|
|
42
|
+
market,
|
|
43
|
+
isOpened: bookIdA > 0 && bookIdB > 0,
|
|
44
|
+
bookIdA,
|
|
45
|
+
bookIdB,
|
|
46
|
+
salt,
|
|
47
|
+
poolKey,
|
|
48
|
+
totalSupply: BigInt(totalSupply),
|
|
49
|
+
decimals: market.base.decimals > market.quote.decimals
|
|
50
|
+
? market.base.decimals
|
|
51
|
+
: market.quote.decimals,
|
|
52
|
+
liquidityA: BigInt(liquidityA),
|
|
53
|
+
liquidityB: BigInt(liquidityB),
|
|
54
|
+
reserveA: BigInt(reserveA),
|
|
55
|
+
reserveB: BigInt(reserveB),
|
|
56
|
+
orderListA: orders.filter((order) => orderListA.includes(BigInt(order.id))),
|
|
57
|
+
orderListB: orders.filter((order) => orderListB.includes(BigInt(order.id))),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
exports.fetchPool = fetchPool;
|
|
61
|
+
//# sourceMappingURL=pool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pool.js","sourceRoot":"","sources":["../../../src/apis/pool.ts"],"names":[],"mappings":";;;AAGA,wCAAoC;AACpC,sDAA2D;AAC3D,gDAA6C;AAC7C,0CAA4C;AAC5C,sEAAkE;AAElE,qCAAsC;AAE/B,KAAK,UAAU,SAAS,CAC7B,YAA0B,EAC1B,OAAkB,EAClB,cAA+B,EAC/B,IAAmB,EACnB,WAAoB;IAEpB,IAAI,cAAc,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAA;IACvC,CAAC;IACD,MAAM,MAAM,GAAG,MAAM,IAAA,oBAAW,EAC9B,YAAY,EACZ,OAAO,EACP,cAAc,EACd,WAAW,CACZ,CAAA;IACD,MAAM,OAAO,GAAG,IAAA,oBAAS,EAAC,MAAM,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,EAAE,IAAI,CAAC,CAAA;IACrE,MAAM,CACJ,EAAE,OAAO,EAAE,OAA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,UAAU,EAAE,MAAM,CAAC,UAAU,CAAC;QAC9B,UAAU,EAAE,MAAM,CAAC,UAAU,CAAC;QAC9B,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC;QAC1B,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC;QAC1B,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;QAC3E,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;KAC5E,CAAC,CAAA;AACJ,CAAC;AAtED,8BAsEC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchStrategyPrice = void 0;
|
|
4
|
+
const addresses_1 = require("../constants/addresses");
|
|
5
|
+
const strategy_abi_1 = require("../abis/rebalancer/strategy-abi");
|
|
6
|
+
const prices_1 = require("../utils/prices");
|
|
7
|
+
const pool_1 = require("./pool");
|
|
8
|
+
async function fetchStrategyPrice(publicClient, chainId, tokenAddresses, salt, useSubgraph) {
|
|
9
|
+
const pool = await (0, pool_1.fetchPool)(publicClient, chainId, tokenAddresses, salt, useSubgraph);
|
|
10
|
+
const getPriceResult = await publicClient.readContract({
|
|
11
|
+
address: addresses_1.CONTRACT_ADDRESSES[chainId].Strategy,
|
|
12
|
+
abi: strategy_abi_1.STRATEGY_ABI,
|
|
13
|
+
functionName: 'getPrice',
|
|
14
|
+
args: [pool.key],
|
|
15
|
+
});
|
|
16
|
+
return {
|
|
17
|
+
oraclePrice: (0, prices_1.formatPrice)(BigInt(getPriceResult.oraclePrice), pool.currencyA.decimals, pool.currencyB.decimals),
|
|
18
|
+
tickA: BigInt(getPriceResult.tickA),
|
|
19
|
+
tickB: BigInt(getPriceResult.tickB),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
exports.fetchStrategyPrice = fetchStrategyPrice;
|
|
23
|
+
//# sourceMappingURL=strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strategy.js","sourceRoot":"","sources":["../../../src/apis/strategy.ts"],"names":[],"mappings":";;;AAGA,sDAA2D;AAE3D,kEAA8D;AAC9D,4CAA6C;AAE7C,iCAAkC;AAE3B,KAAK,UAAU,kBAAkB,CACtC,YAA0B,EAC1B,OAAkB,EAClB,cAA+B,EAC/B,IAAmB,EACnB,WAAoB;IAEpB,MAAM,IAAI,GAAG,MAAM,IAAA,gBAAS,EAC1B,YAAY,EACZ,OAAO,EACP,cAAc,EACd,IAAI,EACJ,WAAW,CACZ,CAAA;IACD,MAAM,cAAc,GAAG,MAAM,YAAY,CAAC,YAAY,CAAC;QACrD,OAAO,EAAE,8BAAkB,CAAC,OAAO,CAAE,CAAC,QAAQ;QAC9C,GAAG,EAAE,2BAAY;QACjB,YAAY,EAAE,UAAU;QACxB,IAAI,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC;KACjB,CAAC,CAAA;IACF,OAAO;QACL,WAAW,EAAE,IAAA,oBAAW,EACtB,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,EAClC,IAAI,CAAC,SAAS,CAAC,QAAQ,EACvB,IAAI,CAAC,SAAS,CAAC,QAAQ,CACxB;QACD,KAAK,EAAE,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC;QACnC,KAAK,EAAE,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC;KACpC,CAAA;AACH,CAAC;AA7BD,gDA6BC"}
|
package/dist/cjs/call.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.cancelOrders = exports.cancelOrder = exports.claimOrders = exports.claimOrder = exports.marketOrder = exports.limitOrder = exports.openMarket = void 0;
|
|
3
|
+
exports.updateStrategyPrice = exports.rebalance = exports.removeLiquidity = exports.addLiquidity = exports.openPool = exports.cancelOrders = exports.cancelOrder = exports.claimOrders = exports.claimOrder = exports.marketOrder = exports.limitOrder = exports.openMarket = void 0;
|
|
4
4
|
const viem_1 = require("viem");
|
|
5
5
|
const chain_1 = require("./constants/chain");
|
|
6
6
|
const unit_size_1 = require("./utils/unit-size");
|
|
@@ -17,6 +17,15 @@ const book_id_1 = require("./utils/book-id");
|
|
|
17
17
|
const approval_1 = require("./utils/approval");
|
|
18
18
|
const order_1 = require("./utils/order");
|
|
19
19
|
const bigint_1 = require("./utils/bigint");
|
|
20
|
+
const pool_1 = require("./apis/pool");
|
|
21
|
+
const rebalancer_abi_1 = require("./abis/rebalancer/rebalancer-abi");
|
|
22
|
+
const pool_2 = require("./utils/pool");
|
|
23
|
+
const odos_1 = require("./apis/odos");
|
|
24
|
+
const minter_abi_1 = require("./abis/rebalancer/minter-abi");
|
|
25
|
+
const permit_1 = require("./constants/permit");
|
|
26
|
+
const math_1 = require("./utils/math");
|
|
27
|
+
const pool_key_1 = require("./utils/pool-key");
|
|
28
|
+
const operator_abi_1 = require("./abis/rebalancer/operator-abi");
|
|
20
29
|
const openMarket = async ({ chainId, userAddress, inputToken, outputToken, options, }) => {
|
|
21
30
|
const publicClient = (0, viem_1.createPublicClient)({
|
|
22
31
|
chain: chain_1.CHAIN_MAP[chainId],
|
|
@@ -504,4 +513,353 @@ const cancelOrders = async ({ chainId, userAddress, ids, options, }) => {
|
|
|
504
513
|
};
|
|
505
514
|
};
|
|
506
515
|
exports.cancelOrders = cancelOrders;
|
|
516
|
+
const openPool = async ({ chainId, userAddress, tokenA, tokenB, salt, options, }) => {
|
|
517
|
+
const publicClient = (0, viem_1.createPublicClient)({
|
|
518
|
+
chain: chain_1.CHAIN_MAP[chainId],
|
|
519
|
+
transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
|
|
520
|
+
});
|
|
521
|
+
const pool = await (0, pool_1.fetchPool)(publicClient, chainId, [tokenA, tokenB], salt, !!(options && options.useSubgraph));
|
|
522
|
+
if (!pool.isOpened) {
|
|
523
|
+
return (0, build_transaction_1.buildTransaction)(publicClient, {
|
|
524
|
+
chain: chain_1.CHAIN_MAP[chainId],
|
|
525
|
+
address: addresses_1.CONTRACT_ADDRESSES[chainId].Rebalancer,
|
|
526
|
+
account: userAddress,
|
|
527
|
+
abi: rebalancer_abi_1.REBALANCER_ABI,
|
|
528
|
+
functionName: 'open',
|
|
529
|
+
args: [
|
|
530
|
+
{
|
|
531
|
+
base: pool.market.bidBook.base.address,
|
|
532
|
+
unitSize: pool.market.bidBook.unitSize,
|
|
533
|
+
quote: pool.market.bidBook.quote.address,
|
|
534
|
+
makerPolicy: fee_1.MAKER_DEFAULT_POLICY[chainId].value,
|
|
535
|
+
hooks: viem_1.zeroAddress,
|
|
536
|
+
takerPolicy: fee_1.TAKER_DEFAULT_POLICY[chainId].value,
|
|
537
|
+
},
|
|
538
|
+
{
|
|
539
|
+
base: pool.market.askBook.base.address,
|
|
540
|
+
unitSize: pool.market.askBook.unitSize,
|
|
541
|
+
quote: pool.market.askBook.quote.address,
|
|
542
|
+
makerPolicy: fee_1.MAKER_DEFAULT_POLICY[chainId].value,
|
|
543
|
+
hooks: viem_1.zeroAddress,
|
|
544
|
+
takerPolicy: fee_1.TAKER_DEFAULT_POLICY[chainId].value,
|
|
545
|
+
},
|
|
546
|
+
(0, pool_key_1.toBytes32)(salt),
|
|
547
|
+
addresses_1.CONTRACT_ADDRESSES[chainId].Strategy,
|
|
548
|
+
],
|
|
549
|
+
}, options?.gasLimit);
|
|
550
|
+
}
|
|
551
|
+
return undefined;
|
|
552
|
+
};
|
|
553
|
+
exports.openPool = openPool;
|
|
554
|
+
const addLiquidity = async ({ chainId, userAddress, token0, token1, salt, amount0, amount1, options, }) => {
|
|
555
|
+
const publicClient = (0, viem_1.createPublicClient)({
|
|
556
|
+
chain: chain_1.CHAIN_MAP[chainId],
|
|
557
|
+
transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
|
|
558
|
+
});
|
|
559
|
+
const pool = await (0, pool_1.fetchPool)(publicClient, chainId, [token0, token1], salt, !!(options && options.useSubgraph));
|
|
560
|
+
if (!pool.isOpened) {
|
|
561
|
+
throw new Error(`
|
|
562
|
+
Open the pool before adding liquidity.
|
|
563
|
+
import { openPool } from '@clober/v2-sdk'
|
|
564
|
+
|
|
565
|
+
const transaction = await openPool({
|
|
566
|
+
chainId: ${chainId},
|
|
567
|
+
tokenA: '${token0}',
|
|
568
|
+
tokenB: '${token1}',
|
|
569
|
+
})
|
|
570
|
+
`);
|
|
571
|
+
}
|
|
572
|
+
const [amountAOrigin, amountBOrigin] = (0, viem_1.isAddressEqual)(pool.currencyA.address, (0, viem_1.getAddress)(token0))
|
|
573
|
+
? [
|
|
574
|
+
(0, viem_1.parseUnits)(amount0 ?? '0', pool.currencyA.decimals),
|
|
575
|
+
(0, viem_1.parseUnits)(amount1 ?? '0', pool.currencyB.decimals),
|
|
576
|
+
]
|
|
577
|
+
: [
|
|
578
|
+
(0, viem_1.parseUnits)(amount1 ?? '0', pool.currencyA.decimals),
|
|
579
|
+
(0, viem_1.parseUnits)(amount0 ?? '0', pool.currencyB.decimals),
|
|
580
|
+
];
|
|
581
|
+
let [amountA, amountB] = [amountAOrigin, amountBOrigin];
|
|
582
|
+
const tokenAPermitParams = (0, viem_1.isAddressEqual)(pool.currencyA.address, (0, viem_1.getAddress)(token0))
|
|
583
|
+
? options?.token0PermitParams ?? permit_1.emptyERC20PermitParams
|
|
584
|
+
: options?.token1PermitParams ?? permit_1.emptyERC20PermitParams;
|
|
585
|
+
const tokenBPermitParams = (0, viem_1.isAddressEqual)(pool.currencyA.address, (0, viem_1.getAddress)(token0))
|
|
586
|
+
? options?.token1PermitParams ?? permit_1.emptyERC20PermitParams
|
|
587
|
+
: options?.token0PermitParams ?? permit_1.emptyERC20PermitParams;
|
|
588
|
+
let disableSwap = !!(options && options.disableSwap);
|
|
589
|
+
if (pool.totalSupply === 0n ||
|
|
590
|
+
(pool.liquidityA === 0n && pool.liquidityB === 0n)) {
|
|
591
|
+
disableSwap = true;
|
|
592
|
+
}
|
|
593
|
+
const slippageLimitPercent = options?.slippage ?? 2;
|
|
594
|
+
const swapParams = {
|
|
595
|
+
inCurrency: viem_1.zeroAddress,
|
|
596
|
+
amount: 0n,
|
|
597
|
+
data: '0x',
|
|
598
|
+
};
|
|
599
|
+
if (!disableSwap) {
|
|
600
|
+
const token0Price = Number(options?.testnetPrice ? options.testnetPrice : '1');
|
|
601
|
+
const currencyBPerCurrencyA = (0, viem_1.isAddressEqual)(token1, pool.currencyB.address)
|
|
602
|
+
? token0Price
|
|
603
|
+
: 1 / token0Price;
|
|
604
|
+
const swapAmountA = (0, viem_1.parseUnits)('1', pool.currencyA.decimals);
|
|
605
|
+
const { amountOut: swapAmountB } = await (0, odos_1.fetchQuote)({
|
|
606
|
+
chainId,
|
|
607
|
+
amountIn: swapAmountA,
|
|
608
|
+
tokenIn: pool.currencyA,
|
|
609
|
+
tokenOut: pool.currencyB,
|
|
610
|
+
slippageLimitPercent: 20,
|
|
611
|
+
userAddress: addresses_1.CONTRACT_ADDRESSES[chainId].Minter,
|
|
612
|
+
testnetPrice: currencyBPerCurrencyA,
|
|
613
|
+
});
|
|
614
|
+
const { deltaA, deltaB } = (0, pool_2.getIdealDelta)(amountA, amountB, pool.liquidityA, pool.liquidityB, swapAmountA, swapAmountB);
|
|
615
|
+
if (deltaA < 0n) {
|
|
616
|
+
swapParams.inCurrency = pool.currencyA.address;
|
|
617
|
+
swapParams.amount = -deltaA;
|
|
618
|
+
const { amountOut: actualDeltaB, data: calldata } = await (0, odos_1.fetchCallData)({
|
|
619
|
+
chainId,
|
|
620
|
+
amountIn: swapParams.amount,
|
|
621
|
+
tokenIn: pool.currencyA,
|
|
622
|
+
tokenOut: pool.currencyB,
|
|
623
|
+
slippageLimitPercent,
|
|
624
|
+
userAddress: addresses_1.CONTRACT_ADDRESSES[chainId].Minter,
|
|
625
|
+
testnetPrice: currencyBPerCurrencyA,
|
|
626
|
+
});
|
|
627
|
+
swapParams.data = calldata;
|
|
628
|
+
amountA += deltaA;
|
|
629
|
+
amountB += actualDeltaB;
|
|
630
|
+
}
|
|
631
|
+
else if (deltaB < 0n) {
|
|
632
|
+
swapParams.inCurrency = pool.currencyB.address;
|
|
633
|
+
swapParams.amount = -deltaB;
|
|
634
|
+
const { amountOut: actualDeltaA, data: calldata } = await (0, odos_1.fetchCallData)({
|
|
635
|
+
chainId,
|
|
636
|
+
amountIn: swapParams.amount,
|
|
637
|
+
tokenIn: pool.currencyB,
|
|
638
|
+
tokenOut: pool.currencyA,
|
|
639
|
+
slippageLimitPercent,
|
|
640
|
+
userAddress: addresses_1.CONTRACT_ADDRESSES[chainId].Minter,
|
|
641
|
+
testnetPrice: 1 / currencyBPerCurrencyA,
|
|
642
|
+
});
|
|
643
|
+
swapParams.data = calldata;
|
|
644
|
+
amountA += actualDeltaA;
|
|
645
|
+
amountB += deltaB;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
const { mintAmount, inAmountA, inAmountB } = (0, pool_2.getExpectedMintResult)(pool.totalSupply, pool.liquidityA, pool.liquidityB, amountA, amountB, pool.currencyA, pool.currencyB);
|
|
649
|
+
if (mintAmount === 0n) {
|
|
650
|
+
return {
|
|
651
|
+
transaction: undefined,
|
|
652
|
+
result: {
|
|
653
|
+
currencyA: {
|
|
654
|
+
currency: pool.currencyA,
|
|
655
|
+
amount: '0',
|
|
656
|
+
direction: 'in',
|
|
657
|
+
},
|
|
658
|
+
currencyB: {
|
|
659
|
+
currency: pool.currencyB,
|
|
660
|
+
amount: '0',
|
|
661
|
+
direction: 'in',
|
|
662
|
+
},
|
|
663
|
+
lpCurrency: {
|
|
664
|
+
currency: pool.currencyLp,
|
|
665
|
+
amount: '0',
|
|
666
|
+
direction: 'out',
|
|
667
|
+
},
|
|
668
|
+
},
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
const minMintAmount = (0, bigint_1.applyPercent)(mintAmount, 100 - slippageLimitPercent);
|
|
672
|
+
const transaction = await (0, build_transaction_1.buildTransaction)(publicClient, {
|
|
673
|
+
chain: chain_1.CHAIN_MAP[chainId],
|
|
674
|
+
account: userAddress,
|
|
675
|
+
address: addresses_1.CONTRACT_ADDRESSES[chainId].Minter,
|
|
676
|
+
abi: minter_abi_1.MINTER_ABI,
|
|
677
|
+
functionName: 'mint',
|
|
678
|
+
args: [
|
|
679
|
+
pool.key,
|
|
680
|
+
amountAOrigin,
|
|
681
|
+
amountBOrigin,
|
|
682
|
+
minMintAmount,
|
|
683
|
+
{
|
|
684
|
+
permitAmount: tokenAPermitParams.permitAmount,
|
|
685
|
+
signature: tokenAPermitParams.signature,
|
|
686
|
+
},
|
|
687
|
+
{
|
|
688
|
+
permitAmount: tokenBPermitParams.permitAmount,
|
|
689
|
+
signature: tokenBPermitParams.signature,
|
|
690
|
+
},
|
|
691
|
+
swapParams,
|
|
692
|
+
],
|
|
693
|
+
}, options?.gasLimit);
|
|
694
|
+
const currencyARefund = amountA - inAmountA;
|
|
695
|
+
const currencyBRefund = amountB - inAmountB;
|
|
696
|
+
const currencyAResultAmount = amountAOrigin - currencyARefund;
|
|
697
|
+
const currencyBResultAmount = amountBOrigin - currencyBRefund;
|
|
698
|
+
return {
|
|
699
|
+
transaction,
|
|
700
|
+
result: {
|
|
701
|
+
currencyA: {
|
|
702
|
+
currency: pool.currencyA,
|
|
703
|
+
amount: (0, viem_1.formatUnits)((0, math_1.abs)(currencyAResultAmount), pool.currencyA.decimals),
|
|
704
|
+
direction: currencyAResultAmount >= 0 ? 'in' : 'out',
|
|
705
|
+
},
|
|
706
|
+
currencyB: {
|
|
707
|
+
currency: pool.currencyB,
|
|
708
|
+
amount: (0, viem_1.formatUnits)((0, math_1.abs)(currencyBResultAmount), pool.currencyB.decimals),
|
|
709
|
+
direction: currencyBResultAmount >= 0 ? 'in' : 'out',
|
|
710
|
+
},
|
|
711
|
+
lpCurrency: {
|
|
712
|
+
currency: pool.currencyLp,
|
|
713
|
+
amount: (0, viem_1.formatUnits)(mintAmount, pool.currencyLp.decimals),
|
|
714
|
+
direction: 'out',
|
|
715
|
+
},
|
|
716
|
+
},
|
|
717
|
+
};
|
|
718
|
+
};
|
|
719
|
+
exports.addLiquidity = addLiquidity;
|
|
720
|
+
const removeLiquidity = async ({ chainId, userAddress, token0, token1, salt, amount, options, }) => {
|
|
721
|
+
const publicClient = (0, viem_1.createPublicClient)({
|
|
722
|
+
chain: chain_1.CHAIN_MAP[chainId],
|
|
723
|
+
transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
|
|
724
|
+
});
|
|
725
|
+
const pool = await (0, pool_1.fetchPool)(publicClient, chainId, [token0, token1], salt, !!(options && options.useSubgraph));
|
|
726
|
+
if (!pool.isOpened) {
|
|
727
|
+
throw new Error(`
|
|
728
|
+
Open the pool before removing liquidity.
|
|
729
|
+
import { openPool } from '@clober/v2-sdk'
|
|
730
|
+
|
|
731
|
+
const transaction = await openPool({
|
|
732
|
+
chainId: ${chainId},
|
|
733
|
+
tokenA: '${token0}',
|
|
734
|
+
tokenB: '${token1}',
|
|
735
|
+
})
|
|
736
|
+
`);
|
|
737
|
+
}
|
|
738
|
+
const burnAmount = (0, viem_1.parseUnits)(amount, pool.currencyLp.decimals);
|
|
739
|
+
const slippageLimitPercent = options?.slippage ?? 2;
|
|
740
|
+
const withdrawAmountA = (burnAmount * pool.liquidityA) / pool.totalSupply;
|
|
741
|
+
const withdrawAmountB = (burnAmount * pool.liquidityB) / pool.totalSupply;
|
|
742
|
+
const minWithdrawAmountA = (0, bigint_1.applyPercent)(withdrawAmountA, 100 - slippageLimitPercent);
|
|
743
|
+
const minWithdrawAmountB = (0, bigint_1.applyPercent)(withdrawAmountB, 100 - slippageLimitPercent);
|
|
744
|
+
if (burnAmount === 0n) {
|
|
745
|
+
return {
|
|
746
|
+
transaction: undefined,
|
|
747
|
+
result: {
|
|
748
|
+
currencyA: {
|
|
749
|
+
currency: pool.currencyA,
|
|
750
|
+
amount: '0',
|
|
751
|
+
direction: 'out',
|
|
752
|
+
},
|
|
753
|
+
currencyB: {
|
|
754
|
+
currency: pool.currencyB,
|
|
755
|
+
amount: '0',
|
|
756
|
+
direction: 'out',
|
|
757
|
+
},
|
|
758
|
+
lpCurrency: {
|
|
759
|
+
currency: pool.currencyLp,
|
|
760
|
+
amount: '0',
|
|
761
|
+
direction: 'in',
|
|
762
|
+
},
|
|
763
|
+
},
|
|
764
|
+
};
|
|
765
|
+
}
|
|
766
|
+
const transaction = await (0, build_transaction_1.buildTransaction)(publicClient, {
|
|
767
|
+
chain: chain_1.CHAIN_MAP[chainId],
|
|
768
|
+
account: userAddress,
|
|
769
|
+
address: addresses_1.CONTRACT_ADDRESSES[chainId].Rebalancer,
|
|
770
|
+
abi: rebalancer_abi_1.REBALANCER_ABI,
|
|
771
|
+
functionName: 'burn',
|
|
772
|
+
args: [pool.key, burnAmount, minWithdrawAmountA, minWithdrawAmountB],
|
|
773
|
+
}, options?.gasLimit);
|
|
774
|
+
return {
|
|
775
|
+
transaction,
|
|
776
|
+
result: {
|
|
777
|
+
currencyA: {
|
|
778
|
+
currency: pool.currencyA,
|
|
779
|
+
amount: (0, viem_1.formatUnits)(withdrawAmountA, pool.currencyA.decimals),
|
|
780
|
+
direction: 'out',
|
|
781
|
+
},
|
|
782
|
+
currencyB: {
|
|
783
|
+
currency: pool.currencyB,
|
|
784
|
+
amount: (0, viem_1.formatUnits)(withdrawAmountB, pool.currencyB.decimals),
|
|
785
|
+
direction: 'out',
|
|
786
|
+
},
|
|
787
|
+
lpCurrency: {
|
|
788
|
+
currency: pool.currencyLp,
|
|
789
|
+
amount: amount,
|
|
790
|
+
direction: 'in',
|
|
791
|
+
},
|
|
792
|
+
},
|
|
793
|
+
};
|
|
794
|
+
};
|
|
795
|
+
exports.removeLiquidity = removeLiquidity;
|
|
796
|
+
const rebalance = async ({ chainId, userAddress, token0, token1, salt, options, }) => {
|
|
797
|
+
const publicClient = (0, viem_1.createPublicClient)({
|
|
798
|
+
chain: chain_1.CHAIN_MAP[chainId],
|
|
799
|
+
transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
|
|
800
|
+
});
|
|
801
|
+
const pool = await (0, pool_1.fetchPool)(publicClient, chainId, [token0, token1], salt, !!(options && options.useSubgraph));
|
|
802
|
+
if (!pool.isOpened) {
|
|
803
|
+
throw new Error(`
|
|
804
|
+
Open the pool before rebalancing pool.
|
|
805
|
+
import { openPool } from '@clober/v2-sdk'
|
|
806
|
+
|
|
807
|
+
const transaction = await openPool({
|
|
808
|
+
chainId: ${chainId},
|
|
809
|
+
tokenA: '${token0}',
|
|
810
|
+
tokenB: '${token1}',
|
|
811
|
+
})
|
|
812
|
+
`);
|
|
813
|
+
}
|
|
814
|
+
return (0, build_transaction_1.buildTransaction)(publicClient, {
|
|
815
|
+
chain: chain_1.CHAIN_MAP[chainId],
|
|
816
|
+
account: userAddress,
|
|
817
|
+
address: addresses_1.CONTRACT_ADDRESSES[chainId].Rebalancer,
|
|
818
|
+
abi: rebalancer_abi_1.REBALANCER_ABI,
|
|
819
|
+
functionName: 'rebalance',
|
|
820
|
+
args: [pool.key],
|
|
821
|
+
}, options?.gasLimit);
|
|
822
|
+
};
|
|
823
|
+
exports.rebalance = rebalance;
|
|
824
|
+
const updateStrategyPrice = async ({ chainId, userAddress, token0, token1, salt, oraclePrice, priceA, priceB, options, }) => {
|
|
825
|
+
const publicClient = (0, viem_1.createPublicClient)({
|
|
826
|
+
chain: chain_1.CHAIN_MAP[chainId],
|
|
827
|
+
transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
|
|
828
|
+
});
|
|
829
|
+
const pool = await (0, pool_1.fetchPool)(publicClient, chainId, [token0, token1], salt, !!(options && options.useSubgraph));
|
|
830
|
+
if (!pool.isOpened) {
|
|
831
|
+
throw new Error(`
|
|
832
|
+
Open the pool before updating strategy price.
|
|
833
|
+
import { openPool } from '@clober/v2-sdk'
|
|
834
|
+
|
|
835
|
+
const transaction = await openPool({
|
|
836
|
+
chainId: ${chainId},
|
|
837
|
+
tokenA: '${token0}',
|
|
838
|
+
tokenB: '${token1}',
|
|
839
|
+
})
|
|
840
|
+
`);
|
|
841
|
+
}
|
|
842
|
+
const [roundingUpPriceA, roundingUpPriceB] = [
|
|
843
|
+
options?.roundingUpPriceA ? options.roundingUpPriceA : false,
|
|
844
|
+
options?.roundingUpPriceB ? options.roundingUpPriceB : false,
|
|
845
|
+
];
|
|
846
|
+
const { roundingDownTick: roundingDownTickA, roundingUpTick: roundingUpTickA, } = (0, prices_1.parsePrice)(Number(priceA), pool.currencyA.decimals, pool.currencyB.decimals);
|
|
847
|
+
const { roundingDownTick: roundingDownTickB, roundingUpTick: roundingUpTickB, } = (0, prices_1.parsePrice)(Number(priceB), pool.currencyA.decimals, pool.currencyB.decimals);
|
|
848
|
+
const oracleRawPrice = (0, prices_1.convertHumanReadablePriceToRawPrice)(Number(oraclePrice), pool.currencyA.decimals, pool.currencyB.decimals);
|
|
849
|
+
const tickA = options?.tickA
|
|
850
|
+
? Number(options.tickA)
|
|
851
|
+
: Number(roundingUpPriceA ? roundingUpTickA : roundingDownTickA);
|
|
852
|
+
const tickB = options?.tickB
|
|
853
|
+
? Number(options.tickB)
|
|
854
|
+
: Number((0, tick_1.invertTick)(roundingUpPriceB ? roundingUpTickB : roundingDownTickB));
|
|
855
|
+
return (0, build_transaction_1.buildTransaction)(publicClient, {
|
|
856
|
+
chain: chain_1.CHAIN_MAP[chainId],
|
|
857
|
+
account: userAddress,
|
|
858
|
+
address: addresses_1.CONTRACT_ADDRESSES[chainId].Operator,
|
|
859
|
+
abi: operator_abi_1.OPERATOR_ABI,
|
|
860
|
+
functionName: 'updatePrice',
|
|
861
|
+
args: [pool.key, oracleRawPrice, tickA, tickB],
|
|
862
|
+
}, options?.gasLimit);
|
|
863
|
+
};
|
|
864
|
+
exports.updateStrategyPrice = updateStrategyPrice;
|
|
507
865
|
//# sourceMappingURL=call.js.map
|