xrpl_cli/account/offers/
remove_offer.rs

1use clap::ArgMatches;
2use libsecp256k1::{PublicKey, SecretKey};
3
4use xrpl_binary_codec::{serialize, sign};
5use xrpl_http_client::{Client, SubmitRequest};
6use xrpl_types::{AccountId, OfferCancelTransaction, Transaction};
7
8// xrpl account <ADDRESS> --public-key="..." --secret-key="..." offers remove <OFFER_SEQUENCE>
9
10pub async fn remove_offer(
11    account: impl AsRef<str>,
12    public_key: impl AsRef<str>,
13    secret_key: impl AsRef<str>,
14    remove_offer_matches: &ArgMatches,
15) -> anyhow::Result<()> {
16    let account = account.as_ref();
17
18    let offer_sequence: &String = remove_offer_matches
19        .get_one("OFFER_SEQ")
20        .expect("offer sequence missing");
21    let offer_sequence: u32 = offer_sequence.parse().expect("offer sequence invalid");
22
23    let client = Client::new();
24
25    let mut tx = OfferCancelTransaction::new(AccountId::from_address(account)?, offer_sequence);
26
27    client.prepare_transaction(tx.common_mut()).await?;
28
29    // #insight
30    // The secret/private key is 32 bytes, the public key is 33 bytes.
31
32    let secret_key = SecretKey::parse_slice(&hex::decode(secret_key.as_ref())?)?;
33    let public_key =
34        PublicKey::parse_compressed(&hex::decode(public_key.as_ref())?.as_slice().try_into()?)?;
35
36    sign::sign_transaction(&mut tx, &public_key, &secret_key)?;
37
38    let tx_blob = serialize::serialize(&tx)?;
39
40    let req = SubmitRequest::new(hex::encode(&tx_blob));
41    let resp = client.call(req).await?;
42
43    println!("{resp:?}");
44
45    Ok(())
46}