git.delta.rocks / unique-network / refs/commits / b683ad2823f9

difftreelog

Add sponsorship methods + transfer refactoring

str-mv2020-08-04parent: #87253dd.patch.diff
in: master

1 file changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
44 NFT(u32),44 NFT(u32),
45 // decimal points45 // decimal points
46 Fungible(u32),46 Fungible(u32),
47 // custom data size47 // custom data size and decimal points
48 ReFungible(u32),48 ReFungible(u32, u32),
49}49}
5050
51impl Into<u8> for CollectionMode {51impl Into<u8> for CollectionMode {
54 CollectionMode::Invalid => 0,54 CollectionMode::Invalid => 0,
55 CollectionMode::NFT(_) => 1,55 CollectionMode::NFT(_) => 1,
56 CollectionMode::Fungible(_) => 2,56 CollectionMode::Fungible(_) => 2,
57 CollectionMode::ReFungible(_) => 3,57 CollectionMode::ReFungible(_, _) => 3,
58 }58 }
59 }59 }
60}60}
150150
151 pub AddressTokens get(fn address_tokens): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) T::AccountId => Vec<u64>;151 pub AddressTokens get(fn address_tokens): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) T::AccountId => Vec<u64>;
152
153 // Sponsorship
154 pub ContractSponsor get(fn contract_sponsor): map hasher(identity) T::AccountId => T::AccountId;
155 pub UnconfirmedContractSponsor get(fn unconfirmed_contract_sponsor): map hasher(identity) T::AccountId => T::AccountId;
152 }156 }
153}157}
154158
183 let who = ensure_signed(origin)?;187 let who = ensure_signed(origin)?;
184 let custom_data_size = match mode {188 let custom_data_size = match mode {
185 CollectionMode::NFT(size) => size,189 CollectionMode::NFT(size) => size,
186 CollectionMode::ReFungible(size) => size,190 CollectionMode::ReFungible(size, _) => size,
187 _ => 0191 _ => 0
188 };192 };
189193
190 let decimal_points = match mode {194 let decimal_points = match mode {
191 CollectionMode::Fungible(points) => points,195 CollectionMode::Fungible(points) => points,
196 CollectionMode::ReFungible(_, points) => points,
192 _ => 0197 _ => 0
193 };198 };
194199
377 Self::add_nft_item(item)?;382 Self::add_nft_item(item)?;
378 383
379 },384 },
380 CollectionMode::ReFungible(_) => {385 CollectionMode::ReFungible(_, _) => {
381 let mut owner_list = Vec::new();386 let mut owner_list = Vec::new();
382 let value = (10 as u128).pow(target_collection.decimal_points);387 let value = (10 as u128).pow(target_collection.decimal_points);
383 owner_list.push(Ownership {owner: owner, fraction: value});388 owner_list.push(Ownership {owner: owner, fraction: value});
413 match target_collection.mode 418 match target_collection.mode
414 {419 {
415 CollectionMode::NFT(_) => Self::burn_nft_item(collection_id, item_id)?,420 CollectionMode::NFT(_) => Self::burn_nft_item(collection_id, item_id)?,
416 CollectionMode::ReFungible(_) => Self::burn_refungible_item(collection_id, item_id, sender.clone())?,421 CollectionMode::ReFungible(_, _) => Self::burn_refungible_item(collection_id, item_id, sender.clone())?,
417 _ => ()422 _ => ()
418 };423 };
419424
427 pub fn transfer(origin, recipient: T::AccountId, collection_id: u64, item_id: u64, value: u64) -> DispatchResult {432 pub fn transfer(origin, recipient: T::AccountId, collection_id: u64, item_id: u64, value: u64) -> DispatchResult {
428433
429 let sender = ensure_signed(origin)?;434 let sender = ensure_signed(origin)?;
430 let item_owner = Self::is_item_owner(sender.clone(), collection_id, item_id);435 ensure!(Self::is_item_owner(sender.clone(), collection_id, item_id), "Only item owner can call transfer method");
431 if !item_owner
432 {
433 Self::check_owner_or_admin_permissions(collection_id, sender.clone())?;
434 }
435436
436 let target_collection = <Collection<T>>::get(collection_id);437 let target_collection = <Collection<T>>::get(collection_id);
437438
438 // TODO: implement other modes439 // TODO: implement other modes
439 match target_collection.mode 440 match target_collection.mode
440 {441 {
441 CollectionMode::NFT(_) => Self::transfer_nft(collection_id, item_id, recipient)?,442 CollectionMode::NFT(_) => Self::transfer_nft(collection_id, item_id, sender.clone(), recipient)?,
442 CollectionMode::ReFungible(_) => Self::transfer_refungible(collection_id, item_id, value, sender.clone(), recipient)?,443 CollectionMode::ReFungible(_, _) => Self::transfer_refungible(collection_id, item_id, value, sender.clone(), recipient)?,
443 _ => ()444 _ => ()
444 };445 };
445446
477 }478 }
478479
479 #[weight = 0]480 #[weight = 0]
480 pub fn transfer_from(origin, recipient: T::AccountId, collection_id: u64, item_id: u64, ) -> DispatchResult {481 pub fn transfer_from(origin, from: T::AccountId, recipient: T::AccountId, collection_id: u64, item_id: u64, value: u64 ) -> DispatchResult {
481482
482 let mut approved: bool = false; 483 let mut approved: bool = false;
483 let sender = ensure_signed(origin)?;484 let sender = ensure_signed(origin)?;
497498
498 match target_collection.mode499 match target_collection.mode
499 {500 {
500 CollectionMode::NFT(_) => Self::transfer_nft(collection_id, item_id, recipient)?,501 CollectionMode::NFT(_) => Self::transfer_nft(collection_id, item_id, from, recipient)?,
502 CollectionMode::ReFungible(_, _) => Self::transfer_refungible(collection_id, item_id, value, from, recipient)?,
501 // TODO: implement other modes503 // TODO: implement other modes
502 _ => ()504 _ => ()
503 };505 };
536 Ok(()) 538 Ok(())
537 }539 }
540
541 // Shonsorship methods
542 #[weight = 0]
543 pub fn set_collection_sponsor(
544 origin,
545 collection_id: u64,
546 address: T::AccountId
547 ) -> DispatchResult {
548 let sender = ensure_signed(origin)?;
549 Self::check_owner_permissions(collection_id, sender)?;
550 let mut collection = <Collection<T>>::get(collection_id);
551 collection.unconfirmed_sponsor = address;
552 <Collection<T>>::insert(collection_id, collection);
553
554 Ok(())
555 }
556
557 #[weight = 0]
558 pub fn confirm_sponsorship(
559 origin,
560 collection_id: u64,
561 address: T::AccountId
562 ) -> DispatchResult {
563 let sender = ensure_signed(origin)?;
564 let mut collection = <Collection<T>>::get(collection_id);
565
566 ensure!(collection.unconfirmed_sponsor == sender, "Only sponsor can confirm sponsorship");
567
568 collection.sponsor = collection.unconfirmed_sponsor;
569 collection.unconfirmed_sponsor = T::AccountId::default();
570 <Collection<T>>::insert(collection_id, collection);
571
572 Ok(())
573 }
574
575 #[weight = 0]
576 pub fn remove_collection_sponsor(
577 origin,
578 collection_id: u64
579 ) -> DispatchResult {
580 let sender = ensure_signed(origin)?;
581 Self::check_owner_permissions(collection_id, sender)?;
582 let mut collection = <Collection<T>>::get(collection_id);
583 collection.unconfirmed_sponsor = T::AccountId::default();
584 collection.sponsor = T::AccountId::default();
585 <Collection<T>>::insert(collection_id, collection);
586
587 Ok(())
588 }
589
538 }590 }
539}591}
580 match target_collection.mode {632 match target_collection.mode {
581 CollectionMode::NFT(_) => <NftItemList<T>>::get(collection_id, item_id).owner == subject,633 CollectionMode::NFT(_) => <NftItemList<T>>::get(collection_id, item_id).owner == subject,
582 CollectionMode::Fungible(_) => <FungibleItemList<T>>::get(collection_id, item_id).owner == subject,634 CollectionMode::Fungible(_) => <FungibleItemList<T>>::get(collection_id, item_id).owner == subject,
583 CollectionMode::ReFungible(_) => <ReFungibleItemList<T>>::get(collection_id, item_id).owner.iter().any(|i| i.owner == subject),635 CollectionMode::ReFungible(_, _) => <ReFungibleItemList<T>>::get(collection_id, item_id).owner.iter().any(|i| i.owner == subject),
584 CollectionMode::Invalid => false636 CollectionMode::Invalid => false
585 }637 }
586 }638 }
695 Ok(())747 Ok(())
696 }748 }
697749
698 fn transfer_nft(collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {750 fn transfer_nft(collection_id: u64, item_id: u64, sender: T::AccountId, new_owner: T::AccountId) -> DispatchResult {
699751
700 let mut item = <NftItemList<T>>::get(collection_id, item_id);752 let mut item = <NftItemList<T>>::get(collection_id, item_id);
753
754 ensure!(sender == item.owner,"sender parameter and item owner must be equal");
701755
702 // update balance756 // update balance
703 let balance_old_owner = <Balance<T>>::get(collection_id, item.owner.clone()).checked_sub(1).unwrap();757 let balance_old_owner = <Balance<T>>::get(collection_id, item.owner.clone()).checked_sub(1).unwrap();