diff --git a/Makefile b/Makefile index 549dbdb..1600c19 100644 --- a/Makefile +++ b/Makefile @@ -26,9 +26,9 @@ test: setup-test cd tests && cargo test clippy: - cd contracts && cargo clippy --bins -- -D warnings - cd contracts && cargo clippy --lib -- -D warnings - cd contracts && cargo clippy --lib --no-default-features -- -D warnings + cd contracts && cargo clippy --bins --target wasm32-unknown-unknown -- -D warnings + cd contracts && cargo clippy --lib --target wasm32-unknown-unknown -- -D warnings + cd contracts && cargo clippy --lib --target wasm32-unknown-unknown --no-default-features -- -D warnings cd tests && cargo clippy --all-targets -- -D warnings check-lint: clippy diff --git a/contracts/Cargo.toml b/contracts/Cargo.toml index d761515..9b555f8 100644 --- a/contracts/Cargo.toml +++ b/contracts/Cargo.toml @@ -10,7 +10,7 @@ readme = "../README.md" repository = "https://github.com/casper-ecosystem/cep-85" [workspace.dependencies] -casper-types = "4.0.1" +casper-types = "4.0.2" [profile.release] codegen-units = 1 diff --git a/contracts/cep85/src/balances.rs b/contracts/cep85/src/balances.rs index 0dc66cd..536f878 100644 --- a/contracts/cep85/src/balances.rs +++ b/contracts/cep85/src/balances.rs @@ -1,12 +1,4 @@ // //! Implementation of balances. - -use alloc::vec::Vec; -use casper_contract::{ - contract_api::runtime::{self, get_key}, - unwrap_or_revert::UnwrapOrRevert, -}; -use casper_types::{Key, U256}; - use crate::{ constants::{ARG_CONTRACT_HASH, DICT_BALANCES}, error::Cep85Error, @@ -14,6 +6,12 @@ use crate::{ get_dictionary_value_from_key, make_dictionary_item_key, set_dictionary_value_for_key, }, }; +use alloc::vec::Vec; +use casper_contract::{ + contract_api::runtime::{self, get_key}, + unwrap_or_revert::UnwrapOrRevert, +}; +use casper_types::{Key, U256}; /// Writes token balance of a specified account into a dictionary. pub fn write_balance_to(account: &Key, id: &U256, amount: &U256) { diff --git a/contracts/cep85/src/constants.rs b/contracts/cep85/src/constants.rs index a05ffcb..0fa5982 100644 --- a/contracts/cep85/src/constants.rs +++ b/contracts/cep85/src/constants.rs @@ -51,7 +51,6 @@ pub const ARG_OWNER: &str = "owner"; pub const ARG_PACKAGE_HASH: &str = "package_hash"; pub const ARG_RECIPIENT: &str = "recipient"; pub const ARG_SESSION_NAMED_KEY_NAME: &str = "session_named_key_name"; -pub const ARG_TOKEN_CONTRACT: &str = "token_contract"; pub const ARG_TOTAL_SUPPLIES: &str = "total_supplies"; pub const ARG_TOTAL_SUPPLY: &str = "total_supply"; pub const ARG_TO: &str = "to"; diff --git a/contracts/cep85/src/main.rs b/contracts/cep85/src/main.rs index 8d917ef..59b7a2c 100644 --- a/contracts/cep85/src/main.rs +++ b/contracts/cep85/src/main.rs @@ -8,8 +8,6 @@ compile_error!("target arch should be wasm32: compile with '--target wasm32-unkn // `no_std` environment. extern crate alloc; -use core::convert::TryInto; - use alloc::{ borrow::ToOwned, collections::BTreeMap, @@ -61,6 +59,7 @@ use cep85::{ make_dictionary_item_key as utils_make_dictionary_item_key, }, }; +use core::convert::TryInto; /// Initiates the contracts states. Only used by the installer call, /// later calls will cause it to revert. @@ -1057,14 +1056,7 @@ pub extern "C" fn upgrade() { record_event_dictionary(Event::Upgrade(Upgrade {})) } -fn install_contract() { - let name: String = get_named_arg_with_user_errors( - ARG_NAME, - Cep85Error::MissingCollectionName, - Cep85Error::InvalidCollectionName, - ) - .unwrap_or_revert(); - +fn install_contract(name: &str) { let uri: String = get_named_arg_with_user_errors( ARG_URI, Cep85Error::MissingUri, @@ -1097,7 +1089,7 @@ fn install_contract() { } let mut named_keys = NamedKeys::new(); - named_keys.insert(ARG_NAME.to_string(), storage::new_uref(name.clone()).into()); + named_keys.insert(ARG_NAME.to_string(), storage::new_uref(name).into()); named_keys.insert(ARG_URI.to_string(), storage::new_uref(uri).into()); named_keys.insert( ARG_EVENTS_MODE.to_string(), @@ -1242,18 +1234,24 @@ fn before_token_transfer( #[no_mangle] pub extern "C" fn call() { + let name: String = get_named_arg_with_user_errors( + ARG_NAME, + Cep85Error::MissingCollectionName, + Cep85Error::InvalidCollectionName, + ) + .unwrap_or_revert(); + let upgrade_flag: Option = get_optional_named_arg_with_user_errors(ARG_UPGRADE_FLAG, Cep85Error::InvalidUpgradeFlag); - if upgrade_flag.is_some() && upgrade_flag.unwrap() { - let name: String = - get_optional_named_arg_with_user_errors(ARG_NAME, Cep85Error::MissingCollectionName) - .unwrap_or_revert_with(Cep85Error::InvalidCollectionName); + let access_key = runtime::get_key(&format!("{PREFIX_ACCESS_KEY_NAME}_{name}")); + + if upgrade_flag.is_some() && upgrade_flag.unwrap() && access_key.is_some() { let contract_package_hash: Key = get_key(&format!("{PREFIX_CONTRACT_PACKAGE_NAME}_{}", name)) .unwrap_or_revert_with(Cep85Error::MissingPackageHash); upgrade_contract(&name, contract_package_hash) } else { - install_contract() + install_contract(&name) } } diff --git a/contracts/test/Cargo.toml b/contracts/test/Cargo.toml index 2927036..4bf65b8 100644 --- a/contracts/test/Cargo.toml +++ b/contracts/test/Cargo.toml @@ -9,7 +9,7 @@ repository.workspace = true [dependencies] cep85 = { path = "../cep85", default-features = false } -casper-contract = { version = "4.0.0", optional = true, default-features = false } +casper-contract = { version = "4.0.0", default-features = false } casper-types.workspace = true [[bin]] @@ -20,4 +20,4 @@ doctest = false test = false [features] -default = ["cep85/contract-support", "casper-contract/no-std-helpers"] +default = ["cep85/contract-support"] diff --git a/contracts/test/src/constants.rs b/contracts/test/src/constants.rs index 4e46946..754c106 100644 --- a/contracts/test/src/constants.rs +++ b/contracts/test/src/constants.rs @@ -1,8 +1,9 @@ pub const CEP85_TEST_CONTRACT_NAME: &str = "cep85_test_contract_hash"; -pub const CEP85_TEST_PACKAGE_NAME: &str = "cep85_test_contract_package_hash"; +pub const CEP85_TEST_CONTRACT_PACKAGE_NAME: &str = "cep85_test_contract_package_hash"; pub const RESULT_KEY: &str = "result"; pub const ARG_FILTER_CONTRACT_RETURN_VALUE: &str = "return_value"; +pub const ARG_TOKEN_CONTRACT: &str = "token_contract"; pub const ENTRY_POINT_CHECK_BALANCE_OF: &str = "check_balance_of"; pub const ENTRY_POINT_CHECK_BALANCE_OF_BATCH: &str = "check_balance_of_batch"; diff --git a/contracts/test/src/main.rs b/contracts/test/src/main.rs index 595914f..9914e96 100644 --- a/contracts/test/src/main.rs +++ b/contracts/test/src/main.rs @@ -28,24 +28,25 @@ use casper_types::{ use cep85::{ constants::{ ARG_ACCOUNT, ARG_ACCOUNTS, ARG_AMOUNTS, ARG_DATA, ARG_FROM, ARG_ID, ARG_IDS, ARG_OPERATOR, - ARG_OWNER, ARG_TO, ARG_TOKEN_CONTRACT, ENTRY_POINT_BALANCE_OF, - ENTRY_POINT_BALANCE_OF_BATCH, ENTRY_POINT_BATCH_BURN, ENTRY_POINT_BATCH_TRANSFER_FROM, - ENTRY_POINT_BURN, ENTRY_POINT_INIT, ENTRY_POINT_IS_APPROVED_FOR_ALL, - ENTRY_POINT_IS_NON_FUNGIBLE, ENTRY_POINT_SUPPLY_OF, ENTRY_POINT_SUPPLY_OF_BATCH, - ENTRY_POINT_TOTAL_FUNGIBLE_SUPPLY, ENTRY_POINT_TOTAL_SUPPLY_OF, - ENTRY_POINT_TOTAL_SUPPLY_OF_BATCH, ENTRY_POINT_TRANSFER_FROM, ENTRY_POINT_URI, + ARG_OWNER, ARG_TO, ENTRY_POINT_BALANCE_OF, ENTRY_POINT_BALANCE_OF_BATCH, + ENTRY_POINT_BATCH_BURN, ENTRY_POINT_BATCH_TRANSFER_FROM, ENTRY_POINT_BURN, + ENTRY_POINT_INIT, ENTRY_POINT_IS_APPROVED_FOR_ALL, ENTRY_POINT_IS_NON_FUNGIBLE, + ENTRY_POINT_SUPPLY_OF, ENTRY_POINT_SUPPLY_OF_BATCH, ENTRY_POINT_TOTAL_FUNGIBLE_SUPPLY, + ENTRY_POINT_TOTAL_SUPPLY_OF, ENTRY_POINT_TOTAL_SUPPLY_OF_BATCH, ENTRY_POINT_TRANSFER_FROM, + ENTRY_POINT_URI, }, modalities::TransferFilterContractResult, }; use constants::{ - ARG_FILTER_CONTRACT_RETURN_VALUE, CEP85_TEST_CONTRACT_NAME, CEP85_TEST_PACKAGE_NAME, - ENTRY_POINT_CHECK_BALANCE_OF, ENTRY_POINT_CHECK_BALANCE_OF_BATCH, - ENTRY_POINT_CHECK_BATCH_TRANSFER_FROM, ENTRY_POINT_CHECK_IS_APPROVED_FOR_ALL, - ENTRY_POINT_CHECK_IS_NON_FUNGIBLE, ENTRY_POINT_CHECK_SUPPLY_OF, - ENTRY_POINT_CHECK_SUPPLY_OF_BATCH, ENTRY_POINT_CHECK_TOTAL_FUNGIBLE_SUPPLY, - ENTRY_POINT_CHECK_TOTAL_SUPPLY_OF, ENTRY_POINT_CHECK_TOTAL_SUPPLY_OF_BATCH, - ENTRY_POINT_CHECK_TRANSFER_FROM, ENTRY_POINT_CHECK_URI, - ENTRY_POINT_SET_FILTER_CONTRACT_RETURN_VALUE, ENTRY_POINT_TRANSFER_FILTER_METHOD, + ARG_FILTER_CONTRACT_RETURN_VALUE, ARG_TOKEN_CONTRACT, CEP85_TEST_CONTRACT_NAME, + CEP85_TEST_CONTRACT_PACKAGE_NAME, ENTRY_POINT_CHECK_BALANCE_OF, + ENTRY_POINT_CHECK_BALANCE_OF_BATCH, ENTRY_POINT_CHECK_BATCH_TRANSFER_FROM, + ENTRY_POINT_CHECK_IS_APPROVED_FOR_ALL, ENTRY_POINT_CHECK_IS_NON_FUNGIBLE, + ENTRY_POINT_CHECK_SUPPLY_OF, ENTRY_POINT_CHECK_SUPPLY_OF_BATCH, + ENTRY_POINT_CHECK_TOTAL_FUNGIBLE_SUPPLY, ENTRY_POINT_CHECK_TOTAL_SUPPLY_OF, + ENTRY_POINT_CHECK_TOTAL_SUPPLY_OF_BATCH, ENTRY_POINT_CHECK_TRANSFER_FROM, + ENTRY_POINT_CHECK_URI, ENTRY_POINT_SET_FILTER_CONTRACT_RETURN_VALUE, + ENTRY_POINT_TRANSFER_FILTER_METHOD, }; use utils::{get_token_contract, store_result}; @@ -68,7 +69,7 @@ pub extern "C" fn set_filter_contract_return_value() { pub extern "C" fn can_transfer() { let _operator: Key = get_named_arg(ARG_OPERATOR); let _from: Key = get_named_arg(ARG_FROM); - let _to: Key = get_named_arg(ARG_FROM); + let _to: Key = get_named_arg(ARG_TO); let _ids: Vec = get_named_arg(ARG_IDS); let _amounts: Vec = get_named_arg(ARG_AMOUNTS); let _data: Option = get_named_arg(ARG_DATA); @@ -513,7 +514,7 @@ pub extern "C" fn call() { let (contract_hash, _version) = storage::new_contract( entry_points, None, - Some(CEP85_TEST_PACKAGE_NAME.to_string()), + Some(CEP85_TEST_CONTRACT_PACKAGE_NAME.to_string()), None, ); diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 55916d2..528a20a 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -8,7 +8,7 @@ cep85 = { path = "../contracts/cep85", default-features = false } cep85-test-contract = { path = "../contracts/test", default-features = false } casper-engine-test-support = "7.0.1" casper-execution-engine = "7.0.1" -casper-types = "4.0.1" +casper-types = "4.0.2" casper-event-standard = { version = "0.5.0", default-features = false } [lib] diff --git a/tests/src/filter.rs b/tests/src/filter.rs index e4cf3e7..50f00ed 100644 --- a/tests/src/filter.rs +++ b/tests/src/filter.rs @@ -10,15 +10,16 @@ use casper_types::{ }; use cep85::{ constants::{ - ARG_DATA, ARG_FROM, ARG_NAME, ARG_TOKEN_CONTRACT, ARG_TRANSFER_FILTER_CONTRACT, - ARG_TRANSFER_FILTER_METHOD, ARG_URI, ENTRY_POINT_INIT, ENTRY_POINT_TRANSFER_FROM, + ARG_DATA, ARG_FROM, ARG_NAME, ARG_TRANSFER_FILTER_CONTRACT, ARG_TRANSFER_FILTER_METHOD, + ARG_URI, ENTRY_POINT_INIT, ENTRY_POINT_TRANSFER_FROM, }, error::Cep85Error, modalities::TransferFilterContractResult, }; use cep85_test_contract::constants::{ - ARG_FILTER_CONTRACT_RETURN_VALUE, CEP85_TEST_CONTRACT_NAME, CEP85_TEST_PACKAGE_NAME, - ENTRY_POINT_SET_FILTER_CONTRACT_RETURN_VALUE, ENTRY_POINT_TRANSFER_FILTER_METHOD, + ARG_FILTER_CONTRACT_RETURN_VALUE, ARG_TOKEN_CONTRACT, CEP85_TEST_CONTRACT_NAME, + CEP85_TEST_CONTRACT_PACKAGE_NAME, ENTRY_POINT_SET_FILTER_CONTRACT_RETURN_VALUE, + ENTRY_POINT_TRANSFER_FILTER_METHOD, }; use crate::utility::{ @@ -98,7 +99,7 @@ fn check_transfers_with_transfer_filter_contract() { let cep85_test_contract_package = account .named_keys() - .get(CEP85_TEST_PACKAGE_NAME) + .get(CEP85_TEST_CONTRACT_PACKAGE_NAME) .and_then(|key| key.into_hash()) .map(ContractPackageHash::new) .expect("should have contract package hash"); diff --git a/tests/src/utility/installer_request_builders.rs b/tests/src/utility/installer_request_builders.rs index 4072c89..66a28e6 100644 --- a/tests/src/utility/installer_request_builders.rs +++ b/tests/src/utility/installer_request_builders.rs @@ -20,8 +20,8 @@ use cep85::{ constants::{ ADMIN_LIST, ARG_ACCOUNT, ARG_ACCOUNTS, ARG_AMOUNTS, ARG_APPROVED, ARG_DATA, ARG_ENABLE_BURN, ARG_EVENTS_MODE, ARG_FROM, ARG_IDS, ARG_NAME, ARG_OPERATOR, ARG_OWNER, - ARG_RECIPIENT, ARG_SESSION_NAMED_KEY_NAME, ARG_TOKEN_CONTRACT, ARG_TOTAL_SUPPLIES, - ARG_TOTAL_SUPPLY, ARG_URI, BURNER_LIST, ENTRY_POINT_BATCH_BURN, ENTRY_POINT_BATCH_MINT, + ARG_RECIPIENT, ARG_SESSION_NAMED_KEY_NAME, ARG_TOTAL_SUPPLIES, ARG_TOTAL_SUPPLY, ARG_URI, + BURNER_LIST, ENTRY_POINT_BATCH_BURN, ENTRY_POINT_BATCH_MINT, ENTRY_POINT_BATCH_TRANSFER_FROM, ENTRY_POINT_BURN, ENTRY_POINT_CHANGE_SECURITY, ENTRY_POINT_MAKE_DICTIONARY_ITEM_KEY, ENTRY_POINT_MINT, ENTRY_POINT_SET_APPROVAL_FOR_ALL, ENTRY_POINT_SET_MODALITIES, ENTRY_POINT_SET_TOTAL_SUPPLY_OF, @@ -31,13 +31,13 @@ use cep85::{ modalities::EventsMode, }; use cep85_test_contract::constants::{ - CEP85_TEST_CONTRACT_NAME, CEP85_TEST_PACKAGE_NAME, ENTRY_POINT_CHECK_BALANCE_OF, - ENTRY_POINT_CHECK_BALANCE_OF_BATCH, ENTRY_POINT_CHECK_BATCH_TRANSFER_FROM, - ENTRY_POINT_CHECK_IS_APPROVED_FOR_ALL, ENTRY_POINT_CHECK_IS_NON_FUNGIBLE, - ENTRY_POINT_CHECK_SUPPLY_OF, ENTRY_POINT_CHECK_SUPPLY_OF_BATCH, - ENTRY_POINT_CHECK_TOTAL_FUNGIBLE_SUPPLY, ENTRY_POINT_CHECK_TOTAL_SUPPLY_OF, - ENTRY_POINT_CHECK_TOTAL_SUPPLY_OF_BATCH, ENTRY_POINT_CHECK_TRANSFER_FROM, - ENTRY_POINT_CHECK_URI, RESULT_KEY, + ARG_TOKEN_CONTRACT, CEP85_TEST_CONTRACT_NAME, CEP85_TEST_CONTRACT_PACKAGE_NAME, + ENTRY_POINT_CHECK_BALANCE_OF, ENTRY_POINT_CHECK_BALANCE_OF_BATCH, + ENTRY_POINT_CHECK_BATCH_TRANSFER_FROM, ENTRY_POINT_CHECK_IS_APPROVED_FOR_ALL, + ENTRY_POINT_CHECK_IS_NON_FUNGIBLE, ENTRY_POINT_CHECK_SUPPLY_OF, + ENTRY_POINT_CHECK_SUPPLY_OF_BATCH, ENTRY_POINT_CHECK_TOTAL_FUNGIBLE_SUPPLY, + ENTRY_POINT_CHECK_TOTAL_SUPPLY_OF, ENTRY_POINT_CHECK_TOTAL_SUPPLY_OF_BATCH, + ENTRY_POINT_CHECK_TRANSFER_FROM, ENTRY_POINT_CHECK_URI, RESULT_KEY, }; use std::collections::HashMap; @@ -130,7 +130,7 @@ pub fn setup_with_args( let cep85_test_contract_package = account .named_keys() - .get(CEP85_TEST_PACKAGE_NAME) + .get(CEP85_TEST_CONTRACT_PACKAGE_NAME) .and_then(|key| key.into_hash()) .map(ContractPackageHash::new) .expect("should have contract package hash");