Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions docs/developer-guide/sdk-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
48 changes: 42 additions & 6 deletions packages/sdk/src/contracts/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrgRecord> {
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.
Expand Down Expand Up @@ -72,31 +93,46 @@ export async function deployTreasury(
token: string,
signXdr: SignXdr,
): Promise<DeployTreasuryResult> {
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),
],
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.`,
);
}
}

// ---------------------------------------------------------------------------
Expand Down