Skip to content

Instantly share code, notes, and snippets.

@avirajkhare00
Created August 31, 2024 15:06
Show Gist options
  • Save avirajkhare00/9b235cb0cd87a2d121292bae4230a044 to your computer and use it in GitHub Desktop.
Save avirajkhare00/9b235cb0cd87a2d121292bae4230a044 to your computer and use it in GitHub Desktop.
Starknet contract assignment
#[starknet::contract]
pub mod SimpleCounter {
#[storage]
struct Storage {
// Counter variable
counter: u128,
}
#[constructor]
fn constructor(ref self: ContractState, init_value: u128) {
// Store initial value
self.counter.write(init_value);
}
#[abi(embed_v0)]
impl SimpleCounter of super::ISimpleCounter<ContractState> {
fn get_current_count(self: @ContractState) -> u128 {
return self.counter.read();
}
fn increment(ref self: ContractState) {
// Store counter value + 1
let counter = self.counter.read() + 1;
self.counter.write(counter);
}
fn decrement(ref self: ContractState) {
// Store counter value - 1
let counter = self.counter.read() - 1;
self.counter.write(counter);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment