Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add royalty_payout and split_contract_funds #100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions contracts/tenk/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,13 @@ impl Contract {
GAS_REQUIRED_TO_CREATE_LINKDROP,
))
}

/// Split funds from contract among royality accounts
/// @allow ["::admins", "::owner"]
pub fn split_contract_funds(&self, amount: U128, ty: RoyaltyType) {
self.assert_owner_or_admin();
let available_balance = env::account_balance() - env::account_locked_balance();
require!(amount.0 < available_balance, "Insufficent balance");
self.royalty_payout(amount, ty).send_funds()
}
}
14 changes: 8 additions & 6 deletions contracts/tenk/src/payout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Payouts for Contract {
self.sale
.royalties
.as_ref()
.map_or(Payout::default(), |r| r.create_payout(balance.0, &owner_id))
.map_or_else(Default::default, |r| r.create_payout(balance.0, Some(&owner_id)))
}

#[payable]
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Royalties {
"total percent of each royalty split must equal 10,000"
)
}
pub(crate) fn create_payout(&self, balance: Balance, owner_id: &AccountId) -> Payout {
pub(crate) fn create_payout(&self, balance: Balance, owner_id: Option<&AccountId>) -> Payout {
let royalty_payment = apply_percent(self.percent, balance);
let mut payout = Payout {
payout: self
Expand All @@ -136,14 +136,16 @@ impl Royalties {
.collect(),
}
.tenk_royalities();
let rest = balance - u128::min(royalty_payment, balance);
let owner_payout: u128 = payout.payout.get(owner_id).map_or(0, |x| x.0) + rest;
payout.payout.insert(owner_id.clone(), owner_payout.into());
if let Some(owner_id) = owner_id {
let rest = balance - u128::min(royalty_payment, balance);
let owner_payout: u128 = payout.payout.get(owner_id).map_or(0, |x| x.0) + rest;
payout.payout.insert(owner_id.clone(), owner_payout.into());
}
payout
}

pub(crate) fn send_funds(&self, balance: Balance, owner_id: &AccountId) {
self.create_payout(balance, owner_id).send_funds();
self.create_payout(balance, Some(owner_id)).send_funds();
}
}

Expand Down
23 changes: 23 additions & 0 deletions contracts/tenk/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ impl Sale {
r.validate()
}
}

pub fn payout(
&self,
amount: u128,
ty: RoyaltyType,
owner_id: Option<&AccountId>,
) -> Option<Payout> {
match ty {
RoyaltyType::Primary => self.initial_royalties.as_ref(),
RoyaltyType::Secondary => self.royalties.as_ref(),
}
.map(|r| r.create_payout(amount, owner_id))
}
}
/// Current state of contract
#[witgen]
Expand Down Expand Up @@ -152,6 +165,16 @@ pub struct SaleInfo {
pub price: U128,
}


/// Primary is during initial mint. Secondary is for market places.
#[witgen]
#[derive(Deserialize, Serialize, BorshSerialize, BorshDeserialize)]
#[serde(crate = "near_sdk::serde")]
pub enum RoyaltyType {
Primary,
Secondary,
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
mod tests {
Expand Down
10 changes: 10 additions & 0 deletions contracts/tenk/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,14 @@ impl Contract {
pub fn initial(&self) -> u64 {
self.raffle.len() + self.nft_total_supply().0 as u64
}

/// How many linkdrops have yet to be claimed
pub fn pending_tokens(&self) -> u32 {
self.pending_tokens
}

/// Royality payout
pub fn royalty_payout(&self, amount: U128, ty: RoyaltyType) -> Payout {
self.sale.payout(amount.0, ty, None).unwrap()
}
}