Skip to content

Commit

Permalink
make suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lambda-0x committed Jul 31, 2024
1 parent 30d200d commit 3ba4859
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 70 deletions.
9 changes: 4 additions & 5 deletions crates/sozo/ops/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ where
let resource_selector =
get_resource_selector(ui, world, &new_writer.resource, default_namespace).await?;
let contract_address =
utils::get_contract_address(world, new_writer.tag_or_address.clone()).await?;
utils::get_contract_address(world, &new_writer.tag_or_address).await?;
calls.push(world.grant_writer_getcall(&resource_selector, &contract_address.into()));
}

Expand Down Expand Up @@ -160,8 +160,7 @@ where

for new_owner in new_owners {
let resource_selector =
get_resource_selector(ui, world, &new_owner.resource.clone(), default_namespace)
.await?;
get_resource_selector(ui, world, &new_owner.resource, default_namespace).await?;
calls.push(world.grant_owner_getcall(&new_owner.owner.into(), &resource_selector));
}

Expand Down Expand Up @@ -200,7 +199,7 @@ where
let resource_selector =
get_resource_selector(ui, world, &new_writer.resource, default_namespace).await?;
let contract_address =
utils::get_contract_address(world, new_writer.tag_or_address.clone()).await?;
utils::get_contract_address(world, &new_writer.tag_or_address).await?;
calls.push(world.revoke_writer_getcall(&resource_selector, &contract_address.into()));
}

Expand Down Expand Up @@ -283,7 +282,7 @@ where
} else {
ensure_namespace(tag_or_address, default_namespace)
};
utils::get_contract_address(world, tag_or_address).await?
utils::get_contract_address(world, &tag_or_address).await?
}
ResourceType::Model(tag_or_name) => {
// TODO: Is some models have version 0 (using the name of the struct instead of the
Expand Down
2 changes: 1 addition & 1 deletion crates/sozo/ops/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub async fn execute<A>(
where
A: ConnectedAccount + Sync + Send + 'static,
{
let contract_address = utils::get_contract_address(world, tag_or_address).await?;
let contract_address = utils::get_contract_address(world, &tag_or_address).await?;
let res = world
.account
.execute_v1(vec![Call {
Expand Down
4 changes: 2 additions & 2 deletions crates/sozo/ops/src/migration/auto_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub async fn auto_authorize<A>(
world: &WorldContract<A>,
txn_config: &TxnConfig,
default_namespace: &str,
grant: &Vec<ResourceWriter>,
revoke: &Vec<ResourceWriter>,
grant: &[ResourceWriter],
revoke: &[ResourceWriter],
) -> Result<()>
where
A: ConnectedAccount + Sync + Send + 'static,
Expand Down
1 change: 1 addition & 0 deletions crates/sozo/ops/src/migration/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ pub async fn update_manifests_and_abis(
Ok(())
}

// For now we juust handle writers, handling of owners might be added in the future
pub async fn find_authorization_diff<A>(
ui: &Ui,
world: &WorldContract<A>,
Expand Down
102 changes: 46 additions & 56 deletions crates/sozo/ops/src/tests/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ async fn call_with_bad_address() {
let provider = sequencer.provider();
let world_reader = WorldContractReader::new(world.address, provider);

assert!(
call::call(
world_reader,
"0xBadCoffeeBadCode".to_string(),
ENTRYPOINT.to_string(),
vec![Felt::ZERO, Felt::ZERO],
None
)
.await
.is_err()
);
assert!(call::call(
world_reader,
"0xBadCoffeeBadCode".to_string(),
ENTRYPOINT.to_string(),
vec![Felt::ZERO, Felt::ZERO],
None
)
.await
.is_err());
}

#[tokio::test]
Expand All @@ -44,17 +42,15 @@ async fn call_with_bad_name() {
let provider = sequencer.provider();
let world_reader = WorldContractReader::new(world.address, provider);

assert!(
call::call(
world_reader,
"BadName".to_string(),
ENTRYPOINT.to_string(),
vec![Felt::ZERO, Felt::ZERO],
None
)
.await
.is_err()
);
assert!(call::call(
world_reader,
"BadName".to_string(),
ENTRYPOINT.to_string(),
vec![Felt::ZERO, Felt::ZERO],
None
)
.await
.is_err());
}

#[tokio::test]
Expand All @@ -65,17 +61,15 @@ async fn call_with_bad_entrypoint() {
let provider = sequencer.provider();
let world_reader = WorldContractReader::new(world.address, provider);

assert!(
call::call(
world_reader,
CONTRACT_TAG.to_string(),
"BadEntryPoint".to_string(),
vec![Felt::ZERO, Felt::ZERO],
None
)
.await
.is_err()
);
assert!(call::call(
world_reader,
CONTRACT_TAG.to_string(),
"BadEntryPoint".to_string(),
vec![Felt::ZERO, Felt::ZERO],
None
)
.await
.is_err());
}

#[tokio::test]
Expand All @@ -86,17 +80,15 @@ async fn call_with_bad_calldata() {
let provider = sequencer.provider();
let world_reader = WorldContractReader::new(world.address, provider);

assert!(
call::call(
world_reader,
CONTRACT_TAG.to_string(),
ENTRYPOINT.to_string(),
vec![Felt::ZERO],
None
)
.await
.is_err()
);
assert!(call::call(
world_reader,
CONTRACT_TAG.to_string(),
ENTRYPOINT.to_string(),
vec![Felt::ZERO],
None
)
.await
.is_err());
}

#[tokio::test]
Expand Down Expand Up @@ -124,19 +116,17 @@ async fn call_with_contract_address() {

let contract_address = utils::get_contract_address::<
SingleOwnerAccount<JsonRpcClient<HttpTransport>, LocalWallet>,
>(&world, CONTRACT_TAG.to_string())
>(&world, CONTRACT_TAG)
.await
.unwrap();

assert!(
call::call(
world_reader,
format!("{:#x}", contract_address),
ENTRYPOINT.to_string(),
vec![],
None,
)
.await
.is_ok()
);
assert!(call::call(
world_reader,
format!("{:#x}", contract_address),
ENTRYPOINT.to_string(),
vec![],
None,
)
.await
.is_ok());
}
5 changes: 2 additions & 3 deletions crates/sozo/ops/src/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ async fn get_contract_address_from_world() {

let world = setup::setup(&sequencer).await.unwrap();

let contract_address =
utils::get_contract_address(&world, ACTION_CONTRACT_TAG.to_string()).await.unwrap();
let contract_address = utils::get_contract_address(&world, ACTION_CONTRACT_TAG).await.unwrap();

assert!(contract_address != Felt::ZERO);
}
Expand All @@ -28,7 +27,7 @@ async fn get_contract_address_from_string() {
let account = sequencer.account(0);
let world = WorldContract::new(Felt::ZERO, account);

let contract_address = utils::get_contract_address(&world, "0x1234".to_string()).await.unwrap();
let contract_address = utils::get_contract_address(&world, "0x1234").await.unwrap();

assert_eq!(contract_address, Felt::from_hex("0x1234").unwrap());
}
Expand Down
6 changes: 3 additions & 3 deletions crates/sozo/ops/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ use starknet::providers::Provider;
/// A [`Felt`] with the address of the contract on success.
pub async fn get_contract_address<A: ConnectedAccount + Sync>(
world: &WorldContract<A>,
tag_or_address: String,
tag_or_address: &str,
) -> Result<Felt> {
if tag_or_address.starts_with("0x") {
Felt::from_hex(&tag_or_address).map_err(anyhow::Error::from)
Felt::from_hex(tag_or_address).map_err(anyhow::Error::from)
} else {
let contract_class_hash = world.base().call().await?;
Ok(starknet::core::utils::get_contract_address(
generate_salt(&get_name_from_tag(&tag_or_address)),
generate_salt(&get_name_from_tag(tag_or_address)),
contract_class_hash.into(),
&[],
world.address,
Expand Down

0 comments on commit 3ba4859

Please sign in to comment.