From f23413df96fbab55737e310cc73ea5d5b387f573 Mon Sep 17 00:00:00 2001 From: Harshal Maniya Date: Mon, 20 May 2024 10:59:51 +0530 Subject: [PATCH 1/8] add referral contracts --- .gitignore | 4 + Scarb.lock | 20 +++ Scarb.toml | 18 +++ snfoundry.toml | 4 + src/lib.cairo | 7 + src/referral_storage.cairo | 110 +++++++++++++ src/test_contracts/referral_storage_v2.cairo | 159 +++++++++++++++++++ tests/lib.cairo | 1 + tests/referral_storage_test.cairo | 62 ++++++++ 9 files changed, 385 insertions(+) create mode 100644 .gitignore create mode 100644 Scarb.lock create mode 100644 Scarb.toml create mode 100644 snfoundry.toml create mode 100644 src/lib.cairo create mode 100644 src/referral_storage.cairo create mode 100644 src/test_contracts/referral_storage_v2.cairo create mode 100644 tests/lib.cairo create mode 100644 tests/referral_storage_test.cairo diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..458b991 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store + +.snfoundry_cache/ +target/ \ No newline at end of file diff --git a/Scarb.lock b/Scarb.lock new file mode 100644 index 0000000..2207882 --- /dev/null +++ b/Scarb.lock @@ -0,0 +1,20 @@ +# Code generated by scarb DO NOT EDIT. +version = 1 + +[[package]] +name = "gmx_referral_cairo" +version = "0.1.0" +dependencies = [ + "openzeppelin", + "snforge_std", +] + +[[package]] +name = "openzeppelin" +version = "0.11.0" +source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.11.0#a83f36b23f1af6e160288962be4a2701c3ecbcda" + +[[package]] +name = "snforge_std" +version = "0.20.1" +source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.20.1#fea2db8f2b20148cc15ee34b08de12028eb42942" diff --git a/Scarb.toml b/Scarb.toml new file mode 100644 index 0000000..f896f40 --- /dev/null +++ b/Scarb.toml @@ -0,0 +1,18 @@ +[package] +name = "gmx_referral_cairo" +version = "0.1.0" + +[lib] + + +[[target.starknet-contract]] +sierra = true +casm = true +casm-add-pythonic-hints = true +allowed-libfuncs-list.name = "all" +build-external-contracts = ["openzeppelin::presets::erc20::ERC20"] + +[dependencies] +starknet = "2.4.3" +openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.11.0" } +snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.20.1" } \ No newline at end of file diff --git a/snfoundry.toml b/snfoundry.toml new file mode 100644 index 0000000..da7836f --- /dev/null +++ b/snfoundry.toml @@ -0,0 +1,4 @@ +[sncast.default] +account = "my_imported_account" +accounts-file = "~/my_accounts.json" +url = "https://api-starknet-sepolia.dwellir.com/dd28e566-3260-4d8d-8180-6ef1a161e41c" \ No newline at end of file diff --git a/src/lib.cairo b/src/lib.cairo new file mode 100644 index 0000000..9cfe5fa --- /dev/null +++ b/src/lib.cairo @@ -0,0 +1,7 @@ +mod referral_storage; + +mod test_contracts { + mod referral_storage_v2; +} + + diff --git a/src/referral_storage.cairo b/src/referral_storage.cairo new file mode 100644 index 0000000..441b3d1 --- /dev/null +++ b/src/referral_storage.cairo @@ -0,0 +1,110 @@ +use starknet::{ContractAddress,ClassHash}; +use zeroable::Zeroable; + + +#[starknet::interface] +trait IReferralStorage { + fn get_trader_referral_code( + self: @TContractState, + _account: ContractAddress, + ) -> ContractAddress; + + fn set_trader_referral_code( + ref self: TContractState, + _code: ContractAddress, + ); + + fn upgrade(ref self: TContractState, new_class_hash: ClassHash); +} + + +#[starknet::contract] +mod ReferralStorage { + use starknet::ContractAddress; + use starknet::get_caller_address; + use zeroable::Zeroable; + use starknet::ClassHash; + use openzeppelin::access::ownable::OwnableComponent; + use openzeppelin::upgrades::UpgradeableComponent; + use openzeppelin::upgrades::interface::IUpgradeable; + + + component!(path: UpgradeableComponent, storage: upgradeable_storage, event: UpgradeableEvent); + component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); + + #[abi(embed_v0)] + /// Ownable + impl OwnableImpl = OwnableComponent::OwnableImpl; + impl OwnableInternalImpl = OwnableComponent::InternalImpl; + + /// Upgradeable + impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl; + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + SetTraderReferralCode: SetTraderReferralCode, + #[flat] + OwnableEvent: OwnableComponent::Event, + #[flat] + UpgradeableEvent: UpgradeableComponent::Event + } + + + #[derive(Drop, starknet::Event)] + struct SetTraderReferralCode { + account: ContractAddress, + code: ContractAddress, + } + + #[storage] + struct Storage { + // @notice Maps the trader to the referee code + trader_referral_codes: LegacyMap::, + + #[substorage(v0)] + ownable: OwnableComponent::Storage, + #[substorage(v0)] + upgradeable_storage: UpgradeableComponent::Storage, + } + + #[constructor] + fn constructor(ref self: ContractState, owner: ContractAddress) { + self.ownable.initializer(owner); + } + + #[abi(embed_v0)] + impl ReferralStorage of super::IReferralStorage { + + fn get_trader_referral_code( + self: @ContractState, + _account: ContractAddress, + ) -> ContractAddress { + self.trader_referral_codes.read(_account) + } + + // @notice Sets the code to be referred by the trader + // @param _code The code to set + // @dev Trader can set the code only once + // @dev Code owner cannot set the code for himself + fn set_trader_referral_code( + ref self: ContractState, + _code: ContractAddress, + ){ + assert!(_code != get_caller_address(), "ReferralStorage: code owner cannot set code for himself"); + + if(!self.trader_referral_codes.read(get_caller_address()).is_non_zero()){ + let _account = get_caller_address(); + self.trader_referral_codes.write(_account, _code); + self.emit(SetTraderReferralCode{account:_account, code: _code}); + } + } + + fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { + self.ownable.assert_only_owner(); + + self.upgradeable_storage._upgrade(new_class_hash); + } + } +} + diff --git a/src/test_contracts/referral_storage_v2.cairo b/src/test_contracts/referral_storage_v2.cairo new file mode 100644 index 0000000..8f23fe4 --- /dev/null +++ b/src/test_contracts/referral_storage_v2.cairo @@ -0,0 +1,159 @@ +use starknet::{ContractAddress,ClassHash}; +use zeroable::Zeroable; + + +#[starknet::interface] +trait IReferralStorageV2 { + fn get_trader_referral_code( + ref self: TContractState, + _account: ContractAddress, + ) -> felt252; + + fn get_code_owner( + ref self: TContractState, + _code: felt252, + ) -> ContractAddress; + + fn get_code_from_owner( + ref self: TContractState, + _account: ContractAddress, + ) -> felt252; + + fn set_trader_referral_code( + ref self: TContractState, + _code: felt252, + ); + + fn register_code( + ref self: TContractState, + _code: felt252, + ); + + fn upgrade(ref self: TContractState, new_class_hash: ClassHash); + +} + + +#[starknet::contract] +mod ReferralStorageV2 { + use starknet::ContractAddress; + use starknet::get_caller_address; + use zeroable::Zeroable; + use starknet::ClassHash; + use openzeppelin::access::ownable::OwnableComponent; + use openzeppelin::upgrades::UpgradeableComponent; + use openzeppelin::upgrades::interface::IUpgradeable; + + + component!(path: UpgradeableComponent, storage: upgradeable_storage, event: UpgradeableEvent); + component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + SetTraderReferralCode: SetTraderReferralCode, + RegisterCode: RegisterCode, + #[flat] + OwnableEvent: OwnableComponent::Event, + #[flat] + UpgradeableEvent: UpgradeableComponent::Event + } + + + #[derive(Drop, starknet::Event)] + struct SetTraderReferralCode { + account: ContractAddress, + code: felt252, + } + + #[derive(Drop, starknet::Event)] + struct RegisterCode { + code: felt252, + account: ContractAddress, + } + + #[storage] + struct Storage { + code_owner: LegacyMap::, + owner_to_code: LegacyMap::, + trader_referral_codes: LegacyMap::, + #[substorage(v0)] + ownable: OwnableComponent::Storage, + #[substorage(v0)] + upgradeable_storage: UpgradeableComponent::Storage, + } + + #[constructor] + fn constructor(ref self: ContractState, owner: ContractAddress) { + self.ownable.initializer(owner); + } + + /// Ownable + #[abi(embed_v0)] + impl OwnableImpl = OwnableComponent::OwnableImpl; + impl OwnableInternalImpl = OwnableComponent::InternalImpl; + + /// Upgradeable + impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl; + + #[abi(embed_v0)] + impl ReferralStorageV2 of super::IReferralStorageV2 { + + fn get_trader_referral_code( + ref self: ContractState, + _account: ContractAddress, + ) -> felt252 { + self.trader_referral_codes.read(_account) + } + + fn get_code_owner( + ref self: ContractState, + _code: felt252, + ) -> ContractAddress { + self.code_owner.read(_code) + } + + fn get_code_from_owner( + ref self: ContractState, + _account: ContractAddress, + ) -> felt252 { + self.owner_to_code.read(_account) + } + + fn set_trader_referral_code( + ref self: ContractState, + _code: felt252, + ){ + assert!(self.code_owner.read(_code).is_non_zero(), "ReferralStorage: code not found"); + assert!(self.code_owner.read(_code) != get_caller_address(), "ReferralStorage: code owner cannot set code for himself"); + + let _account = get_caller_address(); + self.trader_referral_codes.write(_account, _code); + self.emit(SetTraderReferralCode{account:_account, code: _code}); + } + + fn register_code( + ref self: ContractState, + _code: felt252, + ){ + assert!(self.owner_to_code.read(get_caller_address()).is_non_zero(), "ReferralStorage: user already has a code"); + + assert!(!self.code_owner.read(_code).is_non_zero(), "ReferralStorage: code already registered"); + + self.code_owner.write(_code, get_caller_address()); + self.owner_to_code.write(get_caller_address(), _code); + + self.emit(RegisterCode{code:_code, account: get_caller_address()}); + } + + fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { + self.ownable.assert_only_owner(); + + self.upgradeable_storage._upgrade(new_class_hash); + } + } + + + +} + diff --git a/tests/lib.cairo b/tests/lib.cairo new file mode 100644 index 0000000..6e98ca5 --- /dev/null +++ b/tests/lib.cairo @@ -0,0 +1 @@ +mod referral_storage_test; \ No newline at end of file diff --git a/tests/referral_storage_test.cairo b/tests/referral_storage_test.cairo new file mode 100644 index 0000000..9bb8bd5 --- /dev/null +++ b/tests/referral_storage_test.cairo @@ -0,0 +1,62 @@ +use starknet::{ContractAddress,get_caller_address,contract_address_try_from_felt252}; +use zeroable::Zeroable; +use gmx_referral_cairo::referral_storage::{ReferralStorage,IReferralStorageDispatcher,IReferralStorageDispatcherTrait}; +use snforge_std::{ + declare, ContractClassTrait, start_prank, stop_prank, CheatTarget, spy_events, + SpyOn, EventSpy, EventFetcher, Event, EventAssertions +}; +use gmx_referral_cairo::test_contracts::referral_storage_v2::{ReferralStorageV2,IReferralStorageV2Dispatcher,IReferralStorageV2DispatcherTrait}; + +fn setup_referral_storage_dispatcher() -> (ContractAddress,IReferralStorageDispatcher,ContractAddress) { + let contract = declare("ReferralStorage"); + + let mut contract_constructor_calldata = Default::default(); + let owner = contract_address_try_from_felt252('owner').unwrap(); + + Serde::serialize(@owner, ref contract_constructor_calldata); + let contract_address = contract.deploy(@contract_constructor_calldata).unwrap(); + + (contract_address,IReferralStorageDispatcher { contract_address },owner) +} + +#[test] +fn test_set_trader_referral_code() { + let (contract_address,dispatcher,_owner) = setup_referral_storage_dispatcher(); + + let user = 123.try_into().unwrap(); + + let user2 = 124.try_into().unwrap(); + start_prank(CheatTarget::One(contract_address), user2); + dispatcher.set_trader_referral_code(user); + + let user2_referral_code = dispatcher.get_trader_referral_code(user2); + assert_eq!(user2_referral_code, user); +} + +#[test] +#[should_panic(expected: ("ReferralStorage: code owner cannot set code for himself",))] +fn test_set_trader_referral_code_self() { + let (contract_address,dispatcher,_owner) = setup_referral_storage_dispatcher(); + + let user = 123.try_into().unwrap(); + start_prank(CheatTarget::One(contract_address), user); + dispatcher.set_trader_referral_code(user); +} + +#[test] +#[should_panic] +fn test_upgrade_referral_storage_by_not_onwer() { + let (_contract_address,dispatcher,_owner) = setup_referral_storage_dispatcher(); + + let new_pool_class_hash = declare("ReferralStorageV2").class_hash; + dispatcher.upgrade(new_pool_class_hash); +} + +#[test] +fn test_upgrade_referral_storage() { + let (contract_address,dispatcher,owner) = setup_referral_storage_dispatcher(); + + let new_pool_class_hash = declare("ReferralStorageV2").class_hash; + start_prank(CheatTarget::One(contract_address), owner); + dispatcher.upgrade(new_pool_class_hash); +} From 322c004704acccae0374c24976fd2581b7b3aa52 Mon Sep 17 00:00:00 2001 From: Harshal Maniya Date: Wed, 29 May 2024 11:34:09 +0530 Subject: [PATCH 2/8] review update --- Scarb.lock | 2 +- Scarb.toml | 3 +- src/referral_storage.cairo | 41 ++++++++++--------- src/test_contracts/referral_storage_v2.cairo | 22 +++++----- tests/referral_storage_test.cairo | 42 ++++++++++++++------ 5 files changed, 62 insertions(+), 48 deletions(-) diff --git a/Scarb.lock b/Scarb.lock index 2207882..1b0828d 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -2,7 +2,7 @@ version = 1 [[package]] -name = "gmx_referral_cairo" +name = "jediswap_referral" version = "0.1.0" dependencies = [ "openzeppelin", diff --git a/Scarb.toml b/Scarb.toml index f896f40..2d690bb 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -1,5 +1,5 @@ [package] -name = "gmx_referral_cairo" +name = "jediswap_referral" version = "0.1.0" [lib] @@ -10,7 +10,6 @@ sierra = true casm = true casm-add-pythonic-hints = true allowed-libfuncs-list.name = "all" -build-external-contracts = ["openzeppelin::presets::erc20::ERC20"] [dependencies] starknet = "2.4.3" diff --git a/src/referral_storage.cairo b/src/referral_storage.cairo index 441b3d1..0d726e4 100644 --- a/src/referral_storage.cairo +++ b/src/referral_storage.cairo @@ -4,14 +4,14 @@ use zeroable::Zeroable; #[starknet::interface] trait IReferralStorage { - fn get_trader_referral_code( + fn get_referrer( self: @TContractState, _account: ContractAddress, ) -> ContractAddress; - fn set_trader_referral_code( + fn set_referrer( ref self: TContractState, - _code: ContractAddress, + _address: ContractAddress, ); fn upgrade(ref self: TContractState, new_class_hash: ClassHash); @@ -28,7 +28,6 @@ mod ReferralStorage { use openzeppelin::upgrades::UpgradeableComponent; use openzeppelin::upgrades::interface::IUpgradeable; - component!(path: UpgradeableComponent, storage: upgradeable_storage, event: UpgradeableEvent); component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); @@ -43,7 +42,7 @@ mod ReferralStorage { #[event] #[derive(Drop, starknet::Event)] enum Event { - SetTraderReferralCode: SetTraderReferralCode, + SetReferrer: SetReferrer, #[flat] OwnableEvent: OwnableComponent::Event, #[flat] @@ -52,15 +51,15 @@ mod ReferralStorage { #[derive(Drop, starknet::Event)] - struct SetTraderReferralCode { + struct SetReferrer { account: ContractAddress, - code: ContractAddress, + referrer: ContractAddress, } #[storage] struct Storage { - // @notice Maps the trader to the referee code - trader_referral_codes: LegacyMap::, + // @notice Maps the referee to the referrer + referrers: LegacyMap::, #[substorage(v0)] ownable: OwnableComponent::Storage, @@ -76,27 +75,27 @@ mod ReferralStorage { #[abi(embed_v0)] impl ReferralStorage of super::IReferralStorage { - fn get_trader_referral_code( + fn get_referrer( self: @ContractState, _account: ContractAddress, ) -> ContractAddress { - self.trader_referral_codes.read(_account) + self.referrers.read(_account) } - // @notice Sets the code to be referred by the trader - // @param _code The code to set - // @dev Trader can set the code only once - // @dev Code owner cannot set the code for himself - fn set_trader_referral_code( + // @notice Sets the referrer to be referred by the caller + // @param _address The referrer to set + // @dev An address can set the referrer only once + // @dev referrer cannot set refer himself + fn set_referrer( ref self: ContractState, - _code: ContractAddress, + _address: ContractAddress, ){ - assert!(_code != get_caller_address(), "ReferralStorage: code owner cannot set code for himself"); + assert!(_address != get_caller_address(), "ReferralStorage: referrer cannot refer himself"); - if(!self.trader_referral_codes.read(get_caller_address()).is_non_zero()){ + if(!self.referrers.read(get_caller_address()).is_non_zero()){ let _account = get_caller_address(); - self.trader_referral_codes.write(_account, _code); - self.emit(SetTraderReferralCode{account:_account, code: _code}); + self.referrers.write(_account, _address); + self.emit(SetReferrer{account:_account, referrer: _address}); } } diff --git a/src/test_contracts/referral_storage_v2.cairo b/src/test_contracts/referral_storage_v2.cairo index 8f23fe4..f0ce3ca 100644 --- a/src/test_contracts/referral_storage_v2.cairo +++ b/src/test_contracts/referral_storage_v2.cairo @@ -4,7 +4,7 @@ use zeroable::Zeroable; #[starknet::interface] trait IReferralStorageV2 { - fn get_trader_referral_code( + fn get_referrer( ref self: TContractState, _account: ContractAddress, ) -> felt252; @@ -19,7 +19,7 @@ trait IReferralStorageV2 { _account: ContractAddress, ) -> felt252; - fn set_trader_referral_code( + fn set_referrer( ref self: TContractState, _code: felt252, ); @@ -51,7 +51,7 @@ mod ReferralStorageV2 { #[event] #[derive(Drop, starknet::Event)] enum Event { - SetTraderReferralCode: SetTraderReferralCode, + SetReferrer: SetReferrer, RegisterCode: RegisterCode, #[flat] OwnableEvent: OwnableComponent::Event, @@ -61,7 +61,7 @@ mod ReferralStorageV2 { #[derive(Drop, starknet::Event)] - struct SetTraderReferralCode { + struct SetReferrer { account: ContractAddress, code: felt252, } @@ -76,7 +76,7 @@ mod ReferralStorageV2 { struct Storage { code_owner: LegacyMap::, owner_to_code: LegacyMap::, - trader_referral_codes: LegacyMap::, + referrers: LegacyMap::, #[substorage(v0)] ownable: OwnableComponent::Storage, #[substorage(v0)] @@ -99,11 +99,11 @@ mod ReferralStorageV2 { #[abi(embed_v0)] impl ReferralStorageV2 of super::IReferralStorageV2 { - fn get_trader_referral_code( + fn get_referrer( ref self: ContractState, _account: ContractAddress, ) -> felt252 { - self.trader_referral_codes.read(_account) + self.referrers.read(_account) } fn get_code_owner( @@ -120,16 +120,16 @@ mod ReferralStorageV2 { self.owner_to_code.read(_account) } - fn set_trader_referral_code( + fn set_referrer( ref self: ContractState, _code: felt252, ){ assert!(self.code_owner.read(_code).is_non_zero(), "ReferralStorage: code not found"); - assert!(self.code_owner.read(_code) != get_caller_address(), "ReferralStorage: code owner cannot set code for himself"); + assert!(self.code_owner.read(_code) != get_caller_address(), "ReferralStorage: referrer cannot refer himself"); let _account = get_caller_address(); - self.trader_referral_codes.write(_account, _code); - self.emit(SetTraderReferralCode{account:_account, code: _code}); + self.referrers.write(_account, _code); + self.emit(SetReferrer{account:_account, code: _code}); } fn register_code( diff --git a/tests/referral_storage_test.cairo b/tests/referral_storage_test.cairo index 9bb8bd5..a61676a 100644 --- a/tests/referral_storage_test.cairo +++ b/tests/referral_storage_test.cairo @@ -1,11 +1,13 @@ use starknet::{ContractAddress,get_caller_address,contract_address_try_from_felt252}; use zeroable::Zeroable; -use gmx_referral_cairo::referral_storage::{ReferralStorage,IReferralStorageDispatcher,IReferralStorageDispatcherTrait}; +use jediswap_referral::referral_storage::{ReferralStorage,IReferralStorageDispatcher,IReferralStorageDispatcherTrait}; use snforge_std::{ declare, ContractClassTrait, start_prank, stop_prank, CheatTarget, spy_events, SpyOn, EventSpy, EventFetcher, Event, EventAssertions }; -use gmx_referral_cairo::test_contracts::referral_storage_v2::{ReferralStorageV2,IReferralStorageV2Dispatcher,IReferralStorageV2DispatcherTrait}; +use jediswap_referral::test_contracts::referral_storage_v2::{ReferralStorageV2,IReferralStorageV2Dispatcher,IReferralStorageV2DispatcherTrait}; +use openzeppelin::upgrades::upgradeable::UpgradeableComponent; + fn setup_referral_storage_dispatcher() -> (ContractAddress,IReferralStorageDispatcher,ContractAddress) { let contract = declare("ReferralStorage"); @@ -20,43 +22,57 @@ fn setup_referral_storage_dispatcher() -> (ContractAddress,IReferralStorageDispa } #[test] -fn test_set_trader_referral_code() { +fn test_set_referrer() { let (contract_address,dispatcher,_owner) = setup_referral_storage_dispatcher(); let user = 123.try_into().unwrap(); let user2 = 124.try_into().unwrap(); start_prank(CheatTarget::One(contract_address), user2); - dispatcher.set_trader_referral_code(user); + dispatcher.set_referrer(user); - let user2_referral_code = dispatcher.get_trader_referral_code(user2); + let user2_referral_code = dispatcher.get_referrer(user2); assert_eq!(user2_referral_code, user); } #[test] -#[should_panic(expected: ("ReferralStorage: code owner cannot set code for himself",))] -fn test_set_trader_referral_code_self() { +#[should_panic(expected: ("ReferralStorage: referrer cannot refer himself",))] +fn test_set_referrer_self() { let (contract_address,dispatcher,_owner) = setup_referral_storage_dispatcher(); let user = 123.try_into().unwrap(); start_prank(CheatTarget::One(contract_address), user); - dispatcher.set_trader_referral_code(user); + dispatcher.set_referrer(user); } #[test] -#[should_panic] +#[should_panic(expected: ("Caller is not the owner",))] fn test_upgrade_referral_storage_by_not_onwer() { let (_contract_address,dispatcher,_owner) = setup_referral_storage_dispatcher(); - let new_pool_class_hash = declare("ReferralStorageV2").class_hash; - dispatcher.upgrade(new_pool_class_hash); + let new_storage_class_hash = declare("ReferralStorageV2").class_hash; + dispatcher.upgrade(new_storage_class_hash); } #[test] fn test_upgrade_referral_storage() { let (contract_address,dispatcher,owner) = setup_referral_storage_dispatcher(); - let new_pool_class_hash = declare("ReferralStorageV2").class_hash; + let new_storage_class_hash = declare("ReferralStorageV2").class_hash; start_prank(CheatTarget::One(contract_address), owner); - dispatcher.upgrade(new_pool_class_hash); + dispatcher.upgrade(new_storage_class_hash); + + let mut spy = spy_events(SpyOn::One(contract_address)); + + + spy.assert_emitted( + @array![ + ( + contract_address, + UpgradeableComponent::Event::Upgraded( + UpgradeableComponent::Upgraded { class_hash: new_storage_class_hash } + ) + ) + ] + ); } From 688e20ff083c00085af321db8cfaf6340430be14 Mon Sep 17 00:00:00 2001 From: Prince Arora <77785932+princearoragithub@users.noreply.github.com> Date: Tue, 4 Jun 2024 23:45:10 +0530 Subject: [PATCH 3/8] panic error fix --- tests/referral_storage_test.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/referral_storage_test.cairo b/tests/referral_storage_test.cairo index a61676a..9fbcaa8 100644 --- a/tests/referral_storage_test.cairo +++ b/tests/referral_storage_test.cairo @@ -46,7 +46,7 @@ fn test_set_referrer_self() { } #[test] -#[should_panic(expected: ("Caller is not the owner",))] +#[should_panic(expected: ('Caller is not the owner',))] fn test_upgrade_referral_storage_by_not_onwer() { let (_contract_address,dispatcher,_owner) = setup_referral_storage_dispatcher(); From 81b9f5bfc99a31163c0845aa4263ffcb0d74d8d6 Mon Sep 17 00:00:00 2001 From: Prince Arora <77785932+princearoragithub@users.noreply.github.com> Date: Tue, 4 Jun 2024 23:48:56 +0530 Subject: [PATCH 4/8] spying before the function call --- tests/referral_storage_test.cairo | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/tests/referral_storage_test.cairo b/tests/referral_storage_test.cairo index 9fbcaa8..7176646 100644 --- a/tests/referral_storage_test.cairo +++ b/tests/referral_storage_test.cairo @@ -59,20 +59,19 @@ fn test_upgrade_referral_storage() { let (contract_address,dispatcher,owner) = setup_referral_storage_dispatcher(); let new_storage_class_hash = declare("ReferralStorageV2").class_hash; + let mut spy = spy_events(SpyOn::One(contract_address)); start_prank(CheatTarget::One(contract_address), owner); dispatcher.upgrade(new_storage_class_hash); - let mut spy = spy_events(SpyOn::One(contract_address)); - - spy.assert_emitted( - @array![ - ( - contract_address, - UpgradeableComponent::Event::Upgraded( - UpgradeableComponent::Upgraded { class_hash: new_storage_class_hash } - ) + spy.assert_emitted( + @array![ + ( + contract_address, + UpgradeableComponent::Event::Upgraded( + UpgradeableComponent::Upgraded { class_hash: new_storage_class_hash } ) - ] - ); + ) + ] + ); } From 11c2e469abc8bf48f953ad635834a9fbfb3c0f32 Mon Sep 17 00:00:00 2001 From: Harshal Date: Sun, 9 Jun 2024 17:10:22 +0530 Subject: [PATCH 5/8] fix: typos --- src/referral_storage.cairo | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/referral_storage.cairo b/src/referral_storage.cairo index 0d726e4..73efb18 100644 --- a/src/referral_storage.cairo +++ b/src/referral_storage.cairo @@ -85,12 +85,12 @@ mod ReferralStorage { // @notice Sets the referrer to be referred by the caller // @param _address The referrer to set // @dev An address can set the referrer only once - // @dev referrer cannot set refer himself + // @dev referrer cannot set refer themselves fn set_referrer( ref self: ContractState, _address: ContractAddress, ){ - assert!(_address != get_caller_address(), "ReferralStorage: referrer cannot refer himself"); + assert!(_address != get_caller_address(), "ReferralStorage: referrer cannot refer themselves"); if(!self.referrers.read(get_caller_address()).is_non_zero()){ let _account = get_caller_address(); From d9e244b83c7cdb6deb249fe7ed8da7eb4fd8e09e Mon Sep 17 00:00:00 2001 From: Harshal Date: Sun, 9 Jun 2024 17:27:05 +0530 Subject: [PATCH 6/8] add readme --- README.md | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d0fa90c..01d4fae 100644 --- a/README.md +++ b/README.md @@ -1 +1,38 @@ -# gmx-referral-fork-cairo \ No newline at end of file +# JediSwap Referral Contracts + +This repository consists of the referral contracts of JediSwap protocol, a cairo fork of Uniswap V3. CLAMM for Starknet. + +## Testing and Development + +Prerequisites: + +- [Scarb](https://github.com/software-mansion/scarb) for managing the project. +- [starknet-foundry](https://github.com/foundry-rs/starknet-foundry) for testing and writing scripts. + +### Compile Contracts + +``` +scarb build +``` + +### Run Tests + +``` +snforge test +``` + +### Run Scripts + +#### Run a local devnet + +We use [starknet-devnet-rs](https://github.com/0xSpaceShard/starknet-devnet-rs) + +Run the devnet and add one of the predeployed accounts with your preferred name . See the instructions [here](https://foundry-rs.github.io/starknet-foundry/starknet/account.html#importing-an-account). + +#### Run Scripts + +Run sncast in the parent folder by specifying the path to the script file. Example: + +``` +sncast --url http://127.0.0.1:5050 --account --path-to-scarb-toml scripts/Scarb.toml script deploy_factory_and_pool +``` From 90ab9786fb24eb689d00a821fb5e8c0171223a55 Mon Sep 17 00:00:00 2001 From: Harshal Date: Mon, 10 Jun 2024 23:10:53 +0530 Subject: [PATCH 7/8] fix arguments names --- src/referral_storage.cairo | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/referral_storage.cairo b/src/referral_storage.cairo index 73efb18..08b7337 100644 --- a/src/referral_storage.cairo +++ b/src/referral_storage.cairo @@ -5,13 +5,13 @@ use zeroable::Zeroable; #[starknet::interface] trait IReferralStorage { fn get_referrer( - self: @TContractState, - _account: ContractAddress, + self: @TContractState, + account: ContractAddress, ) -> ContractAddress; fn set_referrer( ref self: TContractState, - _address: ContractAddress, + address: ContractAddress, ); fn upgrade(ref self: TContractState, new_class_hash: ClassHash); @@ -76,26 +76,26 @@ mod ReferralStorage { impl ReferralStorage of super::IReferralStorage { fn get_referrer( - self: @ContractState, - _account: ContractAddress, + self: @ContractState, + account: ContractAddress, ) -> ContractAddress { - self.referrers.read(_account) + self.referrers.read(account) } // @notice Sets the referrer to be referred by the caller - // @param _address The referrer to set + // @param address The referrer to set // @dev An address can set the referrer only once // @dev referrer cannot set refer themselves fn set_referrer( ref self: ContractState, - _address: ContractAddress, + address: ContractAddress, ){ - assert!(_address != get_caller_address(), "ReferralStorage: referrer cannot refer themselves"); + assert!(address != get_caller_address(), "ReferralStorage: referrer cannot refer themselves"); if(!self.referrers.read(get_caller_address()).is_non_zero()){ - let _account = get_caller_address(); - self.referrers.write(_account, _address); - self.emit(SetReferrer{account:_account, referrer: _address}); + let account = get_caller_address(); + self.referrers.write(account, address); + self.emit(SetReferrer{account:account, referrer: address}); } } From edbb482d607d2c98f5cb27800fc7f2493f18f2be Mon Sep 17 00:00:00 2001 From: Harshal Date: Tue, 11 Jun 2024 23:25:21 +0530 Subject: [PATCH 8/8] fix : test_set_referrer_self test --- src/test_contracts/referral_storage_v2.cairo | 2 +- tests/referral_storage_test.cairo | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test_contracts/referral_storage_v2.cairo b/src/test_contracts/referral_storage_v2.cairo index f0ce3ca..d671d4a 100644 --- a/src/test_contracts/referral_storage_v2.cairo +++ b/src/test_contracts/referral_storage_v2.cairo @@ -125,7 +125,7 @@ mod ReferralStorageV2 { _code: felt252, ){ assert!(self.code_owner.read(_code).is_non_zero(), "ReferralStorage: code not found"); - assert!(self.code_owner.read(_code) != get_caller_address(), "ReferralStorage: referrer cannot refer himself"); + assert!(self.code_owner.read(_code) != get_caller_address(), "ReferralStorage: referrer cannot refer themselves"); let _account = get_caller_address(); self.referrers.write(_account, _code); diff --git a/tests/referral_storage_test.cairo b/tests/referral_storage_test.cairo index 7176646..3fb4237 100644 --- a/tests/referral_storage_test.cairo +++ b/tests/referral_storage_test.cairo @@ -36,7 +36,7 @@ fn test_set_referrer() { } #[test] -#[should_panic(expected: ("ReferralStorage: referrer cannot refer himself",))] +#[should_panic(expected: ("ReferralStorage: referrer cannot refer themselves",))] fn test_set_referrer_self() { let (contract_address,dispatcher,_owner) = setup_referral_storage_dispatcher();