Skip to content

Commit

Permalink
fix: username not available after sync, helper query to sync all user…
Browse files Browse the repository at this point in the history
…names with nft.io (#580)
  • Loading branch information
justraman authored Aug 28, 2023
1 parent 3aec071 commit 9b3a032
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/mappings/balances/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,13 @@ export async function saveAccounts(ctx: CommonContext, block: SubstrateBlock) {
}
}

await ctx.store.createQueryBuilder().insert().into(Account).values(accounts).orUpdate(['balance', 'nonce'], ['id']).execute()
await ctx.store
.createQueryBuilder()
.insert()
.into(Account)
.values(accounts)
.orUpdate(['balance', 'nonce', 'username', 'image', 'verified_at'], ['id'])
.execute()
accountsSet.clear()
}

Expand Down
2 changes: 2 additions & 0 deletions src/server-extension/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { TokenSalesHistoryResolver } from './token_sales_history'
import { VerifyMessageResolver } from './verify_message'
import { TopCollectionResolver } from './top_collections'
import { MyTokensResolver } from './my_tokens'
import { RefreshAllAccountResolver } from './refresh_all_account'

export {
TokenSalesHistoryResolver,
VerifyMessageResolver,
RefreshMetadataResolver,
RefreshAccountResolver,
RefreshAllAccountResolver,
TopCollectionResolver,
MyTokensResolver,
}
63 changes: 63 additions & 0 deletions src/server-extension/resolvers/refresh_all_account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Query, Resolver } from 'type-graphql'
import 'reflect-metadata'
import type { EntityManager } from 'typeorm'
import { fetchAccountsDetail } from '../../mappings/util/marketplace'
import { Account } from '../../model'

let lastCalled = 0
const LIMIT = 100

function isNotNull<T>(input: null | T): input is T {
return input != null
}

@Resolver()
export class RefreshAllAccountResolver {
constructor(private tx: () => Promise<EntityManager>) {}

@Query(() => Boolean, { nullable: false })
async refreshAllAccount(): Promise<boolean> {
if (lastCalled && Date.now() - lastCalled < 1000 * 60 * 10) {
return false
}

lastCalled = Date.now()
const manager = await this.tx()

const count = await manager.count(Account)

for (let offset = 0; offset < count - LIMIT; offset += LIMIT) {
// eslint-disable-next-line no-await-in-loop
const accounts = await manager.find(Account, { skip: offset, take: LIMIT, select: ['id'] })
const ids = accounts.map((a) => a.id)
// eslint-disable-next-line no-await-in-loop
const data = await fetchAccountsDetail(ids)

if (!data || data.length === 0) break

const accountsToUpdate = data.filter(isNotNull).map((a) => ({
publicKey: a.publicKey ? `'${a.publicKey}'` : 'NULL',
username: a.username ? `'${a.username}'` : 'NULL',
image: a.image ? `'${a.image}'` : 'NULL',
verifiedAt: a.verifiedAt ? `to_timestamp(${a.verifiedAt.getTime() / 1000})` : 'NULL',
}))

// eslint-disable-next-line no-console
console.log(`Updating ${accountsToUpdate.length} accounts, updated ${offset + LIMIT}/${count} so far`)

if (accountsToUpdate.length !== 0) {
// eslint-disable-next-line no-await-in-loop
await manager.query(
`UPDATE account as a SET username = t.username, image = t.image, verified_at = t.verified_at::timestamptz FROM (VALUES${accountsToUpdate
.map((a) => `(${a.publicKey}, ${a.username}, ${a.image}, ${a.verifiedAt})`)
.join(',')}) AS t(id, username, image, verified_at) WHERE a.id = t.id`
)
}
}

// eslint-disable-next-line no-console
console.log('Done updating accounts')

return true
}
}

0 comments on commit 9b3a032

Please sign in to comment.