Skip to content

Commit

Permalink
feat: add referral & pay_buy_domain
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Aug 10, 2023
1 parent 197dd5f commit 1a9c907
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/interface.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod naming;
mod resolver;
mod pricing;
mod referral;
6 changes: 6 additions & 0 deletions src/interface/referral.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use starknet::ContractAddress;

#[starknet::interface]
trait IReferral<TContractState> {
fn add_commission(self: @TContractState, amount: u256, sponsor_addr: ContractAddress);
}
38 changes: 35 additions & 3 deletions src/naming/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ mod Naming {
use array::{ArrayTrait, SpanTrait};
use zeroable::Zeroable;
use starknet::class_hash::ClassHash;
use integer::{u256_safe_divmod, u256_as_non_zero};
use core::pedersen;
use naming::interface::{
naming::{INaming, INamingDispatcher, INamingDispatcherTrait},
resolver::{IResolver, IResolverDispatcher, IResolverDispatcherTrait},
pricing::{IPricing, IPricingDispatcher, IPricingDispatcherTrait},
referral::{IReferral, IReferralDispatcher, IReferralDispatcherTrait},
};
use identity::interface::identity::{IIdentity, IIdentityDispatcher, IIdentityDispatcherTrait};
use integer::{u256_safe_divmod, u256_as_non_zero};
use core::pedersen;
use openzeppelin::token::erc20::interface::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait};


#[derive(Copy, Drop, Serde, starknet::Store)]
struct DomainData {
Expand All @@ -30,6 +34,7 @@ mod Naming {
struct Storage {
starknetid_contract: ContractAddress,
_pricing_contract: ContractAddress,
_referral_contract: ContractAddress,
_admin_address: ContractAddress,
_domain_data: LegacyMap<felt252, DomainData>,
}
Expand All @@ -39,10 +44,12 @@ mod Naming {
ref self: ContractState,
starknetid: ContractAddress,
pricing: ContractAddress,
referral: ContractAddress,
admin: ContractAddress
) {
self.starknetid_contract.write(starknetid);
self._pricing_contract.write(pricing);
self._referral_contract.write(referral);
self._admin_address.write(admin);
}

Expand All @@ -65,7 +72,6 @@ mod Naming {
}.get_crosschecked_user_data(domain_data.owner, field)
}
}
// This function allows to purchase a domain
}

#[generate_trait]
Expand Down Expand Up @@ -133,5 +139,31 @@ mod Naming {
return self.domain_to_resolver(domain, parent_start_id + 1);
}
}

fn pay_buy_domain(
self: @ContractState,
now: u64,
days: u16,
caller: ContractAddress,
domain: felt252,
sponsor: ContractAddress
) -> () {
// find domain cost
let (erc20, price) = IPricingDispatcher {
contract_address: self._pricing_contract.read()
}.compute_buy_price(domain, days);

// pay the price
IERC20Dispatcher {
contract_address: erc20
}.transfer_from(caller, get_contract_address(), price);

// add sponsor commission if eligible
if sponsor.into() != 0 {
IReferralDispatcher {
contract_address: self._referral_contract.read()
}.add_commission(price, sponsor);
}
}
}
}

0 comments on commit 1a9c907

Please sign in to comment.