Skip to content

Commit

Permalink
chore(app): fix referral query of volume (#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeifuChen authored Sep 20, 2023
1 parent 8d02c24 commit 2882aa8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/src/queries/futures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export const queryVolumeByTrader = async (
mintedTime: string
): Promise<FuturesTradeByReferral[]> => {
let queryResponseCount = 0
let lastMintedAtInSeconds = Math.floor(Number(mintedTime) / 1000)
let lastMintedAtInSeconds = Math.floor(Number(mintedTime))
const currentTimeInSeconds = Math.floor(new Date().getTime() / 1000)
const futuresTrades: FuturesTradeByReferral[] = []

Expand Down
44 changes: 32 additions & 12 deletions packages/sdk/src/queries/referrals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,37 @@ export const queryCodesByReferrer = async (
}

export const queryTradersByCode = async (sdk: KwentaSDK, code: string): Promise<BoostHolder[]> => {
const response: { boostHolders: BoostHolder[] } = await request(
sdk.referrals.referralsGqlEndpoint,
gql`
query boostHolders($code: String!) {
boostHolders(where: { code: $code }) {
id
lastMintedAt
let queryResponseCount = 0
const currentTimeInSeconds = Math.floor(new Date().getTime() / 1000)
const boostHolders: BoostHolder[] = []
let maxTimestamp = currentTimeInSeconds

do {
const response: { boostHolders: BoostHolder[] } = await request(
sdk.referrals.referralsGqlEndpoint,
gql`
query boostHolders($code: String!, $maxTimestamp: BigInt!) {
boostHolders(
where: { lastMintedAt_lte: $maxTimestamp, code: $code }
orderBy: lastMintedAt
orderDirection: desc
first: 1000
) {
id
lastMintedAt
}
}
}
`,
{ code: formatBytes32String(code) }
)
return response?.boostHolders || []
`,
{ code: formatBytes32String(code), maxTimestamp }
)

queryResponseCount = response.boostHolders.length

if (queryResponseCount > 0) {
maxTimestamp = Number(response.boostHolders[queryResponseCount - 1].lastMintedAt)
boostHolders.push(...response.boostHolders)
}
} while (queryResponseCount === 1000)

return boostHolders
}
25 changes: 25 additions & 0 deletions packages/sdk/src/queries/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export async function limitConcurrency<T, R>(
tasks: T[],
handler: (task: T) => Promise<R>,
limit: number
): Promise<R[]> {
const results: R[] = []
const taskQueue: T[] = [...tasks]

const executeTask = async () => {
if (taskQueue.length === 0) return

const task = taskQueue.pop()
if (task !== undefined) {
const result = await handler(task)
results.push(result)
}

await executeTask()
}

const initialPromises = Array.from({ length: limit }).map(() => executeTask())
await Promise.all(initialPromises)

return results
}
16 changes: 12 additions & 4 deletions packages/sdk/src/utils/referrals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import KwentaSDK from '..'
import { REFERRALS_ENDPOINTS } from '../constants/referrals'
import { queryVolumeByTrader } from '../queries/futures'
import { queryCodesByReferrer, queryTradersByCode } from '../queries/referrals'
import { limitConcurrency } from '../queries/utils'
import { FuturesTradeByReferral } from '../types/futures'
import { ReferralCumulativeStats } from '../types/referrals'

Expand All @@ -25,10 +26,17 @@ export const getReferralStatisticsByAccount = async (
codes.map(async (code) => {
const traders = await queryTradersByCode(sdk, code)

const totalVolume = await traders.reduce(async (accVolume, trader) => {
const volume = await queryVolumeByTrader(sdk, trader.id, trader.lastMintedAt)
return (await accVolume) + calculateTraderVolume(volume)
}, Promise.resolve(0))
const totalVolumeByTrader = await limitConcurrency(
traders,
async ({ id, lastMintedAt }) => {
const trades = await queryVolumeByTrader(sdk, id, lastMintedAt)
if (!trades.length) return 0
return calculateTraderVolume(trades)
},
200
)

const totalVolume = totalVolumeByTrader.reduce((acc, curr) => acc + curr, 0)

return {
code,
Expand Down

0 comments on commit 2882aa8

Please sign in to comment.