Skip to content

Commit

Permalink
feat: add admin function to disable ar_discount_renew & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed May 6, 2024
1 parent c31f623 commit 92c3aff
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/interface/naming.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,7 @@ trait INaming<TContractState> {
fn whitelist_renewal_contract(ref self: TContractState, contract: ContractAddress);

fn blacklist_renewal_contract(ref self: TContractState, contract: ContractAddress);

fn toggle_ar_discount_renew(ref self: TContractState);

}
12 changes: 11 additions & 1 deletion src/naming/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ mod Naming {
_whitelisted_renewal_contracts: LegacyMap<ContractAddress, bool>,
// a for alpha, as we will probably do this campaign again in the future
_ar_discount_blacklist_a: LegacyMap<felt252, bool>,
_ar_discount_renew_enabled: bool,
#[substorage(v0)]
storage_read: storage_read_component::Storage,
}
Expand Down Expand Up @@ -469,7 +470,10 @@ mod Naming {
fn ar_discount_renew(
ref self: ContractState, domain: felt252, ar_contract: ContractAddress,
) {
// First we check that domain didn't already claim the discount
// First we check the discount is enabled
assert(self._ar_discount_renew_enabled.read(), 'Discount disabled');

// We check that domain didn't already claim the discount
assert(!self._ar_discount_blacklist_a.read(domain), 'You can\'t claim this twice');

// We check it's a valid AR contract, then we check that AR is enabled,
Expand Down Expand Up @@ -768,6 +772,12 @@ mod Naming {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self._whitelisted_renewal_contracts.write(contract, false);
}


fn toggle_ar_discount_renew(ref self: ContractState) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self._ar_discount_renew_enabled.write(!self._ar_discount_renew_enabled.read());
}
}
}

1 change: 1 addition & 0 deletions src/tests/naming.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ mod test_custom_resolver;
mod test_usecases;
mod test_features;
mod test_altcoin;
mod test_ar_discount;
194 changes: 194 additions & 0 deletions src/tests/naming/test_ar_discount.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
use array::ArrayTrait;
use array::SpanTrait;
use option::OptionTrait;
use zeroable::Zeroable;
use traits::Into;
use starknet::testing;
use starknet::ContractAddress;
use starknet::contract_address::ContractAddressZeroable;
use starknet::contract_address_const;
use starknet::testing::set_contract_address;
use super::super::utils;
use openzeppelin::token::erc20::{
interface::{IERC20Camel, IERC20CamelDispatcher, IERC20CamelDispatcherTrait}
};
use identity::{
identity::main::Identity, interface::identity::{IIdentityDispatcher, IIdentityDispatcherTrait}
};
use naming::interface::naming::{INamingDispatcher, INamingDispatcherTrait};
use naming::interface::pricing::{IPricingDispatcher, IPricingDispatcherTrait};
use naming::interface::auto_renewal::{
IAutoRenewal, IAutoRenewalDispatcher, IAutoRenewalDispatcherTrait
};
use naming::naming::main::Naming;
use naming::pricing::Pricing;
use super::common::deploy;
use naming::naming::main::Naming::Discount;


#[starknet::contract]
mod DummyAutoRenewal {
use core::array::ArrayTrait;
use starknet::ContractAddress;
use starknet::{contract_address_const, get_caller_address, get_contract_address};

#[storage]
struct Storage {
erc20: starknet::ContractAddress,
}

#[constructor]
fn constructor(ref self: ContractState, erc20: starknet::ContractAddress) {
self.erc20.write(erc20);
}

#[abi(embed_v0)]
impl DummyImpl of naming::interface::auto_renewal::IAutoRenewal<ContractState> {
fn get_renewing_allowance(
self: @ContractState, domain: felt252, renewer: starknet::ContractAddress,
) -> u256 {
1
}

// naming, erc20, tax
fn get_contracts(
self: @ContractState
) -> (starknet::ContractAddress, starknet::ContractAddress, starknet::ContractAddress) {
(contract_address_const::<0x0>(), self.erc20.read(), contract_address_const::<0x0>())
}
}
}

#[test]
#[available_gas(2000000000)]
fn test_ar_discount() {
// setup
let (eth, pricing, identity, naming) = deploy();
let caller = contract_address_const::<0x123>();
set_contract_address(caller);
let id: u128 = 1;
let th0rgal: felt252 = 33133781693;

//we mint an id
identity.mint(id);

// we check how much a domain costs
let (_, price) = pricing.compute_buy_price(7, 365);

// we allow the naming to take our money
eth.approve(naming.contract_address, price);

// we buy with no resolver, no sponsor, no discount and empty metadata
naming
.buy(
id, th0rgal, 365, ContractAddressZeroable::zero(), ContractAddressZeroable::zero(), 0, 0
);

let auto_renewal = utils::deploy(
DummyAutoRenewal::TEST_CLASS_HASH, array![eth.contract_address.into()]
);

let current_expiry = naming.domain_to_expiry(array![th0rgal].span());

// we set the renewal contract and enable the discount
naming.whitelist_renewal_contract(auto_renewal);
naming.toggle_ar_discount_renew();
let (_, yearly_renewal_price) = pricing.compute_renew_price(7, 365);
eth.approve(auto_renewal, yearly_renewal_price);
let _allowance = eth.allowance(caller, auto_renewal);
naming.ar_discount_renew(th0rgal, auto_renewal);

// we don't set the auto renewal allowance in this test because we
// use a dummy contract which always return 1, theoretically we should
// set it to infinity (2**256-1)
let new_expiry = naming.domain_to_expiry(array![th0rgal].span());
assert(new_expiry - current_expiry == 86400 * 90, 'Invalid expiry');
}


#[test]
#[available_gas(2000000000)]
#[should_panic(expected: ('Discount disabled', 'ENTRYPOINT_FAILED'))]
fn test_ar_discount_not_enabled() {
// setup
let (eth, pricing, identity, naming) = deploy();
let caller = contract_address_const::<0x123>();
set_contract_address(caller);
let id: u128 = 1;
let th0rgal: felt252 = 33133781693;

//we mint an id
identity.mint(id);

// we check how much a domain costs
let (_, price) = pricing.compute_buy_price(7, 365);

// we allow the naming to take our money
eth.approve(naming.contract_address, price);

// we buy with no resolver, no sponsor, no discount and empty metadata
naming
.buy(
id, th0rgal, 365, ContractAddressZeroable::zero(), ContractAddressZeroable::zero(), 0, 0
);

let auto_renewal = utils::deploy(
DummyAutoRenewal::TEST_CLASS_HASH, array![eth.contract_address.into()]
);

// we set the renewal contract and enable the discount
naming.whitelist_renewal_contract(auto_renewal);
//naming.toggle_ar_discount_renew();
let (_, yearly_renewal_price) = pricing.compute_renew_price(7, 365);
eth.approve(auto_renewal, yearly_renewal_price);
let _allowance = eth.allowance(caller, auto_renewal);
naming.ar_discount_renew(th0rgal, auto_renewal);
}


#[test]
#[available_gas(2000000000)]
#[should_panic(expected: ('Invalid ERC20 allowance', 'ENTRYPOINT_FAILED'))]
fn test_ar_discount_wrong_ar_allowance() {
// setup
let (eth, pricing, identity, naming) = deploy();
let caller = contract_address_const::<0x123>();
set_contract_address(caller);
let id: u128 = 1;
let th0rgal: felt252 = 33133781693;

//we mint an id
identity.mint(id);

// we check how much a domain costs
let (_, price) = pricing.compute_buy_price(7, 365);

// we allow the naming to take our money
eth.approve(naming.contract_address, price);

// we buy with no resolver, no sponsor, no discount and empty metadata
naming
.buy(
id, th0rgal, 365, ContractAddressZeroable::zero(), ContractAddressZeroable::zero(), 0, 0
);

let auto_renewal = utils::deploy(
DummyAutoRenewal::TEST_CLASS_HASH, array![eth.contract_address.into()]
);

let current_expiry = naming.domain_to_expiry(array![th0rgal].span());

// we set the renewal contract and enable the discount
naming.whitelist_renewal_contract(auto_renewal);
naming.toggle_ar_discount_renew();
let (_, _yearly_renewal_price) = pricing.compute_renew_price(7, 365);
//eth.approve(auto_renewal, yearly_renewal_price);
let _allowance = eth.allowance(caller, auto_renewal);
naming.ar_discount_renew(th0rgal, auto_renewal);

// we don't set the auto renewal allowance in this test because we
// use a dummy contract which always return 1, theoretically we should
// set it to infinity (2**256-1)
let new_expiry = naming.domain_to_expiry(array![th0rgal].span());
assert(new_expiry - current_expiry == 86400 * 90, 'Invalid expiry');
}

0 comments on commit 92c3aff

Please sign in to comment.