Skip to content

Commit

Permalink
Add support for alias to stellar contract extend|read|restore. (#1413)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando authored Jun 27, 2024
1 parent 41648d9 commit 1fae6c1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
10 changes: 8 additions & 2 deletions cmd/soroban-cli/src/commands/contract/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use soroban_env_host::xdr::{

use crate::{
commands::{
config::{self, data},
config::{self, data, locator},
global, network,
txn_result::{TxnEnvelopeResult, TxnResult},
NetworkRunnable,
Expand Down Expand Up @@ -84,6 +84,8 @@ pub enum Error {
Data(#[from] data::Error),
#[error(transparent)]
Network(#[from] network::Error),
#[error(transparent)]
Locator(#[from] locator::Error),
}

impl Cmd {
Expand Down Expand Up @@ -128,7 +130,11 @@ impl NetworkRunnable for Cmd {
let config = config.unwrap_or(&self.config);
let network = config.get_network()?;
tracing::trace!(?network);
let keys = self.key.parse_keys()?;
let contract = config.locator.resolve_contract_id(
self.key.contract_id.as_ref().unwrap(),
&network.network_passphrase,
)?;
let keys = self.key.parse_keys(contract)?;
let network = &config.get_network()?;
let client = Client::new(&network.rpc_url)?;
let key = config.key_pair()?;
Expand Down
14 changes: 12 additions & 2 deletions cmd/soroban-cli/src/commands/contract/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use soroban_env_host::{
};

use crate::{
commands::{config, global, NetworkRunnable},
commands::{
config::{self, locator},
global, NetworkRunnable,
},
key,
rpc::{self, Client, FullLedgerEntries, FullLedgerEntry},
};
Expand Down Expand Up @@ -86,6 +89,8 @@ pub enum Error {
Key(#[from] key::Error),
#[error("Only contract data and code keys are allowed")]
OnlyDataAllowed,
#[error(transparent)]
Locator(#[from] locator::Error),
}

impl Cmd {
Expand Down Expand Up @@ -173,6 +178,7 @@ impl Cmd {
impl NetworkRunnable for Cmd {
type Error = Error;
type Result = FullLedgerEntries;

async fn run_against_rpc_server(
&self,
_: Option<&global::Args>,
Expand All @@ -182,7 +188,11 @@ impl NetworkRunnable for Cmd {
let network = config.get_network()?;
tracing::trace!(?network);
let client = Client::new(&network.rpc_url)?;
let keys = self.key.parse_keys()?;
let contract = config.locator.resolve_contract_id(
self.key.contract_id.as_ref().unwrap(),
&network.network_passphrase,
)?;
let keys = self.key.parse_keys(contract)?;
Ok(client.get_full_ledger_entries(&keys).await?)
}
}
6 changes: 5 additions & 1 deletion cmd/soroban-cli/src/commands/contract/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ impl NetworkRunnable for Cmd {
let config = config.unwrap_or(&self.config);
let network = config.get_network()?;
tracing::trace!(?network);
let entry_keys = self.key.parse_keys()?;
let contract = config.locator.resolve_contract_id(
self.key.contract_id.as_ref().unwrap(),
&network.network_passphrase,
)?;
let entry_keys = self.key.parse_keys(contract)?;
let client = Client::new(&network.rpc_url)?;
let key = config.key_pair()?;

Expand Down
16 changes: 4 additions & 12 deletions cmd/soroban-cli/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ use soroban_env_host::xdr::{
ScVal,
};
use std::path::PathBuf;
use stellar_strkey::Contract;

use crate::{
commands::contract::Durability,
utils::{self},
wasm,
};
use crate::{commands::contract::Durability, wasm};

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -64,7 +61,7 @@ pub struct Args {
}

impl Args {
pub fn parse_keys(&self) -> Result<Vec<LedgerKey>, Error> {
pub fn parse_keys(&self, contract: Contract) -> Result<Vec<LedgerKey>, Error> {
let keys = if let Some(keys) = &self.key {
keys.iter()
.map(|key| {
Expand All @@ -90,21 +87,16 @@ impl Args {
} else {
vec![ScVal::LedgerKeyContractInstance]
};
let contract_id = contract_id(self.contract_id.as_ref().unwrap())?;

Ok(keys
.into_iter()
.map(|key| {
LedgerKey::ContractData(LedgerKeyContractData {
contract: ScAddress::Contract(xdr::Hash(contract_id)),
contract: ScAddress::Contract(xdr::Hash(contract.0)),
durability: (&self.durability).into(),
key,
})
})
.collect())
}
}

fn contract_id(s: &str) -> Result<[u8; 32], Error> {
utils::contract_id_from_str(s).map_err(|e| Error::CannotParseContractId(s.to_string(), e))
}

0 comments on commit 1fae6c1

Please sign in to comment.