Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/squadswap v3 #750

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/dex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import { EtherFi } from './etherfi';
import { Spark } from './spark/spark';
import { VelodromeSlipstream } from './uniswap-v3/forks/velodrome-slipstream/velodrome-slipstream';
import { AaveV3Stata } from './aave-v3-stata/aave-v3-stata';
import { SquadswapV3 } from './squadswap-v3/squadswap-v3';

const LegacyDexes = [
CurveV2,
Expand Down Expand Up @@ -157,6 +158,7 @@ const Dexes = [
PharaohV1,
Spark,
AaveV3Stata,
SquadswapV3,
];

export type LegacyDexConstructor = new (dexHelper: IDexHelper) => IDexTxBuilder<
Expand Down
34 changes: 34 additions & 0 deletions src/dex/squadswap-v3/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { DexParams } from '../uniswap-v3/types';
import { DexConfigMap, AdapterMappings } from '../../types';
import { Network, SwapSide } from '../../constants';

const SQUAD_SUPPORTED_FEES = [10000n, 2500n, 500n, 100n];

export const SquadswapV3Config: DexConfigMap<DexParams> = {
SquadswapV3: {
[Network.BSC]: {
factory: '0x009c4ef7C0e0Dd6bd1ea28417c01Ea16341367c3',
deployer: '0x38e09D9444B41CFda398DD31eb2713Ca5c3B75eA',
quoter: '0x81Da0D4e1157391a22a656ad84AAb9b2716F21e0',
router: '0xAf4b332ddBa499B6116235a095CEE2f2030BCBC0',
supportedFees: SQUAD_SUPPORTED_FEES,
stateMulticall: '0x9DAd2ED7ADc6eaacf81589Cd043579c9684E5C81',
uniswapMulticall: '0xac1cE734566f390A94b00eb9bf561c2625BF44ea',
chunksCount: 10,
initRetryFrequency: 30,
initHash:
'0xf08a35894b6b71b07d95a23022375630f6cee63a27d724c703617c17c4fc387d',
subgraphURL:
'https://api.studio.thegraph.com/query/59394/test-pcs-uni/v0.0.8',
},
},
};

export const Adapters: Record<number, AdapterMappings> = {
// TODO: add adapters for each chain
// This is an example to copy
[Network.BSC]: {
[SwapSide.SELL]: [{ name: 'BscAdapter02', index: 4 }],
[SwapSide.BUY]: [{ name: 'BscBuyAdapter', index: 5 }],
},
};
36 changes: 36 additions & 0 deletions src/dex/squadswap-v3/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export const SQUADSWAPV3_TICK_GAS_COST = 24_000; // Ceiled
export const SQUADSWAPV3_TICK_BASE_OVERHEAD = 75_000;
export const SQUADSWAPV3_POOL_SEARCH_OVERHEAD = 10_000;

// This is used for price calculation. If out of scope, return 0n
export const TICK_BITMAP_TO_USE = 4n;

// This is used to check if the state is still valid.
export const TICK_BITMAP_BUFFER = 8n;

export const MAX_PRICING_COMPUTATION_STEPS_ALLOWED = 128;

export const SQUADSWAPV3_SUBGRAPH_URL =
'https://api.studio.thegraph.com/query/59394/test-pcs-uni/v0.0.8';

export const SQUADSWAPV3_EFFICIENCY_FACTOR = 3;

export const ZERO_TICK_INFO = {
liquidityGross: 0n,
liquidityNet: 0n,
tickCumulativeOutside: 0n,
secondsPerLiquidityOutsideX128: 0n,
secondsOutside: 0n,
initialized: false,
};

export const ZERO_ORACLE_OBSERVATION = {
blockTimestamp: 0n,
tickCumulative: 0n,
secondsPerLiquidityCumulativeX128: 0n,
initialized: false,
};

export const OUT_OF_RANGE_ERROR_POSTFIX = `INVALID_TICK_BIT_MAP_RANGES`;

export const DEFAULT_POOL_INIT_CODE_HASH = `0xf08a35894b6b71b07d95a23022375630f6cee63a27d724c703617c17c4fc387d`;
90 changes: 90 additions & 0 deletions src/dex/squadswap-v3/contract-math/BitMath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {
BI_MAX_UINT128,
BI_MAX_UINT16,
BI_MAX_UINT32,
BI_MAX_UINT64,
BI_MAX_UINT8,
} from '../../../bigint-constants';
import { _require } from '../../../utils';

export class BitMath {
static mostSignificantBit(x: bigint): bigint {
_require(x > 0, '', { x }, 'x > 0');
let r = 0n;

if (x >= 0x100000000000000000000000000000000n) {
x >>= 128n;
r += 128n;
}
if (x >= 0x10000000000000000n) {
x >>= 64n;
r += 64n;
}
if (x >= 0x100000000n) {
x >>= 32n;
r += 32n;
}
if (x >= 0x10000n) {
x >>= 16n;
r += 16n;
}
if (x >= 0x100n) {
x >>= 8n;
r += 8n;
}
if (x >= 0x10n) {
x >>= 4n;
r += 4n;
}
if (x >= 0x4n) {
x >>= 2n;
r += 2n;
}
if (x >= 0x2n) r += 1n;

return r;
}

static leastSignificantBit(x: bigint): bigint {
_require(x > 0, '', { x }, 'x > 0');

let r = 255n;
if ((x & BI_MAX_UINT128) > 0n) {
r -= 128n;
} else {
x >>= 128n;
}
if ((x & BI_MAX_UINT64) > 0n) {
r -= 64n;
} else {
x >>= 64n;
}
if ((x & BI_MAX_UINT32) > 0n) {
r -= 32n;
} else {
x >>= 32n;
}
if ((x & BI_MAX_UINT16) > 0n) {
r -= 16n;
} else {
x >>= 16n;
}
if ((x & BI_MAX_UINT8) > 0n) {
r -= 8n;
} else {
x >>= 8n;
}
if ((x & 0xfn) > 0n) {
r -= 4n;
} else {
x >>= 4n;
}
if ((x & 0x3n) > 0n) {
r -= 2n;
} else {
x >>= 2n;
}
if ((x & 0x1n) > 0n) r -= 1n;
return r;
}
}
3 changes: 3 additions & 0 deletions src/dex/squadswap-v3/contract-math/FixedPoint128.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class FixedPoint128 {
static readonly Q128 = 0x100000000000000000000000000000000n;
}
4 changes: 4 additions & 0 deletions src/dex/squadswap-v3/contract-math/FixedPoint96.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class FixedPoint96 {
static readonly RESOLUTION = 96n;
static readonly Q96 = 0x1000000000000000000000000n;
}
30 changes: 30 additions & 0 deletions src/dex/squadswap-v3/contract-math/FullMath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BI_MAX_UINT256 } from '../../../bigint-constants';
import { _require } from '../../../utils';

export class FullMath {
static mulDiv(a: bigint, b: bigint, denominator: bigint) {
const result = (a * b) / denominator;

_require(
result <= BI_MAX_UINT256,
'',
{ result, BI_MAX_UINT: BI_MAX_UINT256 },
'result <= BI_MAX_UINT',
);

return result;
}

static mulDivRoundingUp(a: bigint, b: bigint, denominator: bigint) {
const result = (a * b + denominator - 1n) / denominator;

_require(
result <= BI_MAX_UINT256,
'',
{ result, BI_MAX_UINT: BI_MAX_UINT256 },
'result <= BI_MAX_UINT',
);

return result;
}
}
17 changes: 17 additions & 0 deletions src/dex/squadswap-v3/contract-math/LiquidityMath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { _require } from '../../../utils';

export class LiquidityMath {
static addDelta(x: bigint, y: bigint): bigint {
let z;
if (y < 0) {
const _y = BigInt.asUintN(128, -y);
z = x - _y;
_require(z < x, 'LS', { z, x, y, _y }, 'z < x');
} else {
const _y = BigInt.asUintN(128, y);
z = x + _y;
_require(z >= x, 'LA', { z, x, y, _y }, 'z >= x');
}
return z;
}
}
Loading