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

User logs outside loop #340

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
100 changes: 76 additions & 24 deletions packages/adapters-library/src/defiProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Database from 'better-sqlite3'
import { getAddress } from 'ethers'
import { getAddress, Log } from 'ethers'
import {
IMetadataProvider,
buildMetadataProviders,
Expand Down Expand Up @@ -128,11 +128,15 @@ export class DefiProvider {
const startGetPositions = Date.now()
this.initAdapterControllerForUnwrapStage()

const runner = async (adapter: IProtocolAdapter) => {
const runner = async (
adapter: IProtocolAdapter,
provider: CustomJsonRpcProvider,
userTransferLogs: Partial<Record<Chain, Promise<Log[]>>>,
) => {
const blockNumber = blockNumbers?.[adapter.chainId]

const protocolTokenAddresses = await this.buildTokenFilter(
userAddress,
userTransferLogs,
adapter,
filterProtocolTokens,
)
Expand Down Expand Up @@ -217,9 +221,16 @@ export class DefiProvider {
return { tokens }
}

const userTransferLogs = this.prefetchUserLogs({
userAddress,
filterProtocolIds,
filterChainIds,
})

const result = (
await this.runForAllProtocolsAndChains({
runner,
runner: (adapter, provider) =>
runner(adapter, provider, userTransferLogs),
filterProtocolIds,
filterChainIds,
method: 'getPositions',
Expand All @@ -245,7 +256,7 @@ export class DefiProvider {
}

private async buildTokenFilter(
userAddress: string,
userTransferLogs: Partial<Record<Chain, Promise<Log[]>>>,
adapter: IProtocolAdapter,
filterProtocolTokensOverride?: string[],
) {
Expand All @@ -263,13 +274,10 @@ export class DefiProvider {
return undefined
}

const transferLogs =
await this.chainProvider.providers[
adapter.chainId
].getAllTransferLogsToAddress(userAddress)
const transferLogs = await userTransferLogs[adapter.chainId]

// no logs on this chain means nothing done on this chain
if (transferLogs.length === 0) {
if (!transferLogs || transferLogs.length === 0) {
return []
}

Expand Down Expand Up @@ -350,6 +358,7 @@ export class DefiProvider {
const runner = async (
adapter: IProtocolAdapter,
provider: CustomJsonRpcProvider,
userTransferLogs: Partial<Record<Chain, Promise<Log[]>>>,
) => {
if (adapter.chainId === Chain.Bsc) {
throw new NotSupportedError('Profits not supported on BSC')
Expand All @@ -362,7 +371,7 @@ export class DefiProvider {
toBlock - AVERAGE_BLOCKS_PER_DAY[adapter.chainId] * timePeriod

const protocolTokenAddresses = await this.buildTokenFilter(
userAddress,
userTransferLogs,
adapter,
filterProtocolTokens,
)
Expand Down Expand Up @@ -404,9 +413,16 @@ export class DefiProvider {
return profits
}

const userTransferLogs = this.prefetchUserLogs({
userAddress,
filterProtocolIds,
filterChainIds,
})

const result = (
await this.runForAllProtocolsAndChains({
runner,
runner: (adapter, provider) =>
runner(adapter, provider, userTransferLogs),
filterProtocolIds,
filterChainIds,
method: 'getProfits',
Expand Down Expand Up @@ -574,7 +590,7 @@ export class DefiProvider {
}
}

return this.runTaskForAdapter(adapter, provider!, runner)
return this.runTaskForAdapter(adapter, provider, runner)
}

async getTransactionParams(input: GetTransactionParams): Promise<
Expand Down Expand Up @@ -604,7 +620,7 @@ export class DefiProvider {
}
}

return this.runTaskForAdapter(adapter, provider!, runner)
return this.runTaskForAdapter(adapter, provider, runner)
}

async getDeposits({
Expand Down Expand Up @@ -658,7 +674,7 @@ export class DefiProvider {
}
}

return this.runTaskForAdapter(adapter, provider!, runner)
return this.runTaskForAdapter(adapter, provider, runner)
}
async getRepays({
userAddress,
Expand Down Expand Up @@ -712,7 +728,7 @@ export class DefiProvider {
}
}

return this.runTaskForAdapter(adapter, provider!, runner)
return this.runTaskForAdapter(adapter, provider, runner)
}
async getBorrows({
userAddress,
Expand Down Expand Up @@ -766,7 +782,7 @@ export class DefiProvider {
}
}

return this.runTaskForAdapter(adapter, provider!, runner)
return this.runTaskForAdapter(adapter, provider, runner)
}

async getTotalValueLocked({
Expand Down Expand Up @@ -838,13 +854,7 @@ export class DefiProvider {
) => ReturnType
filterProtocolIds?: Protocol[]
filterChainIds?: Chain[]
method:
| 'getPositions'
| 'unwrap'
| 'getProfits'
| 'getWithdrawals'
| 'getDeposits'
| 'getTotalValueLocked'
method: 'getPositions' | 'unwrap' | 'getProfits' | 'getTotalValueLocked'
}): Promise<AdapterResponse<Awaited<ReturnType>>[]> {
const protocolPromises = Object.entries(supportedProtocols)
.filter(
Expand Down Expand Up @@ -964,4 +974,46 @@ export class DefiProvider {

this.adaptersController.init()
}

private prefetchUserLogs({
userAddress,
filterProtocolIds,
filterChainIds,
}: {
userAddress: string
filterProtocolIds?: Protocol[]
filterChainIds?: Chain[]
}) {
const chains = [
...new Set(
Object.entries(supportedProtocols)
.filter(
([protocolIdKey, _]) =>
!filterProtocolIds ||
filterProtocolIds.includes(protocolIdKey as Protocol),
)
.flatMap(([_, supportedChains]) => {
return Object.entries(supportedChains)
.filter(([chainIdKey, _]) => {
return (
!filterChainIds ||
filterChainIds.includes(+chainIdKey as Chain)
)
})
.flatMap(([chainIdKey, _]) => +chainIdKey as Chain)
}),
),
]

return chains.reduce(
(acc, chainId) => {
acc[chainId] =
this.chainProvider.providers[chainId].getAllTransferLogsToAddress(
userAddress,
)
return acc
},
{} as Partial<Record<Chain, Promise<Log[]>>>,
)
}
}
Loading