From 66a02bdc193dcfae6c4be9396e7bf8b4cf9663cf Mon Sep 17 00:00:00 2001 From: GianMarco Date: Thu, 8 Aug 2024 22:20:49 +0700 Subject: [PATCH] Clean challenge-base (#95) --- .../nextjs/utils/scaffold-stark/contract.ts | 2 - packages/snfoundry/contracts/Scarb.toml | 2 +- .../contracts/src/YourContract.cairo | 105 ------------------ packages/snfoundry/contracts/src/lib.cairo | 1 - .../contracts/src/test/TestContract.cairo | 27 ----- 5 files changed, 1 insertion(+), 136 deletions(-) delete mode 100644 packages/snfoundry/contracts/src/YourContract.cairo diff --git a/packages/nextjs/utils/scaffold-stark/contract.ts b/packages/nextjs/utils/scaffold-stark/contract.ts index 8a477ed4..ac9df3cc 100644 --- a/packages/nextjs/utils/scaffold-stark/contract.ts +++ b/packages/nextjs/utils/scaffold-stark/contract.ts @@ -264,8 +264,6 @@ export type ExtractAbiFunctionScaffold< } >; -// let emerson = singleFunction extends listOfFunctions ? true : false; - export type UseScaffoldArgsParam< TContractName extends ContractName, TFunctionName extends ExtractAbiFunctionNamesScaffold< diff --git a/packages/snfoundry/contracts/Scarb.toml b/packages/snfoundry/contracts/Scarb.toml index 68dcaad7..623c1b3f 100644 --- a/packages/snfoundry/contracts/Scarb.toml +++ b/packages/snfoundry/contracts/Scarb.toml @@ -6,7 +6,7 @@ edition = "2023_11" # See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html [dependencies] -starknet = "=2.6.4" +starknet = "2.6.4" openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.14.0" } snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.27.0" } diff --git a/packages/snfoundry/contracts/src/YourContract.cairo b/packages/snfoundry/contracts/src/YourContract.cairo deleted file mode 100644 index d823d136..00000000 --- a/packages/snfoundry/contracts/src/YourContract.cairo +++ /dev/null @@ -1,105 +0,0 @@ -use starknet::ContractAddress; - -#[starknet::interface] -pub trait IYourContract { - fn gretting(self: @TContractState) -> ByteArray; - fn set_gretting(ref self: TContractState, new_greeting: ByteArray, amount_eth: u256); - fn withdraw(ref self: TContractState); - fn premium(self: @TContractState) -> bool; -} - -#[starknet::contract] -mod YourContract { - use openzeppelin::access::ownable::OwnableComponent; - use openzeppelin::token::erc20::interface::{IERC20CamelDispatcher, IERC20CamelDispatcherTrait}; - use starknet::{get_caller_address, get_contract_address}; - use super::{ContractAddress, IYourContract}; - - component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); - - #[abi(embed_v0)] - impl OwnableImpl = OwnableComponent::OwnableImpl; - impl OwnableInternalImpl = OwnableComponent::InternalImpl; - - const ETH_CONTRACT_ADDRESS: felt252 = - 0x49D36570D4E46F48E99674BD3FCC84644DDD6B96F7C741B1562B82F9E004DC7; - - #[event] - #[derive(Drop, starknet::Event)] - enum Event { - #[flat] - OwnableEvent: OwnableComponent::Event, - GreetingChanged: GreetingChanged - } - - #[derive(Drop, starknet::Event)] - struct GreetingChanged { - #[key] - greeting_setter: ContractAddress, - #[key] - new_greeting: ByteArray, - premium: bool, - value: u256, - } - - - #[storage] - struct Storage { - eth_token: IERC20CamelDispatcher, - greeting: ByteArray, - premium: bool, - total_counter: u256, - user_gretting_counter: LegacyMap, - #[substorage(v0)] - ownable: OwnableComponent::Storage, - } - - #[constructor] - fn constructor(ref self: ContractState, owner: ContractAddress) { - let eth_contract_address = ETH_CONTRACT_ADDRESS.try_into().unwrap(); - self.eth_token.write(IERC20CamelDispatcher { contract_address: eth_contract_address }); - self.greeting.write("Building Unstoppable Apps!!!"); - self.ownable.initializer(owner); - } - - #[abi(embed_v0)] - impl YourContractImpl of IYourContract { - fn gretting(self: @ContractState) -> ByteArray { - self.greeting.read() - } - fn set_gretting(ref self: ContractState, new_greeting: ByteArray, amount_eth: u256) { - self.greeting.write(new_greeting); - self.total_counter.write(self.total_counter.read() + 1); - let user_counter = self.user_gretting_counter.read(get_caller_address()); - self.user_gretting_counter.write(get_caller_address(), user_counter + 1); - - if amount_eth > 0 { - // call `approve` on ETH contract before transfer amount_eth - self - .eth_token - .read() - .transferFrom(get_caller_address(), get_contract_address(), amount_eth); - self.premium.write(true); - } else { - self.premium.write(false); - } - self - .emit( - GreetingChanged { - greeting_setter: get_caller_address(), - new_greeting: self.greeting.read(), - premium: true, - value: 100 - } - ); - } - fn withdraw(ref self: ContractState) { - self.ownable.assert_only_owner(); - let balance = self.eth_token.read().balanceOf(get_contract_address()); - self.eth_token.read().transfer(self.ownable.owner(), balance); - } - fn premium(self: @ContractState) -> bool { - self.premium.read() - } - } -} diff --git a/packages/snfoundry/contracts/src/lib.cairo b/packages/snfoundry/contracts/src/lib.cairo index 568f9f72..853bd00a 100644 --- a/packages/snfoundry/contracts/src/lib.cairo +++ b/packages/snfoundry/contracts/src/lib.cairo @@ -1,4 +1,3 @@ -mod YourContract; #[cfg(test)] mod test { mod TestContract; diff --git a/packages/snfoundry/contracts/src/test/TestContract.cairo b/packages/snfoundry/contracts/src/test/TestContract.cairo index b5ebc31d..8b137891 100644 --- a/packages/snfoundry/contracts/src/test/TestContract.cairo +++ b/packages/snfoundry/contracts/src/test/TestContract.cairo @@ -1,28 +1 @@ -use contracts::YourContract::{IYourContractDispatcher, IYourContractDispatcherTrait}; -use openzeppelin::utils::serde::SerializedAppend; -use snforge_std::{declare, ContractClassTrait}; -use starknet::ContractAddress; -fn deploy_contract(name: ByteArray) -> ContractAddress { - let contract = declare(name).unwrap(); - let mut calldata = array![]; - calldata.append_serde(0xb3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca); - let (contract_address, _) = contract.deploy(@calldata).unwrap(); - contract_address -} - -#[test] -fn test_deployment_values() { - let contract_address = deploy_contract("YourContract"); - - let dispatcher = IYourContractDispatcher { contract_address }; - - let current_gretting = dispatcher.gretting(); - let expected_gretting: ByteArray = "Building Unstoppable Apps!!!"; - assert_eq!(current_gretting, expected_gretting, "Should have the right message on deploy"); - - let new_greeting: ByteArray = "Learn Scaffold-Stark 2! :)"; - dispatcher.set_gretting(new_greeting.clone(), 0); // we transfer 0 eth - - assert_eq!(dispatcher.gretting(), new_greeting, "Should allow setting a new message"); -}