Skip to content

Commit

Permalink
reworked how collection owner badges work, reworked tests to be more …
Browse files Browse the repository at this point in the history
…granular, removed update_base_path
  • Loading branch information
Kansuler committed Sep 26, 2023
1 parent 686f9ac commit 7c681e6
Show file tree
Hide file tree
Showing 58 changed files with 2,808 additions and 1,893 deletions.
29 changes: 26 additions & 3 deletions src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod collection {
pub fn new(
trophy_resource_manager: ResourceManager,
repository_owner_badge: ResourceAddress,
collection_owner_badge: ResourceAddress,
collection_owner_badge_proof: CheckedProof,
minter_badge: Bucket,
user_name: String,
user_slug: String,
Expand All @@ -61,6 +61,14 @@ mod collection {
let (reservation, address) =
Runtime::allocate_component_address(Collection::blueprint_id());
let collection_id = Runtime::bech32_encode_address(address);

let component_owner_badge_global_id = NonFungibleGlobalId::new(
collection_owner_badge_proof.resource_address(),
collection_owner_badge_proof
.as_non_fungible()
.non_fungible_local_id(),
);

Self {
minter_badge: Vault::with_bucket(minter_badge),
donations: Vault::new(XRD),
Expand All @@ -84,7 +92,7 @@ mod collection {
}
))
.roles(roles!(
owner => rule!(require(collection_owner_badge));
owner => rule!(require(component_owner_badge_global_id));
))
.with_address(reservation)
.globalize()
Expand All @@ -97,6 +105,10 @@ mod collection {
panic!("This collection is permanently closed.");
}

if tokens.amount() < dec!(100) {
panic!("Minimum donation is 100 XRD");
}

// Push a proof of minter badge to the local auth zone for minting a trophy.
LocalAuthZone::push(self.minter_badge.as_fungible().create_proof_of_amount(1));

Expand Down Expand Up @@ -143,6 +155,10 @@ mod collection {
panic!("This collection is permanently closed.");
}

if tokens.amount() < dec!(100) {
panic!("Minimum donation is 100 XRD");
}

// Push a proof of minter badge to the local auth zone for minting a trophy.
LocalAuthZone::push(self.minter_badge.as_fungible().create_proof_of_amount(1));

Expand Down Expand Up @@ -199,9 +215,16 @@ mod collection {
// close_collection is a method for the collection admin to close the collection
// permanently. This will prevent any further donations to be made to the collection, and
// will prevent any further minting or updating to the trophies.
pub fn close_collection(&mut self) {
pub fn close_collection(&mut self) -> Bucket {
if self.closed.is_some() {
panic!("This collection is permanently closed.");
}

self.closed =
Some(UtcDateTime::from_instant(&Clock::current_time_rounded_to_minutes()).unwrap());

// Withdraw all remaining donations.
self.donations.take_all()
}
}
}
10 changes: 8 additions & 2 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ pub struct Trophy {
pub created: String,
pub info_url: UncheckedUrl,
pub collection_id: String,
pub key_image_url: UncheckedUrl,

#[mutable]
pub donated: Decimal,
#[mutable]
pub key_image_url: UncheckedUrl,
}

#[derive(ScryptoSbor, NonFungibleData, Clone)]
pub struct CollectionOwnerBadge {
pub name: String,
pub description: String,
}
89 changes: 52 additions & 37 deletions src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use crate::collection::collection::Collection;
use crate::data::Trophy;
use crate::data::{CollectionOwnerBadge, Trophy};
use crate::util::*;
use scrypto::prelude::*;

#[blueprint]
#[types(Trophy)]
#[types(Trophy, CollectionOwnerBadge)]
mod repository {
enable_package_royalties! {
new => Free;
merge_trophies => Free;
new_collection_component => Xrd(50.into());
update_base_path => Free;
new_collection_owner_badge => Free;
}

enable_method_auth! {
Expand All @@ -20,14 +20,17 @@ mod repository {
methods {
new_collection_component => PUBLIC;
merge_trophies => PUBLIC;
update_base_path => restrict_to: [OWNER];
new_collection_owner_badge => PUBLIC;
}
}

struct Repository {
// NFT resource address.
// NFT resource manager.
trophy_resource_manager: ResourceManager,

// Collection owner badge resource manager.
collection_owner_badge_manager: ResourceManager,

// Badge for being able to mint trophies.
minter_badge_manager: ResourceManager,

Expand Down Expand Up @@ -62,7 +65,9 @@ mod repository {
init {
"name" => "Trophies Minter", locked;
"description" => "Grants authorization to mint NFs from repository", locked;
"dapp_definition" => dapp_definition_address, locked;
"tags" => vec!["backeum"], locked;
"icon_url" => UncheckedUrl::of(format!("{}{}", base_path.clone(), "/bucket/assets/wallet-assets/trophy-minter-badge.png")), locked;
"info_url" => UncheckedUrl::of(base_path.clone()), locked;
}
))
.mint_roles(mint_roles! {
Expand All @@ -75,6 +80,32 @@ mod repository {
})
.create_with_no_initial_supply();

// Creating an collection owner badge for the trophy collections. This is used to set
// ownership of the collection components. The collection owner badge is handed down to
// the collection blueprint via the factory method new_collection_component.
let collection_owner_badge_manager = ResourceBuilder::new_ruid_non_fungible_with_registered_type::<CollectionOwnerBadge>(OwnerRole::Fixed(
repository_owner_badge_access_rule.clone(),
))
.metadata(metadata!(
init {
"name" => "Backeum Collection Owner Badges", locked;
"description" => "Grants collection ownership of Backeum components", locked;
"icon_url" => UncheckedUrl::of(format!("{}{}", base_path.clone(), "/bucket/assets/wallet-assets/collection-owner-badge.png")), locked;
"tags" => vec!["backeum"], locked;
"info_url" => UncheckedUrl::of(base_path.clone()), locked;
"dapp_definition" => dapp_definition_address, locked;
}
))
.mint_roles(mint_roles! {
minter => rule!(require(global_caller(component_address)));
minter_updater => rule!(deny_all);
})
.burn_roles(burn_roles!(
burner => rule!(require(global_caller(component_address)));
burner_updater => rule!(require(global_caller(component_address)));
))
.create_with_no_initial_supply();

// Manager for minting trophies for a central collection. This manager will be handed
// down to collection components together with a minter badge. This allows all
// collections to mint trophies from the same resource manager.
Expand All @@ -89,10 +120,10 @@ mod repository {
init {
"name" => "Backeum Trophies", locked;
"description" => "Backeum trophies celebrates the patronage of its holder with donations to individual Backeum creators. A unique symbol of support for the community, it's a vibrant testament to financial encouragement.", locked;
"domain" => format!("{}", base_path), updatable;
"domain" => base_path.clone(), updatable;
"icon_url" => UncheckedUrl::of(format!("{}{}", base_path, "/bucket/assets/wallet-assets/trophy.png")), locked;
"tags" => vec!["backeum", "trophy"], locked;
"info_url" => UncheckedUrl::of(base_path), locked;
"info_url" => UncheckedUrl::of(base_path.clone()), locked;
"dapp_definition" => dapp_definition_address, locked;
}
))
Expand All @@ -112,6 +143,7 @@ mod repository {

Self {
trophy_resource_manager,
collection_owner_badge_manager,
minter_badge_manager,
repository_owner_access_badge_address,
dapp_definition_address,
Expand Down Expand Up @@ -146,46 +178,29 @@ mod repository {
user_slug: String,
collection_owner_badge_proof: Proof,
) -> Global<Collection> {
let checked_collection_owner_badge_proof =
collection_owner_badge_proof.check(self.collection_owner_badge_manager.address());

let mint_badge = self.minter_badge_manager.mint(1);
Collection::new(
self.trophy_resource_manager,
self.repository_owner_access_badge_address,
collection_owner_badge_proof.resource_address(),
checked_collection_owner_badge_proof,
mint_badge,
user_name,
user_slug,
self.dapp_definition_address,
)
}

// update_base_path updates the base path for each trophy.
pub fn update_base_path(
&mut self,
new_base_path: String,
update_nft_ids: Vec<NonFungibleLocalId>,
) {
self.trophy_resource_manager
.set_metadata("domain", new_base_path.clone());

for nft_id in update_nft_ids {
// Get data from the Trophy data based on NF id.
let mut data: Trophy = self.trophy_resource_manager.get_non_fungible_data(&nft_id);

// Generate new image url.
data.key_image_url = UncheckedUrl::of(generate_url(
new_base_path.to_string(),
data.donated,
data.created,
data.collection_id,
));

// Update image url.
self.trophy_resource_manager.update_non_fungible_data(
&nft_id,
"key_image_url",
data.key_image_url,
);
}
// Mints a new collection owner badge that the user can use to gain ownership of a
// collection. Ownership badges are free to mint and burn.
pub fn new_collection_owner_badge(&mut self) -> Bucket {
self.collection_owner_badge_manager
.mint_ruid_non_fungible::<CollectionOwnerBadge>(CollectionOwnerBadge {
name: "Collection Owner Badge".to_string(),
description: "Grants ownership of Backeum collection components".to_string(),
})
}

// merge_trophies will take multiple trophies of the same collection id and merge them into
Expand Down
Loading

0 comments on commit 7c681e6

Please sign in to comment.