|
| 1 | +use serde_json::Value; |
| 2 | +use soroban_test::{AssertExt, TestEnv}; |
| 3 | + |
| 4 | +use crate::integration::{ |
| 5 | + token::{add_trustline, deploy_sac, issuer_pays, sac_balance, sac_id}, |
| 6 | + util::{deploy_hello, new_account, test_address}, |
| 7 | +}; |
| 8 | + |
| 9 | +/// Enable the clawback flag on `issuer`, so trustlines created afterwards are |
| 10 | +/// clawback-enabled. `AUTH_CLAWBACK_ENABLED` requires `AUTH_REVOCABLE`, so set |
| 11 | +/// both together. |
| 12 | +fn enable_clawback(sandbox: &TestEnv, issuer: &str) { |
| 13 | + sandbox |
| 14 | + .new_assert_cmd("tx") |
| 15 | + .args([ |
| 16 | + "new", |
| 17 | + "set-options", |
| 18 | + "--set-revocable", |
| 19 | + "--set-clawback-enabled", |
| 20 | + "--source", |
| 21 | + issuer, |
| 22 | + ]) |
| 23 | + .assert() |
| 24 | + .success(); |
| 25 | +} |
| 26 | + |
| 27 | +#[tokio::test] |
| 28 | +async fn clawback_removes_balance_and_returns_receipt() { |
| 29 | + let sandbox = &TestEnv::new(); |
| 30 | + let test = test_address(sandbox); |
| 31 | + let issuer = new_account(sandbox, "issuer"); |
| 32 | + let asset = format!("USDC:{issuer}"); |
| 33 | + |
| 34 | + // Clawback requires the issuer to enable the flag *before* the holder's |
| 35 | + // trustline exists, so the trustline is created clawback-enabled. |
| 36 | + enable_clawback(sandbox, "issuer"); |
| 37 | + add_trustline(sandbox, "test", &asset); |
| 38 | + deploy_sac(sandbox, &asset, "issuer"); |
| 39 | + issuer_pays(sandbox, "issuer", &test, &asset, 10_000_000); |
| 40 | + |
| 41 | + let stdout = sandbox |
| 42 | + .new_assert_cmd("token") |
| 43 | + .args([ |
| 44 | + "clawback", "--id", &asset, "--source", "issuer", "--from", &test, "--amount", |
| 45 | + "4000000", "--output", "json", |
| 46 | + ]) |
| 47 | + .assert() |
| 48 | + .success() |
| 49 | + .stdout_as_str(); |
| 50 | + let receipt: Value = serde_json::from_str(&stdout).unwrap(); |
| 51 | + assert!( |
| 52 | + receipt["tx_hash"].as_str().is_some(), |
| 53 | + "expected a tx hash, got: {receipt}" |
| 54 | + ); |
| 55 | + |
| 56 | + // 10_000_000 minted − 4_000_000 clawed back = 6_000_000 remaining. |
| 57 | + let sac = sac_id(sandbox, &asset); |
| 58 | + assert_eq!( |
| 59 | + sac_balance(sandbox, &sac, &test), |
| 60 | + 6_000_000, |
| 61 | + "expected the remaining balance after clawback" |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +#[tokio::test] |
| 66 | +async fn clawback_fails_when_sac_not_deployed() { |
| 67 | + let sandbox = &TestEnv::new(); |
| 68 | + let test = test_address(sandbox); |
| 69 | + let issuer = new_account(sandbox, "issuer"); |
| 70 | + let asset = format!("USDC:{issuer}"); |
| 71 | + |
| 72 | + // No SAC deployed → structured deploy-pointer error with a typed discriminator. |
| 73 | + let stdout = sandbox |
| 74 | + .new_assert_cmd("token") |
| 75 | + .args([ |
| 76 | + "clawback", "--id", &asset, "--source", "issuer", "--from", &test, "--amount", "1", |
| 77 | + "--output", "json", |
| 78 | + ]) |
| 79 | + .assert() |
| 80 | + .failure() |
| 81 | + .stdout_as_str(); |
| 82 | + let value: Value = serde_json::from_str(&stdout).unwrap(); |
| 83 | + assert_eq!( |
| 84 | + value["error"]["type"], "sac_not_deployed", |
| 85 | + "expected a typed error, got: {stdout}" |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +#[tokio::test] |
| 90 | +async fn clawback_rejects_muxed_source_with_clear_error() { |
| 91 | + let sandbox = &TestEnv::new(); |
| 92 | + let holder = new_account(sandbox, "holder"); |
| 93 | + |
| 94 | + // Muxed (M…) source accounts aren't supported by the invoke pipeline yet |
| 95 | + // (see #2645). Until then the command must reject them up front with a clear |
| 96 | + // message rather than a raw strkey decode error deep in the pipeline. |
| 97 | + let muxed = "MA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAAAAAAAAAPCICBKU"; |
| 98 | + sandbox |
| 99 | + .new_assert_cmd("token") |
| 100 | + .args([ |
| 101 | + "clawback", "--id", "native", "--source", muxed, "--from", &holder, "--amount", "1", |
| 102 | + ]) |
| 103 | + .assert() |
| 104 | + .failure() |
| 105 | + .stderr(predicates::str::contains( |
| 106 | + "muxed (M…) source accounts are not yet supported", |
| 107 | + )); |
| 108 | +} |
| 109 | + |
| 110 | +#[tokio::test] |
| 111 | +async fn clawback_rejects_negative_amount_before_any_rpc() { |
| 112 | + let sandbox = &TestEnv::new(); |
| 113 | + let test = test_address(sandbox); |
| 114 | + |
| 115 | + // A negative clawback is rejected at the CLI layer, before any network call. |
| 116 | + // `=` form so clap reads `-1` as the value, not an unknown flag. |
| 117 | + sandbox |
| 118 | + .new_assert_cmd("token") |
| 119 | + .args([ |
| 120 | + "clawback", |
| 121 | + "--id", |
| 122 | + "native", |
| 123 | + "--source", |
| 124 | + "test", |
| 125 | + "--from", |
| 126 | + &test, |
| 127 | + "--amount=-1", |
| 128 | + ]) |
| 129 | + .assert() |
| 130 | + .failure() |
| 131 | + .stderr(predicates::str::contains("must not be negative")); |
| 132 | +} |
| 133 | + |
| 134 | +#[tokio::test] |
| 135 | +async fn clawback_warns_when_target_is_not_a_sac() { |
| 136 | + let sandbox = &TestEnv::new(); |
| 137 | + let test = test_address(sandbox); |
| 138 | + let contract_id = deploy_hello(sandbox).await; |
| 139 | + |
| 140 | + // Pointing a SAC-admin command at a plain wasm contract warns. The clawback |
| 141 | + // then fails (hello_world has no `clawback`), but the heads-up is the point. |
| 142 | + let stderr = sandbox |
| 143 | + .new_assert_cmd("token") |
| 144 | + .args([ |
| 145 | + "clawback", |
| 146 | + "--id", |
| 147 | + &contract_id, |
| 148 | + "--source", |
| 149 | + "test", |
| 150 | + "--from", |
| 151 | + &test, |
| 152 | + "--amount", |
| 153 | + "1", |
| 154 | + ]) |
| 155 | + .assert() |
| 156 | + .failure() |
| 157 | + .stderr_as_str(); |
| 158 | + assert!( |
| 159 | + stderr.contains("is not a Stellar Asset Contract"), |
| 160 | + "expected a non-SAC warning, got: {stderr}" |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +#[tokio::test] |
| 165 | +async fn clawback_does_not_warn_when_target_is_a_sac() { |
| 166 | + let sandbox = &TestEnv::new(); |
| 167 | + let test = test_address(sandbox); |
| 168 | + let issuer = new_account(sandbox, "issuer"); |
| 169 | + let asset = format!("USDC:{issuer}"); |
| 170 | + |
| 171 | + enable_clawback(sandbox, "issuer"); |
| 172 | + add_trustline(sandbox, "test", &asset); |
| 173 | + deploy_sac(sandbox, &asset, "issuer"); |
| 174 | + issuer_pays(sandbox, "issuer", &test, &asset, 10_000_000); |
| 175 | + // Reference the SAC by its contract id, not the asset, so the check can only |
| 176 | + // clear it by inspecting the on-chain executable — not the id's text form. |
| 177 | + let sac = sac_id(sandbox, &asset); |
| 178 | + |
| 179 | + let stderr = sandbox |
| 180 | + .new_assert_cmd("token") |
| 181 | + .args([ |
| 182 | + "clawback", "--id", &sac, "--source", "issuer", "--from", &test, "--amount", "4000000", |
| 183 | + ]) |
| 184 | + .assert() |
| 185 | + .success() |
| 186 | + .stderr_as_str(); |
| 187 | + assert!( |
| 188 | + !stderr.contains("is not a Stellar Asset Contract"), |
| 189 | + "a genuine SAC should not warn, got: {stderr}" |
| 190 | + ); |
| 191 | +} |
0 commit comments