Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "meta-names-contract"
version = "0.5.0"
version = "0.6.0"
authors = ["Yeboster"]
edition = "2021"

Expand Down
28 changes: 28 additions & 0 deletions contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub fn transfer_from(
to: Address,
token_id: u128,
) -> (ContractState, Vec<EventGroup>) {
assert_contract_enabled(&state);

let mut state = state;
let mut nft_events = nft_actions::execute_transfer_from(
&ctx,
Expand All @@ -94,6 +96,8 @@ pub fn transfer_domain(
to: Address,
domain: String,
) -> (ContractState, Vec<EventGroup>) {
assert_contract_enabled(&state);

let token_id = state.pns.get_token_id(&domain);
assert!(token_id.is_some(), "{}", ContractError::DomainNotMinted);

Expand All @@ -107,6 +111,8 @@ pub fn approve(
approved: Option<Address>,
token_id: u128,
) -> (ContractState, Vec<EventGroup>) {
assert_contract_enabled(&state);

let mut state = state;
let events = nft_actions::execute_approve(
&ctx,
Expand All @@ -124,6 +130,8 @@ pub fn approve_domain(
approved: Option<Address>,
domain: String,
) -> (ContractState, Vec<EventGroup>) {
assert_contract_enabled(&state);

assert!(
state.pns.is_minted(&domain),
"{}",
Expand All @@ -142,6 +150,8 @@ pub fn set_approval_for_all(
operator: Address,
approved: bool,
) -> (ContractState, Vec<EventGroup>) {
assert_contract_enabled(&state);

let mut state = state;
let events = nft_actions::execute_set_approval_for_all(
&ctx,
Expand All @@ -161,6 +171,8 @@ pub fn mint(
token_uri: Option<String>,
parent_id: Option<String>,
) -> (ContractState, Vec<EventGroup>) {
assert_contract_enabled(&state);

// Basic validations
assert!(!state.pns.is_minted(&domain), "{}", ContractError::Minted);

Expand Down Expand Up @@ -221,6 +233,8 @@ pub fn on_mint_callback(
state: ContractState,
msg: MintMsg,
) -> (ContractState, Vec<EventGroup>) {
assert_contract_enabled(&state);

assert_callback_success(&callback_ctx);

action_mint(ctx, state, msg.domain, msg.to, msg.token_uri, msg.parent_id)
Expand All @@ -234,6 +248,8 @@ pub fn mint_record(
class: RecordClass,
data: Vec<u8>,
) -> (ContractState, Vec<EventGroup>) {
assert_contract_enabled(&state);

let mut state = state;
let events = pns_actions::execute_record_mint(
&ctx,
Expand All @@ -256,6 +272,8 @@ pub fn update_record(
class: RecordClass,
data: Vec<u8>,
) -> (ContractState, Vec<EventGroup>) {
assert_contract_enabled(&state);

let mut state = state;
let events = pns_actions::execute_record_update(
&ctx,
Expand All @@ -277,6 +295,8 @@ pub fn delete_record(
domain: String,
class: RecordClass,
) -> (ContractState, Vec<EventGroup>) {
assert_contract_enabled(&state);

let mut state = state;
let events = pns_actions::execute_record_delete(
&ctx,
Expand Down Expand Up @@ -333,3 +353,11 @@ pub fn update_config(

(state, vec![])
}

fn assert_contract_enabled(state: &ContractState) {
assert!(
state.config.contract_enabled,
"{}",
ContractError::ContractDisabled
);
}
3 changes: 3 additions & 0 deletions contract/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use thiserror::Error;
/// This enum describes nft contract errors
#[derive(Error, Debug)]
pub enum ContractError {
#[error("The contract is disabled")]
ContractDisabled,

#[error("The specified domain is not minted")]
DomainNotMinted,

Expand Down
3 changes: 2 additions & 1 deletion contract/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ pub enum UserRole {

#[derive(ReadWriteRPC, ReadWriteState, CreateTypeSpec, PartialEq, Eq, Default, Clone, Debug)]
pub struct ContractConfig {
pub whitelist_enabled: bool,
pub contract_enabled: bool,
pub mint_count_limit_enabled: bool,
pub mint_count_limit: u32,
pub payable_mint_info: PayableMintInfo,
pub whitelist_enabled: bool,
}

#[derive(ReadWriteState, CreateTypeSpec, PartialEq, Eq, Default, Clone, Debug)]
Expand Down
7 changes: 7 additions & 0 deletions contract/tests/cucumber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn get_address_for_user(user: String) -> u8 {
match user.as_str() {
"Alice" => ALICE_ADDRESS,
"Bob" => BOB_ADDRESS,
"contract" => SYSTEM_ADDRESS,
_ => panic!("Unknown user"),
}
}
Expand Down Expand Up @@ -62,6 +63,7 @@ fn get_record_class_given(class: String) -> RecordClass {
#[given(regex = "a meta names contract")]
fn meta_names_contract(world: &mut ContractWorld) {
let config = ContractConfig {
contract_enabled: true,
payable_mint_info: PayableMintInfo {
token: Some(mock_address(PAYABLE_TOKEN_ADDRESS)),
receiver: Some(mock_address(ALICE_ADDRESS)),
Expand All @@ -86,6 +88,11 @@ fn meta_names_contract(world: &mut ContractWorld) {
fn update_contract_config(world: &mut ContractWorld, user: String, key: String, value: String) {
let res = catch_unwind(|| {
let new_config = match key.as_str() {
"contract_enabled" => {
let mut new_config = world.state.config.clone();
new_config.contract_enabled = value == "true";
new_config
}
"whitelist_enabled" => {
let mut new_config = world.state.config.clone();
new_config.whitelist_enabled = value == "true";
Expand Down
7 changes: 7 additions & 0 deletions contract/tests/features/mint.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ Feature: Mint feature
When Alice mints 'meta.name' domain without fees and a parent
Then Alice owns 'meta.name' domain

Scenario: The mint does not occur when the contract is disabled
Given a meta names contract
And Alice user with the admin role
And contract config 'contract_enabled' is 'false'
When Alice mints 'meta.name' domain without a parent
Then 'meta.name' domain is not minted

Scenario: The minting process of a domain without any parent, carried out by an administrator user, is executed correctly
Given a meta names contract
And Alice user with the admin role
Expand Down