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>(&client, &tx, config, &self.resources, &[], quiet, no_cache)
175            .await?;
176
177        if let Some(url) = utils::lab_url_for_contract(&network, &contract_id) {
178            print.linkln(url);
179        }
180        print.checkln("Deployed!");
181
182        Ok(TxnResult::Res(stellar_strkey::Contract(contract_id.0)))
183    }
184}
185
186fn build_wrap_token_tx(
187    asset: impl Into<Asset>,
188    contract_id: &stellar_strkey::Contract,
189    sequence: i64,
190    fee: u32,
191    _network_passphrase: &str,
192    source_account: MuxedAccount,
193) -> Result<Transaction, Error> {
194    let contract = ScAddress::Contract(stellar_xdr::curr::ContractId(Hash(contract_id.0)));
195    let mut read_write = vec![
196        ContractData(LedgerKeyContractData {
197            contract: contract.clone(),
198            key: ScVal::LedgerKeyContractInstance,
199            durability: ContractDataDurability::Persistent,
200        }),
201        ContractData(LedgerKeyContractData {
202            contract: contract.clone(),
203            key: ScVal::Vec(Some(
204                vec![ScVal::Symbol("Metadata".try_into().unwrap())].try_into()?,
205            )),
206            durability: ContractDataDurability::Persistent,
207        }),
208    ];
209    let asset = asset.into();
210    if asset != Asset::Native {
211        read_write.push(ContractData(LedgerKeyContractData {
212            contract,
213            key: ScVal::Vec(Some(
214                vec![ScVal::Symbol("Admin".try_into().unwrap())].try_into()?,
215            )),
216            durability: ContractDataDurability::Persistent,
217        }));
218    }
219
220    let op = Operation {
221        source_account: None,
222        body: OperationBody::InvokeHostFunction(InvokeHostFunctionOp {
223            host_function: HostFunction::CreateContract(CreateContractArgs {
224                contract_id_preimage: ContractIdPreimage::Asset(asset),
225                executable: ContractExecutable::StellarAsset,
226            }),
227            auth: VecM::default(),
228        }),
229    };
230
231    Ok(Transaction {
232        source_account,
233        fee,
234        seq_num: SequenceNumber(sequence),
235        cond: Preconditions::None,
236        memo: Memo::None,
237        operations: vec![op].try_into()?,
238        ext: TransactionExt::V0,
239    })
240}