Skip to content

Commit

Permalink
feat: anyone can create series
Browse files Browse the repository at this point in the history
  • Loading branch information
emarai committed Sep 2, 2021
1 parent 27229aa commit ac68fa1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 82 deletions.
94 changes: 30 additions & 64 deletions paras-nft-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,43 +131,43 @@ impl Contract {
#[payable]
pub fn nft_create_series(
&mut self,
token_series_id: U128,
token_metadata: TokenMetadata,
creator_id: ValidAccountId,
price: Option<U128>,
royalty: Option<HashMap<AccountId, u32>>,
) -> TokenSeriesJson {
let initial_storage_usage = env::storage_usage();
let owner_id = env::predecessor_account_id();
assert_eq!(
owner_id, self.tokens.owner_id,
"Paras: Only owner can set series"
);
let creator_id = env::predecessor_account_id();

let token_series_id: String = format!("{}", token_series_id.0);
let token_series_id = format!("{}", (self.token_series_by_id.len() + 1));

assert!(
self.token_series_by_id.get(&token_series_id).is_none(),
"Paras: duplicate token_series_id"
);

let title = token_metadata.title.clone();
assert!(title.is_some(), "token_metadata.title is required");
assert!(title.is_some(), "Paras: token_metadata.title is required");


let mut total_perpetual = 0;
let mut total_accounts = 0;
let royalty_res = if let Some(royalty) = royalty {
for (_, v) in royalty.iter() {
total_perpetual += *v;
total_accounts += 1;
}
royalty
} else {
HashMap::new()
};

assert!(total_accounts <= 10, "Paras: royalty exceeds 10 accounts");

assert!(
total_perpetual <= 9000,
"Exceeds maximum royalty : 9000",
"Paras Exceeds maximum royalty -> 9000",
);

let price_res: Option<u128> = if price.is_some() {
Some(price.unwrap().0)
} else {
Expand Down Expand Up @@ -899,13 +899,11 @@ mod tests {

fn create_series(
contract: &mut Contract,
token_series_id: U128,
royalty: &HashMap<AccountId, u32>,
price: Option<U128>,
copies: Option<u64>,
) {
contract.nft_create_series(
token_series_id,
TokenMetadata {
title: Some("Tsundere land".to_string()),
description: None,
Expand All @@ -924,7 +922,6 @@ mod tests {
),
reference_hash: None,
},
accounts(1),
price,
Some(royalty.clone()),
);
Expand All @@ -934,7 +931,7 @@ mod tests {
fn test_create_series() {
let (mut context, mut contract) = setup_contract();
testing_env!(context
.predecessor_account_id(accounts(0))
.predecessor_account_id(accounts(1))
.attached_deposit(STORAGE_FOR_CREATE_SERIES)
.build()
);
Expand All @@ -943,7 +940,6 @@ mod tests {
royalty.insert(accounts(1).to_string(), 1000);
create_series(
&mut contract,
U128::from(1),
&royalty,
Some(U128::from(1 * 10u128.pow(24))),
None
Expand Down Expand Up @@ -978,43 +974,14 @@ mod tests {
nft_series_return.metadata.reference.unwrap(),
"bafybeicg4ss7qh5odijfn2eogizuxkrdh3zlv4eftcmgnljwu7dm64uwji".to_string()
);
}

#[test]
#[should_panic(expected = "Paras: duplicate token_series_id")]
fn test_invalid_duplicate_token_series() {
let (mut context, mut contract) = setup_contract();
testing_env!(context
.predecessor_account_id(accounts(0))
.attached_deposit(STORAGE_FOR_CREATE_SERIES)
.build()
);

let mut royalty: HashMap<AccountId, u32> = HashMap::new();
royalty.insert(accounts(1).to_string(), 1000);

create_series(
&mut contract,
U128::from(1),
&royalty,
Some(U128::from(1 * 10u128.pow(24))),
None
);

create_series(
&mut contract,
U128::from(1),
&royalty,
Some(U128::from(1 * 10u128.pow(24))),
None
);
}

#[test]
fn test_buy() {
let (mut context, mut contract) = setup_contract();
testing_env!(context
.predecessor_account_id(accounts(0))
.predecessor_account_id(accounts(1))
.attached_deposit(STORAGE_FOR_CREATE_SERIES)
.build()
);
Expand All @@ -1024,7 +991,6 @@ mod tests {

create_series(
&mut contract,
U128::from(1),
&royalty,
Some(U128::from(1 * 10u128.pow(24))),
None
Expand All @@ -1049,15 +1015,15 @@ mod tests {
fn test_mint() {
let (mut context, mut contract) = setup_contract();
testing_env!(context
.predecessor_account_id(accounts(0))
.predecessor_account_id(accounts(1))
.attached_deposit(STORAGE_FOR_CREATE_SERIES)
.build()
);

let mut royalty: HashMap<AccountId, u32> = HashMap::new();
royalty.insert(accounts(1).to_string(), 1000);

create_series(&mut contract, U128::from(1), &royalty, None, None);
create_series(&mut contract, &royalty, None, None);

testing_env!(context
.predecessor_account_id(accounts(1))
Expand All @@ -1079,15 +1045,15 @@ mod tests {
fn test_invalid_mint_non_mintable() {
let (mut context, mut contract) = setup_contract();
testing_env!(context
.predecessor_account_id(accounts(0))
.predecessor_account_id(accounts(1))
.attached_deposit(STORAGE_FOR_CREATE_SERIES)
.build()
);

let mut royalty: HashMap<AccountId, u32> = HashMap::new();
royalty.insert(accounts(1).to_string(), 1000);

create_series(&mut contract, U128::from(1), &royalty, None, None);
create_series(&mut contract, &royalty, None, None);

testing_env!(context
.predecessor_account_id(accounts(1))
Expand All @@ -1110,15 +1076,15 @@ mod tests {
fn test_invalid_mint_above_copies() {
let (mut context, mut contract) = setup_contract();
testing_env!(context
.predecessor_account_id(accounts(0))
.predecessor_account_id(accounts(1))
.attached_deposit(STORAGE_FOR_CREATE_SERIES)
.build()
);

let mut royalty: HashMap<AccountId, u32> = HashMap::new();
royalty.insert(accounts(1).to_string(), 1000);

create_series(&mut contract, U128::from(1), &royalty, None, Some(1));
create_series(&mut contract, &royalty, None, Some(1));

testing_env!(context
.predecessor_account_id(accounts(1))
Expand All @@ -1134,15 +1100,15 @@ mod tests {
fn test_decrease_copies() {
let (mut context, mut contract) = setup_contract();
testing_env!(context
.predecessor_account_id(accounts(0))
.predecessor_account_id(accounts(1))
.attached_deposit(STORAGE_FOR_CREATE_SERIES)
.build()
);

let mut royalty: HashMap<AccountId, u32> = HashMap::new();
royalty.insert(accounts(1).to_string(), 1000);

create_series(&mut contract, U128::from(1), &royalty, None, Some(5));
create_series(&mut contract, &royalty, None, Some(5));

testing_env!(context
.predecessor_account_id(accounts(1))
Expand All @@ -1167,15 +1133,15 @@ mod tests {
fn test_invalid_decrease_copies() {
let (mut context, mut contract) = setup_contract();
testing_env!(context
.predecessor_account_id(accounts(0))
.predecessor_account_id(accounts(1))
.attached_deposit(STORAGE_FOR_CREATE_SERIES)
.build()
);

let mut royalty: HashMap<AccountId, u32> = HashMap::new();
royalty.insert(accounts(1).to_string(), 1000);

create_series(&mut contract, U128::from(1), &royalty, None, Some(5));
create_series(&mut contract, &royalty, None, Some(5));

testing_env!(context
.predecessor_account_id(accounts(1))
Expand All @@ -1200,15 +1166,15 @@ mod tests {
fn test_invalid_buy_price_null() {
let (mut context, mut contract) = setup_contract();
testing_env!(context
.predecessor_account_id(accounts(0))
.predecessor_account_id(accounts(1))
.attached_deposit(STORAGE_FOR_CREATE_SERIES)
.build()
);

let mut royalty: HashMap<AccountId, u32> = HashMap::new();
royalty.insert(accounts(1).to_string(), 1000);

create_series(&mut contract, U128::from(1), &royalty, None, None);
create_series(&mut contract, &royalty, None, None);

testing_env!(context
.predecessor_account_id(accounts(2))
Expand All @@ -1229,15 +1195,15 @@ mod tests {
fn test_nft_burn() {
let (mut context, mut contract) = setup_contract();
testing_env!(context
.predecessor_account_id(accounts(0))
.predecessor_account_id(accounts(1))
.attached_deposit(STORAGE_FOR_CREATE_SERIES)
.build()
);

let mut royalty: HashMap<AccountId, u32> = HashMap::new();
royalty.insert(accounts(1).to_string(), 1000);

create_series(&mut contract, U128::from(1), &royalty, None, None);
create_series(&mut contract, &royalty, None, None);

testing_env!(context
.predecessor_account_id(accounts(1))
Expand All @@ -1262,15 +1228,15 @@ mod tests {
fn test_nft_transfer() {
let (mut context, mut contract) = setup_contract();
testing_env!(context
.predecessor_account_id(accounts(0))
.predecessor_account_id(accounts(1))
.attached_deposit(STORAGE_FOR_CREATE_SERIES)
.build()
);

let mut royalty: HashMap<AccountId, u32> = HashMap::new();
royalty.insert(accounts(1).to_string(), 1000);

create_series(&mut contract, U128::from(1), &royalty, None, None);
create_series(&mut contract, &royalty, None, None);

testing_env!(context
.predecessor_account_id(accounts(1))
Expand Down Expand Up @@ -1299,15 +1265,15 @@ mod tests {
fn test_nft_transfer_payout() {
let (mut context, mut contract) = setup_contract();
testing_env!(context
.predecessor_account_id(accounts(0))
.predecessor_account_id(accounts(1))
.attached_deposit(STORAGE_FOR_CREATE_SERIES)
.build()
);

let mut royalty: HashMap<AccountId, u32> = HashMap::new();
royalty.insert(accounts(1).to_string(), 1000);

create_series(&mut contract, U128::from(1), &royalty, None, None);
create_series(&mut contract, &royalty, None, None);

testing_env!(context
.predecessor_account_id(accounts(1))
Expand Down
Loading

0 comments on commit ac68fa1

Please sign in to comment.