diff --git a/docs/developer-guide/sdk-reference.md b/docs/developer-guide/sdk-reference.md index 8f4e82f..83f9a37 100644 --- a/docs/developer-guide/sdk-reference.md +++ b/docs/developer-guide/sdk-reference.md @@ -70,21 +70,27 @@ 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. -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). +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; see [Getting +Started](../for-organization-admins/getting-started.md) for what that means for +admins today. ### Reads diff --git a/docs/for-organization-admins/getting-started.md b/docs/for-organization-admins/getting-started.md index 771ca52..4fd1938 100644 --- a/docs/for-organization-admins/getting-started.md +++ b/docs/for-organization-admins/getting-started.md @@ -47,13 +47,18 @@ four things to fill in: Submit with **Deploy treasury**. The button shows "Deploying…" while the transaction is in flight. -### This transaction needs two signatures +### Only the factory's deployer can deploy right now -Deploying goes through the factory contract, and it asks your wallet to sign -**twice** in one flow. That is expected, not a bug. The factory authorizes you -as the deployer, and the new treasury's own `initialize` step requires the -admin — you — to authorize it as a sub-call. Approve both prompts. If you only -approve the first, the deploy will not complete. +Deploying goes through the factory contract, and the contract requires two +authorizations: the factory's stored **deployer**, and the new treasury's +**admin** — the wallet you are connected with. Your wallet signs the transaction +once, and that one signature covers both only when your wallet *is* the +factory's deployer. + +If you connect any other wallet, the deploy is rejected for missing +authorization. Until deploying is opened up to other accounts, ask whoever runs +your Charter factory to deploy the treasury with you as its admin, or use the +deployer wallet. When it finishes, Charter sends you to your new organization's page at `/org/`. That address is your treasury; bookmark it. diff --git a/docs/smart-contracts/factory.md b/docs/smart-contracts/factory.md index 74dcc29..31daee8 100644 --- a/docs/smart-contracts/factory.md +++ b/docs/smart-contracts/factory.md @@ -34,9 +34,11 @@ requiring the admin's signature at the top ties that authorization to the root transaction so the sub-call succeeds. - Panics `NotInitialized` if the factory has not been initialized. -- The treasury's `initialize` runs as part of this call, so its own panics - apply too — for example `InvalidThreshold` if `threshold == 0` or - `threshold > approvers.len()`. +- Panics `TreasuryInitFailed` if the treasury's `initialize` fails for any + reason — for example `threshold == 0` or `threshold > approvers.len()`. The + treasury's own error code is not passed through (treasury and factory codes + overlap with different meanings), and the whole deploy is rolled back, so no + org is recorded. ## `get_org(org_id) -> OrgRecord` @@ -72,6 +74,7 @@ struct OrgRecord { | `AlreadyInitialized` | 2 | `initialize` called twice. | | `NotDeployer` | 3 | Reserved for deployer-authorization failures. | | `OrgNotFound` | 4 | Requested org id does not exist. | +| `TreasuryInitFailed` | 5 | The new treasury's `initialize` failed during `deploy_treasury`; the treasury's own code is not passed through. | The discriminants here differ from the treasury's — the factory's `NotInitialized` is 1 and `AlreadyInitialized` is 2, the reverse of the 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.`, + ); + } } // ---------------------------------------------------------------------------