Skip to content

Commit 2d997eb

Browse files
authored
Informatively throw when value is missing (#703)
### Description otherwise very crypto errror is thrown (missing length on undefined) #### Other changes ### Tested new test ### How to QA create a proposal where the jsonTransactions are missing value ### Related issues inspired by #702 <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on enhancing error handling in the `proposalBuilder` by ensuring that transactions without a `value` property throw a clear error message. Additionally, it updates the changeset mode and adds tests to verify the new error handling. ### Detailed summary - Changed `mode` from `"pre"` to `"exit"` in `.changeset/pre.json`. - Added a new entry for `@celo/governance` in `.changeset/old-badgers-invite.md`. - Implemented error throwing for missing `tx.value` in `packages/sdk/governance/src/proposal-builder.ts`. - Added unit tests for missing and zero `value` cases in `packages/sdk/governance/src/proposal-builder.test.ts`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent e4517d6 commit 2d997eb

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

.changeset/old-badgers-invite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@celo/governance': patch
3+
---
4+
5+
Proposals missing value property now throw with understandable error message

packages/sdk/governance/src/proposal-builder.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,41 @@ testWithAnvilL2('ProposalBuilder', (web3) => {
160160
}
161161
`)
162162
})
163+
it('throws error for missing value', async () => {
164+
const tx = {
165+
contract: 'FeeCurrencyDirectory',
166+
function: 'setCurrencyConfig',
167+
args: [
168+
'0x765DE816845861e75A25fCA122bb6898B8B1282a',
169+
'0xefB84935239dAcdecF7c5bA76d8dE40b077B7b33',
170+
'50000',
171+
],
172+
}
173+
// @ts-expect-error (value is missing)
174+
await expect(proposalBuilder.fromJsonTx(tx)).rejects.toThrowErrorMatchingInlineSnapshot(
175+
`"Missing tx.value"`
176+
)
177+
})
178+
it('successed for 0 value', async () => {
179+
const tx = {
180+
contract: 'FeeCurrencyDirectory',
181+
function: 'setCurrencyConfig',
182+
args: [
183+
'0x765DE816845861e75A25fCA122bb6898B8B1282a',
184+
'0xefB84935239dAcdecF7c5bA76d8dE40b077B7b33',
185+
'50000',
186+
],
187+
value: 0,
188+
}
189+
// @ts-expect-error (value is missing)
190+
await expect(proposalBuilder.fromJsonTx(tx)).resolves.toMatchInlineSnapshot(`
191+
{
192+
"input": "0x216ab7df000000000000000000000000765de816845861e75a25fca122bb6898b8b1282a000000000000000000000000efb84935239dacdecf7c5ba76d8de40b077b7b33000000000000000000000000000000000000000000000000000000000000c350",
193+
"to": "0x5a7D21C9255DAA32109c8136661D7e853Fc5BF63",
194+
"value": 0,
195+
}
196+
`)
197+
})
163198
it('gives info when it fails to build transaction', async () => {
164199
const tx = {
165200
contract: 'SuperContract',

packages/sdk/governance/src/proposal-builder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ export class ProposalBuilder {
233233
fromJsonTx = async (
234234
tx: ProposalTransactionJSON | ExternalProposalTransactionJSON
235235
): Promise<ProposalTransaction> => {
236+
if (tx.value === undefined) {
237+
throw new Error('Missing tx.value')
238+
}
239+
236240
if (isRegistryRepoint(tx)) {
237241
// Update canonical registry addresses
238242
const args = registryRepointArgs(tx)

0 commit comments

Comments
 (0)