Skip to content

Commit

Permalink
fix wasm support for terra classic
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed May 9, 2024
1 parent 05cfea9 commit e04a571
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/scripts/export/handlers/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export const wasm: HandlerMaker<WasmExportData> = async ({
}) => {
const chainId = await cosmWasmClient.getChainId()

// Terra Classic uses different prefixes:
// https://github.com/classic-terra/wasmd/blob/v0.30.0-terra.3/x/wasm/types/keys.go#L31-L32
const CONTRACT_KEY_PREFIX = chainId === 'columbus-5' ? 0x04 : 0x02
const CONTRACT_STORE_PREFIX = chainId === 'columbus-5' ? 0x05 : 0x03

// Get code ID for contract, cached in memory.
const codeIdCache = new LRUCache<string, number>({
max: 1000,
Expand Down Expand Up @@ -101,16 +106,17 @@ export const wasm: HandlerMaker<WasmExportData> = async ({
}

const match: Handler<WasmExportData>['match'] = (trace) => {
// ContractStorePrefix = 0x03
// wasm keys are formatted as:
// ContractStorePrefix || contractAddressBytes || keyBytes

// ContractKeyPrefix = 0x02
// contract info keys are formatted as:
// ContractKeyPrefix || contractAddressBytes

const keyData = fromBase64(trace.key)
if (keyData[0] !== 0x02 && keyData[0] !== 0x03) {
if (
keyData[0] !== CONTRACT_STORE_PREFIX &&
keyData[0] !== CONTRACT_KEY_PREFIX
) {
return
}

Expand All @@ -133,13 +139,13 @@ export const wasm: HandlerMaker<WasmExportData> = async ({
const blockTimeUnixMs = BigInt(trace.blockTimeUnixMs).toString()

// If contract key, save contract info.
if (trace.operation === 'write' && keyData[0] === 0x02) {
if (trace.operation === 'write' && keyData[0] === CONTRACT_KEY_PREFIX) {
// Parse as protobuf to get code ID.
const protobufContractInfo = fromBase64(trace.value)
let contractInfo
try {
contractInfo = ContractInfo.decode(protobufContractInfo)
} catch {
} catch (err) {
// If failed to decode, not contract info.
return
}
Expand Down

0 comments on commit e04a571

Please sign in to comment.