|
| 1 | +#[starknet::interface] |
| 2 | +pub trait IBuyMeACoffee<TContractState> { |
| 3 | + /// Allows a user to send a tip to the contract owner. |
| 4 | + /// # Arguments |
| 5 | + /// * `amount` - The amount of the tip token to send. |
| 6 | + /// * `message` - A message to accompany the tip. |
| 7 | + fn buy_coffee(ref self: TContractState, amount: u256, message: ByteArray); |
| 8 | + /// Allows the owner to withdraw the entire balance of the tip token from the contract. |
| 9 | + fn withdraw(ref self: TContractState); |
| 10 | + /// Returns an array of all tips received. |
| 11 | + fn get_tips(self: @TContractState) -> Array<Tip>; |
| 12 | + /// Returns the owner of the contract. |
| 13 | + fn get_owner(self: @TContractState) -> starknet::ContractAddress; |
| 14 | +} |
| 15 | + |
| 16 | +#[starknet::contract] |
| 17 | +mod BuyMeACoffee { |
| 18 | + use openzeppelin::access::ownable::OwnableComponent; |
| 19 | + use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait}; |
| 20 | + use starknet::{ContractAddress, get_caller_address, get_contract_address}; |
| 21 | + use super::Tip; |
| 22 | + |
| 23 | + component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); |
| 24 | + |
| 25 | + #[abi(embed_v0)] |
| 26 | + impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>; |
| 27 | + impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>; |
| 28 | + |
| 29 | + #[storage] |
| 30 | + struct Storage { |
| 31 | + tip_token_address: ContractAddress, |
| 32 | + tips: LegacyMap<u32, Tip>, |
| 33 | + tips_count: u32, |
| 34 | + #[substorage(v0)] |
| 35 | + ownable: OwnableComponent::Storage, |
| 36 | + } |
| 37 | + |
| 38 | + #[event] |
| 39 | + #[derive(Drop, starknet::Event)] |
| 40 | + enum Event { |
| 41 | + #[flat] |
| 42 | + OwnableEvent: OwnableComponent::Event, |
| 43 | + TipReceived: TipReceived, |
| 44 | + } |
| 45 | + |
| 46 | + #[derive(Drop, starknet::Event)] |
| 47 | + struct TipReceived { |
| 48 | + #[key] |
| 49 | + tipper: ContractAddress, |
| 50 | + amount: u256, |
| 51 | + message: ByteArray, |
| 52 | + } |
| 53 | + |
| 54 | + #[constructor] |
| 55 | + fn constructor( |
| 56 | + ref self: ContractState, owner_address: ContractAddress, tip_token: ContractAddress, |
| 57 | + ) { |
| 58 | + self.ownable.initializer(owner_address); |
| 59 | + self.tip_token_address.write(tip_token); |
| 60 | + } |
| 61 | + |
| 62 | + #[abi(embed_v0)] |
| 63 | + impl IBuyMeACoffeeImpl of super::IBuyMeACoffee<ContractState> { |
| 64 | + fn buy_coffee(ref self: ContractState, amount: u256, message: ByteArray) { |
| 65 | + assert(amount > 0, 'Tip amount must be > 0'); |
| 66 | + let tipper = get_caller_address(); |
| 67 | + let contract_address = get_contract_address(); |
| 68 | + let token_dispatcher = IERC20Dispatcher { |
| 69 | + contract_address: self.tip_token_address.read(), |
| 70 | + }; |
| 71 | + token_dispatcher.transfer_from(tipper, contract_address, amount); |
| 72 | + let tip_id = self.tips_count.read(); |
| 73 | + self.tips.write(tip_id, Tip { tipper, amount, message: message.clone() }); |
| 74 | + self.tips_count.write(tip_id + 1); |
| 75 | + self.emit(TipReceived { tipper, amount, message }); |
| 76 | + } |
| 77 | + |
| 78 | + fn withdraw(ref self: ContractState) { |
| 79 | + self.ownable.assert_only_owner(); |
| 80 | + let owner_address = self.ownable.owner(); |
| 81 | + let contract_address = get_contract_address(); |
| 82 | + let token_dispatcher = IERC20Dispatcher { |
| 83 | + contract_address: self.tip_token_address.read(), |
| 84 | + }; |
| 85 | + let balance = token_dispatcher.balance_of(contract_address); |
| 86 | + if balance > 0 { |
| 87 | + token_dispatcher.transfer(owner_address, balance); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + fn get_tips(self: @ContractState) -> Array<Tip> { |
| 92 | + let mut all_tips = array![]; |
| 93 | + let count = self.tips_count.read(); |
| 94 | + let mut i = 0_u32; |
| 95 | + loop { |
| 96 | + if i >= count { |
| 97 | + break; |
| 98 | + } |
| 99 | + all_tips.append(self.tips.read(i)); |
| 100 | + i += 1; |
| 101 | + } |
| 102 | + all_tips |
| 103 | + } |
| 104 | + |
| 105 | + fn get_owner(self: @ContractState) -> starknet::ContractAddress { |
| 106 | + self.ownable.owner() |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[derive(Drop, starknet::Store, Serde)] |
| 112 | +pub struct Tip { |
| 113 | + tipper: starknet::ContractAddress, |
| 114 | + amount: u256, |
| 115 | + message: ByteArray, |
| 116 | +} |
| 117 | + |
0 commit comments