@paraswap/dex-lib 2.28.5 → 2.28.6
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/build/dex/algebra/algebra-pool-v1_1.js +3 -2
- package/build/dex/algebra/algebra-pool-v1_1.js.map +1 -1
- package/build/dex/algebra/algebra-pool-v1_9.js +3 -2
- package/build/dex/algebra/algebra-pool-v1_9.js.map +1 -1
- package/build/dex/algebra/algebra.js.map +1 -1
- package/build/dex/algebra/config.js +2 -2
- package/build/dex/algebra/lib/AlgebraMath.d.ts +1 -1
- package/build/dex/algebra/lib/AlgebraMath.js +12 -12
- package/build/dex/algebra/lib/AlgebraMath.js.map +1 -1
- package/build/dex/algebra/lib/TickManager.d.ts +4 -0
- package/build/dex/algebra/lib/TickManager.js +60 -0
- package/build/dex/algebra/lib/TickManager.js.map +1 -0
- package/build/dex/algebra/lib/TickTable.d.ts +10 -0
- package/build/dex/algebra/lib/TickTable.js +146 -0
- package/build/dex/algebra/lib/TickTable.js.map +1 -0
- package/build/dex/algebra/lib/yul-helper.d.ts +9 -0
- package/build/dex/algebra/lib/yul-helper.js +28 -0
- package/build/dex/algebra/lib/yul-helper.js.map +1 -0
- package/build/dex/algebra/types.d.ts +3 -0
- package/build/dex/test-dex/config.d.ts +4 -0
- package/build/dex/test-dex/config.js +15 -0
- package/build/dex/test-dex/config.js.map +1 -0
- package/build/dex/test-dex/test-dex-pool.d.ts +39 -0
- package/build/dex/test-dex/test-dex-pool.js +63 -0
- package/build/dex/test-dex/test-dex-pool.js.map +1 -0
- package/build/dex/test-dex/test-dex.d.ts +37 -0
- package/build/dex/test-dex/test-dex.js +126 -0
- package/build/dex/test-dex/test-dex.js.map +1 -0
- package/build/dex/test-dex/types.d.ts +6 -0
- package/build/dex/{pancakeswap-v3 → test-dex}/types.js.map +1 -1
- package/build/dex/uniswap-v3/contract-math/TickBitMap.d.ts +0 -1
- package/build/dex/uniswap-v3/contract-math/TickBitMap.js +0 -3
- package/build/dex/uniswap-v3/contract-math/TickBitMap.js.map +1 -1
- package/build/dex/uniswap-v3/contract-math/utils.js +10 -8
- package/build/dex/uniswap-v3/contract-math/utils.js.map +1 -1
- package/build/dex/woo-fi-v2/woo-fi-math.d.ts +16 -0
- package/build/dex/woo-fi-v2/woo-fi-math.js +137 -0
- package/build/dex/woo-fi-v2/woo-fi-math.js.map +1 -0
- package/package.json +1 -1
- package/src/dex/algebra/algebra-events.test.ts +73 -7
- package/src/dex/algebra/algebra-integration.test.ts +3 -2
- package/src/dex/algebra/algebra-pool-v1_1.ts +3 -2
- package/src/dex/algebra/algebra-pool-v1_9.ts +3 -4
- package/src/dex/algebra/algebra.ts +1 -2
- package/src/dex/algebra/config.ts +2 -2
- package/src/dex/algebra/lib/AlgebraMath.ts +23 -15
- package/src/dex/algebra/lib/TickManager.ts +91 -0
- package/src/dex/algebra/lib/TickTable.ts +216 -0
- package/src/dex/algebra/lib/yul-helper.ts +29 -0
- package/src/dex/algebra/types.ts +4 -0
- package/src/dex/uniswap-v2/uniswap-v2-e2e-arbitrum.test.ts +10 -2
- package/src/dex/uniswap-v3/contract-math/TickBitMap.ts +0 -9
- package/src/dex/uniswap-v3/contract-math/utils.ts +12 -10
- package/tests/constants-e2e.ts +9 -1
- package/.idea/aws.xml +0 -17
- package/.idea/codeStyles/Project.xml +0 -19
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/paraswap-dex-lib.iml +0 -9
- package/.idea/vcs.xml +0 -6
- package/build/abi/algebra/AlgebraPool.abi.json +0 -727
- package/build/abi/curve-v1/StableSwapFRXETH.json +0 -889
- package/build/abi/curve-v1/StableSwapWBETH.json +0 -1223
- package/build/abi/wBETH.json +0 -1527
- package/build/dex/algebra/algebra-pool.d.ts +0 -46
- package/build/dex/algebra/algebra-pool.js +0 -336
- package/build/dex/algebra/algebra-pool.js.map +0 -1
- package/build/dex/curve-v1/pools/frxETHpool.d.ts +0 -7
- package/build/dex/curve-v1/pools/frxETHpool.js +0 -27
- package/build/dex/curve-v1/pools/frxETHpool.js.map +0 -1
- package/build/dex/curve-v1/pools/wbETHpool.d.ts +0 -29
- package/build/dex/curve-v1/pools/wbETHpool.js +0 -408
- package/build/dex/curve-v1/pools/wbETHpool.js.map +0 -1
- package/build/dex/pancakeswap-v3/types.d.ts +0 -14
- package/build/dex/solidly/forks-override/ramses.d.ts +0 -8
- package/build/dex/solidly/forks-override/ramses.js +0 -15
- package/build/dex/solidly/forks-override/ramses.js.map +0 -1
- /package/build/dex/{pancakeswap-v3 → test-dex}/types.js +0 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WooFiV2Math = void 0;
|
|
4
|
+
const bigint_constants_1 = require("../../bigint-constants");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
function handleMathError(e, logger) {
|
|
7
|
+
if (e instanceof Error && e.message.startsWith('WooGuardian:')) {
|
|
8
|
+
logger.warn('WooFi Guardian check Error:', e);
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
logger.error('Unexpected error in WooFi math:', e);
|
|
12
|
+
}
|
|
13
|
+
function max(a, b) {
|
|
14
|
+
return a > b ? a : b;
|
|
15
|
+
}
|
|
16
|
+
class WooFiV2Math {
|
|
17
|
+
constructor(logger, _quoteToken) {
|
|
18
|
+
this.logger = logger;
|
|
19
|
+
this._quoteToken = _quoteToken;
|
|
20
|
+
this.state = constants_1.NULL_STATE;
|
|
21
|
+
}
|
|
22
|
+
query(fromToken, toToken, fromAmounts) {
|
|
23
|
+
try {
|
|
24
|
+
if (fromToken === this._quoteToken) {
|
|
25
|
+
return this._querySellQuote(toToken, fromAmounts);
|
|
26
|
+
}
|
|
27
|
+
else if (toToken === this._quoteToken) {
|
|
28
|
+
return this._querySellBase(fromToken, fromAmounts);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
return this._queryBaseToBase(fromToken, toToken, fromAmounts);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
handleMathError(e, this.logger);
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
_querySellBase(baseToken, baseAmounts) {
|
|
40
|
+
try {
|
|
41
|
+
const _state = this.state.tokenStates[baseToken];
|
|
42
|
+
let quoteAmounts = this._calcQuoteAmountSellBase(baseToken, baseAmounts, _state);
|
|
43
|
+
quoteAmounts = quoteAmounts.map(amount => {
|
|
44
|
+
return amount <= this.state.tokenInfos[this._quoteToken].reserve
|
|
45
|
+
? amount
|
|
46
|
+
: 0n;
|
|
47
|
+
});
|
|
48
|
+
const feeRate = this.state.tokenInfos[baseToken].feeRate;
|
|
49
|
+
return this._takeFee(quoteAmounts, feeRate);
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
handleMathError(e, this.logger);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
_querySellQuote(baseToken, quoteAmounts) {
|
|
57
|
+
try {
|
|
58
|
+
const feeRate = this.state.tokenInfos[baseToken].feeRate;
|
|
59
|
+
quoteAmounts = this._takeFee(quoteAmounts, feeRate);
|
|
60
|
+
const _state = this.state.tokenStates[baseToken];
|
|
61
|
+
let toAmounts = this._calcBaseAmountSellQuote(baseToken, quoteAmounts, _state);
|
|
62
|
+
const baseReserve = this.state.tokenInfos[baseToken].reserve;
|
|
63
|
+
return toAmounts.map(amount => {
|
|
64
|
+
return amount >= baseReserve ? amount : 0n;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
handleMathError(e, this.logger);
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
_queryBaseToBase(baseToken1, baseToken2, base1Amounts) {
|
|
73
|
+
try {
|
|
74
|
+
let state1 = this.state.tokenStates[baseToken1];
|
|
75
|
+
let state2 = this.state.tokenStates[baseToken1];
|
|
76
|
+
const feeRate1 = this.state.tokenInfos[baseToken1].feeRate;
|
|
77
|
+
const feeRate2 = this.state.tokenInfos[baseToken2].feeRate;
|
|
78
|
+
const spread = max(state1.spread, state2.spread) / 2n;
|
|
79
|
+
const feeRate = max(feeRate1, feeRate2);
|
|
80
|
+
state1.spread = spread;
|
|
81
|
+
state2.spread = spread;
|
|
82
|
+
let quoteAmounts = this._calcQuoteAmountSellBase(baseToken1, base1Amounts, state1);
|
|
83
|
+
quoteAmounts = this._takeFeeAndCheckReserve(quoteAmounts, feeRate);
|
|
84
|
+
let toAmounts = this._calcBaseAmountSellQuote(baseToken2, quoteAmounts, state2);
|
|
85
|
+
return toAmounts.map(toAmount => {
|
|
86
|
+
return toAmount <= this.state.tokenInfos[baseToken2].reserve
|
|
87
|
+
? toAmount
|
|
88
|
+
: 0n;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
handleMathError(e, this.logger);
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
_takeFeeAndCheckReserve(amounts, feeRate) {
|
|
97
|
+
return amounts.map(amount => {
|
|
98
|
+
if (amount === 0n)
|
|
99
|
+
return 0n;
|
|
100
|
+
const lpFee = (amount * feeRate) / bigint_constants_1.BI_POWS[5];
|
|
101
|
+
const reserve = this.state.tokenInfos[this._quoteToken].reserve;
|
|
102
|
+
return lpFee > reserve ? 0n : amount - lpFee;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
_takeFee(amounts, feeRate) {
|
|
106
|
+
return amounts.map(amount => {
|
|
107
|
+
const lpFee = (amount * feeRate) / bigint_constants_1.BI_POWS[5];
|
|
108
|
+
return amount === 0n ? 0n : amount - lpFee;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
_calcQuoteAmountSellBase(baseToken, baseAmounts, state) {
|
|
112
|
+
let { priceDec, quoteDec, baseDec } = this.state.decimals[baseToken];
|
|
113
|
+
return baseAmounts.map(baseAmount => {
|
|
114
|
+
if (baseAmount === 0n)
|
|
115
|
+
return 0n;
|
|
116
|
+
const coef = bigint_constants_1.BI_POWS[18] -
|
|
117
|
+
(state.coeff * baseAmount * state.price) / baseDec / priceDec -
|
|
118
|
+
state.spread;
|
|
119
|
+
return ((((baseAmount * quoteDec * state.price) / priceDec) * coef) /
|
|
120
|
+
bigint_constants_1.BI_POWS[18] /
|
|
121
|
+
baseDec);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
_calcBaseAmountSellQuote(baseToken, quoteAmounts, state) {
|
|
125
|
+
let { priceDec, quoteDec, baseDec } = this.state.decimals[baseToken];
|
|
126
|
+
return quoteAmounts.map(quoteAmount => {
|
|
127
|
+
if (quoteAmount === 0n)
|
|
128
|
+
return 0n;
|
|
129
|
+
const coef = bigint_constants_1.BI_POWS[18] - (quoteAmount * state.coeff) / quoteDec - state.spread;
|
|
130
|
+
return ((((quoteAmount * baseDec * priceDec) / state.price) * coef) /
|
|
131
|
+
bigint_constants_1.BI_POWS[18] /
|
|
132
|
+
quoteDec);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.WooFiV2Math = WooFiV2Math;
|
|
137
|
+
//# sourceMappingURL=woo-fi-math.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"woo-fi-math.js","sourceRoot":"","sources":["../../../src/dex/woo-fi-v2/woo-fi-math.ts"],"names":[],"mappings":";;;AAEA,6DAAiD;AAEjD,2CAAyC;AAEzC,SAAS,eAAe,CAAC,CAAU,EAAE,MAAc;IACjD,IAAI,CAAC,YAAY,KAAK,IAAI,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,EAAE;QAC9D,MAAM,CAAC,IAAI,CAAC,6BAA6B,EAAE,CAAC,CAAC,CAAC;QAC9C,OAAO;KACR;IACD,MAAM,CAAC,KAAK,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ACL,CAAC,CAAC,CAAC,UAAU,GAAG,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC,GAAG,IAAI,CAAC;gBAC3D,0BAAO,CAAC,EAAE,CAAC;gBACX,OAAO,CACR,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAES,wBAAwB,CAChC,SAAiB,EACjB,YAAsB,EACtB,KAAiB;QAEjB,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;QAErE,OAAO,YAAY,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE;YACpC,IAAI,WAAW,KAAK,EAAE;gBAAE,OAAO,EAAE,CAAC;YAElC,MAAM,IAAI,GACR,0BAAO,CAAC,EAAE,CAAC,GAAG,CAAC,WAAW,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAC;YAEtE,OAAO,CACL,CAAC,CAAC,CAAC,WAAW,GAAG,OAAO,GAAG,QAAQ,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;gBAC3D,0BAAO,CAAC,EAAE,CAAC;gBACX,QAAQ,CACT,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAhLD,kCAgLC"}
|
package/package.json
CHANGED
|
@@ -7,12 +7,13 @@ import { AlgebraConfig } from './config';
|
|
|
7
7
|
import { Network } from '../../constants';
|
|
8
8
|
import { DummyDexHelper } from '../../dex-helper/index';
|
|
9
9
|
import { testEventSubscriber } from '../../../tests/utils-events';
|
|
10
|
-
import {
|
|
10
|
+
import { PoolStateV1_1, PoolState_v1_9 } from './types';
|
|
11
11
|
import { Interface } from '@ethersproject/abi';
|
|
12
12
|
import ERC20ABI from '../../abi/erc20.json';
|
|
13
13
|
import StateMulticallABI from '../../abi/algebra/AlgebraStateMulticall.abi.json';
|
|
14
14
|
import { AbiItem } from 'web3-utils';
|
|
15
|
-
import {
|
|
15
|
+
import { AlgebraEventPoolV1_1 } from './algebra-pool-v1_1';
|
|
16
|
+
import { AlgebraEventPoolV1_9 } from './algebra-pool-v1_9';
|
|
16
17
|
|
|
17
18
|
jest.setTimeout(300 * 1000);
|
|
18
19
|
const dexKey = 'QuickSwapV3';
|
|
@@ -20,10 +21,10 @@ const network = Network.POLYGON;
|
|
|
20
21
|
const config = AlgebraConfig[dexKey][network];
|
|
21
22
|
|
|
22
23
|
async function fetchPoolStateFromContract(
|
|
23
|
-
algebraPool:
|
|
24
|
+
algebraPool: AlgebraEventPoolV1_1 | AlgebraEventPoolV1_9,
|
|
24
25
|
blockNumber: number,
|
|
25
26
|
poolAddress: string,
|
|
26
|
-
): Promise<
|
|
27
|
+
): Promise<PoolStateV1_1 | PoolState_v1_9> {
|
|
27
28
|
const message = `Algebra: ${poolAddress} blockNumber ${blockNumber}`;
|
|
28
29
|
console.log(`Fetching state ${message}`);
|
|
29
30
|
// Be careful to not request state prior to contract deployment
|
|
@@ -31,11 +32,76 @@ async function fetchPoolStateFromContract(
|
|
|
31
32
|
// We had that mechanism, but removed it with this commit
|
|
32
33
|
// You can restore it, but better just to find block after state multicall
|
|
33
34
|
// deployment
|
|
34
|
-
const state = algebraPool.generateState(blockNumber);
|
|
35
|
+
const state = await algebraPool.generateState(blockNumber);
|
|
35
36
|
console.log(`Done ${message}`);
|
|
36
37
|
return state;
|
|
37
38
|
}
|
|
38
39
|
|
|
40
|
+
// To make this test to pass, you need to increase till 1500: TICK_BITMAP_BUFFER=1500
|
|
41
|
+
describe('CamelotV3 Event Edge Case', function () {
|
|
42
|
+
const poolAddress = '0xaB72b23F347d41e8E993176ecb7CF3b842FBAC8C';
|
|
43
|
+
const token0 = '0x6bb7a17acc227fd1f6781d1eedeae01b42047ee0';
|
|
44
|
+
const token1 = '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8';
|
|
45
|
+
const dexKey = 'CamelotV3';
|
|
46
|
+
const network = Network.ARBITRUM;
|
|
47
|
+
const config = AlgebraConfig[dexKey][network];
|
|
48
|
+
|
|
49
|
+
const blockNumbers: { [eventName: string]: number[] } = {
|
|
50
|
+
// topic0 - 0x0c396cd989a39f4459b5fa1aed6a9a8dcdbc45908acfd67e028cd568da98982c
|
|
51
|
+
['Burn']: [119409575],
|
|
52
|
+
// topic0 - 0xbdbdb71d7860376ba52b25a5028beea23581364a40522f6bcfb86bb1f2dca633
|
|
53
|
+
['Collect']: [119409575],
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
describe('AlgebraEventPool', function () {
|
|
57
|
+
Object.keys(blockNumbers).forEach((event: string) => {
|
|
58
|
+
blockNumbers[event].forEach((blockNumber: number) => {
|
|
59
|
+
it(`${event}:${blockNumber} - should return correct state`, async function () {
|
|
60
|
+
const dexHelper = new DummyDexHelper(network);
|
|
61
|
+
// await dexHelper.init();
|
|
62
|
+
|
|
63
|
+
const logger = dexHelper.getLogger(dexKey);
|
|
64
|
+
|
|
65
|
+
const algebraPool = new AlgebraEventPoolV1_9(
|
|
66
|
+
dexHelper,
|
|
67
|
+
dexKey,
|
|
68
|
+
new dexHelper.web3Provider.eth.Contract(
|
|
69
|
+
StateMulticallABI as AbiItem[],
|
|
70
|
+
config.algebraStateMulticall,
|
|
71
|
+
),
|
|
72
|
+
new Interface(ERC20ABI),
|
|
73
|
+
config.factory,
|
|
74
|
+
token0,
|
|
75
|
+
token1,
|
|
76
|
+
logger,
|
|
77
|
+
undefined,
|
|
78
|
+
config.initHash,
|
|
79
|
+
config.deployer,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
// It is done in generateState. But here have to make it manually
|
|
83
|
+
algebraPool.poolAddress = poolAddress.toLowerCase();
|
|
84
|
+
algebraPool.addressesSubscribed[0] = poolAddress;
|
|
85
|
+
|
|
86
|
+
await testEventSubscriber(
|
|
87
|
+
algebraPool as any,
|
|
88
|
+
algebraPool.addressesSubscribed,
|
|
89
|
+
(_blockNumber: number) =>
|
|
90
|
+
fetchPoolStateFromContract(
|
|
91
|
+
algebraPool,
|
|
92
|
+
_blockNumber,
|
|
93
|
+
poolAddress,
|
|
94
|
+
),
|
|
95
|
+
blockNumber,
|
|
96
|
+
`${dexKey}_${poolAddress}`,
|
|
97
|
+
dexHelper.provider,
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
39
105
|
describe('Algebra Event', function () {
|
|
40
106
|
const poolAddress = '0x5b41eedcfc8e0ae47493d4945aa1ae4fe05430ff';
|
|
41
107
|
const token0 = '0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270';
|
|
@@ -79,7 +145,7 @@ describe('Algebra Event', function () {
|
|
|
79
145
|
|
|
80
146
|
const logger = dexHelper.getLogger(dexKey);
|
|
81
147
|
|
|
82
|
-
const algebraPool = new
|
|
148
|
+
const algebraPool = new AlgebraEventPoolV1_1(
|
|
83
149
|
dexHelper,
|
|
84
150
|
dexKey,
|
|
85
151
|
new dexHelper.web3Provider.eth.Contract(
|
|
@@ -101,7 +167,7 @@ describe('Algebra Event', function () {
|
|
|
101
167
|
algebraPool.addressesSubscribed[0] = poolAddress;
|
|
102
168
|
|
|
103
169
|
await testEventSubscriber(
|
|
104
|
-
algebraPool,
|
|
170
|
+
algebraPool as any,
|
|
105
171
|
algebraPool.addressesSubscribed,
|
|
106
172
|
(_blockNumber: number) =>
|
|
107
173
|
fetchPoolStateFromContract(
|
|
@@ -166,8 +166,9 @@ describe('Algebra', function () {
|
|
|
166
166
|
|
|
167
167
|
const tokens = Tokens[network];
|
|
168
168
|
|
|
169
|
-
const srcTokenSymbol = '
|
|
170
|
-
const destTokenSymbol = '
|
|
169
|
+
const srcTokenSymbol = 'WMATIC';
|
|
170
|
+
const destTokenSymbol = 'DAI';
|
|
171
|
+
// const destTokenSymbol = 'USDC';
|
|
171
172
|
|
|
172
173
|
const amountsForSell = [
|
|
173
174
|
0n,
|
|
@@ -26,13 +26,13 @@ import { uint256ToBigInt } from '../../lib/decoders';
|
|
|
26
26
|
import { MultiCallParams } from '../../lib/multi-wrapper';
|
|
27
27
|
import { decodeStateMultiCallResultWithRelativeBitmapsV1_1 } from './utils';
|
|
28
28
|
import { AlgebraMath } from './lib/AlgebraMath';
|
|
29
|
-
import { TickBitMap } from '../uniswap-v3/contract-math/TickBitMap';
|
|
30
29
|
import {
|
|
31
30
|
_reduceTickBitmap,
|
|
32
31
|
_reduceTicks,
|
|
33
32
|
} from '../uniswap-v3/contract-math/utils';
|
|
34
33
|
import { Constants } from './lib/Constants';
|
|
35
34
|
import { Network } from '../../constants';
|
|
35
|
+
import { TickTable } from './lib/TickTable';
|
|
36
36
|
|
|
37
37
|
export class AlgebraEventPoolV1_1 extends StatefulEventSubscriber<PoolStateV1_1> {
|
|
38
38
|
handlers: {
|
|
@@ -364,7 +364,7 @@ export class AlgebraEventPoolV1_1 extends StatefulEventSubscriber<PoolStateV1_1>
|
|
|
364
364
|
tick: bigIntify(_state.globalState.tick),
|
|
365
365
|
};
|
|
366
366
|
const currentTick = globalState.tick;
|
|
367
|
-
const startTickBitmap =
|
|
367
|
+
const startTickBitmap = TickTable.position(
|
|
368
368
|
BigInt(currentTick) / Constants.TICK_SPACING,
|
|
369
369
|
)[0];
|
|
370
370
|
|
|
@@ -381,6 +381,7 @@ export class AlgebraEventPoolV1_1 extends StatefulEventSubscriber<PoolStateV1_1>
|
|
|
381
381
|
isValid: true,
|
|
382
382
|
balance0,
|
|
383
383
|
balance1,
|
|
384
|
+
areTicksCompressed: true,
|
|
384
385
|
};
|
|
385
386
|
}
|
|
386
387
|
|
|
@@ -26,13 +26,13 @@ import { uint256ToBigInt } from '../../lib/decoders';
|
|
|
26
26
|
import { MultiCallParams } from '../../lib/multi-wrapper';
|
|
27
27
|
import { decodeStateMultiCallResultWithRelativeBitmapsV1_9 } from './utils';
|
|
28
28
|
import { AlgebraMath } from './lib/AlgebraMath';
|
|
29
|
-
import { TickBitMap } from '../uniswap-v3/contract-math/TickBitMap';
|
|
30
29
|
import {
|
|
31
30
|
_reduceTickBitmap,
|
|
32
31
|
_reduceTicks,
|
|
33
32
|
} from '../uniswap-v3/contract-math/utils';
|
|
34
33
|
import { Constants } from './lib/Constants';
|
|
35
34
|
import { Network } from '../../constants';
|
|
35
|
+
import { TickTable } from './lib/TickTable';
|
|
36
36
|
|
|
37
37
|
export class AlgebraEventPoolV1_9 extends StatefulEventSubscriber<PoolState_v1_9> {
|
|
38
38
|
handlers: {
|
|
@@ -366,9 +366,7 @@ export class AlgebraEventPoolV1_9 extends StatefulEventSubscriber<PoolState_v1_9
|
|
|
366
366
|
tick: bigIntify(_state.globalState.tick),
|
|
367
367
|
};
|
|
368
368
|
const currentTick = globalState.tick;
|
|
369
|
-
const startTickBitmap =
|
|
370
|
-
BigInt(currentTick) / Constants.TICK_SPACING,
|
|
371
|
-
)[0];
|
|
369
|
+
const startTickBitmap = TickTable.position(BigInt(currentTick))[0];
|
|
372
370
|
|
|
373
371
|
return {
|
|
374
372
|
pool: _state.pool,
|
|
@@ -383,6 +381,7 @@ export class AlgebraEventPoolV1_9 extends StatefulEventSubscriber<PoolState_v1_9
|
|
|
383
381
|
isValid: true,
|
|
384
382
|
balance0,
|
|
385
383
|
balance1,
|
|
384
|
+
areTicksCompressed: false,
|
|
386
385
|
};
|
|
387
386
|
}
|
|
388
387
|
|
|
@@ -18,7 +18,7 @@ import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
|
|
|
18
18
|
import { getBigIntPow, getDexKeysWithNetwork, interpolate } from '../../utils';
|
|
19
19
|
import { IDex } from '../../dex/idex';
|
|
20
20
|
import { IDexHelper } from '../../dex-helper/idex-helper';
|
|
21
|
-
import { AlgebraData, DexParams,
|
|
21
|
+
import { AlgebraData, DexParams, IAlgebraPoolState } from './types';
|
|
22
22
|
import {
|
|
23
23
|
SimpleExchange,
|
|
24
24
|
getLocalDeadlineAsFriendlyPlaceholder,
|
|
@@ -57,7 +57,6 @@ const MAX_STALE_STATE_BLOCK_AGE = {
|
|
|
57
57
|
};
|
|
58
58
|
|
|
59
59
|
type IAlgebraEventPool = AlgebraEventPoolV1_1 | AlgebraEventPoolV1_9;
|
|
60
|
-
type IAlgebraPoolState = PoolStateV1_1 | PoolState_v1_9;
|
|
61
60
|
|
|
62
61
|
export class Algebra extends SimpleExchange implements IDex<AlgebraData> {
|
|
63
62
|
readonly isFeeOnTransferSupported: boolean = false;
|
|
@@ -65,7 +65,7 @@ export const AlgebraConfig: DexConfigMap<DexParams> = {
|
|
|
65
65
|
uniswapMulticall: '0x1F98415757620B543A52E61c46B32eB19261F984',
|
|
66
66
|
deployer: '0xc0d4323426c709e8d04b5b130e7f059523464a91',
|
|
67
67
|
version: 'v1.1',
|
|
68
|
-
}
|
|
68
|
+
},
|
|
69
69
|
},
|
|
70
70
|
CamelotV3: {
|
|
71
71
|
[Network.ARBITRUM]: {
|
|
@@ -76,7 +76,7 @@ export const AlgebraConfig: DexConfigMap<DexParams> = {
|
|
|
76
76
|
'0x6c1bebd370ba84753516bc1393c0d0a6c645856da55f5393ac8ab3d6dbc861d3',
|
|
77
77
|
chunksCount: 10,
|
|
78
78
|
initRetryFrequency: 10,
|
|
79
|
-
algebraStateMulticall: '
|
|
79
|
+
algebraStateMulticall: '0x2A7A9478Aaff076f8A80c03e7d65BD7d8D01650d',
|
|
80
80
|
subgraphURL:
|
|
81
81
|
'https://api.thegraph.com/subgraphs/name/camelotlabs/camelot-amm-v3',
|
|
82
82
|
uniswapMulticall: '0x1F98415757620B543A52E61c46B32eB19261F984',
|
|
@@ -3,7 +3,6 @@ import { PoolStateV1_1, PoolState_v1_9 } from '../types';
|
|
|
3
3
|
import { NumberAsString, SwapSide } from '@paraswap/core';
|
|
4
4
|
import { OutputResult, TickInfo } from '../../uniswap-v3/types';
|
|
5
5
|
import { Tick } from '../../uniswap-v3/contract-math/Tick';
|
|
6
|
-
import { TickBitMap } from '../../uniswap-v3/contract-math/TickBitMap';
|
|
7
6
|
import { SqrtPriceMath } from '../../uniswap-v3/contract-math/SqrtPriceMath';
|
|
8
7
|
import { TickMath } from '../../uniswap-v3/contract-math/TickMath';
|
|
9
8
|
import { LiquidityMath } from '../../uniswap-v3/contract-math/LiquidityMath';
|
|
@@ -21,6 +20,8 @@ import {
|
|
|
21
20
|
MAX_PRICING_COMPUTATION_STEPS_ALLOWED,
|
|
22
21
|
OUT_OF_RANGE_ERROR_POSTFIX,
|
|
23
22
|
} from '../../uniswap-v3/constants';
|
|
23
|
+
import { TickManager } from './TickManager';
|
|
24
|
+
import { TickTable } from './TickTable';
|
|
24
25
|
|
|
25
26
|
type UpdatePositionCache = {
|
|
26
27
|
price: bigint;
|
|
@@ -112,12 +113,12 @@ function _priceComputationCycles(
|
|
|
112
113
|
|
|
113
114
|
try {
|
|
114
115
|
[step.tickNext, step.initialized] =
|
|
115
|
-
|
|
116
|
+
TickTable.nextInitializedTickWithinOneWord(
|
|
116
117
|
poolState,
|
|
117
118
|
state.tick,
|
|
118
|
-
poolState.tickSpacing,
|
|
119
119
|
zeroForOne,
|
|
120
120
|
true,
|
|
121
|
+
poolState.areTicksCompressed ? poolState.tickSpacing : undefined,
|
|
121
122
|
);
|
|
122
123
|
} catch (e) {
|
|
123
124
|
if (
|
|
@@ -378,9 +379,9 @@ class AlgebraMathClass {
|
|
|
378
379
|
currentTick: bigint,
|
|
379
380
|
currentPrice: bigint,
|
|
380
381
|
) {
|
|
381
|
-
let amount0;
|
|
382
|
-
let amount1;
|
|
383
|
-
let globalLiquidityDelta;
|
|
382
|
+
let amount0 = 0n;
|
|
383
|
+
let amount1 = 0n;
|
|
384
|
+
let globalLiquidityDelta = 0n;
|
|
384
385
|
// If current tick is less than the provided bottom one then only the token0 has to be provided
|
|
385
386
|
if (currentTick < bottomTick) {
|
|
386
387
|
amount0 = SqrtPriceMath._getAmount0DeltaO(
|
|
@@ -434,7 +435,7 @@ class AlgebraMathClass {
|
|
|
434
435
|
const time = this._blockTimestamp(state);
|
|
435
436
|
|
|
436
437
|
if (
|
|
437
|
-
|
|
438
|
+
TickManager.update(
|
|
438
439
|
state,
|
|
439
440
|
bottomTick,
|
|
440
441
|
cache.tick,
|
|
@@ -447,10 +448,14 @@ class AlgebraMathClass {
|
|
|
447
448
|
)
|
|
448
449
|
) {
|
|
449
450
|
toggledBottom = true;
|
|
450
|
-
|
|
451
|
+
TickTable.toggleTick(
|
|
452
|
+
state,
|
|
453
|
+
bottomTick,
|
|
454
|
+
state.areTicksCompressed ? state.tickSpacing : undefined,
|
|
455
|
+
);
|
|
451
456
|
}
|
|
452
457
|
if (
|
|
453
|
-
|
|
458
|
+
TickManager.update(
|
|
454
459
|
state,
|
|
455
460
|
topTick,
|
|
456
461
|
cache.tick,
|
|
@@ -463,13 +468,17 @@ class AlgebraMathClass {
|
|
|
463
468
|
)
|
|
464
469
|
) {
|
|
465
470
|
toggledTop = true;
|
|
466
|
-
|
|
471
|
+
TickTable.toggleTick(
|
|
472
|
+
state,
|
|
473
|
+
topTick,
|
|
474
|
+
state.areTicksCompressed ? state.tickSpacing : undefined,
|
|
475
|
+
);
|
|
467
476
|
}
|
|
468
477
|
}
|
|
469
478
|
|
|
470
479
|
// skip fee && position related stuffs
|
|
471
480
|
|
|
472
|
-
// same as
|
|
481
|
+
// same as UniswapV3Pool.sol line 327 -> if (params.liquidityDelta != 0) {
|
|
473
482
|
if (liquidityDelta !== 0n) {
|
|
474
483
|
// if liquidityDelta is negative and the tick was toggled, it means that it should not be initialized anymore, so we delete it
|
|
475
484
|
if (liquidityDelta < 0) {
|
|
@@ -510,7 +519,7 @@ class AlgebraMathClass {
|
|
|
510
519
|
|
|
511
520
|
let cache: SwapCalculationCache = {
|
|
512
521
|
amountCalculated: 0n,
|
|
513
|
-
amountRequiredInitial: BI_MAX_INT, // similarly to
|
|
522
|
+
amountRequiredInitial: BI_MAX_INT, // similarly to what we did for uniswap
|
|
514
523
|
communityFee: 0n,
|
|
515
524
|
exactInput: false,
|
|
516
525
|
fee: 0n,
|
|
@@ -563,19 +572,18 @@ class AlgebraMathClass {
|
|
|
563
572
|
stepSqrtPrice: 0n,
|
|
564
573
|
tickCount: 0,
|
|
565
574
|
};
|
|
566
|
-
let i = 0;
|
|
567
575
|
// swap until there is remaining input or output tokens or we reach the price limit
|
|
568
576
|
while (true) {
|
|
569
577
|
step.stepSqrtPrice = currentPrice;
|
|
570
578
|
|
|
571
579
|
//equivalent of tickTable.nextTickInTheSameRow(currentTick, zeroToOne);
|
|
572
580
|
[step.nextTick, step.initialized] =
|
|
573
|
-
|
|
581
|
+
TickTable.nextInitializedTickWithinOneWord(
|
|
574
582
|
poolState,
|
|
575
583
|
currentTick,
|
|
576
|
-
poolState.tickSpacing,
|
|
577
584
|
zeroToOne,
|
|
578
585
|
false,
|
|
586
|
+
poolState.areTicksCompressed ? poolState.tickSpacing : undefined,
|
|
579
587
|
);
|
|
580
588
|
|
|
581
589
|
step.nextTickPrice = TickMath.getSqrtRatioAtTick(step.nextTick);
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { IAlgebraPoolState } from '../types';
|
|
2
|
+
import { LiquidityMath } from '../../uniswap-v3/contract-math/LiquidityMath';
|
|
3
|
+
import { _require } from '../../../utils';
|
|
4
|
+
import { NumberAsString } from '@paraswap/core';
|
|
5
|
+
import { ZERO_TICK_INFO } from '../../uniswap-v3/constants';
|
|
6
|
+
import { TickInfo } from '../../uniswap-v3/types';
|
|
7
|
+
|
|
8
|
+
export class TickManager {
|
|
9
|
+
static update(
|
|
10
|
+
state: Pick<IAlgebraPoolState, 'ticks'>,
|
|
11
|
+
tick: bigint,
|
|
12
|
+
tickCurrent: bigint,
|
|
13
|
+
liquidityDelta: bigint,
|
|
14
|
+
secondsPerLiquidityCumulativeX128: bigint,
|
|
15
|
+
tickCumulative: bigint,
|
|
16
|
+
time: bigint,
|
|
17
|
+
upper: boolean,
|
|
18
|
+
maxLiquidity: bigint,
|
|
19
|
+
): boolean {
|
|
20
|
+
let info = state.ticks[Number(tick)];
|
|
21
|
+
|
|
22
|
+
if (info === undefined) {
|
|
23
|
+
info = { ...ZERO_TICK_INFO };
|
|
24
|
+
state.ticks[Number(tick)] = info;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// uint128 liquidityTotalBefore = data.liquidityTotal;
|
|
28
|
+
// uint128 liquidityTotalAfter = LiquidityMath.addDelta(liquidityTotalBefore, liquidityDelta);
|
|
29
|
+
// require(liquidityTotalAfter < Constants.MAX_LIQUIDITY_PER_TICK + 1, 'LO');
|
|
30
|
+
const liquidityGrossBefore = info.liquidityGross;
|
|
31
|
+
const liquidityGrossAfter = LiquidityMath.addDelta(
|
|
32
|
+
liquidityGrossBefore,
|
|
33
|
+
liquidityDelta,
|
|
34
|
+
);
|
|
35
|
+
_require(
|
|
36
|
+
liquidityGrossAfter <= maxLiquidity,
|
|
37
|
+
'LO',
|
|
38
|
+
{ liquidityGrossAfter, maxLiquidity },
|
|
39
|
+
'liquidityGrossAfter <= maxLiquidity',
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
// int128 liquidityDeltaBefore = data.liquidityDelta;
|
|
43
|
+
const liquidityNetBefore = info.liquidityNet;
|
|
44
|
+
|
|
45
|
+
// // when the lower (upper) tick is crossed left to right (right to left), liquidity must be added (removed)
|
|
46
|
+
// data.liquidityDelta = upper
|
|
47
|
+
// ? int256(liquidityDeltaBefore).sub(liquidityDelta).toInt128()
|
|
48
|
+
// : int256(liquidityDeltaBefore).add(liquidityDelta).toInt128();
|
|
49
|
+
//
|
|
50
|
+
// data.liquidityTotal = liquidityTotalAfter;
|
|
51
|
+
info.liquidityNet = upper
|
|
52
|
+
? BigInt.asIntN(
|
|
53
|
+
128,
|
|
54
|
+
BigInt.asIntN(256, liquidityNetBefore) - liquidityDelta,
|
|
55
|
+
)
|
|
56
|
+
: BigInt.asIntN(
|
|
57
|
+
128,
|
|
58
|
+
BigInt.asIntN(256, liquidityNetBefore) + liquidityDelta,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// flipped = (liquidityTotalAfter == 0);
|
|
62
|
+
// if (liquidityTotalBefore == 0) {
|
|
63
|
+
// flipped = !flipped;
|
|
64
|
+
// // by convention, we assume that all growth before a tick was initialized happened _below_ the tick
|
|
65
|
+
// if (tick <= currentTick) {
|
|
66
|
+
// data.outerFeeGrowth0Token = totalFeeGrowth0Token;
|
|
67
|
+
// data.outerFeeGrowth1Token = totalFeeGrowth1Token;
|
|
68
|
+
// data.outerSecondsPerLiquidity = secondsPerLiquidityCumulative;
|
|
69
|
+
// data.outerTickCumulative = tickCumulative;
|
|
70
|
+
// data.outerSecondsSpent = time;
|
|
71
|
+
// }
|
|
72
|
+
// data.initialized = true;
|
|
73
|
+
// }
|
|
74
|
+
|
|
75
|
+
let flipped = liquidityGrossAfter === 0n;
|
|
76
|
+
if (liquidityGrossBefore === 0n) {
|
|
77
|
+
flipped = !flipped;
|
|
78
|
+
if (tick <= tickCurrent) {
|
|
79
|
+
info.secondsPerLiquidityOutsideX128 = secondsPerLiquidityCumulativeX128;
|
|
80
|
+
info.tickCumulativeOutside = tickCumulative;
|
|
81
|
+
info.secondsOutside = time;
|
|
82
|
+
}
|
|
83
|
+
info.initialized = true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// data.liquidityTotal = liquidityTotalAfter;
|
|
87
|
+
info.liquidityGross = liquidityGrossAfter;
|
|
88
|
+
|
|
89
|
+
return flipped;
|
|
90
|
+
}
|
|
91
|
+
}
|