Skip to content

Commit

Permalink
fix: fixes bug with token amount, Add totalBalance and Drop `tokenV…
Browse files Browse the repository at this point in the history
…alues` from tokenAccount (#719)
  • Loading branch information
justraman committed Nov 28, 2023
1 parent 3e92288 commit f796087
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 38 deletions.
13 changes: 13 additions & 0 deletions db/migrations/1700476842756-Data.js
Original file line number Diff line number Diff line change
@@ -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"`)
}
}
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ type Account @entity {
tokenEvents: [AccountTokenEvent] @derivedFrom(field: "from")

# Extras
tokenValues: BigInt!
lastUpdateBlock: Int

# profile
Expand Down Expand Up @@ -524,6 +523,7 @@ type TokenAccount @entity {
id: ID!

# Storage defaults
totalBalance: BigInt!
balance: BigInt!
reservedBalance: BigInt!
lockedBalance: BigInt!
Expand Down
2 changes: 0 additions & 2 deletions src/mappings/balances/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -244,7 +243,6 @@ export async function saveAccounts(ctx: CommonContext, block: SubstrateBlock) {
feeFrozen: accountData.feeFrozen,
miscFrozen: accountData.miscFrozen,
}),
tokenValues: 0n,
})
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/mappings/multiTokens/events/burned.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/multiTokens/events/collection_mutated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export async function collectionMutated(
}
}

ctx.store.save(collection)
await ctx.store.save(collection)

return getEvent(item)
}
8 changes: 4 additions & 4 deletions src/mappings/multiTokens/events/frozen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>(CollectionAccount, {
Expand All @@ -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>(Token, {
where: { id: `${data.collectionId}-${data.tokenId}` },
Expand All @@ -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>(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)
Expand Down
1 change: 1 addition & 0 deletions src/mappings/multiTokens/events/minted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)])

Expand Down
5 changes: 3 additions & 2 deletions src/mappings/multiTokens/events/reserved.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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())
Expand Down
8 changes: 4 additions & 4 deletions src/mappings/multiTokens/events/thawed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>(CollectionAccount, {
Expand All @@ -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>(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>(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)
Expand Down
5 changes: 3 additions & 2 deletions src/mappings/multiTokens/events/token_account_created.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/multiTokens/events/token_account_destroyed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>(TokenAccount, {
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/multiTokens/events/token_mutated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions src/mappings/multiTokens/events/transferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/multiTokens/events/unapproved.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>(CollectionAccount, {
where: { id: `${data.collectionId}-${address}` },
Expand All @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions src/mappings/multiTokens/events/unreserved.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/mappings/util/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export async function getOrCreateAccount(
feeFrozen: 0n,
}),
nonce: 0,
tokenValues: 0n,
})
await ctx.store.insert(Account, account as any)
}
Expand Down
3 changes: 0 additions & 3 deletions src/model/generated/account.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/model/generated/tokenAccount.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/populateBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ async function getAccountsMap(
feeFrozen: 0n,
}),
nonce: 0,
tokenValues: 0n,
})

await ctx.store.insert(Account, account as any)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f796087

Please sign in to comment.