diff --git a/src/core/env.ts b/src/core/env.ts index d6953ced..e1056249 100644 --- a/src/core/env.ts +++ b/src/core/env.ts @@ -1016,7 +1016,7 @@ export const getEnv = ({ // Call hook. await onFetch?.([event]) - return BigInt(event.balance) + return event.balance } const getBalances: FormulaBalancesGetter = async (address) => { @@ -1084,9 +1084,9 @@ export const getEnv = ({ return events.reduce( (acc, { denom, balance }) => ({ ...acc, - [denom]: BigInt(balance), + [denom]: balance, }), - {} as Record + {} as Record ) } diff --git a/src/core/types.ts b/src/core/types.ts index 5ff00ce6..221dfa2a 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -230,11 +230,11 @@ export type FormulaTxEventsGetter = ( export type FormulaBalanceGetter = ( address: string, denom: string -) => Promise +) => Promise export type FormulaBalancesGetter = ( address: string -) => Promise | undefined> +) => Promise | undefined> export type Env = {}> = { chainId: string diff --git a/src/db/migrations/20230930091910-alter-bank-state-event-balance-type-bigint-to-string.ts b/src/db/migrations/20230930091910-alter-bank-state-event-balance-type-bigint-to-string.ts new file mode 100644 index 00000000..e8e41113 --- /dev/null +++ b/src/db/migrations/20230930091910-alter-bank-state-event-balance-type-bigint-to-string.ts @@ -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, + }) + }, +} diff --git a/src/db/models/BankStateEvent.ts b/src/db/models/BankStateEvent.ts index db212f5b..678f3d2d 100644 --- a/src/db/models/BankStateEvent.ts +++ b/src/db/models/BankStateEvent.ts @@ -52,7 +52,7 @@ export class BankStateEvent extends DependendableEventModel { denom!: string @AllowNull(false) - @Column(DataType.BIGINT) + @Column(DataType.TEXT) balance!: string get block(): Block { diff --git a/src/scripts/export/handlers/bank.ts b/src/scripts/export/handlers/bank.ts index a102b8a9..f67df315 100644 --- a/src/scripts/export/handlers/bank.ts +++ b/src/scripts/export/handlers/bank.ts @@ -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. }