From 792523856099fa19bffde57ba15dbd27b4a612e6 Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Mon, 7 Aug 2023 17:12:32 +0100 Subject: [PATCH] wip: add naming contract --- .github/workflows/test.yml | 2 +- Scarb.toml | 5 +-- src/interface.cairo | 3 +- src/interface/naming.cairo | 6 ++++ src/lib.cairo | 4 +-- src/naming.cairo | 1 + src/naming/main.cairo | 38 ++++++++++++++++++++++ src/pricing.cairo | 2 +- src/tests.cairo | 1 + src/tests/test_naming.cairo | 43 +++++++++++++++++++++++++ src/tests/test_pricing.cairo | 2 +- src/tests/utils.cairo | 2 +- src/upgrades/upgradeable.cairo | 58 ---------------------------------- 13 files changed, 100 insertions(+), 67 deletions(-) create mode 100644 src/interface/naming.cairo create mode 100644 src/naming.cairo create mode 100644 src/naming/main.cairo create mode 100644 src/tests/test_naming.cairo delete mode 100644 src/upgrades/upgradeable.cairo diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3d38217..8544929 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -3,7 +3,7 @@ name: CI Tests on: [push, pull_request, pull_request_target] env: - SCARB_VERSION: 0.6.0-alpha.4 + SCARB_VERSION: 0.6.0 jobs: scarb-tests: diff --git a/Scarb.toml b/Scarb.toml index 4c4c5d6..7ba64e7 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -5,7 +5,8 @@ version = "0.1.0" # See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest [dependencies] -starknet = "2.1.0-rc4" +starknet = "2.1.0" +# openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", branch = "cairo-2" } [[target.starknet-contract]] # Enable Sierra codegen. @@ -15,4 +16,4 @@ sierra = true casm = true # Emit Python-powered hints in order to run compiled CASM class with legacy Cairo VM. -casm-add-pythonic-hints = true \ No newline at end of file +casm-add-pythonic-hints = true diff --git a/src/interface.cairo b/src/interface.cairo index 41623ea..22c0ad0 100644 --- a/src/interface.cairo +++ b/src/interface.cairo @@ -1 +1,2 @@ -mod pricing; \ No newline at end of file +mod pricing; +mod naming; \ No newline at end of file diff --git a/src/interface/naming.cairo b/src/interface/naming.cairo new file mode 100644 index 0000000..2f9b0a1 --- /dev/null +++ b/src/interface/naming.cairo @@ -0,0 +1,6 @@ +use starknet::ContractAddress; + +#[starknet::interface] +trait INaming { + fn resolve(self: @TContractState, domain: felt252, field: felt252) -> felt252; +} diff --git a/src/lib.cairo b/src/lib.cairo index ac84a6f..d906b6b 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,4 +1,4 @@ -mod upgrades; mod interface; mod pricing; -mod tests; \ No newline at end of file +mod naming; +mod tests; diff --git a/src/naming.cairo b/src/naming.cairo new file mode 100644 index 0000000..018cdff --- /dev/null +++ b/src/naming.cairo @@ -0,0 +1 @@ +mod main; \ No newline at end of file diff --git a/src/naming/main.cairo b/src/naming/main.cairo new file mode 100644 index 0000000..e8dffeb --- /dev/null +++ b/src/naming/main.cairo @@ -0,0 +1,38 @@ +#[starknet::contract] +mod Naming { + use starknet::ContractAddress; + use starknet::{get_caller_address, get_contract_address}; + use traits::Into; + use array::{ArrayTrait}; + use zeroable::Zeroable; + use starknet::class_hash::ClassHash; + use naming::interface::naming::{INaming, INamingDispatcher, INamingDispatcherTrait}; + use integer::{u256_safe_divmod, u256_as_non_zero}; + + #[storage] + struct Storage { + starknetid_contract: ContractAddress, + _pricing_contract: ContractAddress, + _admin_address: ContractAddress, + } + + #[constructor] + fn constructor( + ref self: ContractState, + starknetid: ContractAddress, + pricing: ContractAddress, + admin: ContractAddress + ) { + self.starknetid_contract.write(starknetid); + self._pricing_contract.write(pricing); + self._admin_address.write(admin); + } + + + #[external(v0)] + impl PricingImpl of INaming { + fn resolve(self: @ContractState, domain: felt252, field: felt252) -> felt252 { + 1 + } + } +} diff --git a/src/pricing.cairo b/src/pricing.cairo index a2d1e66..f561fbf 100644 --- a/src/pricing.cairo +++ b/src/pricing.cairo @@ -55,7 +55,7 @@ mod Pricing { } fn get_price_per_day(self: @ContractState, domain: felt252) -> u128 { - let number_of_character = self.get_price_per_day(domain.into()); + let number_of_character = self.get_amount_of_chars(domain.into()); if number_of_character == 1 { return 1068493150684932; diff --git a/src/tests.cairo b/src/tests.cairo index b3327d6..dc48c93 100644 --- a/src/tests.cairo +++ b/src/tests.cairo @@ -1,2 +1,3 @@ mod utils; mod test_pricing; +mod test_naming; diff --git a/src/tests/test_naming.cairo b/src/tests/test_naming.cairo new file mode 100644 index 0000000..9441ae0 --- /dev/null +++ b/src/tests/test_naming.cairo @@ -0,0 +1,43 @@ +use array::ArrayTrait; +use debug::PrintTrait; +use zeroable::Zeroable; +use traits::Into; + +use starknet::ContractAddress; +use starknet::testing; +use super::utils; +use naming::interface::naming::{INamingDispatcher, INamingDispatcherTrait}; +use naming::naming::main::Naming; +use naming::pricing::Pricing; + +#[cfg(test)] +fn deploy() -> INamingDispatcher { + + //erc20 + let mut calldata = ArrayTrait::::new(); + calldata.append(0x789); + let pricing = utils::deploy(Pricing::TEST_CLASS_HASH, calldata); + + // pricing + let mut calldata = ArrayTrait::::new(); + calldata.append(0x789); + let pricing = utils::deploy(Pricing::TEST_CLASS_HASH, calldata); + + // naming + let mut calldata = ArrayTrait::::new(); + let starknetid = 0x456; + let admin = 0x123; + calldata.append(starknetid); + calldata.append(pricing.into()); + calldata.append(admin); + let address = utils::deploy(Naming::TEST_CLASS_HASH, calldata); + INamingDispatcher { contract_address: address } +} + +#[cfg(test)] +#[test] +#[available_gas(20000000000)] +fn test_buy_price() { + deploy(); +} + diff --git a/src/tests/test_pricing.cairo b/src/tests/test_pricing.cairo index 51febd9..9537758 100644 --- a/src/tests/test_pricing.cairo +++ b/src/tests/test_pricing.cairo @@ -94,4 +94,4 @@ fn test_get_amount_of_chars() { ) == 30, 'Should return 30' ); -} +} \ No newline at end of file diff --git a/src/tests/utils.cairo b/src/tests/utils.cairo index d3b720c..66facc5 100644 --- a/src/tests/utils.cairo +++ b/src/tests/utils.cairo @@ -11,4 +11,4 @@ fn deploy(contract_class_hash: felt252, calldata: Array) -> ContractAdd ) .unwrap(); address -} +} \ No newline at end of file diff --git a/src/upgrades/upgradeable.cairo b/src/upgrades/upgradeable.cairo deleted file mode 100644 index 570ed11..0000000 --- a/src/upgrades/upgradeable.cairo +++ /dev/null @@ -1,58 +0,0 @@ -use starknet::class_hash::ClassHash; - -#[starknet::interface] -trait IUpgradeable { - fn upgrade(ref self: TContractState, impl_hash: ClassHash); - fn upgrade_and_call( - ref self: TContractState, impl_hash: ClassHash, selector: felt252, calldata: Array - ); -} - -#[starknet::contract] -mod Upgradeable { - use super::IUpgradeable; - use starknet::ClassHash; - use starknet::get_contract_address; - use starknet::SyscallResult; - use zeroable::Zeroable; - - #[storage] - struct Storage {} - - // - // Events - // - - #[event] - #[derive(Drop, starknet::Event)] - enum Event { - Upgraded: Upgraded, - } - - #[derive(Drop, starknet::Event)] - struct Upgraded { - implementation: ClassHash, - } - - #[generate_trait] - impl InternalImpl of InternalState { - fn _upgrade(ref self: ContractState, impl_hash: ClassHash) { - assert(!impl_hash.is_zero(), 'Class hash cannot be zero'); - starknet::replace_class_syscall(impl_hash).unwrap_syscall(); - self.emit(Upgraded { implementation: impl_hash }); - } - - fn _upgrade_and_call( - ref self: ContractState, - impl_hash: ClassHash, - selector: felt252, - calldata: Span - ) { - self._upgrade(impl_hash); - // The call_contract syscall is used in order to call a selector from the new class. - // See: https://docs.starknet.io/documentation/architecture_and_concepts/Contracts/system-calls-cairo1/#replace_class - starknet::call_contract_syscall(get_contract_address(), selector, calldata) - .unwrap_syscall(); - } - } -}