From e06be00efa8987bc24b37d2e43ffbfee11a58e75 Mon Sep 17 00:00:00 2001 From: fadesany Date: Tue, 15 Sep 2026 21:16:53 +0100 Subject: [PATCH] fix(sdk): call deploy_treasury with contract arg order and org-id return deployTreasury encoded its args as (admin, name, ...) but the contract signature is deploy_treasury(name, admin, approvers, threshold, token) -> u32 (charter-contract contracts/factory/src/lib.rs L87-94). Every call trapped with Error(WasmVm, InvalidAction) before any contract logic ran, so org creation was broken on every factory deployment. The SDK also expected a treasury address string back, but the contract returns the new org id, so a successful deploy would still have thrown. Read the org record back with get_org (retrying once for testnet RPC read-after-write lag) and return { hash, orgId, treasuryAddress }. A failed read-back after a confirmed deploy is reported as such rather than as a contract rejection. Update the SDK reference docs for the return shape and argument order. --- docs/developer-guide/sdk-reference.md | 15 ++++++--- packages/sdk/src/contracts/factory.ts | 48 +++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/docs/developer-guide/sdk-reference.md b/docs/developer-guide/sdk-reference.md index 8f4e82f..3a87fbe 100644 --- a/docs/developer-guide/sdk-reference.md +++ b/docs/developer-guide/sdk-reference.md @@ -70,18 +70,25 @@ factory.deployTreasury( `initialize` registers the treasury wasm the factory deploys from; it is an admin-only, run-once setup call, surfaced here for tooling. -`deployTreasury` deploys a per-org treasury and returns both the transaction -hash and the new treasury address: +`deployTreasury` deploys a per-org treasury. The contract's `deploy_treasury` +returns only the new org id (`u32`); once the transaction confirms, the SDK reads +that org record back with `get_org` to get the treasury address, and returns all +three: ```ts interface DeployTreasuryResult { hash: string; + orgId: number; treasuryAddress: string; } ``` -Note the argument order: the caller passes `admin` and the SDK uses it as the -account that authorizes and pays. This is the call the new-org form makes. +Note the argument order: the SDK function takes `admin` first, but the contract +signature is `deploy_treasury(name, admin, approvers, threshold, token)` and the +SDK encodes the call in that contract order. `admin` is also the account that +authorizes and pays. Because the SDK signs only the transaction envelope, the +call succeeds only when `admin` is the factory's stored deployer (both must +authorize). This is the call the new-org form makes. Because the treasury's own `initialize` runs as a sub-call that requires the admin's authorization, the wallet is asked to sign twice — see [Getting Started](../for-organization-admins/getting-started.md). diff --git a/packages/sdk/src/contracts/factory.ts b/packages/sdk/src/contracts/factory.ts index 4ab1289..1aea305 100644 --- a/packages/sdk/src/contracts/factory.ts +++ b/packages/sdk/src/contracts/factory.ts @@ -21,12 +21,33 @@ export class FactoryCallError extends Error { } } -/** Outcome of `deploy_treasury`: the confirmed tx hash plus the new treasury address. */ +/** + * Outcome of `deploy_treasury`: the confirmed tx hash, the org id the contract + * returns, and the new treasury address (read back from the org record). + */ export interface DeployTreasuryResult { hash: string; + orgId: number; treasuryAddress: string; } +/** How long to wait before retrying the post-deploy `get_org` read. */ +const ORG_READ_RETRY_DELAY_MS = 2_000; + +/** + * Reads the org record written by a just-confirmed deploy. Testnet RPC can + * simulate against a snapshot that predates the write for a ledger or two, so + * a first-try `OrgNotFound` is retried once before giving up. + */ +async function readDeployedOrg(factoryId: string, orgId: number): Promise { + try { + return await getOrg(factoryId, orgId); + } catch { + await new Promise((resolve) => setTimeout(resolve, ORG_READ_RETRY_DELAY_MS)); + return getOrg(factoryId, orgId); + } +} + /** * Registers the treasury program the factory deploys from. Admin-only, run once * at factory setup — surfaced here for completeness and operational tooling. @@ -72,13 +93,17 @@ export async function deployTreasury( token: string, signXdr: SignXdr, ): Promise { + let hash: string; + let orgId: number; try { + // Contract signature: deploy_treasury(name, admin, approvers, threshold, token) -> u32. + // The TS parameter order above differs; the XDR args must follow the contract. const result = await writeContract( factoryId, 'deploy_treasury', [ - addressArg(admin), stringArg(name), + addressArg(admin), addressVecArg(approvers), u32Arg(threshold), addressArg(token), @@ -86,17 +111,28 @@ export async function deployTreasury( admin, signXdr, ); - const treasuryAddress = result.returnValue; - if (typeof treasuryAddress !== 'string') { - throw new Error('deploy_treasury did not return a treasury address'); + if (typeof result.returnValue !== 'number') { + throw new Error('deploy_treasury did not return an org id'); } - return { hash: result.hash, treasuryAddress }; + hash = result.hash; + orgId = result.returnValue; } catch (error) { if (error instanceof ContractError) { throw new FactoryCallError(error.code); } throw error; } + + // The deploy is already confirmed here, so a failed read-back must not be + // reported as a contract rejection of the deploy itself. + try { + const org = await readDeployedOrg(factoryId, orgId); + return { hash, orgId, treasuryAddress: org.treasury }; + } catch { + throw new Error( + `Organization #${orgId} was created (tx ${hash}), but its treasury address could not be loaded yet. Refresh to find it in the directory.`, + ); + } } // ---------------------------------------------------------------------------