GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Add `stellar token burn` command by fnando · Pull Request #2744 · stellar/stellar-cli
Add
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| use serde_json::Value; | ||
| use soroban_test::{AssertExt, TestEnv}; | ||
|
|
||
| use crate::integration::{ | ||
| token::{add_trustline, deploy_sac, issuer_pays, sac_balance, sac_id}, | ||
| util::{new_account, test_address}, | ||
| }; | ||
|
|
||
| #[tokio::test] | ||
| async fn burn_destroys_funds() { | ||
| let sandbox = &TestEnv::new(); | ||
| let test = test_address(sandbox); | ||
| let issuer = new_account(sandbox, "issuer"); | ||
| let asset = format!("USDC:{issuer}"); | ||
|
|
||
| // Burn is invalid on the native asset, so use an issued asset: the holder | ||
| // (`test`) needs a trustline and a balance from the issuer to have something | ||
| // to destroy. | ||
| add_trustline(sandbox, "test", &asset); | ||
| issuer_pays(sandbox, "issuer", &test, &asset, 1_000); | ||
| deploy_sac(sandbox, &asset, "issuer"); | ||
| let contract_id = sac_id(sandbox, &asset); | ||
|
|
||
| let amount: i128 = 400; | ||
| let before = sac_balance(sandbox, &contract_id, &test); | ||
|
|
||
| // `test` signs, destroying its own tokens. | ||
| let stdout = sandbox | ||
| .new_assert_cmd("token") | ||
| .args([ | ||
| "burn", | ||
| "--id", | ||
| &asset, | ||
| "--from", | ||
| "test", | ||
| "--amount", | ||
| &amount.to_string(), | ||
| "--output", | ||
| "json", | ||
| ]) | ||
| .assert() | ||
| .success() | ||
| .stdout_as_str(); | ||
| let receipt: Value = serde_json::from_str(&stdout).unwrap(); | ||
| assert!( | ||
| receipt["tx_hash"].as_str().is_some_and(|h| !h.is_empty()), | ||
| "expected a non-empty tx_hash, got: {receipt}" | ||
| ); | ||
|
|
||
| let after = sac_balance(sandbox, &contract_id, &test); | ||
| assert_eq!(after, before - amount, "balance should decrease by amount"); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn burn_fails_when_sac_not_deployed() { | ||
| let sandbox = &TestEnv::new(); | ||
| let issuer = new_account(sandbox, "issuer"); | ||
| let asset = format!("USDC:{issuer}"); | ||
|
|
||
| // No SAC deployed → structured deploy-pointer error with a typed discriminator. | ||
| let stdout = sandbox | ||
| .new_assert_cmd("token") | ||
| .args([ | ||
| "burn", "--id", &asset, "--from", &issuer, "--amount", "1", "--output", "json", | ||
| ]) | ||
| .assert() | ||
| .failure() | ||
| .stdout_as_str(); | ||
| let value: Value = serde_json::from_str(&stdout).unwrap(); | ||
| assert_eq!( | ||
| value["error"]["type"], "sac_not_deployed", | ||
| "expected a typed error, got: {stdout}" | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn burn_rejects_muxed_from_with_clear_error() { | ||
| let sandbox = &TestEnv::new(); | ||
|
|
||
| // Muxed (M…) source accounts aren't supported by the invoke pipeline yet | ||
| // (see #2645). The signer here is `--from`, so the guard must name it. | ||
| let muxed = "MA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAAAAAAAAAPCICBKU"; | ||
| sandbox | ||
| .new_assert_cmd("token") | ||
| .args(["burn", "--id", "native", "--from", muxed, "--amount", "1"]) | ||
| .assert() | ||
| .failure() | ||
| .stderr(predicates::str::contains( | ||
| "muxed (M…) source accounts are not yet supported", | ||
| )); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn burn_rejects_negative_amount_before_any_rpc() { | ||
| let sandbox = &TestEnv::new(); | ||
|
|
||
| // A negative amount is rejected at the clap layer, before any network call. | ||
| sandbox | ||
| .new_assert_cmd("token") | ||
| .args([ | ||
| "burn", | ||
| "--id", | ||
| "native", | ||
| "--from", | ||
| "test", | ||
| // `=` form so clap reads `-1` as the value, not an unknown flag. | ||
| "--amount=-1", | ||
| ]) | ||
| .assert() | ||
| .failure() | ||
| .stderr(predicates::str::contains("amount must not be negative")); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| use clap::Parser; | ||
|
|
||
| use crate::{ | ||
| commands::{ | ||
| contract::invoke, | ||
| global, | ||
| token::args::{self, OutputFormat}, | ||
| }, | ||
| config::{self, locator, network, sign_with, token::UnresolvedToken, UnresolvedMuxedAccount}, | ||
| output::Output, | ||
| }; | ||
|
|
||
| #[derive(Debug, Parser, Clone)] | ||
| #[group(skip)] | ||
| pub struct Cmd { | ||
| /// The token to burn: a contract id or alias, `native`, or a classic asset | ||
| /// as `CODE:ISSUER`. | ||
| #[arg(long = "id")] | ||
| pub id: UnresolvedToken, | ||
|
|
||
| /// Account whose tokens are destroyed. Signs and authorizes the burn, so it | ||
| /// must be an identity or secret key you control. | ||
| #[arg(long)] | ||
| pub from: UnresolvedMuxedAccount, | ||
|
|
||
| /// Amount to burn, in the token's smallest unit (stroops for a Stellar Asset | ||
| /// Contract). | ||
| #[arg(long, value_parser = parse_nonneg_i128)] | ||
| pub amount: i128, | ||
|
|
||
| /// Format of the output. | ||
| #[arg(long, default_value = "text")] | ||
| pub output: OutputFormat, | ||
|
|
||
| #[command(flatten)] | ||
| pub network: network::Args, | ||
|
|
||
| #[command(flatten)] | ||
| pub locator: locator::Args, | ||
|
|
||
| #[command(flatten)] | ||
| pub sign_with: sign_with::Args, | ||
| } | ||
|
|
||
| #[derive(thiserror::Error, Debug)] | ||
| pub enum Error { | ||
| #[error(transparent)] | ||
| Config(#[from] config::Error), | ||
| #[error(transparent)] | ||
| Network(#[from] network::Error), | ||
| #[error(transparent)] | ||
| Args(#[from] args::Error), | ||
| #[error(transparent)] | ||
| Token(#[from] config::token::Error), | ||
| #[error(transparent)] | ||
| Invoke(#[from] invoke::Error), | ||
| #[error(transparent)] | ||
| Serde(#[from] serde_json::Error), | ||
|
|
||
| #[error( | ||
| "muxed (M…) source accounts are not yet supported for `token burn`; \ | ||
| use the underlying G… account as `--from` instead" | ||
| )] | ||
| MuxedSourceNotSupported, | ||
| } | ||
|
|
||
| /// Parse `--amount` as a non-negative `i128`. A negative burn amount is always | ||
| /// invalid, so reject it at the clap layer instead of letting it reach the | ||
| /// contract and fail as an opaque `HostError` deep in simulation. | ||
| fn parse_nonneg_i128(value: &str) -> Result<i128, String> { | ||
| let amount: i128 = value | ||
| .parse() | ||
| .map_err(|_| format!("invalid amount: {value}"))?; | ||
| if amount < 0 { | ||
| return Err(format!("amount must not be negative: {value}")); | ||
| } | ||
| Ok(amount) | ||
| } | ||
|
|
||
| impl Error { | ||
| /// Machine-readable discriminator for the JSON error envelope's `type` field. | ||
| #[must_use] | ||
| pub fn error_type(&self) -> &'static str { | ||
| match self { | ||
| Error::Config(_) => "config", | ||
| Error::Network(_) => "network", | ||
| Error::Args(e) => e.error_type(), | ||
| Error::Token(e) => e.error_type(), | ||
| Error::Invoke(_) => "invoke", | ||
| Error::Serde(_) => "internal", | ||
| Error::MuxedSourceNotSupported => "unsupported", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// The machine-readable result of a token burn. | ||
| #[derive(Debug, serde::Serialize)] | ||
| struct BurnResult { | ||
| /// Hex-encoded hash of the submitted transaction. | ||
| tx_hash: Option<String>, | ||
| /// The decoded contract return value (`null` for SEP-41 `burn`, which | ||
| /// returns nothing). | ||
| result: serde_json::Value, | ||
| } | ||
|
|
||
| impl Cmd { | ||
| /// Assemble a full [`config::Args`] for the underlying invocation, using | ||
| /// `--from` as the source account that signs and authorizes the burn. Fees | ||
| /// are left unset so the pipeline applies its default inclusion fee — this | ||
| /// command intentionally exposes no fee or sequence knobs. | ||
| fn config(&self) -> config::Args { | ||
| config::Args { | ||
| network: self.network.clone(), | ||
| source_account: self.from.clone(), | ||
| locator: self.locator.clone(), | ||
| sign_with: self.sign_with.clone(), | ||
| fee: None, | ||
| inclusion_fee: None, | ||
| } | ||
| } | ||
|
|
||
| pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> { | ||
| let output = Output::new(self.output.into(), global_args.quiet); | ||
| // In JSON mode the underlying invoke pipeline's human-readable status | ||
| // logging (which writes to stderr) would still fire; run it quietly so | ||
| // machine consumers get clean output without needing `--quiet`. | ||
| let quiet = global_args.quiet || output.is_json(); | ||
| let config = self.config(); | ||
| let network = config.get_network()?; | ||
|
|
||
| let token = self | ||
| .id | ||
| .resolve(&config.locator, &network.network_passphrase)?; | ||
|
|
||
| // SEP-41 `burn(from, amount)`: `from` is the source account (which also | ||
| // signs and authorizes), whose tokens are destroyed. | ||
| // | ||
| // The invoke pipeline can't source a transaction from a muxed account | ||
| // yet (see #2645), and a muxed strkey in the `burn` arg is rejected | ||
| // mid-simulation with an opaque host error; reject it up front with a | ||
| // clear message instead. | ||
| let source_account = config.source_account()?; | ||
| if matches!(source_account, crate::xdr::MuxedAccount::MuxedEd25519(_)) { | ||
| return Err(Error::MuxedSourceNotSupported); | ||
| } | ||
| let from = source_account.to_string(); | ||
| let amount = self.amount.to_string(); | ||
|
|
||
| // SEP-41 `burn(from, amount)` — supply the values in that order and let | ||
| // the contract's parameters be matched by position, so a token that | ||
| // names them anything still works. A burn always intends to submit, so | ||
| // force `Send::Yes`: a token whose `burn` records no writes/events/auth | ||
| // can't be classified read-only and silently exit 0 without ever | ||
| // destroying funds. | ||
| let invoke_result = args::invoke_by_position( | ||
| &config, | ||
| quiet, | ||
| global_args.no_cache, | ||
| &token, | ||
| "burn", | ||
| vec![from, amount], | ||
| invoke::Send::Yes, | ||
| ) | ||
| .await | ||
| .map_err(|e| args::not_deployed_error(&token, &e).map_or(Error::Invoke(e), Error::Args))? | ||
| .into_result(); | ||
|
|
||
| // `burn` always writes, so the invocation is submitted rather than | ||
| // resolved as a build-only transaction; a missing result would mean | ||
| // `--build-only`, which this command never sets. | ||
| let Some(invoke_result) = invoke_result else { | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| let result = if invoke_result.output.is_empty() { | ||
| serde_json::Value::Null | ||
| } else { | ||
| serde_json::from_str(&invoke_result.output) | ||
| .unwrap_or(serde_json::Value::String(invoke_result.output.clone())) | ||
| }; | ||
|
|
||
| // The pipeline already logs submission status and the explorer link to | ||
| // stderr; echo the hash to stdout so readable output is scriptable too. | ||
| if !output.is_json() { | ||
| if let Some(tx_hash) = &invoke_result.tx_hash { | ||
| println!("{tx_hash}"); | ||
| } | ||
| } | ||
|
|
||
| output.json_value(&BurnResult { | ||
| tx_hash: invoke_result.tx_hash, | ||
| result, | ||
| })?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } |
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.