Skip to content

Commit

Permalink
Changed bank state event balance type from bigint to string since som…
Browse files Browse the repository at this point in the history
…e balances are larger than postgres's bigint type can hold.
  • Loading branch information
NoahSaso committed Sep 30, 2023
1 parent aaf343b commit bf3ba26
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/core/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ export const getEnv = ({
// Call hook.
await onFetch?.([event])

return BigInt(event.balance)
return event.balance
}

const getBalances: FormulaBalancesGetter = async (address) => {
Expand Down Expand Up @@ -1084,9 +1084,9 @@ export const getEnv = ({
return events.reduce(
(acc, { denom, balance }) => ({
...acc,
[denom]: BigInt(balance),
[denom]: balance,
}),
{} as Record<string, bigint>
{} as Record<string, string>
)
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ export type FormulaTxEventsGetter = (
export type FormulaBalanceGetter = (
address: string,
denom: string
) => Promise<bigint | undefined>
) => Promise<string | undefined>

export type FormulaBalancesGetter = (
address: string
) => Promise<Record<string, bigint> | undefined>
) => Promise<Record<string, string> | undefined>

export type Env<Args extends Record<string, string> = {}> = {
chainId: string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { QueryInterface } from 'sequelize'

module.exports = {
async up(queryInterface: QueryInterface) {
await queryInterface.changeColumn('BankStateEvents', 'balance', {
type: 'text',
allowNull: false,
})
},

async down(queryInterface: QueryInterface) {
await queryInterface.changeColumn('BankStateEvents', 'balance', {
type: 'bigint',
allowNull: false,
})
},
}
2 changes: 1 addition & 1 deletion src/db/models/BankStateEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class BankStateEvent extends DependendableEventModel {
denom!: string

@AllowNull(false)
@Column(DataType.BIGINT)
@Column(DataType.TEXT)
balance!: string

get block(): Block {
Expand Down
20 changes: 16 additions & 4 deletions src/scripts/export/handlers/bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,30 @@ export const bank: HandlerMaker = async ({

// Try to decode as JSON-encoded number.
try {
balance = BigInt(JSON.parse(fromUtf8(valueData))).toString()
const decodedValue = JSON.parse(fromUtf8(valueData))
if (
(typeof decodedValue === 'string' && /^[0-9]+$/.test(decodedValue)) ||
typeof decodedValue === 'number'
) {
balance =
typeof decodedValue === 'number'
? BigInt(decodedValue).toString()
: decodedValue
}
} catch {
// Ignore decoding errors.
}

// Try to decode as legacy Coin protobuf, and ensure amount can be parsed
// as a bigint. Otherwise, ignore. The protobuf will decode (and not
// Try to decode as legacy Coin protobuf, and ensure amount consists of
// only numbers. Otherwise, ignore. The protobuf will decode (and not
// error) if the value is another protobuf, but amount will likely contain
// other data instead of a number. There's no way to ensure it's actually
// a coin protobuf, so this is the best we can do.
try {
balance = BigInt(Coin.decode(valueData).amount).toString()
const { amount } = Coin.decode(valueData)
if (/^[0-9]+$/.test(amount)) {
balance = amount
}
} catch {
// Ignore decoding errors.
}
Expand Down

0 comments on commit bf3ba26

Please sign in to comment.