Skip to content

Commit

Permalink
Fixed legacy coin protobuf decoding for bank balances.
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Sep 25, 2023
1 parent da00d45 commit 294399e
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions src/scripts/export/handlers/bank.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { fromBase64, fromUtf8, toBech32 } from '@cosmjs/encoding'
import retry from 'async-await-retry'
import { Coin } from 'cosmjs-types/cosmos/base/v1beta1/coin'
import { Sequelize } from 'sequelize'

import { ParsedBankStateEvent, objectMatchesStructure } from '@/core'
import { ParsedBankStateEvent } from '@/core'
import {
BankStateEvent,
State,
Expand Down Expand Up @@ -91,33 +92,41 @@ export const bank: HandlerMaker = async ({
// Mimics behavior of `UnmarshalBalanceCompat` in `x/bank/keeper/view.go` to
// decode balance.

let balance = '0'
let balance: string | undefined
// If write operation, balance is updated. Otherwise (delete), balance is 0.
if (trace.operation === 'write') {
let value
// Decode base64-encoded JSON.
let valueData
try {
value = trace.value && JSON.parse(fromUtf8(fromBase64(trace.value)))
valueData = trace.value && fromBase64(trace.value)
} catch {
// Ignore decoding errors.
return
}

// If legacy format, extract balance.
if (
objectMatchesStructure(value, {
denom: {},
amount: {},
})
) {
balance = BigInt(value.amount).toString()
// Otherwise it should be a number.
} else if (typeof value === 'number') {
balance = BigInt(value).toString()
} else {
// This should never happen.
// If no data, ignore.
if (!valueData) {
return
}

// Try to decode as JSON-encoded number.
try {
balance = BigInt(JSON.parse(fromUtf8(valueData))).toString()
} catch {
// Ignore decoding errors.
}

// Try to decode as legacy Coin protobuf.
try {
balance = Coin.decode(valueData).amount
} catch {
// Ignore decoding errors.
}
} else if (trace.operation === 'delete') {
balance = '0'
}

// If could not find balance, ignore.
if (!balance) {
return
}

pending.push({
Expand Down

0 comments on commit 294399e

Please sign in to comment.