From 0de01ccfbc7804f403f4c7dac9ade4fcda26e9af Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Wed, 4 Oct 2023 12:12:06 +0700 Subject: [PATCH] fix: use snakeCase interface --- src/naming/main.cairo | 15 +- src/tests.cairo | 1 - src/tests/erc20.cairo | 232 --------------------------- src/tests/naming/common.cairo | 13 +- src/tests/naming/test_abuses.cairo | 9 +- src/tests/naming/test_features.cairo | 15 +- src/tests/naming/test_usecases.cairo | 9 +- 7 files changed, 38 insertions(+), 256 deletions(-) delete mode 100644 src/tests/erc20.cairo diff --git a/src/naming/main.cairo b/src/naming/main.cairo index 3b0f716..101d4ad 100644 --- a/src/naming/main.cairo +++ b/src/naming/main.cairo @@ -19,7 +19,9 @@ mod Naming { use clone::Clone; use array::ArrayTCloneImpl; use identity::interface::identity::{IIdentity, IIdentityDispatcher, IIdentityDispatcherTrait}; - use openzeppelin::token::erc20::interface::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait}; + use openzeppelin::token::erc20::interface::{ + IERC20Camel, IERC20CamelDispatcher, IERC20CamelDispatcherTrait + }; use debug::PrintTrait; #[event] @@ -367,9 +369,10 @@ mod Naming { fn claim_balance(ref self: ContractState, erc20: ContractAddress) { assert(get_caller_address() == self._admin_address.read(), 'you are not admin'); - let balance = IERC20Dispatcher { contract_address: erc20 } - .balance_of(get_contract_address()); - IERC20Dispatcher { contract_address: erc20 }.transfer(get_caller_address(), balance); + let balance = IERC20CamelDispatcher { contract_address: erc20 } + .balanceOf(get_contract_address()); + IERC20CamelDispatcher { contract_address: erc20 } + .transfer(get_caller_address(), balance); } fn set_discount(ref self: ContractState, discount_id: felt252, discount: Discount) { @@ -558,8 +561,8 @@ mod Naming { }; // pay the price - IERC20Dispatcher { contract_address: erc20 } - .transfer_from(get_caller_address(), get_contract_address(), discounted_price); + IERC20CamelDispatcher { contract_address: erc20 } + .transferFrom(get_caller_address(), get_contract_address(), discounted_price); // add sponsor commission if eligible if sponsor.into() != 0 { IReferralDispatcher { contract_address: self._referral_contract.read() } diff --git a/src/tests.cairo b/src/tests.cairo index 07c6c70..06ae230 100644 --- a/src/tests.cairo +++ b/src/tests.cairo @@ -1,4 +1,3 @@ -mod erc20; mod utils; mod test_pricing; mod naming; diff --git a/src/tests/erc20.cairo b/src/tests/erc20.cairo deleted file mode 100644 index 906759e..0000000 --- a/src/tests/erc20.cairo +++ /dev/null @@ -1,232 +0,0 @@ -#[starknet::contract] -mod ERC20 { - use integer::BoundedInt; - use openzeppelin::token::erc20::interface::IERC20; - use openzeppelin::token::erc20::interface::IERC20CamelOnly; - use starknet::ContractAddress; - use starknet::get_caller_address; - use zeroable::Zeroable; - - #[storage] - struct Storage { - _name: felt252, - _symbol: felt252, - _total_supply: u256, - _balances: LegacyMap, - _allowances: LegacyMap<(ContractAddress, ContractAddress), u256>, - } - - #[event] - #[derive(Drop, starknet::Event)] - enum Event { - Transfer: Transfer, - Approval: Approval, - } - - #[derive(Drop, starknet::Event)] - struct Transfer { - from: ContractAddress, - to: ContractAddress, - value: u256 - } - - #[derive(Drop, starknet::Event)] - struct Approval { - owner: ContractAddress, - spender: ContractAddress, - value: u256 - } - - #[constructor] - fn constructor( - ref self: ContractState, - name: felt252, - symbol: felt252, - initial_supply: u256, - recipient: ContractAddress - ) { - self.initializer(name, symbol); - self._mint(recipient, initial_supply); - } - - // - // External - // - - #[external(v0)] - impl ERC20Impl of IERC20 { - fn name(self: @ContractState) -> felt252 { - self._name.read() - } - - fn symbol(self: @ContractState) -> felt252 { - self._symbol.read() - } - - fn decimals(self: @ContractState) -> u8 { - 18 - } - - fn total_supply(self: @ContractState) -> u256 { - self._total_supply.read() - } - - fn balance_of(self: @ContractState, account: ContractAddress) -> u256 { - self._balances.read(account) - } - - fn allowance( - self: @ContractState, owner: ContractAddress, spender: ContractAddress - ) -> u256 { - self._allowances.read((owner, spender)) - } - - fn transfer(ref self: ContractState, recipient: ContractAddress, amount: u256) -> bool { - let sender = get_caller_address(); - self._transfer(sender, recipient, amount); - true - } - - fn transfer_from( - ref self: ContractState, - sender: ContractAddress, - recipient: ContractAddress, - amount: u256 - ) -> bool { - let caller = get_caller_address(); - self._spend_allowance(sender, caller, amount); - self._transfer(sender, recipient, amount); - true - } - - fn approve(ref self: ContractState, spender: ContractAddress, amount: u256) -> bool { - let caller = get_caller_address(); - self._approve(caller, spender, amount); - true - } - } - - #[external(v0)] - impl ERC20CamelOnlyImpl of IERC20CamelOnly { - fn totalSupply(self: @ContractState) -> u256 { - ERC20Impl::total_supply(self) - } - - fn balanceOf(self: @ContractState, account: ContractAddress) -> u256 { - ERC20Impl::balance_of(self, account) - } - - fn transferFrom( - ref self: ContractState, - sender: ContractAddress, - recipient: ContractAddress, - amount: u256 - ) -> bool { - ERC20Impl::transfer_from(ref self, sender, recipient, amount) - } - } - - #[external(v0)] - fn increase_allowance( - ref self: ContractState, spender: ContractAddress, added_value: u256 - ) -> bool { - self._increase_allowance(spender, added_value) - } - - #[external(v0)] - fn increaseAllowance( - ref self: ContractState, spender: ContractAddress, addedValue: u256 - ) -> bool { - increase_allowance(ref self, spender, addedValue) - } - - #[external(v0)] - fn decrease_allowance( - ref self: ContractState, spender: ContractAddress, subtracted_value: u256 - ) -> bool { - self._decrease_allowance(spender, subtracted_value) - } - - #[external(v0)] - fn decreaseAllowance( - ref self: ContractState, spender: ContractAddress, subtractedValue: u256 - ) -> bool { - decrease_allowance(ref self, spender, subtractedValue) - } - - // - // Internal - // - - #[generate_trait] - impl InternalImpl of InternalTrait { - fn initializer(ref self: ContractState, name_: felt252, symbol_: felt252) { - self._name.write(name_); - self._symbol.write(symbol_); - } - - fn _increase_allowance( - ref self: ContractState, spender: ContractAddress, added_value: u256 - ) -> bool { - let caller = get_caller_address(); - self._approve(caller, spender, self._allowances.read((caller, spender)) + added_value); - true - } - - fn _decrease_allowance( - ref self: ContractState, spender: ContractAddress, subtracted_value: u256 - ) -> bool { - let caller = get_caller_address(); - self - ._approve( - caller, spender, self._allowances.read((caller, spender)) - subtracted_value - ); - true - } - - fn _mint(ref self: ContractState, recipient: ContractAddress, amount: u256) { - assert(!recipient.is_zero(), 'ERC20: mint to 0'); - self._total_supply.write(self._total_supply.read() + amount); - self._balances.write(recipient, self._balances.read(recipient) + amount); - self.emit(Transfer { from: Zeroable::zero(), to: recipient, value: amount }); - } - - fn _burn(ref self: ContractState, account: ContractAddress, amount: u256) { - assert(!account.is_zero(), 'ERC20: burn from 0'); - self._total_supply.write(self._total_supply.read() - amount); - self._balances.write(account, self._balances.read(account) - amount); - self.emit(Transfer { from: account, to: Zeroable::zero(), value: amount }); - } - - fn _approve( - ref self: ContractState, owner: ContractAddress, spender: ContractAddress, amount: u256 - ) { - assert(!owner.is_zero(), 'ERC20: approve from 0'); - assert(!spender.is_zero(), 'ERC20: approve to 0'); - self._allowances.write((owner, spender), amount); - self.emit(Approval { owner, spender, value: amount }); - } - - fn _transfer( - ref self: ContractState, - sender: ContractAddress, - recipient: ContractAddress, - amount: u256 - ) { - assert(!sender.is_zero(), 'ERC20: transfer from 0'); - assert(!recipient.is_zero(), 'ERC20: transfer to 0'); - self._balances.write(sender, self._balances.read(sender) - amount); - self._balances.write(recipient, self._balances.read(recipient) + amount); - self.emit(Transfer { from: sender, to: recipient, value: amount }); - } - - fn _spend_allowance( - ref self: ContractState, owner: ContractAddress, spender: ContractAddress, amount: u256 - ) { - let current_allowance = self._allowances.read((owner, spender)); - if current_allowance != BoundedInt::max() { - self._approve(owner, spender, current_allowance - amount); - } - } - } -} diff --git a/src/tests/naming/common.cairo b/src/tests/naming/common.cairo index 476f310..007b7fe 100644 --- a/src/tests/naming/common.cairo +++ b/src/tests/naming/common.cairo @@ -10,15 +10,18 @@ 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::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait}; -use identity::{identity::main::Identity, interface::identity::{IIdentityDispatcher, IIdentityDispatcherTrait}}; +use openzeppelin::token::erc20::{ + erc20::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::naming::main::Naming; use naming::pricing::Pricing; -use super::super::erc20::ERC20; -fn deploy() -> (IERC20Dispatcher, IPricingDispatcher, IIdentityDispatcher, INamingDispatcher) { +fn deploy() -> (IERC20CamelDispatcher, IPricingDispatcher, IIdentityDispatcher, INamingDispatcher) { //erc20 let mut calldata = ArrayTrait::::new(); // 0, 1 = low and high of ETH supply @@ -36,7 +39,7 @@ fn deploy() -> (IERC20Dispatcher, IPricingDispatcher, IIdentityDispatcher, INami Naming::TEST_CLASS_HASH, array![identity.into(), pricing.into(), 0, admin] ); ( - IERC20Dispatcher { contract_address: eth }, + IERC20CamelDispatcher { contract_address: eth }, IPricingDispatcher { contract_address: pricing }, IIdentityDispatcher { contract_address: identity }, INamingDispatcher { contract_address: address } diff --git a/src/tests/naming/test_abuses.cairo b/src/tests/naming/test_abuses.cairo index 705f2fe..2e6008b 100644 --- a/src/tests/naming/test_abuses.cairo +++ b/src/tests/naming/test_abuses.cairo @@ -10,13 +10,16 @@ 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::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait}; -use identity::{identity::main::Identity, interface::identity::{IIdentityDispatcher, IIdentityDispatcherTrait}}; +use openzeppelin::token::erc20::{ + erc20::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::naming::main::Naming; use naming::pricing::Pricing; -use super::super::erc20::ERC20; use super::common::deploy; diff --git a/src/tests/naming/test_features.cairo b/src/tests/naming/test_features.cairo index fb956cb..870fdd7 100644 --- a/src/tests/naming/test_features.cairo +++ b/src/tests/naming/test_features.cairo @@ -10,13 +10,16 @@ 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::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait}; -use identity::{identity::main::Identity, interface::identity::{IIdentityDispatcher, IIdentityDispatcherTrait}}; +use openzeppelin::token::erc20::{ + erc20::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::naming::main::Naming; use naming::pricing::Pricing; -use super::super::erc20::ERC20; use super::common::deploy; @@ -104,11 +107,11 @@ fn test_claim_balance() { id, th0rgal, 365, ContractAddressZeroable::zero(), ContractAddressZeroable::zero(), 0, 0 ); - let contract_bal = eth.balance_of(naming.contract_address); - let admin_balance = eth.balance_of(caller); + let contract_bal = eth.balanceOf(naming.contract_address); + let admin_balance = eth.balanceOf(caller); assert(contract_bal == price, 'naming has wrong balance'); naming.claim_balance(eth.contract_address); - assert(admin_balance + price == eth.balance_of(caller), 'balance didn\'t increase'); + assert(admin_balance + price == eth.balanceOf(caller), 'balance didn\'t increase'); } diff --git a/src/tests/naming/test_usecases.cairo b/src/tests/naming/test_usecases.cairo index 8900203..7411448 100644 --- a/src/tests/naming/test_usecases.cairo +++ b/src/tests/naming/test_usecases.cairo @@ -10,13 +10,16 @@ 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::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait}; -use identity::{identity::main::Identity, interface::identity::{IIdentityDispatcher, IIdentityDispatcherTrait}}; +use openzeppelin::token::erc20::{ + erc20::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::naming::main::Naming; use naming::pricing::Pricing; -use super::super::erc20::ERC20; use super::common::deploy; use naming::naming::main::Naming::Discount;