Skip to content

Commit

Permalink
fix: conditional DDB (#1383)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsign authored Mar 29, 2024
1 parent e69db7d commit 090436f
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ jobs:
CLOUDFLARE_R2_PUBLIC_URL: ${{ secrets.CLOUDFLARE_R2_PUBLIC_URL }}
SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
#
ANKR_API_KEY: ${{ secrets.ANKR_API_KEY }}
NFTPORT_API_KEY: ${{ secrets.NFTPORT_API_KEY }}
OPENSEA_API_KEY: ${{ secrets.OPENSEA_API_KEY }}
Expand All @@ -48,6 +47,7 @@ jobs:
QUICKNODE_API_KEY: ${{ secrets.QUICKNODE_API_KEY }}
QUICKNODE_HTTP_URL: ${{ secrets.QUICKNODE_HTTP_URL }}
RESERVOIR_API_KEY: ${{ secrets.RESERVOIR_API_KEY }}
BALANCES_DDB: ${{ secrets.BALANCES_DDB }}
steps:
- name: 'Checkout'
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ jobs:
CLOUDFLARE_R2_PUBLIC_URL: ${{ secrets.CLOUDFLARE_R2_PUBLIC_URL }}
SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
#
ANKR_API_KEY: ${{ secrets.ANKR_API_KEY }}
NFTPORT_API_KEY: ${{ secrets.NFTPORT_API_KEY }}
OPENSEA_API_KEY: ${{ secrets.OPENSEA_API_KEY }}
Expand All @@ -46,6 +45,7 @@ jobs:
QUICKNODE_API_KEY: ${{ secrets.QUICKNODE_API_KEY }}
QUICKNODE_HTTP_URL: ${{ secrets.QUICKNODE_HTTP_URL }}
RESERVOIR_API_KEY: ${{ secrets.RESERVOIR_API_KEY }}
BALANCES_DDB: ${{ secrets.BALANCES_DDB }}
steps:
- name: 'Checkout'
uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const environmentSchema = z.object({
// Slack
SLACK_TOKEN: z.string().optional(),
SLACK_CHANNEL_ID: z.string().optional(),
BALANCES_DDB: z.boolean().optional().default(false),
})

export type Environment = z.infer<typeof environmentSchema>
Expand Down
2 changes: 1 addition & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ provider:
CLOUDFLARE_R2_PUBLIC_URL: ${env:CLOUDFLARE_R2_PUBLIC_URL}
SLACK_TOKEN: ${env:SLACK_TOKEN}
SLACK_CHANNEL_ID: ${env:SLACK_CHANNEL_ID}
#
ANKR_API_KEY: ${env:ANKR_API_KEY}
NFTPORT_API_KEY: ${env:NFTPORT_API_KEY}
OPENSEA_API_KEY: ${env:OPENSEA_API_KEY}
Expand All @@ -69,6 +68,7 @@ provider:
QUICKNODE_API_KEY: ${env:QUICKNODE_API_KEY}
QUICKNODE_HTTP_URL: ${env:QUICKNODE_HTTP_URL}
RESERVOIR_API_KEY: ${env:RESERVOIR_API_KEY}
BALANCES_DDB: ${env:BALANCES_DDB}
tableName: ${self:custom.tableName}

functions:
Expand Down
8 changes: 6 additions & 2 deletions src/handlers/getBalances.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { LatestProtocolBalances } from '@db/balances'
import { type LatestProtocolBalances, selectLatestProtocolsBalancesByFromAddresses } from '@db/balances'
import { getBatchBalancesDDB } from '@db/balances-ddb'
import { client } from '@db/clickhouse'
import environment from '@environment'
import { badRequest, serverError, success } from '@handlers/response'
import type { BalancesContext } from '@lib/adapter'
import { BALANCE_UPDATE_THRESHOLD_SEC } from '@lib/balance'
Expand All @@ -25,7 +27,9 @@ export const handler: APIGatewayProxyHandler = async (event) => {
}

try {
const { updatedAt, protocolsBalances, staleAddresses } = await getBatchBalancesDDB({ addresses })
const { updatedAt, protocolsBalances, staleAddresses } = environment.BALANCES_DDB
? await getBatchBalancesDDB({ addresses })
: await selectLatestProtocolsBalancesByFromAddresses(client, addresses)

const status: Status = updatedAt === undefined || staleAddresses.length > 0 ? 'stale' : 'success'

Expand Down
9 changes: 7 additions & 2 deletions src/handlers/updateBalances.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { adapterById } from '@adapters/index'
import type { ClickHouseClient } from '@clickhouse/client'
import { formatBalance } from '@db/balances'
import { formatBalance, insertBalances } from '@db/balances'
import { insertBalancesDDB } from '@db/balances-ddb'
import { client } from '@db/clickhouse'
import { getContractsInteractions, groupContracts } from '@db/contracts'
import environment from '@environment'
import { badRequest, serverError, success } from '@handlers/response'
import { type Balance, type BalancesContext, GET_BALANCES_TIMEOUT, type PricedBalance } from '@lib/adapter'
import { groupBy, groupBy2 } from '@lib/array'
Expand Down Expand Up @@ -175,7 +176,11 @@ export async function updateBalances(client: ClickHouseClient, address: `0x${str

const updatedAt = unixFromDate(new Date())

await insertBalancesDDB({ address, updatedAt, balances: dbBalances })
if (environment.BALANCES_DDB) {
await insertBalancesDDB({ address, updatedAt, balances: dbBalances })
} else {
await insertBalances(client, dbBalances)
}

return { updatedAt }
}
Expand Down

0 comments on commit 090436f

Please sign in to comment.