From f796087a3714889f5076a7d3b4852e0c5f50c27a Mon Sep 17 00:00:00 2001 From: Raman Shekhawat Date: Tue, 21 Nov 2023 14:07:06 +0530 Subject: [PATCH] fix: fixes bug with token amount, Add `totalBalance` and Drop `tokenValues` from tokenAccount (#719) --- db/migrations/1700476842756-Data.js | 13 +++++++++++++ schema.graphql | 2 +- src/mappings/balances/processor.ts | 2 -- src/mappings/multiTokens/events/burned.ts | 9 ++------- .../events/collection_account_created.ts | 2 +- .../multiTokens/events/collection_mutated.ts | 2 +- src/mappings/multiTokens/events/frozen.ts | 8 ++++---- src/mappings/multiTokens/events/minted.ts | 1 + src/mappings/multiTokens/events/reserved.ts | 5 +++-- src/mappings/multiTokens/events/thawed.ts | 8 ++++---- .../multiTokens/events/token_account_created.ts | 5 +++-- .../multiTokens/events/token_account_destroyed.ts | 2 +- src/mappings/multiTokens/events/token_mutated.ts | 2 +- src/mappings/multiTokens/events/transferred.ts | 2 ++ src/mappings/multiTokens/events/unapproved.ts | 4 ++-- src/mappings/multiTokens/events/unreserved.ts | 11 ++++++----- src/mappings/util/entities.ts | 1 - src/model/generated/account.model.ts | 3 --- src/model/generated/tokenAccount.model.ts | 3 +++ src/populateBlock.ts | 2 +- 20 files changed, 49 insertions(+), 38 deletions(-) create mode 100644 db/migrations/1700476842756-Data.js diff --git a/db/migrations/1700476842756-Data.js b/db/migrations/1700476842756-Data.js new file mode 100644 index 000000000..73d108502 --- /dev/null +++ b/db/migrations/1700476842756-Data.js @@ -0,0 +1,13 @@ +module.exports = class Data1700476842756 { + name = 'Data1700476842756' + + async up(db) { + await db.query(`ALTER TABLE "account" DROP COLUMN "token_values"`) + await db.query(`ALTER TABLE "token_account" ADD "total_balance" numeric NOT NULL`) + } + + async down(db) { + await db.query(`ALTER TABLE "account" ADD "token_values" numeric NOT NULL`) + await db.query(`ALTER TABLE "token_account" DROP COLUMN "total_balance"`) + } +} diff --git a/schema.graphql b/schema.graphql index ebe6b865c..3feb202aa 100644 --- a/schema.graphql +++ b/schema.graphql @@ -301,7 +301,6 @@ type Account @entity { tokenEvents: [AccountTokenEvent] @derivedFrom(field: "from") # Extras - tokenValues: BigInt! lastUpdateBlock: Int # profile @@ -524,6 +523,7 @@ type TokenAccount @entity { id: ID! # Storage defaults + totalBalance: BigInt! balance: BigInt! reservedBalance: BigInt! lockedBalance: BigInt! diff --git a/src/mappings/balances/processor.ts b/src/mappings/balances/processor.ts index b56edd9e7..439fb5813 100644 --- a/src/mappings/balances/processor.ts +++ b/src/mappings/balances/processor.ts @@ -230,7 +230,6 @@ export async function saveAccounts(ctx: CommonContext, block: SubstrateBlock) { feeFrozen: 0n, miscFrozen: 0n, }), - tokenValues: 0n, }) } else if ('miscFrozen' in accountData) { accounts.push({ @@ -244,7 +243,6 @@ export async function saveAccounts(ctx: CommonContext, block: SubstrateBlock) { feeFrozen: accountData.feeFrozen, miscFrozen: accountData.miscFrozen, }), - tokenValues: 0n, }) } } diff --git a/src/mappings/multiTokens/events/burned.ts b/src/mappings/multiTokens/events/burned.ts index 2d6c32223..b5ffdb8c9 100644 --- a/src/mappings/multiTokens/events/burned.ts +++ b/src/mappings/multiTokens/events/burned.ts @@ -97,8 +97,9 @@ export async function burned( if (tokenAccount) { tokenAccount.balance -= data.amount + tokenAccount.totalBalance -= data.amount tokenAccount.updatedAt = new Date(block.timestamp) - ctx.store.save(tokenAccount) + await ctx.store.save(tokenAccount) } if (token) { @@ -108,11 +109,5 @@ export async function burned( syncCollectionStats(data.collectionId.toString()) } - if (tokenAccount && token) { - const { account } = tokenAccount - account.tokenValues -= data.amount * (token.unitPrice ?? 10_000_000_000_000n) - ctx.store.save(account) - } - return getEvent(item, data) } diff --git a/src/mappings/multiTokens/events/collection_account_created.ts b/src/mappings/multiTokens/events/collection_account_created.ts index d72d14494..be42ffcf9 100644 --- a/src/mappings/multiTokens/events/collection_account_created.ts +++ b/src/mappings/multiTokens/events/collection_account_created.ts @@ -71,7 +71,7 @@ export async function collectionAccountCreated( createdAt: new Date(block.timestamp), updatedAt: new Date(block.timestamp), }) - ctx.store.insert(CollectionAccount, newAccount as any) + await ctx.store.insert(CollectionAccount, newAccount as any) } return getEvent(item, data) diff --git a/src/mappings/multiTokens/events/collection_mutated.ts b/src/mappings/multiTokens/events/collection_mutated.ts index b7d5430ac..d6e77e00e 100644 --- a/src/mappings/multiTokens/events/collection_mutated.ts +++ b/src/mappings/multiTokens/events/collection_mutated.ts @@ -117,7 +117,7 @@ export async function collectionMutated( } } - ctx.store.save(collection) + await ctx.store.save(collection) return getEvent(item) } diff --git a/src/mappings/multiTokens/events/frozen.ts b/src/mappings/multiTokens/events/frozen.ts index 846f3cf34..b09bd4966 100644 --- a/src/mappings/multiTokens/events/frozen.ts +++ b/src/mappings/multiTokens/events/frozen.ts @@ -102,7 +102,7 @@ export async function frozen( tokenAccount.isFrozen = true tokenAccount.updatedAt = new Date(block.timestamp) - ctx.store.save(tokenAccount) + await ctx.store.save(tokenAccount) } else if (data.collectionAccount) { const address = u8aToHex(data.collectionAccount) const collectionAccount = await ctx.store.findOneOrFail(CollectionAccount, { @@ -111,7 +111,7 @@ export async function frozen( collectionAccount.isFrozen = true collectionAccount.updatedAt = new Date(block.timestamp) - ctx.store.save(collectionAccount) + await ctx.store.save(collectionAccount) } else if (data.tokenId !== undefined) { const token = await ctx.store.findOneOrFail(Token, { where: { id: `${data.collectionId}-${data.tokenId}` }, @@ -134,14 +134,14 @@ export async function frozen( token.isFrozen = isTokenFrozen(token.freezeState) - ctx.store.save(token) + await ctx.store.save(token) } else { const collection = await ctx.store.findOneOrFail(Collection, { where: { id: data.collectionId.toString() }, }) collection.transferPolicy = new TransferPolicy({ isFrozen: true }) - ctx.store.save(collection) + await ctx.store.save(collection) } return getEvent(item, data) diff --git a/src/mappings/multiTokens/events/minted.ts b/src/mappings/multiTokens/events/minted.ts index 33795f05c..34bf700f1 100644 --- a/src/mappings/multiTokens/events/minted.ts +++ b/src/mappings/multiTokens/events/minted.ts @@ -105,6 +105,7 @@ export async function minted( token.nonFungible = isNonFungible(token) tokenAccount.balance += data.amount + tokenAccount.totalBalance += data.amount tokenAccount.updatedAt = new Date(block.timestamp) await Promise.all([ctx.store.save(tokenAccount), ctx.store.save(token)]) diff --git a/src/mappings/multiTokens/events/reserved.ts b/src/mappings/multiTokens/events/reserved.ts index a3971ac98..a1771cb84 100644 --- a/src/mappings/multiTokens/events/reserved.ts +++ b/src/mappings/multiTokens/events/reserved.ts @@ -6,6 +6,7 @@ import { MultiTokensReservedEvent } from '../../../types/generated/events' import { Event } from '../../../types/generated/support' import { CommonContext } from '../../types/contexts' import { syncCollectionStats } from '../../../jobs/collection-stats' +import { UnknownVersionError } from '../../../common/errors' function getEventData(ctx: CommonContext, eventItem: Event) { const event = new MultiTokensReservedEvent(ctx, eventItem) @@ -14,7 +15,7 @@ function getEventData(ctx: CommonContext, eventItem: Event) { return event.asMatrixEnjinV603 } - return null + throw new UnknownVersionError(event.constructor.name) } export async function reserved( @@ -47,7 +48,7 @@ export async function reserved( tokenAccount.updatedAt = new Date(block.timestamp) - ctx.store.save(tokenAccount) + await ctx.store.save(tokenAccount) } syncCollectionStats(data.collectionId.toString()) diff --git a/src/mappings/multiTokens/events/thawed.ts b/src/mappings/multiTokens/events/thawed.ts index 3c4df8fb8..b1cab4acb 100644 --- a/src/mappings/multiTokens/events/thawed.ts +++ b/src/mappings/multiTokens/events/thawed.ts @@ -96,7 +96,7 @@ export async function thawed( tokenAccount.isFrozen = false tokenAccount.updatedAt = new Date(block.timestamp) - ctx.store.save(tokenAccount) + await ctx.store.save(tokenAccount) } else if (data.collectionAccount) { const address = u8aToHex(data.collectionAccount) const collectionAccount = await ctx.store.findOneOrFail(CollectionAccount, { @@ -105,21 +105,21 @@ export async function thawed( collectionAccount.isFrozen = false collectionAccount.updatedAt = new Date(block.timestamp) - ctx.store.save(collectionAccount) + await ctx.store.save(collectionAccount) } else if (data.tokenId !== undefined) { const token = await ctx.store.findOneOrFail(Token, { where: { id: `${data.collectionId}-${data.tokenId}` }, }) token.isFrozen = false - ctx.store.save(token) + await ctx.store.save(token) } else { const collection = await ctx.store.findOneOrFail(Collection, { where: { id: data.collectionId.toString() }, }) collection.transferPolicy = new TransferPolicy({ isFrozen: false }) - ctx.store.save(collection) + await ctx.store.save(collection) } return getEvent(item, data) diff --git a/src/mappings/multiTokens/events/token_account_created.ts b/src/mappings/multiTokens/events/token_account_created.ts index 588b1b330..42c389e62 100644 --- a/src/mappings/multiTokens/events/token_account_created.ts +++ b/src/mappings/multiTokens/events/token_account_created.ts @@ -74,16 +74,17 @@ export async function tokenAccountCreated( createdAt: new Date(block.timestamp), updatedAt: new Date(block.timestamp), }) - ctx.store.insert(CollectionAccount, newCA as any) + await ctx.store.insert(CollectionAccount, newCA as any) } else { collectionAccount.accountCount += 1 - ctx.store.save(collectionAccount) + await ctx.store.save(collectionAccount) } const tokenAccount = new TokenAccount({ id: `${u8aToHex(data.accountId)}-${data.collectionId}-${data.tokenId}`, balance: 0n, // The balance is updated on Mint event reservedBalance: 0n, + totalBalance: 0n, lockedBalance: 0n, namedReserves: null, locks: null, diff --git a/src/mappings/multiTokens/events/token_account_destroyed.ts b/src/mappings/multiTokens/events/token_account_destroyed.ts index faf5839db..c34c4f28d 100644 --- a/src/mappings/multiTokens/events/token_account_destroyed.ts +++ b/src/mappings/multiTokens/events/token_account_destroyed.ts @@ -55,7 +55,7 @@ export async function tokenAccountDestroyed( }) if (collectionAccount) { collectionAccount.accountCount -= 1 - ctx.store.save(collectionAccount) + await ctx.store.save(collectionAccount) } const tokenAccount = await ctx.store.findOne(TokenAccount, { diff --git a/src/mappings/multiTokens/events/token_mutated.ts b/src/mappings/multiTokens/events/token_mutated.ts index bce7a170d..70d38f2ba 100644 --- a/src/mappings/multiTokens/events/token_mutated.ts +++ b/src/mappings/multiTokens/events/token_mutated.ts @@ -98,7 +98,7 @@ export async function tokenMutated( } token.nonFungible = isNonFungible(token) - ctx.store.save(token) + await ctx.store.save(token) return getEvent(item, data) } diff --git a/src/mappings/multiTokens/events/transferred.ts b/src/mappings/multiTokens/events/transferred.ts index becdd7633..99ad8f283 100644 --- a/src/mappings/multiTokens/events/transferred.ts +++ b/src/mappings/multiTokens/events/transferred.ts @@ -87,12 +87,14 @@ export async function transferred( if (fromTokenAccount) { fromTokenAccount.balance -= data.amount + fromTokenAccount.totalBalance -= data.amount fromTokenAccount.updatedAt = new Date(block.timestamp) await ctx.store.save(fromTokenAccount) } if (toTokenAccount) { toTokenAccount.balance += data.amount + toTokenAccount.totalBalance += data.amount toTokenAccount.updatedAt = new Date(block.timestamp) await ctx.store.save(toTokenAccount) } diff --git a/src/mappings/multiTokens/events/unapproved.ts b/src/mappings/multiTokens/events/unapproved.ts index 26933961b..ad4900e7e 100644 --- a/src/mappings/multiTokens/events/unapproved.ts +++ b/src/mappings/multiTokens/events/unapproved.ts @@ -63,7 +63,7 @@ export async function unapproved( tokenAccount.approvals = tokenAccount.approvals?.filter((approval) => approval.account !== encodeId(data.operator)) tokenAccount.updatedAt = new Date(block.timestamp) - ctx.store.save(tokenAccount) + await ctx.store.save(tokenAccount) } else { const collectionAccount = await ctx.store.findOneOrFail(CollectionAccount, { where: { id: `${data.collectionId}-${address}` }, @@ -74,7 +74,7 @@ export async function unapproved( ) collectionAccount.updatedAt = new Date(block.timestamp) - ctx.store.save(collectionAccount) + await ctx.store.save(collectionAccount) } return getEvent(item, data) diff --git a/src/mappings/multiTokens/events/unreserved.ts b/src/mappings/multiTokens/events/unreserved.ts index 574c7158b..9c3a9fd81 100644 --- a/src/mappings/multiTokens/events/unreserved.ts +++ b/src/mappings/multiTokens/events/unreserved.ts @@ -5,15 +5,16 @@ import { TokenAccount } from '../../../model' import { MultiTokensUnreservedEvent } from '../../../types/generated/events' import { Event } from '../../../types/generated/support' import { CommonContext } from '../../types/contexts' +import { UnknownVersionError } from '../../../common/errors' function getEventData(ctx: CommonContext, eventItem: Event) { - const event = new MultiTokensUnreservedEvent(ctx, eventItem) + const data = new MultiTokensUnreservedEvent(ctx, eventItem) - if (event.isMatrixEnjinV603) { - return event.asMatrixEnjinV603 + if (data.isMatrixEnjinV603) { + return data.asMatrixEnjinV603 } - return null + throw new UnknownVersionError(data.constructor.name) } export async function unreserved( @@ -47,7 +48,7 @@ export async function unreserved( tokenAccount.updatedAt = new Date(block.timestamp) - ctx.store.save(tokenAccount) + await ctx.store.save(tokenAccount) } return undefined diff --git a/src/mappings/util/entities.ts b/src/mappings/util/entities.ts index 3f49e163e..f89c14e93 100644 --- a/src/mappings/util/entities.ts +++ b/src/mappings/util/entities.ts @@ -24,7 +24,6 @@ export async function getOrCreateAccount( feeFrozen: 0n, }), nonce: 0, - tokenValues: 0n, }) await ctx.store.insert(Account, account as any) } diff --git a/src/model/generated/account.model.ts b/src/model/generated/account.model.ts index 06dd7fd09..f4d7d00e3 100644 --- a/src/model/generated/account.model.ts +++ b/src/model/generated/account.model.ts @@ -41,9 +41,6 @@ export class Account { @OneToMany_(() => AccountTokenEvent, e => e.from) tokenEvents!: AccountTokenEvent[] - @Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false}) - tokenValues!: bigint - @Column_("int4", {nullable: true}) lastUpdateBlock!: number | undefined | null diff --git a/src/model/generated/tokenAccount.model.ts b/src/model/generated/tokenAccount.model.ts index 7c17701e5..d8c91c89a 100644 --- a/src/model/generated/tokenAccount.model.ts +++ b/src/model/generated/tokenAccount.model.ts @@ -16,6 +16,9 @@ export class TokenAccount { @PrimaryColumn_() id!: string + @Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false}) + totalBalance!: bigint + @Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false}) balance!: bigint diff --git a/src/populateBlock.ts b/src/populateBlock.ts index 3576fe55d..d3b3d67c5 100644 --- a/src/populateBlock.ts +++ b/src/populateBlock.ts @@ -87,7 +87,6 @@ async function getAccountsMap( feeFrozen: 0n, }), nonce: 0, - tokenValues: 0n, }) await ctx.store.insert(Account, account as any) @@ -459,6 +458,7 @@ async function syncTokenAccounts(ctx: CommonContext, block: SubstrateBlock) { id: `${accountId}-${collectionId}-${tokenId}`, balance: data.balance, reservedBalance: data.reservedBalance, + totalBalance: data.balance + data.reservedBalance, lockedBalance: data.lockedBalance, namedReserves, locks,