From e3c5bde96599404efba676b1e8a81f681ba808fe Mon Sep 17 00:00:00 2001 From: fadesany Date: Tue, 15 Sep 2026 12:44:49 +0100 Subject: [PATCH 1/2] fix(sdk): align contract error enums with on-chain errors.rs TreasuryError and FactoryError had drifted from the contracts, so most contract failures showed the wrong message (e.g. treasury #7 RequestNotPending rendered the budget-cap text). Mirror charter-contract@be72224 exactly: - contracts/treasury/src/errors.rs L10-20 (11 variants) - contracts/factory/src/errors.rs L7-13 (4 variants) Rewrite both message tables, covering the overloaded raise sites (RequestNotPending for missing requests, InvalidAmount for missing categories / cap below spent). Drop the known-drift doc notes; the contract docs tables already matched the source. --- docs/developer-guide/sdk-reference.md | 8 --- docs/smart-contracts/treasury.md | 4 -- packages/sdk/src/types.ts | 79 +++++++++++++-------------- 3 files changed, 37 insertions(+), 54 deletions(-) diff --git a/docs/developer-guide/sdk-reference.md b/docs/developer-guide/sdk-reference.md index fc0542b..8f4e82f 100644 --- a/docs/developer-guide/sdk-reference.md +++ b/docs/developer-guide/sdk-reference.md @@ -162,14 +162,6 @@ Both wrap the lower-level `ContractError` thrown by the RPC layer. The numeric `code` is the contract's `#[contracterror]` discriminant, parsed from Soroban's `Error(Contract, #N)` message. -> **Known drift.** The SDK's own `TreasuryError` / `FactoryError` enums in -> `packages/sdk/src/types.ts` do not currently line up one-for-one with the -> contract's `errors.rs`. When you need the authoritative code-to-meaning -> mapping, use the contract source and the tables on the -> [treasury](../smart-contracts/treasury.md) and -> [factory](../smart-contracts/factory.md) pages, not the SDK enum. This is a -> tracked bug, not a documentation gap. - ## Reads for display come from the indexer, not the SDK The SDK's view functions read live contract state by simulation. The web app diff --git a/docs/smart-contracts/treasury.md b/docs/smart-contracts/treasury.md index 163d031..c2bf708 100644 --- a/docs/smart-contracts/treasury.md +++ b/docs/smart-contracts/treasury.md @@ -183,10 +183,6 @@ The treasury error enum, with its on-chain discriminants: | `NotRequester` | 10 | Cancel attempted by someone other than the requester. | | `InvalidAmount` | 11 | Non-positive amount/cap, missing category, or cap below spent. | -Note that the SDK's `TreasuryError` enum in `packages/sdk/src/types.ts` does not -currently match this list one-for-one. When you need the authoritative mapping, -use `errors.rs` and this table, not the SDK enum. - ## Events The treasury emits an event on every state change. The indexer reads these: diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 765bbf6..2a4f06c 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -66,65 +66,60 @@ export interface OrgRecord { /** * Numeric `#[contracterror]` discriminants for the `treasury` contract. * - * NOTE: the app build spec restates the contract's method/type interface but - * not its error enum, and forbids reading the contract repo. These variants and - * their ordering mirror `charter-contract`'s declared `#[contracterror]` for - * this domain; any discriminant that ever drifts is caught by the graceful - * fallback in {@link treasuryErrorMessage}, so a raw error string is never - * shown to a user regardless. + * Mirrors `charter-contract/contracts/treasury/src/errors.rs` exactly — names, + * discriminants, and order. Update both together. Any unknown code still falls + * back gracefully in {@link treasuryErrorMessage}. */ export enum TreasuryError { AlreadyInitialized = 1, - NotAuthorized = 2, - NotAnApprover = 3, - InvalidThreshold = 4, - CategoryNotFound = 5, - CategoryInactive = 6, - CapExceeded = 7, - InsufficientBalance = 8, - RequestNotFound = 9, - RequestNotPending = 10, - AlreadyApproved = 11, - InvalidAmount = 12, - DuplicateApprover = 13, - ApproverNotFound = 14, + NotInitialized = 2, + NotAdmin = 3, + NotApprover = 4, + CategoryInactive = 5, + CapExceeded = 6, + RequestNotPending = 7, + InvalidThreshold = 8, + AlreadyApproved = 9, + NotRequester = 10, + InvalidAmount = 11, } -/** Numeric `#[contracterror]` discriminants for the `factory` contract. */ +/** + * Numeric `#[contracterror]` discriminants for the `factory` contract. + * + * Mirrors `charter-contract/contracts/factory/src/errors.rs` exactly. Note the + * first two discriminants are the reverse of the treasury enum. + */ export enum FactoryError { - AlreadyInitialized = 1, - NotAuthorized = 2, - InvalidThreshold = 3, - InvalidWasmHash = 4, - OrgNotFound = 5, + NotInitialized = 1, + AlreadyInitialized = 2, + NotDeployer = 3, + OrgNotFound = 4, } const TREASURY_ERROR_MESSAGES: Readonly> = { [TreasuryError.AlreadyInitialized]: 'This treasury has already been set up.', - [TreasuryError.NotAuthorized]: 'You do not have permission to do this.', - [TreasuryError.NotAnApprover]: 'Only a designated approver can act on this request.', - [TreasuryError.InvalidThreshold]: - 'The approval threshold must be at least 1 and no greater than the number of approvers.', - [TreasuryError.CategoryNotFound]: 'That budget category no longer exists.', + [TreasuryError.NotInitialized]: 'This treasury has not been set up yet.', + [TreasuryError.NotAdmin]: 'Only the treasury admin can do this.', + [TreasuryError.NotApprover]: 'Only a designated approver can act on this request.', [TreasuryError.CategoryInactive]: 'This budget category is inactive and cannot be spent from.', - [TreasuryError.CapExceeded]: 'This request would exceed the category’s remaining budget.', - [TreasuryError.InsufficientBalance]: - 'The treasury does not hold enough funds to cover this request.', - [TreasuryError.RequestNotFound]: 'That request no longer exists.', + [TreasuryError.CapExceeded]: 'This would exceed the category’s budget cap.', + // The contract also raises this for a request id that does not exist. [TreasuryError.RequestNotPending]: - 'This request has already been executed, rejected, or cancelled.', + 'This request no longer exists or has already been executed, rejected, or cancelled.', + [TreasuryError.InvalidThreshold]: + 'The approval threshold must be at least 1 and no greater than the number of approvers.', [TreasuryError.AlreadyApproved]: 'You have already approved this request.', - [TreasuryError.InvalidAmount]: 'The amount must be greater than zero.', - [TreasuryError.DuplicateApprover]: 'That address is already an approver.', - [TreasuryError.ApproverNotFound]: 'That address is not currently an approver.', + [TreasuryError.NotRequester]: 'Only the person who submitted this request can cancel it.', + // The contract also raises this for a missing category or a cap below spent. + [TreasuryError.InvalidAmount]: + 'The amount is invalid, or the budget category does not exist. Amounts must be greater than zero, and a cap cannot be set below what has already been spent.', }; const FACTORY_ERROR_MESSAGES: Readonly> = { + [FactoryError.NotInitialized]: 'The factory has not been initialized yet.', [FactoryError.AlreadyInitialized]: 'The factory has already been initialized.', - [FactoryError.NotAuthorized]: 'You do not have permission to deploy a treasury.', - [FactoryError.InvalidThreshold]: - 'The approval threshold must be at least 1 and no greater than the number of approvers.', - [FactoryError.InvalidWasmHash]: 'The treasury program reference is invalid.', + [FactoryError.NotDeployer]: 'You do not have permission to deploy a treasury.', [FactoryError.OrgNotFound]: 'That organization could not be found.', }; From f87f96012aa0b88f5a2490a243901f49dac403a3 Mon Sep 17 00:00:00 2001 From: fadesany Date: Tue, 15 Sep 2026 20:30:15 +0100 Subject: [PATCH 2/2] feat(sdk): add FactoryError.TreasuryInitFailed (#5) The factory contract now wraps any failure of the new treasury's initialize in its own TreasuryInitFailed = 5 instead of letting treasury codes (which collide with factory codes 1-4) leak through. Mirror charter-contract contracts/factory/src/errors.rs L16 and add a message. The contract reverts the whole deploy (org count stays 0), so the message says no organization was created. --- packages/sdk/src/types.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 2a4f06c..12d7e58 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -95,6 +95,8 @@ export enum FactoryError { AlreadyInitialized = 2, NotDeployer = 3, OrgNotFound = 4, + /** Wraps any failure of the new treasury's `initialize`; treasury codes never surface as factory codes. */ + TreasuryInitFailed = 5, } const TREASURY_ERROR_MESSAGES: Readonly> = { @@ -121,6 +123,9 @@ const FACTORY_ERROR_MESSAGES: Readonly> = { [FactoryError.AlreadyInitialized]: 'The factory has already been initialized.', [FactoryError.NotDeployer]: 'You do not have permission to deploy a treasury.', [FactoryError.OrgNotFound]: 'That organization could not be found.', + // The contract reverts the whole deploy, so neither the org nor the treasury exists. + [FactoryError.TreasuryInitFailed]: + 'The treasury for this organization could not be set up, so no organization was created.', }; const GENERIC_CONTRACT_ERROR = 'The contract rejected this action. Please review your input and try again.';