Skip to content

fix(sdk): call deploy_treasury with contract arg order and org-id return - #7

Merged
K1NGD4VID merged 1 commit into
fix/sdk-contract-error-enumsfrom
fix/sdk-deploy-treasury-args
Sep 15, 2026
Merged

K1NGD4VID merged 1 commit into
fix/sdk-contract-error-enumsfrom
fix/sdk-deploy-treasury-args

Conversation

@fadesany

Copy link
Copy Markdown
Contributor

Stacked on #6. Merge #6 first; this PR's base will then retarget to main. The failure-path check below relies on #6's TreasuryInitFailed message.

Summary

Org creation was broken: factory.deployTreasury couldn't create an org on any factory deployment.

The contract signature (charter-contract/contracts/factory/src/lib.rs:87-94) is:

pub fn deploy_treasury(env: Env, name: String, admin: Address, approvers: Vec<Address>, threshold: u32, token: Address) -> u32

1. Wrong argument order. The SDK passed (admin, name, …), so every call trapped with HostError: Error(WasmVm, InvalidAction) before any contract logic ran. The XDR args now follow the contract's order. The SDK function's own TypeScript parameters are unchanged, so new-org-form.tsx needs no change.

2. Wrong return type. The contract returns the new org id (u32), but the SDK required a treasury address string, so even a correctly ordered deploy would have thrown. The SDK now reads the org record back with get_org and returns { hash, orgId, treasuryAddress }. The read is retried once because testnet RPC can briefly return stale data after a transaction. If it still fails after a confirmed deploy, the error says the org was created instead of claiming the contract rejected the deploy.

Also updates docs/developer-guide/sdk-reference.md, which repeated both bugs.

No other call sites: in this repo, deploy_treasury is only called from factory.ts, via new-org-form.tsx. The contract repo's verify.sh and Rust tests already use the correct order.

Testing

npm run typecheck passes for @charter/sdk and @charter/web.

Live on the new testnet factory CBLPNUIGY3RTZSLBRIAZPOSLJADUP3XSJF5ARULKZMODNT25WXYJ37CG, running the real SDK code:

=== 1. OLD argument order (admin, name, ...) — simulation only ===
HostError: Error(WasmVm, InvalidAction)

=== 2. FIXED order, valid threshold 2 of 2 — real deploy via SDK ===
deployTreasury result: {
  "hash": "b1fe3777aadc7dff68f92a1a772d245d65adf9d6677d544e5b2e94ffacfdcf73",
  "orgId": 1,
  "treasuryAddress": "CAZKKTMSNF3P4E26MCBJHH7DUYY52ASWDBHY7AC7I66ACVTQNEZL3W2X"
}
treasury get_threshold: 2
treasury get_approvers: [ 'GBBA…7QS5', 'GCUR…SDBY' ]
org count after: 1

=== 3. FIXED order, threshold 3 with 2 approvers — via SDK (fails at simulation, nothing sent) ===
error.name: FactoryCallError
error.code: 5
error.message: The treasury for this organization could not be set up, so no organization was created.
org count unchanged: 1 -> 1

The deploy transaction was confirmed separately with RPC getTransaction: status: SUCCESS, ledger 4695937.

Known limitation (not fixed here)

deploy_treasury requires both the factory's stored deployer and admin to sign, but the SDK only signs the transaction envelope. A deploy therefore only works when the connected wallet is the deployer; the live test passed because admin was the deployer key. Any other wallet using the new-org form will still fail authorization. Fixing that needs a separate decision: change the contract's auth model or add a deployer co-signing step.

NEXT_PUBLIC_FACTORY_CONTRACT_ID is intentionally unchanged; repointing the app at the new factory is a separate decision.

🤖 Generated with Claude Code

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.
@vercel

vercel Bot commented Sep 15, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
app-web Ready Ready Preview Sep 15, 2026 8:17pm UTC

@K1NGD4VID
K1NGD4VID merged commit b62e37b into fix/sdk-contract-error-enums Sep 15, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants