Skip to content

Commit

Permalink
fix: fmt and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored and gitbutler-client committed Dec 3, 2024
1 parent 267d964 commit 1cb961f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
23 changes: 22 additions & 1 deletion cmd/crates/soroban-test/tests/it/integration/custom_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use soroban_test::TestEnv;

use crate::integration::util::{deploy_custom, extend_contract};

use super::util::invoke_with_roundtrip;
use super::util::{invoke, invoke_with_roundtrip};

fn invoke_custom(e: &TestEnv, id: &str, func: &str) -> assert_cmd::Command {
let mut s = e.new_assert_cmd("contract");
Expand Down Expand Up @@ -40,7 +40,9 @@ async fn parse() {
negative_i32(sandbox, id).await;
negative_i64(sandbox, id).await;
account_address(sandbox, id).await;
// account_address_with_alias(sandbox, id).await;
contract_address(sandbox, id).await;
// contract_address_with_alias(sandbox, id).await;
bytes(sandbox, id).await;
const_enum(sandbox, id).await;
number_arg_return_ok(sandbox, id);
Expand Down Expand Up @@ -237,6 +239,11 @@ async fn account_address(sandbox: &TestEnv, id: &str) {
.await;
}

async fn account_address_with_alias(sandbox: &TestEnv, id: &str) {
let res = invoke(sandbox, id, "addresse", &json!("test").to_string()).await;
assert_eq!(sandbox.test_address(0), res);
}

async fn contract_address(sandbox: &TestEnv, id: &str) {
invoke_with_roundtrip(
sandbox,
Expand All @@ -247,6 +254,20 @@ async fn contract_address(sandbox: &TestEnv, id: &str) {
.await;
}

async fn contract_address_with_alias(sandbox: &TestEnv, id: &str) {
sandbox
.new_assert_cmd("contract")
.arg("alias")
.arg("add")
.arg("--alias=test_contract")
.arg("--address=CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE");
let res = invoke(sandbox, id, "addresse", &json!("test_contract").to_string()).await;
assert_eq!(
res,
"CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE"
);
}

async fn bytes(sandbox: &TestEnv, id: &str) {
invoke_with_roundtrip(sandbox, id, "bytes", json!("7374656c6c6172")).await;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use soroban_cli::{
contract::{self, fetch},
txn_result::TxnResult,
},
config::{address::Address, locator, secret},
config::{address::UnresolvedMuxedAccount, locator, secret},
};
use soroban_rpc::GetLatestLedgerResponse;
use soroban_test::{AssertExt, TestEnv, LOCAL_NETWORK_PASSPHRASE};
Expand Down
11 changes: 7 additions & 4 deletions cmd/crates/soroban-test/tests/it/integration/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ pub const CUSTOM_TYPES: &Wasm = &Wasm::Custom("test-wasms", "test_custom_types")
pub const CUSTOM_ACCOUNT: &Wasm = &Wasm::Custom("test-wasms", "test_custom_account");
pub const SWAP: &Wasm = &Wasm::Custom("test-wasms", "test_swap");

pub async fn invoke(sandbox: &TestEnv, id: &str, func: &str, data: &str) -> String {
sandbox
.invoke_with_test(&["--id", id, "--", func, &format!("--{func}"), data])
.await
.unwrap()
}
pub async fn invoke_with_roundtrip<D>(e: &TestEnv, id: &str, func: &str, data: D)
where
D: Display,
{
let data = data.to_string();
println!("{data}");
let res = e
.invoke_with_test(&["--id", id, "--", func, &format!("--{func}"), &data])
.await
.unwrap();
let res = invoke(e, id, func, &data).await;
assert_eq!(res, data);
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/soroban-cli/src/config/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ impl UnresolvedMuxedAccount {

pub fn resolve_secret(&self, locator: &locator::Args) -> Result<secret::Secret, Error> {
match &self {
UnresolvedMuxedAccount::Resolved(muxed_account) => Err(Error::CannotSign(muxed_account.clone())),
UnresolvedMuxedAccount::Resolved(muxed_account) => {
Err(Error::CannotSign(muxed_account.clone()))
}
UnresolvedMuxedAccount::AliasOrSecret(alias) => Ok(locator.read_identity(alias)?),
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub mod secret;
pub mod sign_with;
pub mod upgrade_check;

pub use alias::UnresolvedContract;
pub use address::UnresolvedMuxedAccount;
pub use alias::UnresolvedContract;
pub use sc_address::UnresolvedScAddress;

#[derive(thiserror::Error, Debug)]
Expand Down
9 changes: 6 additions & 3 deletions cmd/soroban-cli/src/config/sc_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ impl FromStr for UnresolvedScAddress {
type Err = Error;

fn from_str(value: &str) -> Result<Self, Self::Err> {
Ok(xdr::ScAddress::from_str(value)
.map_or_else(|_| UnresolvedScAddress::Alias(value.to_string()), UnresolvedScAddress::Resolved))
Ok(xdr::ScAddress::from_str(value).map_or_else(
|_| UnresolvedScAddress::Alias(value.to_string()),
UnresolvedScAddress::Resolved,
))
}
}

Expand All @@ -48,7 +50,8 @@ impl UnresolvedScAddress {
UnresolvedScAddress::Alias(alias) => alias,
};
let contract = UnresolvedContract::resolve_alias(&alias, locator, network_passphrase);
let muxed_account = super::UnresolvedMuxedAccount::resolve_muxed_account_with_alias(&alias, locator, None);
let muxed_account =
super::UnresolvedMuxedAccount::resolve_muxed_account_with_alias(&alias, locator, None);
match (contract, muxed_account) {
(Ok(contract), _) => Ok(xdr::ScAddress::Contract(xdr::Hash(contract.0))),
(_, Ok(muxed_account)) => Ok(xdr::ScAddress::Account(muxed_account.account_id())),
Expand Down

0 comments on commit 1cb961f

Please sign in to comment.