Skip to content

Commit

Permalink
style fixes (royalty.rs)
Browse files Browse the repository at this point in the history
  • Loading branch information
garikbesson committed Feb 14, 2024
1 parent f13e449 commit fca226d
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions nft-contract/src/royalty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,45 @@ pub trait NonFungibleTokenCore {

#[near_bindgen]
impl NonFungibleTokenCore for Contract {

//calculates the payout for a token given the passed in balance. This is a view method
fn nft_payout(&self, token_id: TokenId, balance: U128, max_len_payout: u32) -> Payout {
//get the token object
let token = self.tokens_by_id.get(&token_id).expect("No token");
let token = self.tokens_by_id.get(&token_id).expect("No token");

//get the owner of the token
let owner_id = token.owner_id;
//keep track of the total perpetual royalties
let mut total_perpetual = 0;
//get the u128 version of the passed in balance (which was U128 before)
let balance_u128 = u128::from(balance);
//keep track of the payout object to send back
//keep track of the payout object to send back
let mut payout_object = Payout {
payout: HashMap::new()
};
//get the royalty object from token
let royalty = token.royalty;
let royalty = token.royalty;

//make sure we're not paying out to too many people (GAS limits this)
assert!(royalty.len() as u32 <= max_len_payout, "Market cannot payout to that many receivers");
assert!(royalty.len() as u32 <= max_len_payout, "Market cannot payout to that many receivers");

//go through each key and value in the royalty object
for (k, v) in royalty.iter() {
//go through each key and value in the royalty object
for (k, v) in royalty.iter() {
//get the key
let key = k.clone();
let key = k.clone();

//only insert into the payout if the key isn't the token owner (we add their payout at the end)
if key != owner_id {
//
payout_object.payout.insert(key, royalty_to_payout(*v, balance_u128));
total_perpetual += *v;
}
}
if key != owner_id {
payout_object.payout.insert(key, royalty_to_payout(*v, balance_u128));
total_perpetual += *v;
}
}

// payout to previous owner who gets 100% - total perpetual royalties
payout_object.payout.insert(owner_id, royalty_to_payout(10000 - total_perpetual, balance_u128));
// payout to previous owner who gets 100% - total perpetual royalties
payout_object.payout.insert(owner_id, royalty_to_payout(10000 - total_perpetual, balance_u128));

//return the payout object
payout_object
}
payout_object
}

//transfers the token to the receiver ID and returns the payout object that should be payed given the passed in balance.
#[payable]
Expand Down Expand Up @@ -95,32 +94,32 @@ impl NonFungibleTokenCore for Contract {
let mut total_perpetual = 0;
//get the u128 version of the passed in balance (which was U128 before)
let balance_u128 = u128::from(balance);
//keep track of the payout object to send back
//keep track of the payout object to send back
let mut payout_object = Payout {
payout: HashMap::new()
};
//get the royalty object from token
let royalty = previous_token.royalty;
let royalty = previous_token.royalty;

//make sure we're not paying out to too many people (GAS limits this)
assert!(royalty.len() as u32 <= max_len_payout, "Market cannot payout to that many receivers");
assert!(royalty.len() as u32 <= max_len_payout, "Market cannot payout to that many receivers");

//go through each key and value in the royalty object
for (k, v) in royalty.iter() {
for (k, v) in royalty.iter() {
//get the key
let key = k.clone();
let key = k.clone();

//only insert into the payout if the key isn't the token owner (we add their payout at the end)
if key != owner_id {
//
payout_object.payout.insert(key, royalty_to_payout(*v, balance_u128));
total_perpetual += *v;
}
}
if key != owner_id {
payout_object.payout.insert(key, royalty_to_payout(*v, balance_u128));
total_perpetual += *v;
}
}

// payout to previous owner who gets 100% - total perpetual royalties
payout_object.payout.insert(owner_id, royalty_to_payout(10000 - total_perpetual, balance_u128));
// payout to previous owner who gets 100% - total perpetual royalties
payout_object.payout.insert(owner_id, royalty_to_payout(10000 - total_perpetual, balance_u128));

//return the payout object
payout_object
payout_object
}
}

0 comments on commit fca226d

Please sign in to comment.