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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ struct OrgRecord { name: String, treasury: Address, admin: Address, created_ledg
| 2 | `AlreadyInitialized` | `initialize` is called a second time |
| 3 | `NotDeployer` | reserved — deploy authority is enforced via `require_auth` on the stored deployer |
| 4 | `OrgNotFound` | `get_org` is called with an unknown id |
| 5 | `TreasuryInitFailed` | the new treasury's `initialize` fails (e.g. invalid threshold); the treasury's own error code is not passed through |

### Test token

Expand Down
3 changes: 3 additions & 0 deletions contracts/factory/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ pub enum Error {
NotDeployer = 3,
/// Org id does not exist.
OrgNotFound = 4,
/// The new treasury's `initialize` sub-call failed. Wraps any treasury
/// error so treasury codes never surface as factory codes.
TreasuryInitFailed = 5,
}
14 changes: 12 additions & 2 deletions contracts/factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ impl FactoryContract {
///
/// # Panics
/// * `Error::NotInitialized` if the factory was not initialized.
/// * `Error::TreasuryInitFailed` if the treasury's `initialize` fails for
/// any reason (e.g. an invalid threshold). The treasury's own error code
/// is not propagated.
pub fn deploy_treasury(
env: Env,
name: String,
Expand Down Expand Up @@ -121,12 +124,19 @@ impl FactoryContract {
.with_current_contract(salt)
.deploy_v2(wasm_hash, ());

TreasuryContractClient::new(&env, &treasury).initialize(
// Treasury and factory error codes overlap with different meanings, so
// a treasury failure must not propagate as-is. Any failure (contract
// error, auth or host error) is reported as `TreasuryInitFailed`; the
// panic rolls back the deployment above along with everything else.
match TreasuryContractClient::new(&env, &treasury).try_initialize(
&admin,
&approvers,
&threshold,
&token,
);
) {
Ok(Ok(())) => {}
_ => panic_with_error!(&env, Error::TreasuryInitFailed),
}

env.storage().instance().set(&DataKey::OrgCount, &org_id);
env.storage().persistent().set(
Expand Down
25 changes: 24 additions & 1 deletion contracts/factory/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FactoryContract, FactoryContractClient, TreasuryContractClient};
use crate::{Error, FactoryContract, FactoryContractClient, TreasuryContractClient};
use soroban_sdk::testutils::{Address as _, Events as _};
use soroban_sdk::{vec, Address, Env, IntoVal, String, Symbol};

Expand Down Expand Up @@ -214,6 +214,29 @@ fn test_deploy_treasury_not_deployer_panics() {
);
}

#[test]
fn test_deploy_treasury_invalid_threshold_returns_factory_error() {
let ctx = TestContext::new();
// Threshold above the approver count: the treasury rejects it with its own
// InvalidThreshold (#8), which must surface as the factory's error instead.
let result = ctx.client.try_deploy_treasury(
&String::from_str(&ctx.env, "Charter Org"),
&ctx.admin,
&vec![&ctx.env, ctx.approver1.clone(), ctx.approver2.clone()],
&3,
&ctx.token,
);
assert_eq!(result, Err(Ok(Error::TreasuryInitFailed.into())));
assert_eq!(ctx.client.get_org_count(), 0);
}

#[test]
#[should_panic(expected = "Error(Contract, #5)")]
fn test_deploy_treasury_zero_threshold_panics() {
let ctx = TestContext::new();
deploy_org(&ctx, "Charter Org", 0);
}

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
fn test_deploy_treasury_not_initialized_panics() {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading