|
| 1 | +#[starknet::contract] |
| 2 | +pub mod TimelyCapsuleToken { |
| 3 | + use core::num::traits::Zero; |
| 4 | + use core::starknet::storage::{ |
| 5 | + Map, StoragePathEntry, StoragePointerReadAccess, StoragePointerWriteAccess, |
| 6 | + }; |
| 7 | + use starknet::event::EventEmitter; |
| 8 | + use starknet::{ContractAddress, get_caller_address}; |
| 9 | + use crate::interfaces::IERC20; |
| 10 | + |
| 11 | + #[storage] |
| 12 | + pub struct Storage { |
| 13 | + balances: Map<ContractAddress, u256>, |
| 14 | + allowances: Map<(ContractAddress, ContractAddress), u256>, |
| 15 | + token_name: ByteArray, |
| 16 | + symbol: ByteArray, |
| 17 | + decimal: u8, |
| 18 | + total_supply: u256, |
| 19 | + owner: ContractAddress, |
| 20 | + } |
| 21 | + |
| 22 | + #[event] |
| 23 | + #[derive(Drop, starknet::Event)] |
| 24 | + pub enum Event { |
| 25 | + Transfer: Transfer, |
| 26 | + Approval: Approval, |
| 27 | + } |
| 28 | + |
| 29 | + #[derive(Drop, starknet::Event)] |
| 30 | + pub struct Transfer { |
| 31 | + #[key] |
| 32 | + from: ContractAddress, |
| 33 | + #[key] |
| 34 | + to: ContractAddress, |
| 35 | + amount: u256, |
| 36 | + } |
| 37 | + |
| 38 | + #[derive(Drop, starknet::Event)] |
| 39 | + pub struct Approval { |
| 40 | + #[key] |
| 41 | + owner: ContractAddress, |
| 42 | + #[key] |
| 43 | + spender: ContractAddress, |
| 44 | + value: u256, |
| 45 | + } |
| 46 | + |
| 47 | + #[constructor] |
| 48 | + fn constructor(ref self: ContractState, owner: ContractAddress) { |
| 49 | + self.token_name.write("TimelyCapsule"); |
| 50 | + self.symbol.write("DWT"); |
| 51 | + self.decimal.write(18); |
| 52 | + self.owner.write(owner); |
| 53 | + } |
| 54 | + |
| 55 | + #[abi(embed_v0)] |
| 56 | + impl TimelyCapsuleTokenImpl of IERC20<ContractState> { |
| 57 | + fn total_supply(self: @ContractState) -> u256 { |
| 58 | + self.total_supply.read() |
| 59 | + } |
| 60 | + |
| 61 | + fn balance_of(self: @ContractState, account: ContractAddress) -> u256 { |
| 62 | + let balance = self.balances.entry(account).read(); |
| 63 | + |
| 64 | + balance |
| 65 | + } |
| 66 | + |
| 67 | + fn allowance( |
| 68 | + self: @ContractState, owner: ContractAddress, spender: ContractAddress, |
| 69 | + ) -> u256 { |
| 70 | + let allowance = self.allowances.entry((owner, spender)).read(); |
| 71 | + |
| 72 | + allowance |
| 73 | + } |
| 74 | + |
| 75 | + fn transfer(ref self: ContractState, recipient: ContractAddress, amount: u256) -> bool { |
| 76 | + let sender = get_caller_address(); |
| 77 | + |
| 78 | + let sender_prev_balance = self.balances.entry(sender).read(); |
| 79 | + let recipient_prev_balance = self.balances.entry(recipient).read(); |
| 80 | + |
| 81 | + assert(sender_prev_balance >= amount, 'Insufficient amount'); |
| 82 | + |
| 83 | + self.balances.entry(sender).write(sender_prev_balance - amount); |
| 84 | + self.balances.entry(recipient).write(recipient_prev_balance + amount); |
| 85 | + |
| 86 | + assert( |
| 87 | + self.balances.entry(recipient).read() > recipient_prev_balance, |
| 88 | + 'Transaction failed', |
| 89 | + ); |
| 90 | + |
| 91 | + self.emit(Transfer { from: sender, to: recipient, amount }); |
| 92 | + |
| 93 | + true |
| 94 | + } |
| 95 | + |
| 96 | + fn transfer_from( |
| 97 | + ref self: ContractState, |
| 98 | + sender: ContractAddress, |
| 99 | + recipient: ContractAddress, |
| 100 | + amount: u256, |
| 101 | + ) -> bool { |
| 102 | + let spender = get_caller_address(); |
| 103 | + |
| 104 | + let spender_allowance = self.allowances.entry((sender, spender)).read(); |
| 105 | + let sender_balance = self.balances.entry(sender).read(); |
| 106 | + let recipient_balance = self.balances.entry(recipient).read(); |
| 107 | + |
| 108 | + assert(amount <= spender_allowance, 'amount exceeds allowance'); |
| 109 | + assert(amount <= sender_balance, 'amount exceeds balance'); |
| 110 | + |
| 111 | + self.allowances.entry((sender, spender)).write(spender_allowance - amount); |
| 112 | + self.balances.entry(sender).write(sender_balance - amount); |
| 113 | + self.balances.entry(recipient).write(recipient_balance + amount); |
| 114 | + |
| 115 | + self.emit(Transfer { from: sender, to: recipient, amount }); |
| 116 | + |
| 117 | + true |
| 118 | + } |
| 119 | + |
| 120 | + fn approve(ref self: ContractState, spender: ContractAddress, amount: u256) -> bool { |
| 121 | + let caller = get_caller_address(); |
| 122 | + |
| 123 | + self.allowances.entry((caller, spender)).write(amount); |
| 124 | + |
| 125 | + self.emit(Approval { owner: caller, spender, value: amount }); |
| 126 | + |
| 127 | + true |
| 128 | + } |
| 129 | + |
| 130 | + fn name(self: @ContractState) -> ByteArray { |
| 131 | + self.token_name.read() |
| 132 | + } |
| 133 | + |
| 134 | + fn symbol(self: @ContractState) -> ByteArray { |
| 135 | + self.symbol.read() |
| 136 | + } |
| 137 | + |
| 138 | + fn decimals(self: @ContractState) -> u8 { |
| 139 | + self.decimal.read() |
| 140 | + } |
| 141 | + |
| 142 | + fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) -> bool { |
| 143 | + let previous_total_supply = self.total_supply.read(); |
| 144 | + let previous_balance = self.balances.entry(recipient).read(); |
| 145 | + |
| 146 | + self.total_supply.write(previous_total_supply + amount); |
| 147 | + self.balances.entry(recipient).write(previous_balance + amount); |
| 148 | + |
| 149 | + let zero_address = Zero::zero(); |
| 150 | + |
| 151 | + self.emit(Transfer { from: zero_address, to: recipient, amount }); |
| 152 | + |
| 153 | + true |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments