Skip to content

Commit

Permalink
Small fixes + update
Browse files Browse the repository at this point in the history
  • Loading branch information
gRoussac committed Nov 23, 2024
1 parent edd2b48 commit 36d5f80
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 64 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions contracts/cep85/src/balances.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
// //! 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,
utils::{
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) {
Expand Down
1 change: 0 additions & 1 deletion contracts/cep85/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
30 changes: 14 additions & 16 deletions contracts/cep85/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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<bool> =
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)
}
}
4 changes: 2 additions & 2 deletions contracts/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand All @@ -20,4 +20,4 @@ doctest = false
test = false

[features]
default = ["cep85/contract-support", "casper-contract/no-std-helpers"]
default = ["cep85/contract-support"]
3 changes: 2 additions & 1 deletion contracts/test/src/constants.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
33 changes: 17 additions & 16 deletions contracts/test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<U256> = get_named_arg(ARG_IDS);
let _amounts: Vec<U256> = get_named_arg(ARG_AMOUNTS);
let _data: Option<Bytes> = get_named_arg(ARG_DATA);
Expand Down Expand Up @@ -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,
);

Expand Down
2 changes: 1 addition & 1 deletion tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 6 additions & 5 deletions tests/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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");
Expand Down
20 changes: 10 additions & 10 deletions tests/src/utility/installer_request_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 36d5f80

Please sign in to comment.