Skip to content

Commit 7c0b38e

Browse files
committed
add entity valdiations, work out some issues
1 parent f4f1d04 commit 7c0b38e

File tree

9 files changed

+1570
-1164
lines changed

9 files changed

+1570
-1164
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"codegen": "echo '# GENERATED, DO NOT MODIFY\n' > schema.graphql && cat schema/*.graphql >> schema.graphql && sqd codegen && git add src/model/generated/*",
66
"migration:generate": "sqd down && sqd up && sqd migration:generate && git add db/migrations/*",
77
"generate": "npm-run-all codegen migration:generate",
8+
"generate:validations": "ts-node scripts/generate-validations.ts",
89
"build": "rm -rf lib && tsc",
910
"prettier-check": "prettier --check src",
1011
"prettier-fix": "prettier --write src",

scripts/generate-validations.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs'
22

33
import { addresses } from './../src/utils/addresses'
44
import { baseAddresses } from './../src/utils/addresses-base'
5+
import { retry } from './../src/utils/retry'
56

67
const LIMIT = 1000
78

@@ -23,7 +24,7 @@ const executeQuery = async (query: string) => {
2324
throw err
2425
}
2526
}
26-
const takePortion = (arr: any[], takeEvery: number) => {
27+
const takeEvery = (arr: any[], takeEvery: number) => {
2728
if (takeEvery <= 0) {
2829
throw new Error('takeEvery must be greater than 0')
2930
}
@@ -78,7 +79,7 @@ const oTokenHistories = (prefix: string, address: string) => {
7879
return gql(`
7980
${prefix}_oTokenHistories: oTokenHistories(
8081
limit: ${LIMIT},
81-
orderBy: timestamp_ASC,
82+
orderBy: id_ASC,
8283
where: { otoken_eq: "${address}" }
8384
) {
8485
id
@@ -87,6 +88,7 @@ const oTokenHistories = (prefix: string, address: string) => {
8788
chainId
8889
balance
8990
otoken
91+
address { address }
9092
type
9193
txHash
9294
value
@@ -185,7 +187,7 @@ const erc20Balances = (prefix: string, address: string) => {
185187
return gql(`
186188
${prefix}_erc20Balances: erc20Balances(
187189
limit: ${LIMIT},
188-
orderBy: id_ASC,
190+
orderBy: [blockNumber_ASC, account_ASC],
189191
where: { address_eq: "${address}" }
190192
) {
191193
id
@@ -215,13 +217,13 @@ const main = async () => {
215217
for (let i = 0; i < queries.length; i++) {
216218
const query = queries[i]
217219
console.log(`Executing: \`${query.replace(/(\n|\s)+/g, ' ').slice(0, 80)}\`...`)
218-
const result = await executeQuery(query)
220+
const result = await retry(() => executeQuery(query), 5)
219221
if (!result.data) {
220222
console.log(result)
221223
throw new Error('Query failed')
222224
}
223225
for (const key of Object.keys(result.data)) {
224-
entities[key] = takePortion(result.data[key], 25)
226+
entities[key] = takeEvery(result.data[key], 25)
225227
}
226228
}
227229

src/base/validate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ERC20Balance, OToken, OTokenAPY, OTokenDailyStat, OTokenRebase } from '@model'
1+
import { ERC20Balance, OToken, OTokenAPY, OTokenDailyStat, OTokenHistory, OTokenRebase } from '@model'
22
import { Context } from '@processor'
33
import { env } from '@utils/env'
44
import { entities } from '@validation/entities'
@@ -13,7 +13,7 @@ export const process = async (ctx: Context) => {
1313
for (const block of ctx.blocks) {
1414
await validateExpectations(ctx, block, OToken, firstBlock, entities.superoethb_oTokens)
1515
await validateExpectations(ctx, block, OTokenAPY, firstBlock, entities.superoethb_oTokenApies)
16-
// await validateExpectations(ctx, block, OTokenHistory, firstBlock, entities.superoethb_oTokenHistories)
16+
await validateExpectations(ctx, block, OTokenHistory, firstBlock, entities.superoethb_oTokenHistories)
1717
await validateExpectations(ctx, block, OTokenRebase, firstBlock, entities.superoethb_oTokenRebases)
1818
await validateExpectations(ctx, block, OTokenDailyStat, firstBlock, entities.superoethb_oTokenDailyStats)
1919
await validateExpectations(ctx, block, ERC20Balance, firstBlock, entities.superoethb_erc20Balances)

src/mainnet/validators/validate-mainnet.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const process = async (ctx: Context) => {
1212
if (env.BLOCK_FROM) return
1313
for (const block of ctx.blocks) {
1414
await validateExpectations(ctx, block, ERC20Balance, firstBlock, entities.ogn_erc20Balances)
15+
await validateExpectations(ctx, block, ERC20Balance, firstBlock, entities.oeth_erc20Balances)
1516
firstBlock = false
1617
}
1718
}

src/oeth/validators/validate-oeth/validate-oeth.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { sortBy } from 'lodash'
22

33
import {
4-
ERC20Balance,
54
OETHDailyStat,
65
OETHMorphoAave,
76
OETHVault,
87
OToken,
98
OTokenAPY,
109
OTokenDailyStat,
10+
OTokenHistory,
1111
OTokenRebase,
1212
StrategyBalance,
1313
} from '@model'
@@ -25,10 +25,9 @@ export const process = async (ctx: Context) => {
2525
for (const block of ctx.blocks) {
2626
await validateExpectations(ctx, block, OToken, firstBlock, entities.oeth_oTokens)
2727
await validateExpectations(ctx, block, OTokenAPY, firstBlock, entities.oeth_oTokenApies)
28-
// await validateExpectations(ctx, block, OTokenHistory, firstBlock, entities.oeth_oTokenHistories)
28+
await validateExpectations(ctx, block, OTokenHistory, firstBlock, entities.oeth_oTokenHistories)
2929
await validateExpectations(ctx, block, OTokenRebase, firstBlock, entities.oeth_oTokenRebases)
3030
await validateExpectations(ctx, block, OTokenDailyStat, firstBlock, entities.oeth_oTokenDailyStats)
31-
await validateExpectations(ctx, block, ERC20Balance, firstBlock, entities.oeth_erc20Balances)
3231
await validateExpectations(ctx, block, OETHVault, firstBlock, expectations.oethVaults)
3332
await validateExpectations(ctx, block, OETHMorphoAave, firstBlock, expectations.oethMorphoAave)
3433
await validateExpectations(ctx, block, StrategyBalance, firstBlock, expectations.strategyBalances)

src/ousd/validators/validate-ousd/validate-ousd.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ERC20Balance, OToken, OTokenAPY, OTokenDailyStat, OTokenRebase } from '@model'
1+
import { ERC20Balance, OToken, OTokenAPY, OTokenDailyStat, OTokenHistory, OTokenRebase } from '@model'
22
import { Context } from '@processor'
33
import { env } from '@utils/env'
44
import { entities } from '@validation/entities'
@@ -13,7 +13,7 @@ export const process = async (ctx: Context) => {
1313
for (const block of ctx.blocks) {
1414
await validateExpectations(ctx, block, OToken, firstBlock, entities.ousd_oTokens)
1515
await validateExpectations(ctx, block, OTokenAPY, firstBlock, entities.ousd_oTokenApies)
16-
// await validateExpectations(ctx, block, OTokenHistory, firstBlock, entities.ousd_oTokenHistories)
16+
await validateExpectations(ctx, block, OTokenHistory, firstBlock, entities.ousd_oTokenHistories)
1717
await validateExpectations(ctx, block, OTokenRebase, firstBlock, entities.ousd_oTokenRebases)
1818
await validateExpectations(ctx, block, OTokenDailyStat, firstBlock, entities.ousd_oTokenDailyStats)
1919
await validateExpectations(ctx, block, ERC20Balance, firstBlock, entities.ousd_erc20Balances)

src/templates/otoken/utils.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dayjs from 'dayjs'
22
import utc from 'dayjs/plugin/utc'
33
import { findLast, last } from 'lodash'
4-
import { LessThan, MoreThanOrEqual } from 'typeorm'
4+
import { In, LessThan, MoreThanOrEqual, Not } from 'typeorm'
55

66
import * as otoken from '@abi/otoken'
77
import { OTokenAPY, OTokenAddress, OTokenRebase, RebasingOption } from '@model'
@@ -208,14 +208,18 @@ export async function createRebaseAPY(
208208
days: 30,
209209
}
210210

211-
const last30daysAPYs = await ctx.store.find(OTokenAPY, {
212-
where: {
213-
chainId: ctx.chain.id,
214-
otoken: otokenAddress,
215-
date: MoreThanOrEqual(last30daysDateId.value),
216-
},
217-
order: { id: 'asc' },
218-
})
211+
let last30daysAPYs: OTokenAPY[] = apies.filter((apy) => apy.date >= last30daysDateId.value)
212+
last30daysAPYs = last30daysAPYs.concat(
213+
await ctx.store.find(OTokenAPY, {
214+
where: {
215+
chainId: ctx.chain.id,
216+
otoken: otokenAddress,
217+
date: MoreThanOrEqual(last30daysDateId.value),
218+
id: Not(In(last30daysAPYs.map((apy) => apy.id))),
219+
},
220+
order: { id: 'asc' },
221+
}),
222+
)
219223

220224
const blockDateId = generateId(block.header.timestamp)
221225
for (const i of [last7daysDateId, last14daysDateId, last30daysDateId]) {
@@ -224,7 +228,7 @@ export async function createRebaseAPY(
224228
return dateId >= i.value && dateId < blockDateId
225229
})
226230
// console.log(i.days, pastAPYs.length)
227-
apy![i.key] = pastAPYs.reduce((acc, cur) => acc + cur.apy, apy!.apy) / (pastAPYs.length + 1)
231+
apy![i.key] = pastAPYs.reduce((acc, cur) => acc + cur.apy, apy!.apy) / i.days
228232
}
229233

230234
return rebase

src/utils/retry.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const retry = async <T>(fn: () => Promise<T>, retries: number) => {
2+
for (let i = 0; i < retries; i++) {
3+
try {
4+
return await fn()
5+
} catch (e) {
6+
console.error(e)
7+
if (i === retries - 1) {
8+
throw e
9+
} else {
10+
console.log(`Retrying... ${i + 1} of ${retries}`)
11+
}
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)