Skip to content

Commit

Permalink
refactor(namada): separate fetchProposalWasm
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Aug 23, 2024
1 parent e61b7c8 commit 2474a14
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
3 changes: 3 additions & 0 deletions packages/namada/NamadaChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export default class NamadaChain extends CW.Chain {
fetchProposalInfo (id: number|bigint) {
return this.getConnection().fetchProposalInfoImpl(id)
}
fetchProposalWasm (id: number|bigint) {
return this.getConnection().fetchProposalWasmImpl(id)
}
fetchEpoch (...args: Parameters<NamadaConnection["fetchEpochImpl"]>) {
return this.getConnection().fetchEpochImpl(...args)
}
Expand Down
3 changes: 3 additions & 0 deletions packages/namada/NamadaConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export default class NamadaConnection extends CW.Connection {
fetchProposalInfoImpl (id: number|bigint) {
return Gov.fetchProposalInfo(this, id)
}
fetchProposalWasmImpl (id: number|bigint) {
return Gov.fetchProposalWasm(this, id)
}

fetchPGFParametersImpl () {
return PGF.fetchPGFParameters(this)
Expand Down
29 changes: 17 additions & 12 deletions packages/namada/NamadaGov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,23 @@ export async function fetchProposalInfo (
: decodeResultResponse(connection.decode.gov_result(resultResponse.slice(1)) as
Required<ReturnType<NamadaDecoder["gov_result"]>>)

const bigId = BigInt(id)
return { id: BigInt(id), proposal, votes, result }
}

export async function fetchProposalWasm (
connection: Pick<Connection, 'abciQuery'|'decode'>, id: number|bigint
) {
id = BigInt(id)
const codeKey = connection.decode.gov_proposal_code_key(BigInt(id))
let wasm
if (proposal.type?.type === 'DefaultWithWasm') {
const hasKey = await connection.abciQuery(`/shell/has_key/${codeKey}`)
if (hasKey[0] === 1) {
wasm = await connection.abciQuery(`/shell/value/${codeKey}`)
wasm = wasm.slice(4) // trim length prefix
//const { writeFile } = await import('node:fs/promises')
//writeFile(`${bigId}.wasm`, wasm)
} else {
console.warn(`WASM for proposal ${id} was not found at key ${codeKey}`)
}
const hasKey = await connection.abciQuery(`/shell/has_key/${codeKey}`)
if (hasKey[0] === 1) {
wasm = await connection.abciQuery(`/shell/value/${codeKey}`)
wasm = wasm.slice(4) // trim length prefix
return { id, codeKey, wasm }
} else {
throw new Error(`WASM for proposal ${id} was not found at key ${codeKey}`)
}
return { id: bigId, proposal, votes, result, codeKey, wasm }
}

const decodeResultResponse = (
Expand Down Expand Up @@ -77,6 +79,9 @@ interface NamadaGovernanceProposal {
readonly proposal: ReturnType<NamadaDecoder["gov_proposal"]>
readonly votes: ReturnType<NamadaDecoder["gov_votes"]>
readonly result: NamadaGovernanceProposalResult|null
}

interface NamadaGovernanceProposalWasm {
readonly codeKey: string
readonly wasm?: Uint8Array
}
Expand Down

0 comments on commit 2474a14

Please sign in to comment.