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

local testing tools #283

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,7 @@ dist
.pnp.*

#
*.tsbuildinfo
*.tsbuildinfo

# Local
transferLogs.json
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/adapters-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@
"@types/inquirer": "^8.2.6",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.14.197",
"bottleneck": "^2.19.5",
"chalk": "^4.1.2",
"commander": "^11.0.0",
"ethereum-block-by-date": "^1.4.9",
"inquirer": "^8.2.6",
"jest": "^29.7.0",
"npm-run-all": "^4.1.5",
"p-queue": "^6.6.2",
"patch-package": "^8.0.0",
"pino-pretty": "^10.2.0",
"recast": "^0.23.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { count } from '../../metricsCount'
import { AVERAGE_BLOCKS_PER_10_MINUTES } from '../constants/AVERAGE_BLOCKS_PER_10_MINS'
import { Chain } from '../constants/chains'
import { NotSupportedUnlimitedGetLogsBlockRange } from '../errors/errors'
import { logger } from '../utils/logger'
import { retryHandlerFactory } from './retryHandlerFactory'

export type CustomJsonRpcProviderOptions = {
Expand Down Expand Up @@ -109,11 +110,24 @@ export class CustomJsonRpcProvider extends JsonRpcProvider {
}

const entryPromise = (async () => {
const result = this.callRetryHandler(() => super.call(transaction))

return {
result: await result,
timestamp: Date.now(),
try {
const result = await this.callRetryHandler(() =>
super.call(transaction),
)

return {
result: result,
timestamp: Date.now(),
}
} catch (error) {
logger.error({
source: 'rpc:call',
chainId: this.chainId,
transaction,
error: error instanceof Error ? error.message : error,
})

throw error
}
})()

Expand All @@ -139,11 +153,22 @@ export class CustomJsonRpcProvider extends JsonRpcProvider {
}

const entryPromise = (async () => {
const result = this.logsRetryHandler(() => super.getLogs(filter))

return {
result: await result,
timestamp: Date.now(),
try {
const result = await this.logsRetryHandler(() => super.getLogs(filter))

return {
result: result,
timestamp: Date.now(),
}
} catch (error) {
logger.error({
source: 'rpc:getLogs',
chainId: this.chainId,
filter,
error: error instanceof Error ? error.message : error,
})

throw error
}
})()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ describe('retryHandlerFactory', () => {
expect(result).toEqual({})
})

it('does not set a timer when timeout is set to 0', async () => {
const retryHandler = retryHandlerFactory({
timeoutInMs: 0,
maxRetries: 0,
})

const result = await retryHandler(async () => {
return await new Promise((resolve) => {
setTimeout(() => {
resolve({})
}, 1000)
})
})

expect(result).toEqual({})
})

it('throws if the timeout is reached', async () => {
const retryHandler = retryHandlerFactory({
timeoutInMs: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export function retryHandlerFactory({
retryCount = 0,
): Promise<T> {
try {
if (timeoutInMs <= 0) {
return await action()
}

return await new Promise<T>((resolve, reject) => {
const timeout = setTimeout(
() => reject(new Error(TimeoutErrorMessage)),
Expand Down
31 changes: 21 additions & 10 deletions packages/adapters-library/src/defiProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ export class DefiProvider {
blockNumbers,
filterProtocolTokens,
filterTokenIds,
chainTransferContractAddresses,
}: {
userAddress: string
filterProtocolIds?: Protocol[]
filterChainIds?: Chain[]
blockNumbers?: Partial<Record<Chain, number>>
filterProtocolTokens?: string[]
filterTokenIds?: string[]
chainTransferContractAddresses?: Partial<Record<Chain, string[]>>
}): Promise<DefiPositionResponse[]> {
const startGetPositions = Date.now()
this.initAdapterControllerForUnwrapStage()
Expand All @@ -111,6 +113,7 @@ export class DefiProvider {
userAddress,
adapter,
filterProtocolTokens,
chainTransferContractAddresses?.[adapter.chainId],
)

// no transfers so we return
Expand Down Expand Up @@ -207,7 +210,8 @@ export class DefiProvider {
private async buildTokenFilter(
userAddress: string,
adapter: IProtocolAdapter,
filterProtocolTokensOverride?: string[],
filterProtocolTokensOverride: string[] | undefined,
userTransferEventAddresses: string[] | undefined,
) {
try {
// we use the overrides if provided
Expand All @@ -223,10 +227,17 @@ export class DefiProvider {
return undefined
}

const transferLogs =
await this.chainProvider.providers[
adapter.chainId
].getAllTransferLogsToAddress(userAddress)
const transferLogs = userTransferEventAddresses
? userTransferEventAddresses
: Array.from(
new Set(
(
await this.chainProvider.providers[
adapter.chainId
].getAllTransferLogsToAddress(userAddress)
).map((log) => log.address),
),
)

// no logs on this chain means nothing done on this chain
if (transferLogs.length === 0) {
Expand All @@ -240,13 +251,9 @@ export class DefiProvider {
return undefined
}

const uniqueAddresses = Array.from(
new Set(transferLogs.map((log) => log.address)),
)

// we can build the filter
const matchingProtocolTokenAddresses = await filterMapAsync(
uniqueAddresses,
transferLogs,
async (address) => {
const isAdapterToken =
await this.adaptersController.isTokenBelongToAdapter(
Expand All @@ -271,6 +278,7 @@ export class DefiProvider {

logger.warn(
{
userAddress: `${userAddress.slice(0, 7)}...${userAddress.slice(-5)}`,
chainId: adapter.chainId,
protocolId: adapter.protocolId,
productId: adapter.productId,
Expand All @@ -293,6 +301,7 @@ export class DefiProvider {
filterProtocolTokens,
includeRawValues = false,
filterTokenIds,
chainUserTransferEventAddresses,
}: {
userAddress: string
timePeriod?: TimePeriod
Expand All @@ -302,6 +311,7 @@ export class DefiProvider {
filterProtocolTokens?: string[]
filterTokenIds?: string[]
includeRawValues?: boolean
chainUserTransferEventAddresses?: Record<Chain, string[] | undefined>
}): Promise<DefiProfitsResponse[]> {
this.initAdapterControllerForUnwrapStage()

Expand All @@ -323,6 +333,7 @@ export class DefiProvider {
userAddress,
adapter,
filterProtocolTokens,
chainUserTransferEventAddresses?.[adapter.chainId],
)

// no transfers so we return
Expand Down
3 changes: 3 additions & 0 deletions packages/adapters-library/src/scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { buildSnapshots } from './buildSnapshots'
import { buildContractTypes } from './buildTypes'
import { copyAdapter } from './copyAdapter'
import { featureCommands } from './featureCommands'
import { localTestingCommands } from './localTesting'
import { newAdapter2Command } from './newAdapter2Command'
import { newAdapterCommand } from './newAdapterCommand'
import { performance } from './performance'
Expand Down Expand Up @@ -60,4 +61,6 @@ program
},
)

localTestingCommands(program, defiProvider)

program.parseAsync()
Loading
Loading