|
| 1 | +#[starknet::contract] |
| 2 | +pub mod ChainLib { |
| 3 | + use starknet::storage::{ |
| 4 | + Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess, |
| 5 | + StoragePointerWriteAccess, |
| 6 | + }; |
| 7 | + use starknet::{ContractAddress, get_block_timestamp, get_caller_address}; |
| 8 | + use crate::interfaces::IChainLib::IChainLib; |
| 9 | + use crate::base::types::{TokenBoundAccount}; |
1 | 10 |
|
| 11 | + |
| 12 | + #[storage] |
| 13 | + struct Storage { |
| 14 | + // Contract addresses for component management |
| 15 | + deployed: bool, |
| 16 | + current_account_id: u256, |
| 17 | + accounts: Map<u256, TokenBoundAccount>, |
| 18 | + accountsaddr: Map<ContractAddress, TokenBoundAccount>, |
| 19 | + next_course_id: u256, |
| 20 | + nuum: Map<u8, u8>, |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + #[constructor] |
| 25 | + fn constructor(ref self: ContractState) { |
| 26 | + // Store the values in contract state |
| 27 | + self.deployed.write(true); |
| 28 | + } |
| 29 | + |
| 30 | + #[event] |
| 31 | + #[derive(Drop, starknet::Event)] |
| 32 | + pub enum Event { |
| 33 | + TokenBountAccountcreated: TokenBountAccountcreated, |
| 34 | + } |
| 35 | + |
| 36 | + #[derive(Drop, starknet::Event)] |
| 37 | + pub struct TokenBountAccountcreated { |
| 38 | + pub id: u256, |
| 39 | + } |
| 40 | + |
| 41 | + #[abi(embed_v0)] |
| 42 | + impl ChainLibNetImpl of IChainLib<ContractState> { |
| 43 | + fn create_token_account( |
| 44 | + ref self: ContractState, user_name: felt252, init_param1: felt252, init_param2: felt252, |
| 45 | + ) -> u256 { |
| 46 | + // Validate input parameters. |
| 47 | + assert!(user_name != 0, "User name cannot be empty"); |
| 48 | + assert!(init_param1 != 0, "Initialization parameter 1 cannot be empty"); |
| 49 | + |
| 50 | + // Retrieve the current account ID before incrementing. |
| 51 | + let account_id = self.current_account_id.read(); |
| 52 | + |
| 53 | + // Create a new token bound account struct. |
| 54 | + let new_token_bound_account = TokenBoundAccount { |
| 55 | + id: account_id, |
| 56 | + address: get_caller_address(), |
| 57 | + user_name: user_name, |
| 58 | + init_param1: init_param1, |
| 59 | + init_param2: init_param2, |
| 60 | + created_at: get_block_timestamp(), |
| 61 | + updated_at: get_block_timestamp(), |
| 62 | + }; |
| 63 | + |
| 64 | + // Store the new account in the accounts map. |
| 65 | + self.accounts.write(account_id, new_token_bound_account); |
| 66 | + |
| 67 | + // Increment the account ID counter after using the current value. |
| 68 | + self.current_account_id.write(account_id + 1); |
| 69 | + |
| 70 | + // Emit an event to signal the creation of the token bound account. |
| 71 | + self.emit(TokenBountAccountcreated { id: account_id }); |
| 72 | + |
| 73 | + account_id |
| 74 | + } |
| 75 | + |
| 76 | + fn get_token_bound_account(ref self: ContractState, id: u256) -> TokenBoundAccount { |
| 77 | + let token_bound_account = self.accounts.read(id); |
| 78 | + token_bound_account |
| 79 | + } |
| 80 | + fn get_token_bound_account_by_owner( |
| 81 | + ref self: ContractState, address: ContractAddress |
| 82 | + ) -> TokenBoundAccount { |
| 83 | + let token_bound_account = self.accountsaddr.read(address); |
| 84 | + token_bound_account |
| 85 | + } |
| 86 | + fn test_deployment(ref self: ContractState) -> bool { |
| 87 | + self.deployed.read() |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments