Skip to content

Commit

Permalink
feat: refactor vault history fetch to use subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
DillonLin authored Dec 1, 2022
1 parent e7e154b commit 4d642f1
Show file tree
Hide file tree
Showing 16 changed files with 568 additions and 190 deletions.
7 changes: 2 additions & 5 deletions app/utils/getVaultsHistoryCSV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ export const getVaultsHistoryCSV = (events: VaultHistory) => {
const headers = [
{ label: 'Date Time', key: 'datetime' },
{ label: 'Timestamp', key: 'timestamp' },
{ label: 'Block Number', key: 'blockNumber' },
{ label: 'Transaction Hash', key: 'transactionHash' },
{ label: 'Action', key: 'action' },
{ label: 'Value (in sUSD)', key: 'value' },
]

const data = [...events.deposits, ...events.withdrawals].map(event => {
const blockNumber = event.__processed?.blockNumber ?? 0
const value = fromBigNumber(event.value ?? ZERO_BN)
let datetime = ''
let timestamp = 0
Expand All @@ -25,19 +23,18 @@ export const getVaultsHistoryCSV = (events: VaultHistory) => {
const date = new Date(event.depositTimestamp * 1000)
datetime = date.toISOString()
timestamp = event.depositTimestamp
transactionHash = event.__processed?.blockHash ?? ''
transactionHash = event.__processed?.transactionHash ?? ''
action = 'deposit'
} else if (event instanceof LiquidityWithdrawal) {
const date = new Date(event.withdrawalTimestamp * 1000)
datetime = date.toISOString()
timestamp = event.withdrawalTimestamp
transactionHash = event.__processed?.blockHash ?? ''
transactionHash = event.__processed?.transactionHash ?? ''
action = 'withdraw'
}
return {
datetime,
timestamp,
blockNumber,
transactionHash,
action,
value,
Expand Down
112 changes: 112 additions & 0 deletions sdk/src/constants/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,66 @@ export const STRIKE_IV_AND_GREEKS_SNAPSHOT_FRAGMENT = `
iv
`

export const LIQUIDITY_DEPOSIT_FRAGMENT = `
pool {
market {
id
}
}
user
pendingDepositsAndWithdrawals(where: {
isDeposit: true
}) {
id
queueID
pendingAmount
processedAmount
timestamp
transactionHash
}
depositsAndWithdrawals(where: {
isDeposit: true
}) {
id
queueID
quoteAmount
tokenAmount
tokenPrice
timestamp
transactionHash
}
`

export const LIQUIDITY_WITHDRAWAL_FRAGMENT = `
pool {
market {
id
}
}
user
pendingDepositsAndWithdrawals(where: {
isDeposit: false
}) {
id
queueID
pendingAmount
processedAmount
timestamp
transactionHash
}
depositsAndWithdrawals(where: {
isDeposit: false
}) {
id
queueID
quoteAmount
tokenAmount
tokenPrice
timestamp
transactionHash
}
`

export type TradeQueryResult = {
timestamp: number
blockNumber: number
Expand Down Expand Up @@ -564,3 +624,55 @@ export type TokenTransfer = {
to: string
tokenAddress: string
}

export type LiquidityDepositQueryResult = {
pool: {
id: string
}
user: string
pendingDepositsAndWithdrawals: {
id: string
isDeposit: boolean
queueID: string
pendingAmount: string
processedAmount: string
timestamp: number
transactionHash: string
}[]
depositsAndWithdrawals: {
id: string
isDeposit: boolean
queueID: string
quoteAmount: string
tokenAmount: string
tokenPrice: string
timestamp: number
transactionHash: string
}[]
}

export type LiquidityWithdrawalQueryResult = {
pool: {
id: string
}
user: string
pendingDepositsAndWithdrawals: {
id: string
isDeposit: boolean
queueID: string
pendingAmount: string
processedAmount: string
transactionHash: string
timestamp: number
}[]
depositsAndWithdrawals: {
id: string
isDeposit: boolean
queueID: string
quoteAmount: string
tokenAmount: string
tokenPrice: string
timestamp: number
transactionHash: string
}[]
}
2 changes: 1 addition & 1 deletion sdk/src/examples/liquidity-deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import yargs from 'yargs'

import getLyra from './utils/getLyra'

export default async function liquidityDeposit(argv: string[]) {
export default async function liquidityDeposits(argv: string[]) {
const lyra = getLyra()
const args = await yargs(argv).options({
market: { type: 'string', alias: 'm', require: true },
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/examples/liquidity-withdrawal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import yargs from 'yargs'

import getLyra from './utils/getLyra'

export default async function liquidityWithdrawal(argv: string[]) {
export default async function liquidityWithdrawals(argv: string[]) {
const lyra = getLyra()
const args = await yargs(argv).options({
market: { type: 'string', alias: 'm', require: true },
Expand Down
68 changes: 43 additions & 25 deletions sdk/src/liquidity_deposit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { BigNumber } from '@ethersproject/bignumber'
import { PopulatedTransaction } from '@ethersproject/contracts'

import { LyraMarketContractId } from '../constants/contracts'
import { DepositProcessedEvent, DepositQueuedEvent } from '../contracts/typechain/LiquidityPool'
import Lyra from '../lyra'
import { Market } from '../market'
import buildTxWithGasEstimate from '../utils/buildTxWithGasEstimate'
import fetchLiquidityDelayReason from '../utils/fetchLiquidityDelayReason'
import fetchLiquidityDepositEventDataByID from '../utils/fetchLiquidityDepositEventDataByID'
import fetchLiquidityDepositEventDataByOwner from '../utils/fetchLiquidityDepositEventDataByOwner'
import getLyraMarketContract from '../utils/getLyraMarketContract'

Expand All @@ -17,10 +15,40 @@ export enum LiquidityDelayReason {
Keeper = 'Keeper',
}

export type LiquidityDepositFilter = {
user: string
}

export type DepositQueuedOrProcessedEvent = {
queued?: LiquidityDepositQueuedEvent
processed?: LiquidityDepositProcessedEvent
}

export type LiquidityDepositQueuedEvent = {
depositor: string
beneficiary: string
depositQueueId: BigNumber
amountDeposited: BigNumber
totalQueuedDeposits: BigNumber
timestamp: BigNumber
transactionHash: string
}

export type LiquidityDepositProcessedEvent = {
caller: string
beneficiary: string
depositQueueId: BigNumber
amountDeposited: BigNumber
tokenPrice: BigNumber
tokensReceived: BigNumber
timestamp: BigNumber
transactionHash: string
}

export class LiquidityDeposit {
lyra: Lyra
__queued?: DepositQueuedEvent
__processed?: DepositProcessedEvent
__queued?: LiquidityDepositQueuedEvent
__processed?: LiquidityDepositProcessedEvent
__market: Market
queueId?: number
beneficiary: string
Expand All @@ -35,8 +63,8 @@ export class LiquidityDeposit {
lyra: Lyra,
market: Market,
data: {
queued?: DepositQueuedEvent
processed?: DepositProcessedEvent
queued?: LiquidityDepositQueuedEvent
processed?: LiquidityDepositProcessedEvent
delayReason: LiquidityDelayReason | null
}
) {
Expand All @@ -53,17 +81,17 @@ export class LiquidityDeposit {
if (!queuedOrProcessed) {
throw new Error('No queued or processed event for LiquidityDeposit')
}
this.queueId = queuedOrProcessed.args.depositQueueId.toNumber()
this.beneficiary = queuedOrProcessed.args.beneficiary
this.value = queuedOrProcessed.args.amountDeposited
this.tokenPriceAtDeposit = processed?.args.tokenPrice
this.balance = processed?.args.tokensReceived
this.queueId = queuedOrProcessed.depositQueueId.toNumber()
this.beneficiary = queuedOrProcessed.beneficiary
this.value = queuedOrProcessed.amountDeposited
this.tokenPriceAtDeposit = processed?.tokenPrice
this.balance = processed?.tokensReceived
this.isPending = !processed
this.depositRequestedTimestamp = queuedOrProcessed.args.timestamp.toNumber()
this.depositRequestedTimestamp = queuedOrProcessed.timestamp.toNumber()
this.depositTimestamp = processed
? processed.args.timestamp.toNumber()
? processed.timestamp.toNumber()
: queued
? queued.args.timestamp.add(market.__marketData.marketParameters.lpParams.depositDelay).toNumber()
? queued.timestamp.add(market.__marketData.marketParameters.lpParams.depositDelay).toNumber()
: // Should never happen
0
this.delayReason = data.delayReason
Expand All @@ -73,7 +101,7 @@ export class LiquidityDeposit {

static async getByOwner(lyra: Lyra, marketAddress: string, owner: string): Promise<LiquidityDeposit[]> {
const market = await Market.get(lyra, marketAddress)
const { events } = await fetchLiquidityDepositEventDataByOwner(lyra, market, owner)
const { events } = await fetchLiquidityDepositEventDataByOwner(lyra, owner, market)
const liquidityDeposits: LiquidityDeposit[] = await Promise.all(
events.map(async event => {
const delayReason = await fetchLiquidityDelayReason(lyra, market, event)
Expand All @@ -86,16 +114,6 @@ export class LiquidityDeposit {
return liquidityDeposits
}

static async getByQueueId(lyra: Lyra, marketAddress: string, id: string): Promise<LiquidityDeposit> {
const market = await Market.get(lyra, marketAddress)
const event = await fetchLiquidityDepositEventDataByID(lyra, market, id)
const delayReason = await fetchLiquidityDelayReason(lyra, market, event)
return new LiquidityDeposit(lyra, market, {
...event,
delayReason,
})
}

// Initiate Deposit

static async deposit(
Expand Down
Loading

0 comments on commit 4d642f1

Please sign in to comment.