|
| 1 | +use starknet::ContractAddress; |
| 2 | + |
| 3 | +#[starknet::interface] |
| 4 | +trait ICounterContract<TContractState> { |
| 5 | + fn increase_counter(ref self: TContractState, amount: u128); |
| 6 | + fn decrease_counter(ref self: TContractState, amount: u128); |
| 7 | + fn get_counter(self: @TContractState) -> u128; |
| 8 | +} |
| 9 | + |
| 10 | + |
| 11 | +#[starknet::contract] |
| 12 | +mod counter_contract { |
| 13 | + use starknet::{ContractAddress, get_caller_address}; |
| 14 | + |
| 15 | + use dependency1::upgradable::upgradable as upgradable_component; |
| 16 | + |
| 17 | + component!(path: upgradable_component, storage: upgradable, event: UpgradableEvent); |
| 18 | + |
| 19 | + #[abi(embed_v0)] |
| 20 | + impl Upgradable = upgradable_component::UpgradableImpl<ContractState>; |
| 21 | + |
| 22 | + impl Ownable of dependency1::upgradable::OwnableTrait<ContractState> { |
| 23 | + fn is_owner(self: @ContractState, address: ContractAddress) -> bool { |
| 24 | + let caller = get_caller_address(); |
| 25 | + let owner = self.owner_address.read(); |
| 26 | + caller == owner |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + #[storage] |
| 31 | + struct Storage { |
| 32 | + counter: u128, |
| 33 | + owner_address: ContractAddress, |
| 34 | + #[substorage(v0)] |
| 35 | + upgradable: upgradable_component::Storage |
| 36 | + } |
| 37 | + |
| 38 | + #[event] |
| 39 | + #[derive(Drop, starknet::Event)] |
| 40 | + enum Event { |
| 41 | + CounterIncreased: CounterIncreased, |
| 42 | + CounterDecreased: CounterDecreased, |
| 43 | + UpgradableEvent: upgradable_component::Event |
| 44 | + } |
| 45 | + |
| 46 | + #[derive(Drop, starknet::Event)] |
| 47 | + struct CounterIncreased { |
| 48 | + amount: u128 |
| 49 | + } |
| 50 | + |
| 51 | + #[derive(Drop, starknet::Event)] |
| 52 | + struct CounterDecreased { |
| 53 | + amount: u128 |
| 54 | + } |
| 55 | + |
| 56 | + #[constructor] |
| 57 | + fn constructor(ref self: ContractState, initial_counter: u128) { |
| 58 | + self.counter.write(initial_counter); |
| 59 | + } |
| 60 | + |
| 61 | + #[external(v0)] |
| 62 | + impl CounterContract of super::ICounterContract<ContractState> { |
| 63 | + fn get_counter(self: @ContractState) -> u128 { |
| 64 | + self.counter.read() |
| 65 | + } |
| 66 | + |
| 67 | + fn increase_counter(ref self: ContractState, amount: u128) { |
| 68 | + if self.is_owner(get_caller_address()) { |
| 69 | + let current = self.counter.read(); |
| 70 | + self.counter.write(current + amount); |
| 71 | + self.emit(CounterIncreased { amount }); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + fn decrease_counter(ref self: ContractState, amount: u128) { |
| 76 | + if self.is_owner(get_caller_address()) { |
| 77 | + let current = self.counter.read(); |
| 78 | + self.counter.write(current - amount); |
| 79 | + self.emit(CounterDecreased { amount }); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments