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

feat: add spread profit, fill and sort #121

Merged
merged 2 commits into from
Oct 22, 2024
Merged
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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@clober/v2-sdk",
"version": "0.0.75.dev",
"version": "0.0.76.dev",
"description": "🛠 An SDK for building applications on top of Clober V2",
"files": [
"dist"
Expand Down
12 changes: 10 additions & 2 deletions src/apis/pool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { PublicClient } from 'viem'

import { CHAIN_IDS } from '../constants/chain'
import { Pool, PoolSnapshotDto, PoolVolumeDto } from '../model/pool'
import {
Pool,
PoolSnapshotDto,
PoolSpreadProfitDto,
PoolVolumeDto,
} from '../model/pool'
import { CONTRACT_ADDRESSES } from '../constants/addresses'
import { toPoolKey } from '../utils/pool-key'
import { REBALANCER_ABI } from '../abis/rebalancer/rebalancer-abi'
Expand All @@ -15,20 +20,23 @@
poolKey: `0x${string}`,
volumeFromTimestamp: number,
snapshotFromTimestamp: number,
spreadProfitFromTimestamp: number,
) => {
return Subgraph.get<{
data: {
poolVolumes: PoolVolumeDto[]
poolSnapshots: PoolSnapshotDto[]
poolSpreadProfits: PoolSpreadProfitDto[]
}
}>(
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 } }',
'query getPoolPerformanceData($poolKey: String!, $volumeFrom: BigInt!, $snapshotFrom: BigInt!, $spreadProfitFrom: 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 } poolSpreadProfits( where: { intervalType: "1h", timestamp_gte: $spreadProfitFrom, } ) { id intervalType timestamp accumulatedProfitInUsd } }',
{
poolKey,
volumeFrom: BigInt(volumeFromTimestamp),
snapshotFrom: BigInt(snapshotFromTimestamp),
spreadProfitFrom: BigInt(spreadProfitFromTimestamp),
},
)
}
Expand Down Expand Up @@ -58,7 +66,7 @@
{ bookIdA, bookIdB, reserveA, reserveB, orderListA, orderListB, paused },
totalSupply,
[totalLiquidityA, totalLiquidityB],
] = await publicClient.multicall({

Check failure on line 69 in src/apis/pool.ts

View workflow job for this annotation

GitHub Actions / NPM Test (18.10)

add-liquidity.test.ts > Add liquidity without swap - 1

ContractFunctionExecutionError: Position `320` is out of bounds (`0 < position < 320`). Contract Call: address: 0xCF556d850277BC579c99C0729F4E72e62C57D811 function: getPool(bytes32 key) args: (0x7f1a575ceb4d9d07a92abe57886551352626b6b600bcada45fa83c3e58cc7e3d) Docs: https://viem.sh/docs/contract/multicall Version: [email protected] ❯ getContractError ../node_modules/viem/utils/errors/getContractError.ts:72:10 ❯ multicall ../node_modules/viem/actions/public/multicall.ts:271:23 ❯ Module.fetchPool ../src/apis/pool.ts:69:7 ❯ Module.getPool ../src/view.ts:185:16 ❯ add-liquidity.test.ts:31:16 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { details: undefined, docsPath: '/docs/contract/multicall', metaMessages: [ 'Contract Call:', ' address: 0xCF556d850277BC579c99C0729F4E72e62C57D811\n function: getPool(bytes32 key)\n args: (0x7f1a575ceb4d9d07a92abe57886551352626b6b600bcada45fa83c3e58cc7e3d)' ], shortMessage: 'Position `320` is out of bounds (`0 < position < 320`).', version: '[email protected]', abi: [ { inputs: [ { internalType: 'contract IBookManager', name: 'bookManager_', type: 'address' }, { internalType: 'address', name: 'initialOwner_', type: 'address' } ], stateMutability: 'nonpayable', type: 'constructor' }, { inputs: [], name: 'AlreadyOpened', type: 'error' }, { inputs: [], name: 'ERC20TransferFailed', type: 'error' }, { inputs: [], name: 'InvalidAmount', type: 'error' }, { inputs: [], name: 'InvalidBookPair', type: 'error' }, { inputs: [], name: 'InvalidHook', type: 'error' }, { inputs: [], name: 'InvalidLockAcquiredSender', type: 'error' }, { inputs: [], name: 'InvalidLockCaller', type: 'error' }, { inputs: [], name: 'InvalidMaker', type: 'error' }, { inputs: [], name: 'InvalidStrategy', type: 'error' }, { inputs: [], name: 'InvalidTick', type: 'error' }, { inputs: [], name: 'InvalidValue', type: 'error' }, { inputs: [], name: 'LockFailure', type: 'error' }, { inputs: [], name: 'NativeTransferFailed', type: 'error' }, { inputs: [], name: 'NotSelf', type: 'error' }, { inputs: [ { internalType: 'address', name: 'owner', type: 'address' } ], name: 'OwnableInvalidOwner', type: 'error' }, { inputs: [ { internalType: 'address', name: 'account', type: 'address' } ], 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: [ { internalType: 'address', name: 'token', type: 'address' } ], name: 'SafeERC20FailedOperation', type: 'error' }, { inputs: [], name: 'Slippage', type: 'error' }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'owner', type: 'address' }, { indexed: true, internalType: 'address', name: 'spender', type: 'address' }, { indexed: true, internalType: 'uint256', name: 'id', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'amount', type: 'uint256' } ], name: 'Approval', type: 'event' }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'user', type: 'address' }, { indexed: true, internalType: 'bytes32', name: 'key', type: 'bytes32' }, { indexed: false, internalType: 'uint256', name: 'amountA', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'amountB', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'lpAmount', type: 'uint256' } ], 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',

Check failure on line 69 in src/apis/pool.ts

View workflow job for this annotation

GitHub Actions / NPM Test (18.10)

add-liquidity.test.ts > Add liquidity without swap - 2

ContractFunctionExecutionError: Position `320` is out of bounds (`0 < position < 320`). Contract Call: address: 0xCF556d850277BC579c99C0729F4E72e62C57D811 function: getPool(bytes32 key) args: (0x7f1a575ceb4d9d07a92abe57886551352626b6b600bcada45fa83c3e58cc7e3d) Docs: https://viem.sh/docs/contract/multicall Version: [email protected] ❯ getContractError ../node_modules/viem/utils/errors/getContractError.ts:72:10 ❯ multicall ../node_modules/viem/actions/public/multicall.ts:271:23 ❯ Module.fetchPool ../src/apis/pool.ts:69:7 ❯ Module.getPool ../src/view.ts:185:16 ❯ add-liquidity.test.ts:193:16 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { details: undefined, docsPath: '/docs/contract/multicall', metaMessages: [ 'Contract Call:', ' address: 0xCF556d850277BC579c99C0729F4E72e62C57D811\n function: getPool(bytes32 key)\n args: (0x7f1a575ceb4d9d07a92abe57886551352626b6b600bcada45fa83c3e58cc7e3d)' ], shortMessage: 'Position `320` is out of bounds (`0 < position < 320`).', version: '[email protected]', abi: [ { inputs: [ { internalType: 'contract IBookManager', name: 'bookManager_', type: 'address' }, { internalType: 'address', name: 'initialOwner_', type: 'address' } ], stateMutability: 'nonpayable', type: 'constructor' }, { inputs: [], name: 'AlreadyOpened', type: 'error' }, { inputs: [], name: 'ERC20TransferFailed', type: 'error' }, { inputs: [], name: 'InvalidAmount', type: 'error' }, { inputs: [], name: 'InvalidBookPair', type: 'error' }, { inputs: [], name: 'InvalidHook', type: 'error' }, { inputs: [], name: 'InvalidLockAcquiredSender', type: 'error' }, { inputs: [], name: 'InvalidLockCaller', type: 'error' }, { inputs: [], name: 'InvalidMaker', type: 'error' }, { inputs: [], name: 'InvalidStrategy', type: 'error' }, { inputs: [], name: 'InvalidTick', type: 'error' }, { inputs: [], name: 'InvalidValue', type: 'error' }, { inputs: [], name: 'LockFailure', type: 'error' }, { inputs: [], name: 'NativeTransferFailed', type: 'error' }, { inputs: [], name: 'NotSelf', type: 'error' }, { inputs: [ { internalType: 'address', name: 'owner', type: 'address' } ], name: 'OwnableInvalidOwner', type: 'error' }, { inputs: [ { internalType: 'address', name: 'account', type: 'address' } ], 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: [ { internalType: 'address', name: 'token', type: 'address' } ], name: 'SafeERC20FailedOperation', type: 'error' }, { inputs: [], name: 'Slippage', type: 'error' }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'owner', type: 'address' }, { indexed: true, internalType: 'address', name: 'spender', type: 'address' }, { indexed: true, internalType: 'uint256', name: 'id', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'amount', type: 'uint256' } ], name: 'Approval', type: 'event' }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'user', type: 'address' }, { indexed: true, internalType: 'bytes32', name: 'key', type: 'bytes32' }, { indexed: false, internalType: 'uint256', name: 'amountA', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'amountB', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'lpAmount', type: 'uint256' } ], 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'

Check failure on line 69 in src/apis/pool.ts

View workflow job for this annotation

GitHub Actions / NPM Test (18.10)

add-liquidity.test.ts > Add liquidity one side with swap

ContractFunctionExecutionError: Position `320` is out of bounds (`0 < position < 320`). Contract Call: address: 0xCF556d850277BC579c99C0729F4E72e62C57D811 function: getPool(bytes32 key) args: (0x7f1a575ceb4d9d07a92abe57886551352626b6b600bcada45fa83c3e58cc7e3d) Docs: https://viem.sh/docs/contract/multicall Version: [email protected] ❯ getContractError ../node_modules/viem/utils/errors/getContractError.ts:72:10 ❯ multicall ../node_modules/viem/actions/public/multicall.ts:271:23 ❯ Module.fetchPool ../src/apis/pool.ts:69:7 ❯ Module.getPool ../src/view.ts:185:16 ❯ add-liquidity.test.ts:355:16 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { details: undefined, docsPath: '/docs/contract/multicall', metaMessages: [ 'Contract Call:', ' address: 0xCF556d850277BC579c99C0729F4E72e62C57D811\n function: getPool(bytes32 key)\n args: (0x7f1a575ceb4d9d07a92abe57886551352626b6b600bcada45fa83c3e58cc7e3d)' ], shortMessage: 'Position `320` is out of bounds (`0 < position < 320`).', version: '[email protected]', abi: [ { inputs: [ { internalType: 'contract IBookManager', name: 'bookManager_', type: 'address' }, { internalType: 'address', name: 'initialOwner_', type: 'address' } ], stateMutability: 'nonpayable', type: 'constructor' }, { inputs: [], name: 'AlreadyOpened', type: 'error' }, { inputs: [], name: 'ERC20TransferFailed', type: 'error' }, { inputs: [], name: 'InvalidAmount', type: 'error' }, { inputs: [], name: 'InvalidBookPair', type: 'error' }, { inputs: [], name: 'InvalidHook', type: 'error' }, { inputs: [], name: 'InvalidLockAcquiredSender', type: 'error' }, { inputs: [], name: 'InvalidLockCaller', type: 'error' }, { inputs: [], name: 'InvalidMaker', type: 'error' }, { inputs: [], name: 'InvalidStrategy', type: 'error' }, { inputs: [], name: 'InvalidTick', type: 'error' }, { inputs: [], name: 'InvalidValue', type: 'error' }, { inputs: [], name: 'LockFailure', type: 'error' }, { inputs: [], name: 'NativeTransferFailed', type: 'error' }, { inputs: [], name: 'NotSelf', type: 'error' }, { inputs: [ { internalType: 'address', name: 'owner', type: 'address' } ], name: 'OwnableInvalidOwner', type: 'error' }, { inputs: [ { internalType: 'address', name: 'account', type: 'address' } ], 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: [ { internalType: 'address', name: 'token', type: 'address' } ], name: 'SafeERC20FailedOperation', type: 'error' }, { inputs: [], name: 'Slippage', type: 'error' }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'owner', type: 'address' }, { indexed: true, internalType: 'address', name: 'spender', type: 'address' }, { indexed: true, internalType: 'uint256', name: 'id', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'amount', type: 'uint256' } ], name: 'Approval', type: 'event' }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'user', type: 'address' }, { indexed: true, internalType: 'bytes32', name: 'key', type: 'bytes32' }, { indexed: false, internalType: 'uint256', name: 'amountA', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'amountB', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'lpAmount', type: 'uint256' } ], 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'

Check failure on line 69 in src/apis/pool.ts

View workflow job for this annotation

GitHub Actions / NPM Test (18.10)

add-liquidity.test.ts > Add liquidity two sides with swap

ContractFunctionExecutionError: Position `320` is out of bounds (`0 < position < 320`). Contract Call: address: 0xCF556d850277BC579c99C0729F4E72e62C57D811 function: getPool(bytes32 key) args: (0x7f1a575ceb4d9d07a92abe57886551352626b6b600bcada45fa83c3e58cc7e3d) Docs: https://viem.sh/docs/contract/multicall Version: [email protected] ❯ getContractError ../node_modules/viem/utils/errors/getContractError.ts:72:10 ❯ multicall ../node_modules/viem/actions/public/multicall.ts:271:23 ❯ Module.fetchPool ../src/apis/pool.ts:69:7 ❯ Module.getPool ../src/view.ts:185:16 ❯ add-liquidity.test.ts:593:16 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { details: undefined, docsPath: '/docs/contract/multicall', metaMessages: [ 'Contract Call:', ' address: 0xCF556d850277BC579c99C0729F4E72e62C57D811\n function: getPool(bytes32 key)\n args: (0x7f1a575ceb4d9d07a92abe57886551352626b6b600bcada45fa83c3e58cc7e3d)' ], shortMessage: 'Position `320` is out of bounds (`0 < position < 320`).', version: '[email protected]', abi: [ { inputs: [ { internalType: 'contract IBookManager', name: 'bookManager_', type: 'address' }, { internalType: 'address', name: 'initialOwner_', type: 'address' } ], stateMutability: 'nonpayable', type: 'constructor' }, { inputs: [], name: 'AlreadyOpened', type: 'error' }, { inputs: [], name: 'ERC20TransferFailed', type: 'error' }, { inputs: [], name: 'InvalidAmount', type: 'error' }, { inputs: [], name: 'InvalidBookPair', type: 'error' }, { inputs: [], name: 'InvalidHook', type: 'error' }, { inputs: [], name: 'InvalidLockAcquiredSender', type: 'error' }, { inputs: [], name: 'InvalidLockCaller', type: 'error' }, { inputs: [], name: 'InvalidMaker', type: 'error' }, { inputs: [], name: 'InvalidStrategy', type: 'error' }, { inputs: [], name: 'InvalidTick', type: 'error' }, { inputs: [], name: 'InvalidValue', type: 'error' }, { inputs: [], name: 'LockFailure', type: 'error' }, { inputs: [], name: 'NativeTransferFailed', type: 'error' }, { inputs: [], name: 'NotSelf', type: 'error' }, { inputs: [ { internalType: 'address', name: 'owner', type: 'address' } ], name: 'OwnableInvalidOwner', type: 'error' }, { inputs: [ { internalType: 'address', name: 'account', type: 'address' } ], 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: [ { internalType: 'address', name: 'token', type: 'address' } ], name: 'SafeERC20FailedOperation', type: 'error' }, { inputs: [], name: 'Slippage', type: 'error' }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'owner', type: 'address' }, { indexed: true, internalType: 'address', name: 'spender', type: 'address' }, { indexed: true, internalType: 'uint256', name: 'id', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'amount', type: 'uint256' } ], name: 'Approval', type: 'event' }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'user', type: 'address' }, { indexed: true, internalType: 'bytes32', name: 'key', type: 'bytes32' }, { indexed: false, internalType: 'uint256', name: 'amountA', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'amountB', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'lpAmount', type: 'uint256' } ], 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'

Check failure on line 69 in src/apis/pool.ts

View workflow job for this annotation

GitHub Actions / NPM Test (18.10)

open.test.ts > try open pool

ContractFunctionExecutionError: Position `320` is out of bounds (`0 < position < 320`). Contract Call: address: 0xCF556d850277BC579c99C0729F4E72e62C57D811 function: getPool(bytes32 key) args: (0x6cf422b2bb53aafd274d55740e6c8437055eba392cb2f69633c35462ab920bb7) Docs: https://viem.sh/docs/contract/multicall Version: [email protected] ❯ getContractError ../node_modules/viem/utils/errors/getContractError.ts:72:10 ❯ multicall ../node_modules/viem/actions/public/multicall.ts:271:23 ❯ Module.fetchPool ../src/apis/pool.ts:69:7 ❯ Module.getPool ../src/view.ts:185:16 ❯ open.test.ts:140:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { details: undefined, docsPath: '/docs/contract/multicall', metaMessages: [ 'Contract Call:', ' address: 0xCF556d850277BC579c99C0729F4E72e62C57D811\n function: getPool(bytes32 key)\n args: (0x6cf422b2bb53aafd274d55740e6c8437055eba392cb2f69633c35462ab920bb7)' ], shortMessage: 'Position `320` is out of bounds (`0 < position < 320`).', version: '[email protected]', abi: [ { inputs: [ { internalType: 'contract IBookManager', name: 'bookManager_', type: 'address' }, { internalType: 'address', name: 'initialOwner_', type: 'address' } ], stateMutability: 'nonpayable', type: 'constructor' }, { inputs: [], name: 'AlreadyOpened', type: 'error' }, { inputs: [], name: 'ERC20TransferFailed', type: 'error' }, { inputs: [], name: 'InvalidAmount', type: 'error' }, { inputs: [], name: 'InvalidBookPair', type: 'error' }, { inputs: [], name: 'InvalidHook', type: 'error' }, { inputs: [], name: 'InvalidLockAcquiredSender', type: 'error' }, { inputs: [], name: 'InvalidLockCaller', type: 'error' }, { inputs: [], name: 'InvalidMaker', type: 'error' }, { inputs: [], name: 'InvalidStrategy', type: 'error' }, { inputs: [], name: 'InvalidTick', type: 'error' }, { inputs: [], name: 'InvalidValue', type: 'error' }, { inputs: [], name: 'LockFailure', type: 'error' }, { inputs: [], name: 'NativeTransferFailed', type: 'error' }, { inputs: [], name: 'NotSelf', type: 'error' }, { inputs: [ { internalType: 'address', name: 'owner', type: 'address' } ], name: 'OwnableInvalidOwner', type: 'error' }, { inputs: [ { internalType: 'address', name: 'account', type: 'address' } ], 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: [ { internalType: 'address', name: 'token', type: 'address' } ], name: 'SafeERC20FailedOperation', type: 'error' }, { inputs: [], name: 'Slippage', type: 'error' }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'owner', type: 'address' }, { indexed: true, internalType: 'address', name: 'spender', type: 'address' }, { indexed: true, internalType: 'uint256', name: 'id', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'amount', type: 'uint256' } ], name: 'Approval', type: 'event' }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'user', type: 'address' }, { indexed: true, internalType: 'bytes32', name: 'key', type: 'bytes32' }, { indexed: false, internalType: 'uint256', name: 'amountA', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'amountB', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'lpAmount', type: 'uint256' } ], 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: '

Check failure on line 69 in src/apis/pool.ts

View workflow job for this annotation

GitHub Actions / NPM Test (18.10)

remove-liquidity.test.ts > Remove liquidity

ContractFunctionExecutionError: Position `320` is out of bounds (`0 < position < 320`). Contract Call: address: 0xCF556d850277BC579c99C0729F4E72e62C57D811 function: getPool(bytes32 key) args: (0x7f1a575ceb4d9d07a92abe57886551352626b6b600bcada45fa83c3e58cc7e3d) Docs: https://viem.sh/docs/contract/multicall Version: [email protected] ❯ getContractError ../node_modules/viem/utils/errors/getContractError.ts:72:10 ❯ multicall ../node_modules/viem/actions/public/multicall.ts:271:23 ❯ Module.fetchPool ../src/apis/pool.ts:69:7 ❯ Module.getPool ../src/view.ts:185:16 ❯ remove-liquidity.test.ts:31:16 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { details: undefined, docsPath: '/docs/contract/multicall', metaMessages: [ 'Contract Call:', ' address: 0xCF556d850277BC579c99C0729F4E72e62C57D811\n function: getPool(bytes32 key)\n args: (0x7f1a575ceb4d9d07a92abe57886551352626b6b600bcada45fa83c3e58cc7e3d)' ], shortMessage: 'Position `320` is out of bounds (`0 < position < 320`).', version: '[email protected]', abi: [ { inputs: [ { internalType: 'contract IBookManager', name: 'bookManager_', type: 'address' }, { internalType: 'address', name: 'initialOwner_', type: 'address' } ], stateMutability: 'nonpayable', type: 'constructor' }, { inputs: [], name: 'AlreadyOpened', type: 'error' }, { inputs: [], name: 'ERC20TransferFailed', type: 'error' }, { inputs: [], name: 'InvalidAmount', type: 'error' }, { inputs: [], name: 'InvalidBookPair', type: 'error' }, { inputs: [], name: 'InvalidHook', type: 'error' }, { inputs: [], name: 'InvalidLockAcquiredSender', type: 'error' }, { inputs: [], name: 'InvalidLockCaller', type: 'error' }, { inputs: [], name: 'InvalidMaker', type: 'error' }, { inputs: [], name: 'InvalidStrategy', type: 'error' }, { inputs: [], name: 'InvalidTick', type: 'error' }, { inputs: [], name: 'InvalidValue', type: 'error' }, { inputs: [], name: 'LockFailure', type: 'error' }, { inputs: [], name: 'NativeTransferFailed', type: 'error' }, { inputs: [], name: 'NotSelf', type: 'error' }, { inputs: [ { internalType: 'address', name: 'owner', type: 'address' } ], name: 'OwnableInvalidOwner', type: 'error' }, { inputs: [ { internalType: 'address', name: 'account', type: 'address' } ], 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: [ { internalType: 'address', name: 'token', type: 'address' } ], name: 'SafeERC20FailedOperation', type: 'error' }, { inputs: [], name: 'Slippage', type: 'error' }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'owner', type: 'address' }, { indexed: true, internalType: 'address', name: 'spender', type: 'address' }, { indexed: true, internalType: 'uint256', name: 'id', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'amount', type: 'uint256' } ], name: 'Approval', type: 'event' }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'user', type: 'address' }, { indexed: true, internalType: 'bytes32', name: 'key', type: 'bytes32' }, { indexed: false, internalType: 'uint256', name: 'amountA', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'amountB', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'lpAmount', type: 'uint256' } ], 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: 'claimedAmount
allowFailure: false,
contracts: [
{
Expand Down
7 changes: 7 additions & 0 deletions src/model/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,10 @@ export type PoolSnapshotDto = {
liquidityB: bigint
totalSupply: bigint
}

export type PoolSpreadProfitDto = {
id: string
intervalType: '1h'
timestamp: bigint
accumulatedProfitInUsd: string
}
7 changes: 7 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,14 @@ export type PoolSnapshotDto = {
totalSupply: Currency6909Amount
}

export type PoolSpreadProfitDto = {
intervalType: '1h'
timestamp: number
accumulatedProfitInUsd: string
}

export type PoolPerformanceData = {
poolVolumes: PoolVolumeDto[]
poolSnapshots: PoolSnapshotDto[]
poolSpreadProfits: PoolSpreadProfitDto[]
}
31 changes: 31 additions & 0 deletions src/utils/time-series.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export function fillAndSortByTimestamp<
T extends { timestamp: number | bigint },
>(
data: T[],
granularity: number,
emptyObjectGenerator: (timestamp: number, prev: T) => T,
): T[] {
const sortedData = data.sort(
(a, b) => Number(a.timestamp) - Number(b.timestamp),
)

const result: T[] = []

for (let i = 0; i < sortedData.length; i++) {
result.push(sortedData[i])

if (i < sortedData.length - 1) {
const currentTimestamp = sortedData[i].timestamp
const nextTimestamp = sortedData[i + 1].timestamp
for (
let ts = Number(currentTimestamp) + granularity;
ts < Number(nextTimestamp);
ts += granularity
) {
result.push(emptyObjectGenerator(ts, sortedData[i]))
}
}
}

return result
}
67 changes: 65 additions & 2 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ import { MAX_TICK, MIN_TICK } from './constants/tick'
import { fetchPool, fetchPoolPerformance } from './apis/pool'
import { fetchStrategyPrice } from './apis/strategy'
import { Subgraph } from './constants/subgraph'
import { fillAndSortByTimestamp } from './utils/time-series'
import {
PoolSnapshotDto as ModelPoolSnapshot,
PoolSpreadProfitDto as ModelPoolSpreadProfit,
PoolVolumeDto as ModelPoolVolume,
} from './model/pool'

/**
* Get contract addresses by chain id
Expand Down Expand Up @@ -194,6 +200,7 @@ export const getPoolPerformance = async ({
salt,
volumeFromTimestamp,
snapshotFromTimestamp,
spreadProfitFromTimestamp,
options,
}: {
chainId: CHAIN_IDS
Expand All @@ -202,6 +209,7 @@ export const getPoolPerformance = async ({
salt: `0x${string}`
volumeFromTimestamp: number
snapshotFromTimestamp: number
spreadProfitFromTimestamp: number
options?: {
pool?: Pool
useSubgraph?: boolean
Expand Down Expand Up @@ -237,9 +245,59 @@ export const getPoolPerformance = async ({
pool.key,
volumeFromTimestamp,
snapshotFromTimestamp,
spreadProfitFromTimestamp,
)
const poolVolumes = fillAndSortByTimestamp(
poolPerformance.data.poolVolumes,
24 * 60 * 60,
(timestamp: number) => {
const emptyPoolVolume: ModelPoolVolume = {
id: '',
poolKey: pool.key,
intervalType: '1d',
timestamp: BigInt(timestamp),
currencyAVolume: 0n,
currencyBVolume: 0n,
bookACurrencyAVolume: 0n,
bookACurrencyBVolume: 0n,
bookBCurrencyAVolume: 0n,
bookBCurrencyBVolume: 0n,
}
return emptyPoolVolume
},
)
const poolSnapshots = fillAndSortByTimestamp(
poolPerformance.data.poolSnapshots,
60 * 60,
(timestamp: number, prev: ModelPoolSnapshot) => {
const emptyPoolSnapshot: ModelPoolSnapshot = {
id: '',
poolKey: pool.key,
intervalType: '1h',
timestamp: BigInt(timestamp),
price: prev.price,
liquidityA: prev.liquidityA,
liquidityB: prev.liquidityB,
totalSupply: prev.totalSupply,
}
return emptyPoolSnapshot
},
)
const poolSpreadProfits = fillAndSortByTimestamp(
poolPerformance.data.poolSpreadProfits,
60 * 60,
(timestamp: number) => {
const emptyPoolSpreadProfit: ModelPoolSpreadProfit = {
id: '',
intervalType: '1h',
timestamp: BigInt(timestamp),
accumulatedProfitInUsd: '0',
}
return emptyPoolSpreadProfit
},
)
return {
poolVolumes: poolPerformance.data.poolVolumes.map((poolVolume) => ({
poolVolumes: poolVolumes.map((poolVolume) => ({
poolKey: poolVolume.poolKey,
intervalType: poolVolume.intervalType,
timestamp: Number(poolVolume.timestamp),
Expand All @@ -252,7 +310,7 @@ export const getPoolPerformance = async ({
value: formatUnits(poolVolume.currencyBVolume, pool.currencyB.decimals),
},
})),
poolSnapshots: poolPerformance.data.poolSnapshots.map((poolSnapshot) => ({
poolSnapshots: poolSnapshots.map((poolSnapshot) => ({
poolKey: poolSnapshot.poolKey,
intervalType: poolSnapshot.intervalType,
timestamp: Number(poolSnapshot.timestamp),
Expand All @@ -270,6 +328,11 @@ export const getPoolPerformance = async ({
value: formatUnits(poolSnapshot.totalSupply, pool.currencyLp.decimals),
},
})),
poolSpreadProfits: poolSpreadProfits.map((poolSpreadProfit) => ({
intervalType: poolSpreadProfit.intervalType,
timestamp: Number(poolSpreadProfit.timestamp),
accumulatedProfitInUsd: poolSpreadProfit.accumulatedProfitInUsd,
})),
}
}

Expand Down
Loading