Skip to main content

soroban_cli/commands/contract/deploy/
asset.rs

1use crate::config::locator;
2use crate::print::Print;
3use crate::tx::sim_sign_and_send_tx;
4use crate::utils;
5use crate::xdr::{
6    Asset, ContractDataDurability, ContractExecutable, ContractIdPreimage, CreateContractArgs,
7    Error as XdrError, Hash, HostFunction, InvokeHostFunctionOp, LedgerKey::ContractData,
8    LedgerKeyContractData, Limits, Memo, MuxedAccount, Operation, OperationBody, Preconditions,
9    ScAddress, ScVal, SequenceNumber, Transaction, TransactionExt, VecM, WriteXdr,
10};
11use clap::Parser;
12use std::convert::Infallible;
13use std::{array::TryFromSliceError, fmt::Debug, num::ParseIntError};
14
15use crate::commands::tx::fetch;
16use crate::{
17    commands::{
18        global,
19        txn_result::{TxnEnvelopeResult, TxnResult},
20        HEADING_TRANSACTION,
21    },
22    config::{self, data, network},
23    rpc::Error as SorobanRpcError,
24    tx::builder,
25    utils::contract_id_hash_from_asset,
26};
27
28use crate::config::address::AliasName;
29
30#[derive(thiserror::Error, Debug)]
31pub enum Error {
32    #[error("error parsing int: {0}")]
33    ParseIntError(#[from] ParseIntError),
34
35    #[error(transparent)]
36    Client(#[from] SorobanRpcError),
37
38    #[error("internal conversion error: {0}")]
39    TryFromSliceError(#[from] TryFromSliceError),
40
41    #[error("xdr processing error: {0}")]
42    Xdr(#[from] XdrError),
43
44    #[error(transparent)]
45    Config(#[from] config::Error),
46
47    #[error(transparent)]
48    Data(#[from] data::Error),
49
50    #[error(transparent)]
51    Network(#[from] network::Error),
52
53    #[error(transparent)]
54    Builder(#[from] builder::Error),
55
56    #[error(transparent)]
57    Asset(#[from] builder::asset::Error),
58
59    #[error(transparent)]
60    Locator(#[from] locator::Error),
61
62    #[error(transparent)]
63    Fee(#[from] fetch::fee::Error),
64
65    #[error(transparent)]
66    Fetch(#[from] fetch::Error),
67}
68
69impl From<Infallible> for Error {
70    fn from(_: Infallible) -> Self {
71        unreachable!()
72    }
73}
74
75#[derive(Parser, Debug, Clone)]
76#[group(skip)]
77pub struct Cmd {
78    /// ID of the Stellar classic asset to wrap, e.g. "USDC:G...5"
79    #[arg(long)]
80    pub asset: builder::Asset,
81
82    #[command(flatten)]
83    pub config: config::Args,
84
85    #[command(flatten)]
86    pub resources: crate::resources::Args,
87
88    /// The alias that will be used to save the assets's id.
89    /// Whenever used, `--alias` will always overwrite the existing contract id
90    /// configuration without asking for confirmation.
91    #[arg(long)]
92    pub alias: Option<AliasName>,
93
94    /// Build the transaction and only write the base64 xdr to stdout
95    #[arg(long, help_heading = HEADING_TRANSACTION)]
96    pub build_only: bool,
97}
98
99impl Cmd {
100    pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
101        let res = self
102            .execute(&self.config, global_args.quiet, global_args.no_cache)
103            .await?
104            .to_envelope();
105        match res {
106            TxnEnvelopeResult::TxnEnvelope(tx) => println!("{}", tx.to_xdr_base64(Limits::none())?),
107            TxnEnvelopeResult::Res(contract) => {
108                let network = self.config.get_network()?;
109
110                if let Some(alias) = self.alias.clone() {
111                    if let Some(existing_contract) = self
112                        .config
113                        .locator
114                        .get_contract_id(&alias, &network.network_passphrase)?
115                    {
116                        let print = Print::new(global_args.quiet);
117                        print.warnln(format!(
118                            "Overwriting existing contract id: {existing_contract}"
119                        ));
120                    }
121
122                    self.config.locator.save_contract_id(
123                        &network.network_passphrase,
124                        &contract,
125                        &alias,
126                    )?;
127                }
128
129                println!("{contract}");
130            }
131        }
132        Ok(())
133    }
134
135    pub async fn execute(
136        &self,
137        config: &config::Args,
138        quiet: bool,
139        no_cache: bool,
140    ) -> Result<TxnResult<stellar_strkey::Contract>, Error> {
141        // Parse asset
142        let print = Print::new(quiet);
143        let asset = self.asset.resolve(&config.locator)?;
144
145        let network = config.get_network()?;
146        let client = network.rpc_client()?;
147        client
148            .verify_network_passphrase(Some(&network.network_passphrase))
149            .await?;
150
151        let source_account = config.source_account()?;
152
153        // Get the account sequence number
154        // TODO: use symbols for the method names (both here and in serve)
155        let account_details = client
156            .get_account(&source_account.clone().to_string())
157            .await?;
158        let sequence: i64 = account_details.seq_num.into();
159        let network_passphrase = &network.network_passphrase;
160        let contract_id = contract_id_hash_from_asset(&asset, network_passphrase);
161        let tx = build_wrap_token_tx(
162            asset,
163            &contract_id,
164            sequence + 1,
165            config.get_inclusion_fee()?,
166            network_passphrase,
167            source_account,
168        )?;
169
170        if self.build_only {
171            return Ok(TxnResult::Txn(Box::new(tx)));
172        }
173
174        sim_sign_and_send_tx::<Error>(
175            &client,
176            &tx,
177            config,
178            &self.resources,
179            &[],
180            // Asset wrapping cannot accept simulateTransaction authMode.
181            None,
182            quiet,
183            no_cache,
184        )
185        .await?;
186
187        if let Some(url) = utils::lab_url_for_contract(&network, &contract_id) {
188            print.linkln(url);
189        }
190        print.checkln("Deployed!");
191
192        Ok(TxnResult::Res(stellar_strkey::Contract(contract_id.0)))
193    }
194}
195
196fn build_wrap_token_tx(
197    asset: impl Into<Asset>,
198    contract_id: &stellar_strkey::Contract,
199    sequence: i64,
200    fee: u32,
201    _network_passphrase: &str,
202    source_account: MuxedAccount,
203) -> Result<Transaction, Error> {
204    let contract = ScAddress::Contract(stellar_xdr::ContractId(Hash(contract_id.0)));
205    let mut read_write = vec![
206        ContractData(LedgerKeyContractData {
207            contract: contract.clone(),
208            key: ScVal::LedgerKeyContractInstance,
209            durability: ContractDataDurability::Persistent,
210        }),
211        ContractData(LedgerKeyContractData {
212            contract: contract.clone(),
213            key: ScVal::Vec(Some(
214                vec![ScVal::Symbol("Metadata".try_into().unwrap())].try_into()?,
215            )),
216            durability: ContractDataDurability::Persistent,
217        }),
218    ];
219    let asset = asset.into();
220    if asset != Asset::Native {
221        read_write.push(ContractData(LedgerKeyContractData {
222            contract,
223            key: ScVal::Vec(Some(
224                vec![ScVal::Symbol("Admin".try_into().unwrap())].try_into()?,
225            )),
226            durability: ContractDataDurability::Persistent,
227        }));
228    }
229
230    let op = Operation {
231        source_account: None,
232        body: OperationBody::InvokeHostFunction(InvokeHostFunctionOp {
233            host_function: HostFunction::CreateContract(CreateContractArgs {
234                contract_id_preimage: ContractIdPreimage::Asset(asset),
235                executable: ContractExecutable::StellarAsset,
236            }),
237            auth: VecM::default(),
238        }),
239    };
240
241    Ok(Transaction {
242        source_account,
243        fee,
244        seq_num: SequenceNumber(sequence),
245        cond: Preconditions::None,
246        memo: Memo::None,
247        operations: vec![op].try_into()?,
248        ext: TransactionExt::V0,
249    })
250}