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

feat: WAPI-1186 Add UserFuelBudget resolver #712

Merged
merged 2 commits into from
Nov 6, 2023
Merged
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
7 changes: 5 additions & 2 deletions src/populateBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,12 @@ async function populateBlockInternal(ctx: CommonContext, block: SubstrateBlock)
await syncTokens(ctx, block)
spinner.succeed(`Successfully imported ${await ctx.store.count(Token)} tokens`)

spinner.start('Syncing token/collection accounts...')
await Promise.all([syncTokenAccounts(ctx, block), syncCollectionAccounts(ctx, block)])
spinner.start('Syncing token accounts...')
await syncTokenAccounts(ctx, block)
spinner.succeed(`Successfully imported ${await ctx.store.count(TokenAccount)} token accounts`)

spinner.start('Syncing collection accounts...')
await syncCollectionAccounts(ctx, block)
spinner.succeed(`Successfully imported ${await ctx.store.count(CollectionAccount)} collection accounts`)

spinner.start('Syncing attributes...')
Expand Down
12 changes: 1 addition & 11 deletions src/server-extension/resolvers/claims_account_nonce.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-classes-per-file */
import { Query, Resolver, Arg, ObjectType, Field, registerEnumType } from 'type-graphql'
import { Query, Resolver, Arg, ObjectType, Field } from 'type-graphql'
import 'reflect-metadata'
import { ApiPromise, WsProvider } from '@polkadot/api'
import config from '../../config'
Expand All @@ -17,16 +17,6 @@ export class ClaimsAccountNonceResult {
}
}

enum AccountType {
Substrate = 'Substrate',
EVM = 'EVM',
}

registerEnumType(AccountType, {
name: 'AccountType',
description: 'account of either evm or substrate',
})

@Resolver()
export class ClaimsAccountNonceResolver {
@Query(() => ClaimsAccountNonceResult)
Expand Down
100 changes: 100 additions & 0 deletions src/server-extension/resolvers/fueltanks-accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* eslint-disable max-classes-per-file */
import { Query, Resolver, Arg, ObjectType, Field } from 'type-graphql'
import 'reflect-metadata'
import { ApiPromise, WsProvider } from '@polkadot/api'
import { hexToU8a } from '@polkadot/util'
import config from '../../config'

const wsProvider = new WsProvider(config.dataSource.chain)
const apiPromise = ApiPromise.create({
provider: wsProvider,
})

const customTypes = {
UserFuelBudget: {
amount: 'PalletFuelTanksBudget',
userCount: 'Compact<u32>',
},
}

@ObjectType()
class UserFuelBudgetAmountType {
@Field({ nullable: false })
amount!: string

@Field({ nullable: false })
resetPeriod!: number

constructor(props: Partial<UserFuelBudgetAmountType>) {
Object.assign(this, props)
}
}

@ObjectType()
class UserFuelBudgetType {
@Field({ nullable: false })
amount!: UserFuelBudgetAmountType

@Field({ nullable: false })
userCount!: number

constructor(props: Partial<UserFuelBudgetType>) {
Object.assign(this, props)
}
}

@ObjectType()
class FuelTanksAccountsResult {
@Field({ nullable: false })
tankDeposit!: string

@Field({ nullable: false })
userDeposit!: number

@Field({ nullable: true })
userFuelBudget?: UserFuelBudgetType

constructor(props: Partial<FuelTanksAccountsResult>) {
Object.assign(this, props)
}
}

@Resolver()
export class FuelTanksAccountsResolver {
@Query(() => FuelTanksAccountsResult, { nullable: true })
async fuelTanksAccounts(
@Arg('fuelTank', {
description: 'address of fuelTank',
})
fuelTank: string,
@Arg('account', {
description: 'address of account',
})
account: string
): Promise<FuelTanksAccountsResult | null> {
const api = await apiPromise
api.registerTypes(customTypes)

const res = await api.query.fuelTanks.accounts(fuelTank, account)

const resJson: any = res.toJSON()

if (!resJson) {
return null
}

let userFuelBudget: undefined | UserFuelBudgetType

if (resJson && resJson.ruleDataSets && resJson.ruleDataSets[0] && resJson.ruleDataSets[0].UserFuelBudget) {
userFuelBudget = res.registry
.createType('UserFuelBudget', hexToU8a(resJson.ruleDataSets[0].UserFuelBudget))
.toJSON() as any
}

return {
tankDeposit: resJson.tankDeposit,
userDeposit: resJson.userDeposit,
userFuelBudget,
}
}
}
2 changes: 2 additions & 0 deletions src/server-extension/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { TopCollectionResolver } from './top_collections'
import { MyTokensResolver } from './my_tokens'
import { ClaimsAccountNonceResolver } from './claims_account_nonce'
import { RefreshCollectionsResolver } from './refresh_collections'
import { FuelTanksAccountsResolver } from './fueltanks-accounts'

export {
TokenSalesHistoryResolver,
VerifyMessageResolver,
RefreshMetadataResolver,
RefreshAccountResolver,
FuelTanksAccountsResolver,
RefreshCollectionsResolver,
TopCollectionResolver,
MyTokensResolver,
Expand Down