From 95b73563107bf1e6b205221ed839e68e8cf4e13e Mon Sep 17 00:00:00 2001 From: Sean Date: Tue, 8 Oct 2024 13:52:57 +0900 Subject: [PATCH 1/3] feat: deploy fixed controller --- src/constants/addresses.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/constants/addresses.ts b/src/constants/addresses.ts index 982e2a8..8b61ac8 100644 --- a/src/constants/addresses.ts +++ b/src/constants/addresses.ts @@ -32,36 +32,36 @@ export const CONTRACT_ADDRESSES: { Operator: getAddress('0x33559576B062D08230b467ea7DC7Ce75aFcbdE92'), }, [CHAIN_IDS.ARBITRUM_SEPOLIA]: { - Controller: getAddress('0xFbbA685a39bE6640B8EB08c6E6DDf2664fD1D668'), + Controller: getAddress('0xb278E0Cf476394b84385C7a9d936B6009fddC30b'), BookManager: getAddress('0xC528b9ED5d56d1D0d3C18A2342954CE1069138a4'), - BookViewer: getAddress('0x73c524e103C94Bf2743659d739586395B1A9e1BE'), + BookViewer: getAddress('0xb291cb35361a740Af7BDfA1B2CbDD4522Ed9a50F'), Rebalancer: getAddress('0xCF556d850277BC579c99C0729F4E72e62C57D811'), Strategy: getAddress('0x8aDF62b0b6078EaE5a2D54e9e5DD2AA71F6748C4'), Minter: getAddress('0xF2f51B00C2e9b77F23fD66649bbabf8a025c39eF'), Operator: getAddress('0x33559576B062D08230b467ea7DC7Ce75aFcbdE92'), }, [CHAIN_IDS.BASE]: { - Controller: getAddress('0x57dDD0d3DF50685444442076AC59F9c7Df75D150'), + Controller: getAddress('0xe4AB03992e214acfdCD05ccFB5C5C16e3d0Ca371'), BookManager: getAddress('0x382CCccbD3b142D7DA063bF68cd0c89634767F76'), - BookViewer: getAddress('0xcAf89a60D0911AeB8C2b997B5eF9b2017a19Be0d'), + BookViewer: getAddress('0xbfb608D67340fa54bA31614C293750EeB573c795'), Rebalancer: zeroAddress, Strategy: zeroAddress, Minter: zeroAddress, Operator: zeroAddress, }, [CHAIN_IDS.BERACHAIN_TESTNET]: { - Controller: getAddress('0x7d06c636bA86BD1fc2C38B11F1e5701145CABc30'), + Controller: getAddress('0xce3F3C90970C08Fe451998441b30879560AA6757'), BookManager: getAddress('0x874b1B795993653fbFC3f1c1fc0469214cC9F4A5'), - BookViewer: getAddress('0x5C91A02B8B5D10597fc6cA23faF56F9718D1feD0'), + BookViewer: getAddress('0xb735FdD82497Dd9AbfEEAdc659b960473BF896E0'), Rebalancer: zeroAddress, Strategy: zeroAddress, Minter: zeroAddress, Operator: zeroAddress, }, [CHAIN_IDS.ZKSYNC]: { - Controller: getAddress('0x9aF80CC61AAd734604f139A53E22c56Cdbf9a158'), + Controller: getAddress('0x2Bd904F455928833F8E8C706d1cf01Eb5daaee7C'), BookManager: getAddress('0xAc6AdB2727F99C309acd511D942c0b2812e03614'), - BookViewer: getAddress('0x13E8F7b988013729EcF00b0A3F8e0E47915aA893'), + BookViewer: getAddress('0x6e717aA9BB48129aeDC408D435Cc54F79E8A747a'), Rebalancer: zeroAddress, Strategy: zeroAddress, Minter: zeroAddress, From 92b2c04b39b2f10aa37bc4d4a492891347e79cce Mon Sep 17 00:00:00 2001 From: Xavier Date: Tue, 8 Oct 2024 17:27:27 +0900 Subject: [PATCH 2/3] feat: add get pool performance api --- src/apis/pool.ts | 26 +++++++++++++- src/model/pool.ts | 25 +++++++++++++ src/type.ts | 24 +++++++++++++ src/view.ts | 89 ++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 162 insertions(+), 2 deletions(-) diff --git a/src/apis/pool.ts b/src/apis/pool.ts index 5d30211..b6533da 100644 --- a/src/apis/pool.ts +++ b/src/apis/pool.ts @@ -1,14 +1,38 @@ import { PublicClient } from 'viem' import { CHAIN_IDS } from '../constants/chain' -import { Pool } from '../model/pool' +import { Pool, PoolSnapshotDto, PoolVolumeDto } from '../model/pool' import { CONTRACT_ADDRESSES } from '../constants/addresses' import { toPoolKey } from '../utils/pool-key' import { REBALANCER_ABI } from '../abis/rebalancer/rebalancer-abi' import { Market } from '../type' +import { Subgraph } from '../constants/subgraph' import { fetchMarket } from './market' +export const fetchPoolPerformance = async ( + chainId: CHAIN_IDS, + poolKey: `0x${string}`, + volumeFromTimestamp: number, + snapshotFromTimestamp: number, +) => { + return Subgraph.get<{ + data: { + poolVolumes: PoolVolumeDto[] + poolSnapshots: PoolSnapshotDto[] + } + }>( + chainId, + 'getPoolPerformanceData', + 'query getPoolPerformanceData($poolKey: String!, $volumeFrom: BigInt!, $snapshotFrom: BigInt!) { poolVolumes(where: { poolKey: $poolKey, intervalType: "1d", timestamp_gte: $volumeFrom, }) { id poolKey intervalType timestamp currencyAVolume currencyBVolume bookACurrencyAVolume bookACurrencyBVolume bookBCurrencyAVolume bookBCurrencyBVolume } poolSnapshots( where: { poolKey: $poolKey, intervalType: "1h", timestamp_gte: $snapshotFrom, } ) { id poolKey intervalType timestamp price liquidityA liquidityB totalSupply } }', + { + poolKey, + volumeFrom: BigInt(volumeFromTimestamp), + snapshotFrom: BigInt(snapshotFromTimestamp), + }, + ) +} + export async function fetchPool( publicClient: PublicClient, chainId: CHAIN_IDS, diff --git a/src/model/pool.ts b/src/model/pool.ts index 990271b..ebde687 100644 --- a/src/model/pool.ts +++ b/src/model/pool.ts @@ -124,6 +124,7 @@ export class Pool { strategy: this.strategy, currencyA: this.currencyA, currencyB: this.currencyB, + currencyLp: this.currencyLp, liquidityA: { total: { currency: this.currencyA, @@ -169,3 +170,27 @@ export class Pool { } } } + +export type PoolVolumeDto = { + id: string + poolKey: `0x${string}` + intervalType: '1d' + timestamp: bigint + currencyAVolume: bigint + currencyBVolume: bigint + bookACurrencyAVolume: bigint + bookACurrencyBVolume: bigint + bookBCurrencyAVolume: bigint + bookBCurrencyBVolume: bigint +} + +export type PoolSnapshotDto = { + id: string + poolKey: `0x${string}` + intervalType: '1h' + timestamp: bigint + price: bigint + liquidityA: bigint + liquidityB: bigint + totalSupply: bigint +} diff --git a/src/type.ts b/src/type.ts index a509f93..a9f5a48 100644 --- a/src/type.ts +++ b/src/type.ts @@ -47,6 +47,7 @@ export type Pool = { strategy: `0x${string}` currencyA: Currency currencyB: Currency + currencyLp: Currency6909 liquidityA: { total: CurrencyAmount reserve: CurrencyAmount @@ -135,3 +136,26 @@ export enum CHART_LOG_INTERVALS { export type CurrencyAmount = { currency: Currency; value: string } export type Currency6909Amount = { currency: Currency6909; value: string } + +export type PoolVolumeDto = { + poolKey: `0x${string}` + intervalType: '1d' + timestamp: number + currencyAVolume: CurrencyAmount + currencyBVolume: CurrencyAmount +} + +export type PoolSnapshotDto = { + poolKey: `0x${string}` + intervalType: '1h' + timestamp: number + price: string + liquidityA: CurrencyAmount + liquidityB: CurrencyAmount + totalSupply: Currency6909Amount +} + +export type PoolPerformanceData = { + poolVolumes: PoolVolumeDto[] + poolSnapshots: PoolSnapshotDto[] +} diff --git a/src/view.ts b/src/view.ts index b3a35af..9c0dd26 100644 --- a/src/view.ts +++ b/src/view.ts @@ -15,6 +15,7 @@ import type { DefaultReadContractOptions, Market, Pool, + PoolPerformanceData, StrategyPrice, } from './type' import { CHART_LOG_INTERVALS } from './type' @@ -26,7 +27,7 @@ import { getMarketId } from './utils/market' import { CONTRACT_ADDRESSES } from './constants/addresses' import { invertTick, toPrice } from './utils/tick' import { MAX_TICK, MIN_TICK } from './constants/tick' -import { fetchPool } from './apis/pool' +import { fetchPool, fetchPoolPerformance } from './apis/pool' import { fetchStrategyPrice } from './apis/strategy' import { Subgraph } from './constants/subgraph' @@ -186,6 +187,92 @@ export const getPool = async ({ return pool.toJson() } +export const getPoolPerformance = async ({ + chainId, + token0, + token1, + salt, + volumeFromTimestamp, + snapshotFromTimestamp, + options, +}: { + chainId: CHAIN_IDS + token0: `0x${string}` + token1: `0x${string}` + salt: `0x${string}` + volumeFromTimestamp: number + snapshotFromTimestamp: number + options?: { + pool?: Pool + useSubgraph?: boolean + } & DefaultReadContractOptions +}): Promise => { + if (isAddressEqual(token0, token1)) { + throw new Error('Token0 and token1 must be different') + } + if (!options?.useSubgraph) { + throw new Error('useSubgraph must be true') + } + let pool: Pool + if (options?.pool) { + pool = options.pool + } else { + const publicClient = createPublicClient({ + chain: CHAIN_MAP[chainId], + transport: options?.rpcUrl ? http(options.rpcUrl) : http(), + }) + pool = ( + await fetchPool( + publicClient, + chainId, + [token0, token1], + salt, + !!(options && options.useSubgraph), + undefined, + ) + ).toJson() + } + const poolPerformance = await fetchPoolPerformance( + chainId, + pool.key, + volumeFromTimestamp, + snapshotFromTimestamp, + ) + return { + poolVolumes: poolPerformance.data.poolVolumes.map((poolVolume) => ({ + poolKey: poolVolume.poolKey, + intervalType: poolVolume.intervalType, + timestamp: Number(poolVolume.timestamp), + currencyAVolume: { + currency: pool.currencyA, + value: formatUnits(poolVolume.currencyAVolume, pool.currencyA.decimals), + }, + currencyBVolume: { + currency: pool.currencyB, + value: formatUnits(poolVolume.currencyBVolume, pool.currencyB.decimals), + }, + })), + poolSnapshots: poolPerformance.data.poolSnapshots.map((poolSnapshot) => ({ + poolKey: poolSnapshot.poolKey, + intervalType: poolSnapshot.intervalType, + timestamp: Number(poolSnapshot.timestamp), + price: formatUnits(poolSnapshot.price, 8), + liquidityA: { + currency: pool.currencyA, + value: formatUnits(poolSnapshot.liquidityA, pool.currencyA.decimals), + }, + liquidityB: { + currency: pool.currencyB, + value: formatUnits(poolSnapshot.liquidityB, pool.currencyB.decimals), + }, + totalSupply: { + currency: pool.currencyLp, + value: formatUnits(poolSnapshot.totalSupply, pool.currencyLp.decimals), + }, + })), + } +} + export const getStrategyPrice = async ({ chainId, token0, From 1a03bff539446f092e55f9e7184e1e4b2f3c3af8 Mon Sep 17 00:00:00 2001 From: Xavier Date: Fri, 18 Oct 2024 14:42:43 +0900 Subject: [PATCH 3/3] feat: update sdk --- src/abis/rebalancer/rebalancer-abi.ts | 188 ++++++++++++++++++++++---- src/abis/rebalancer/strategy-abi.ts | 130 +++++++++++++++++- src/apis/pool.ts | 3 +- src/call.ts | 140 +++++++++++++++++++ src/constants/addresses.ts | 28 ++-- src/model/pool.ts | 5 + src/type.ts | 1 + 7 files changed, 445 insertions(+), 50 deletions(-) diff --git a/src/abis/rebalancer/rebalancer-abi.ts b/src/abis/rebalancer/rebalancer-abi.ts index 5a92a54..2e955b4 100644 --- a/src/abis/rebalancer/rebalancer-abi.ts +++ b/src/abis/rebalancer/rebalancer-abi.ts @@ -107,6 +107,27 @@ export const REBALANCER_ABI = [ name: 'OwnableUnauthorizedAccount', type: 'error', }, + { + inputs: [], + name: 'Paused', + type: 'error', + }, + { + inputs: [ + { + internalType: 'uint8', + name: 'bits', + type: 'uint8', + }, + { + internalType: 'uint256', + name: 'value', + type: 'uint256', + }, + ], + name: 'SafeCastOverflowedUintDowncast', + type: 'error', + }, { inputs: [ { @@ -191,6 +212,56 @@ export const REBALANCER_ABI = [ name: 'Burn', type: 'event', }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'bytes32', + name: 'key', + type: 'bytes32', + }, + { + indexed: false, + internalType: 'uint256', + name: 'canceledAmountA', + type: 'uint256', + }, + { + indexed: false, + internalType: 'uint256', + name: 'canceledAmountB', + type: 'uint256', + }, + ], + name: 'Cancel', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'bytes32', + name: 'key', + type: 'bytes32', + }, + { + indexed: false, + internalType: 'uint256', + name: 'claimedAmountA', + type: 'uint256', + }, + { + indexed: false, + internalType: 'uint256', + name: 'claimedAmountB', + type: 'uint256', + }, + ], + name: 'Claim', + type: 'event', + }, { anonymous: false, inputs: [ @@ -328,6 +399,25 @@ export const REBALANCER_ABI = [ name: 'OwnershipTransferred', type: 'event', }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'bytes32', + name: 'key', + type: 'bytes32', + }, + { + indexed: false, + internalType: 'bool', + name: 'paused', + type: 'bool', + }, + ], + name: 'Pause', + type: 'event', + }, { anonymous: false, inputs: [ @@ -378,6 +468,19 @@ export const REBALANCER_ABI = [ name: 'Transfer', type: 'event', }, + { + inputs: [], + name: 'RATE_PRECISION', + outputs: [ + { + internalType: 'uint256', + name: '', + type: 'uint256', + }, + ], + stateMutability: 'view', + type: 'function', + }, { inputs: [ { @@ -386,34 +489,17 @@ export const REBALANCER_ABI = [ type: 'bytes32', }, { - components: [ - { - internalType: 'address', - name: 'user', - type: 'address', - }, - { - internalType: 'uint256', - name: 'burnAmount', - type: 'uint256', - }, - { - internalType: 'uint256', - name: 'minAmountA', - type: 'uint256', - }, - { - internalType: 'uint256', - name: 'minAmountB', - type: 'uint256', - }, - ], - internalType: 'struct Rebalancer.BurnParams', - name: 'burnParams', - type: 'tuple', + internalType: 'address', + name: 'user', + type: 'address', + }, + { + internalType: 'uint256', + name: 'burnAmount', + type: 'uint256', }, ], - name: '_burnAndRebalance', + name: '_burn', outputs: [ { internalType: 'uint256', @@ -527,6 +613,19 @@ export const REBALANCER_ABI = [ stateMutability: 'nonpayable', type: 'function', }, + { + inputs: [ + { + internalType: 'bytes32', + name: 'key', + type: 'bytes32', + }, + ], + name: '_rebalance', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, { inputs: [], name: 'acceptOwnership', @@ -675,12 +774,12 @@ export const REBALANCER_ABI = [ outputs: [ { internalType: 'uint256', - name: '', + name: 'withdrawalA', type: 'uint256', }, { internalType: 'uint256', - name: '', + name: 'withdrawalB', type: 'uint256', }, ], @@ -815,6 +914,11 @@ export const REBALANCER_ABI = [ name: 'strategy', type: 'address', }, + { + internalType: 'bool', + name: 'paused', + type: 'bool', + }, { internalType: 'uint256', name: 'reserveA', @@ -836,7 +940,7 @@ export const REBALANCER_ABI = [ type: 'uint256[]', }, ], - internalType: 'struct IPoolStorage.Pool', + internalType: 'struct IRebalancer.Pool', name: '', type: 'tuple', }, @@ -1037,6 +1141,19 @@ export const REBALANCER_ABI = [ stateMutability: 'view', type: 'function', }, + { + inputs: [ + { + internalType: 'bytes32', + name: 'key', + type: 'bytes32', + }, + ], + name: 'pause', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, { inputs: [], name: 'pendingOwner', @@ -1070,6 +1187,19 @@ export const REBALANCER_ABI = [ stateMutability: 'nonpayable', type: 'function', }, + { + inputs: [ + { + internalType: 'bytes32', + name: 'key', + type: 'bytes32', + }, + ], + name: 'resume', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, { inputs: [ { diff --git a/src/abis/rebalancer/strategy-abi.ts b/src/abis/rebalancer/strategy-abi.ts index b5d6c01..0f8cb3e 100644 --- a/src/abis/rebalancer/strategy-abi.ts +++ b/src/abis/rebalancer/strategy-abi.ts @@ -7,8 +7,8 @@ export const STRATEGY_ABI = [ type: 'address', }, { - internalType: 'contract IPoolStorage', - name: 'poolStorage_', + internalType: 'contract IRebalancer', + name: 'rebalancer_', type: 'address', }, { @@ -30,6 +30,11 @@ export const STRATEGY_ABI = [ name: 'ExceedsThreshold', type: 'error', }, + { + inputs: [], + name: 'InvalidAccess', + type: 'error', + }, { inputs: [], name: 'InvalidConfig', @@ -166,6 +171,11 @@ export const STRATEGY_ABI = [ name: 'referenceThreshold', type: 'uint24', }, + { + internalType: 'uint24', + name: 'rebalanceThreshold', + type: 'uint24', + }, { internalType: 'uint24', name: 'rateA', @@ -278,6 +288,11 @@ export const STRATEGY_ABI = [ }, { inputs: [ + { + internalType: 'address', + name: 'sender', + type: 'address', + }, { internalType: 'bytes32', name: 'key', @@ -285,15 +300,28 @@ export const STRATEGY_ABI = [ }, { internalType: 'uint256', - name: 'amountA', + name: 'burnAmount', type: 'uint256', }, { internalType: 'uint256', - name: 'amountB', + name: 'totalSupply', type: 'uint256', }, ], + name: 'burnHook', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + internalType: 'bytes32', + name: 'key', + type: 'bytes32', + }, + ], name: 'computeOrders', outputs: [ { @@ -364,6 +392,11 @@ export const STRATEGY_ABI = [ name: 'referenceThreshold', type: 'uint24', }, + { + internalType: 'uint24', + name: 'rebalanceThreshold', + type: 'uint24', + }, { internalType: 'uint24', name: 'rateA', @@ -477,6 +510,34 @@ export const STRATEGY_ABI = [ stateMutability: 'view', type: 'function', }, + { + inputs: [ + { + internalType: 'address', + name: 'sender', + type: 'address', + }, + { + internalType: 'bytes32', + name: 'key', + type: 'bytes32', + }, + { + internalType: 'uint256', + name: 'mintAmount', + type: 'uint256', + }, + { + internalType: 'uint256', + name: 'totalSupply', + type: 'uint256', + }, + ], + name: 'mintHook', + outputs: [], + stateMutability: 'view', + type: 'function', + }, { inputs: [], name: 'owner', @@ -503,12 +564,64 @@ export const STRATEGY_ABI = [ stateMutability: 'view', type: 'function', }, + { + inputs: [ + { + internalType: 'address', + name: 'sender', + type: 'address', + }, + { + internalType: 'bytes32', + name: 'key', + type: 'bytes32', + }, + { + components: [ + { + internalType: 'Tick', + name: 'tick', + type: 'int24', + }, + { + internalType: 'uint64', + name: 'rawAmount', + type: 'uint64', + }, + ], + internalType: 'struct IStrategy.Order[]', + name: 'liquidityA', + type: 'tuple[]', + }, + { + components: [ + { + internalType: 'Tick', + name: 'tick', + type: 'int24', + }, + { + internalType: 'uint64', + name: 'rawAmount', + type: 'uint64', + }, + ], + internalType: 'struct IStrategy.Order[]', + name: 'liquidityB', + type: 'tuple[]', + }, + ], + name: 'rebalanceHook', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, { inputs: [], - name: 'poolStorage', + name: 'rebalancer', outputs: [ { - internalType: 'contract IPoolStorage', + internalType: 'contract IRebalancer', name: '', type: 'address', }, @@ -550,6 +663,11 @@ export const STRATEGY_ABI = [ name: 'referenceThreshold', type: 'uint24', }, + { + internalType: 'uint24', + name: 'rebalanceThreshold', + type: 'uint24', + }, { internalType: 'uint24', name: 'rateA', diff --git a/src/apis/pool.ts b/src/apis/pool.ts index b6533da..38e19b6 100644 --- a/src/apis/pool.ts +++ b/src/apis/pool.ts @@ -55,7 +55,7 @@ export async function fetchPool( salt, ) const [ - { bookIdA, bookIdB, reserveA, reserveB, orderListA, orderListB }, + { bookIdA, bookIdB, reserveA, reserveB, orderListA, orderListB, paused }, totalSupply, [totalLiquidityA, totalLiquidityB], ] = await publicClient.multicall({ @@ -109,5 +109,6 @@ export async function fetchPool( reserveB: BigInt(reserveB), orderListA: orderListA.map((id: bigint) => BigInt(id)), orderListB: orderListB.map((id: bigint) => BigInt(id)), + paused, }) } diff --git a/src/call.ts b/src/call.ts index 3448dc0..f7b4c40 100644 --- a/src/call.ts +++ b/src/call.ts @@ -1669,6 +1669,7 @@ export const setStrategyConfig = async ({ salt: `0x${string}` config: { referenceThreshold: string // 0 <= referenceThreshold <= 1 + rebalanceThreshold: string // 0 <= rebalanceThreshold <= 1 rateA: string // 0 <= rateA <= 1 rateB: string // 0 <= rateB <= 1 minRateA: string // 0 <= minRateA <= rateA @@ -1687,6 +1688,12 @@ export const setStrategyConfig = async ({ ) { throw new Error('Reference threshold must be in the range [0, 1]') } + if ( + Number(config.rebalanceThreshold) < 0 || + Number(config.rebalanceThreshold) > 1 + ) { + throw new Error('Rebalance threshold must be in the range [0, 1]') + } if ( Number(config.priceThresholdA) < 0 || Number(config.priceThresholdA) > 1 || @@ -1743,6 +1750,7 @@ export const setStrategyConfig = async ({ const configRaw = { referenceThreshold: parseUnits(config.referenceThreshold, 6), + rebalanceThreshold: parseUnits(config.rebalanceThreshold, 6), rateA: parseUnits(config.rateA, 6), rateB: parseUnits(config.rateB, 6), minRateA: parseUnits(config.minRateA, 6), @@ -1763,3 +1771,135 @@ export const setStrategyConfig = async ({ options?.gasLimit, ) } + +export const pausePool = async ({ + chainId, + userAddress, + token0, + token1, + salt, + options, +}: { + chainId: CHAIN_IDS + userAddress: `0x${string}` + token0: `0x${string}` + token1: `0x${string}` + salt: `0x${string}` + options?: { + useSubgraph?: boolean + pool?: Pool + } & DefaultWriteContractOptions +}): Promise => { + const publicClient = createPublicClient({ + chain: CHAIN_MAP[chainId], + transport: options?.rpcUrl ? http(options.rpcUrl) : http(), + }) + const pool = options?.pool + ? options.pool + : ( + await fetchPool( + publicClient, + chainId, + [token0, token1], + salt, + !!(options && options.useSubgraph), + ) + ).toJson() + if (!pool.isOpened) { + throw new Error(` + Open the pool before trying pause. + import { openPool } from '@clober/v2-sdk' + + const transaction = await openPool({ + chainId: ${chainId}, + tokenA: '${token0}', + tokenB: '${token1}', + salt: '0x0000000000000000000000000000000000000000000000000000000000000000', + }) + `) + } + + if (pool.paused) { + return undefined + } + + return buildTransaction( + publicClient, + { + chain: CHAIN_MAP[chainId], + account: userAddress, + address: CONTRACT_ADDRESSES[chainId]!.Rebalancer, + abi: REBALANCER_ABI, + functionName: 'pause', + args: [pool.key], + }, + options?.gasLimit, + options?.gasPriceLimit, + ) +} + +export const resumePool = async ({ + chainId, + userAddress, + token0, + token1, + salt, + options, +}: { + chainId: CHAIN_IDS + userAddress: `0x${string}` + token0: `0x${string}` + token1: `0x${string}` + salt: `0x${string}` + options?: { + useSubgraph?: boolean + pool?: Pool + } & DefaultWriteContractOptions +}): Promise => { + const publicClient = createPublicClient({ + chain: CHAIN_MAP[chainId], + transport: options?.rpcUrl ? http(options.rpcUrl) : http(), + }) + const pool = options?.pool + ? options.pool + : ( + await fetchPool( + publicClient, + chainId, + [token0, token1], + salt, + !!(options && options.useSubgraph), + ) + ).toJson() + if (!pool.isOpened) { + throw new Error(` + Open the pool before trying resume. + import { openPool } from '@clober/v2-sdk' + + const transaction = await openPool({ + chainId: ${chainId}, + tokenA: '${token0}', + tokenB: '${token1}', + salt: '0x0000000000000000000000000000000000000000000000000000000000000000', + }) + `) + } + + if (!pool.paused) { + return undefined + } + + return buildTransaction( + publicClient, + { + chain: CHAIN_MAP[chainId], + account: userAddress, + address: CONTRACT_ADDRESSES[chainId]!.Rebalancer, + abi: REBALANCER_ABI, + functionName: 'resume', + args: [pool.key], + }, + options?.gasLimit, + options?.gasPriceLimit, + ) +} diff --git a/src/constants/addresses.ts b/src/constants/addresses.ts index 8b61ac8..7b318da 100644 --- a/src/constants/addresses.ts +++ b/src/constants/addresses.ts @@ -23,22 +23,22 @@ export const CONTRACT_ADDRESSES: { Operator: getAddress('0x33559576B062D08230b467ea7DC7Ce75aFcbdE92'), }, [CHAIN_IDS.CLOBER_TESTNET_2]: { - Controller: getAddress('0xFbbA685a39bE6640B8EB08c6E6DDf2664fD1D668'), - BookManager: getAddress('0xC528b9ED5d56d1D0d3C18A2342954CE1069138a4'), - BookViewer: getAddress('0x73c524e103C94Bf2743659d739586395B1A9e1BE'), - Rebalancer: getAddress('0xCF556d850277BC579c99C0729F4E72e62C57D811'), - Strategy: getAddress('0x8aDF62b0b6078EaE5a2D54e9e5DD2AA71F6748C4'), - Minter: getAddress('0xF2f51B00C2e9b77F23fD66649bbabf8a025c39eF'), - Operator: getAddress('0x33559576B062D08230b467ea7DC7Ce75aFcbdE92'), + Controller: getAddress('0xE64aCE1bF550E57461cd4e24706633d7faC9D7b0'), + BookManager: getAddress('0xAA9575d63dFC224b9583fC303dB3188C08d5C85A'), + BookViewer: getAddress('0x3e22d091F90ae759733B7CB06a6f7b440d84a425'), + Rebalancer: getAddress('0x6A35651C455898021492d236149Afb7bF354bE1F'), + Strategy: getAddress('0x888E1fafb5f2574b901142f877D186DE6C5ef89d'), + Minter: getAddress('0xF2B48E2fc5463A8cD165363d0C7cd14E18449521'), + Operator: getAddress('0x78FC5635f80441eB3708240C1A1EB7CbFba39B29'), }, [CHAIN_IDS.ARBITRUM_SEPOLIA]: { - Controller: getAddress('0xb278E0Cf476394b84385C7a9d936B6009fddC30b'), - BookManager: getAddress('0xC528b9ED5d56d1D0d3C18A2342954CE1069138a4'), - BookViewer: getAddress('0xb291cb35361a740Af7BDfA1B2CbDD4522Ed9a50F'), - Rebalancer: getAddress('0xCF556d850277BC579c99C0729F4E72e62C57D811'), - Strategy: getAddress('0x8aDF62b0b6078EaE5a2D54e9e5DD2AA71F6748C4'), - Minter: getAddress('0xF2f51B00C2e9b77F23fD66649bbabf8a025c39eF'), - Operator: getAddress('0x33559576B062D08230b467ea7DC7Ce75aFcbdE92'), + Controller: getAddress('0xE64aCE1bF550E57461cd4e24706633d7faC9D7b0'), + BookManager: getAddress('0xAA9575d63dFC224b9583fC303dB3188C08d5C85A'), + BookViewer: getAddress('0x3e22d091F90ae759733B7CB06a6f7b440d84a425'), + Rebalancer: getAddress('0x6A35651C455898021492d236149Afb7bF354bE1F'), + Strategy: getAddress('0x888E1fafb5f2574b901142f877D186DE6C5ef89d'), + Minter: getAddress('0xF2B48E2fc5463A8cD165363d0C7cd14E18449521'), + Operator: getAddress('0x78FC5635f80441eB3708240C1A1EB7CbFba39B29'), }, [CHAIN_IDS.BASE]: { Controller: getAddress('0xe4AB03992e214acfdCD05ccFB5C5C16e3d0Ca371'), diff --git a/src/model/pool.ts b/src/model/pool.ts index ebde687..3ca14cc 100644 --- a/src/model/pool.ts +++ b/src/model/pool.ts @@ -28,6 +28,7 @@ export class Pool { claimableB: bigint orderListA: bigint[] orderListB: bigint[] + paused: boolean constructor({ chainId, @@ -49,6 +50,7 @@ export class Pool { claimableB, orderListA, orderListB, + paused, }: { chainId: CHAIN_IDS market: Market @@ -69,6 +71,7 @@ export class Pool { claimableB: bigint orderListA: bigint[] orderListB: bigint[] + paused: boolean }) { this.chainId = chainId this.key = toPoolKey(bookIdA, bookIdB, salt) @@ -113,6 +116,7 @@ export class Pool { this.reserveB = reserveB this.orderListA = orderListA this.orderListB = orderListB + this.paused = paused } toJson = (): PoolType => { @@ -167,6 +171,7 @@ export class Pool { }, orderListA: this.orderListA.map((order) => order.toString()), orderListB: this.orderListB.map((order) => order.toString()), + paused: this.paused, } } } diff --git a/src/type.ts b/src/type.ts index a9f5a48..190d110 100644 --- a/src/type.ts +++ b/src/type.ts @@ -63,6 +63,7 @@ export type Pool = { totalSupply: Currency6909Amount orderListA: string[] orderListB: string[] + paused: boolean } export type Transaction = {