Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.2 kB
1
Indexable
Never
#[starknet::contract]
pub mod ERC20{
    use core::num::traits::zero::Zero;
    use erc20::interface;
    use starknet::{
        ContractAddress,
        get_caller_address,
        contract_address_const
    };
    use erc20::error::Errors;

    #[storage]
    struct Storage {
        name: felt252,
        symbol: felt252,
        decimals: u8,
        total_supply: felt252,
        balances: LegacyMap::<ContractAddress, felt252>,
        allowances: LegacyMap::<(ContractAddress, ContractAddress), felt252>,
    }
    
    #[event]
    #[derive(Copy, Drop, Debug, PartialEq, starknet::Event)]
    pub enum Event {
        Transfer: Transfer,
        Approval: Approval,
    }

    #[derive(Copy, Drop, Debug, PartialEq, starknet::Event)]
    pub struct Transfer {
        from: ContractAddress,
        to: ContractAddress,
        value: felt252,
    }

    #[derive(Copy, Drop, Debug, PartialEq, starknet::Event)]
    pub struct Approval {
        owner: ContractAddress,
        spender: ContractAddress,
        value: felt252,
    }

    #[abi(embed_v0)]
    impl IERC20MetadataImpl of interface::IERC20Metadata<ContractState>{
        fn name(self: @ContractState) -> felt252 {
            return self.name.read();
        }

        fn symbol(self: @ContractState) -> felt252 {
            return self.symbol.read();
        }
        
        fn decimals(self: @ContractState) -> u8 {
            return self.decimals.read();
        }
    }

    #[abi(embed_v0)]
    impl IERC20Impl of interface::IERC20<ContractState>{
        fn totalSuply(self: @ContractState) -> felt252 {
            return self.total_supply.read();
        }

        fn balanceOf(self: @ContractState, account: ContractAddress) -> felt252 {
            return self.balances.read(account);
        }
        
        fn allowance(self: @ContractState, owner: ContractAddress, spender: ContractAddress) -> felt252 {
            return self.allowances.read((owner, spender));
        }
    }

    #[constructor]
    fn constructor(
        ref self: ContractState,
        recipient: ContractAddress,
        name: felt252,
        decimals: u8,
        initial_supply: felt252,
        symbol: felt252,
    ) {
        self.name.write(name);
        self.decimals.write(decimals);
        self.symbol.write(symbol);
        self.mint(recipient, initial_supply);
    }

    #[generate_trait]
    impl InternalImpl of InternalTrait {
        fn mint(ref self: ContractState, recipient: ContractAddress, amount: felt252) {
            assert(recipient.is_non_zero(), Errors::MINT_TO_ZERO);
            let supply =  self.total_supply.read() + amount;
            self.total_supply.write(supply);
            let balance = self.balances.read(recipient) + amount;
            self.balances.write(recipient, balance);
            self.emit(
                Event::Transfer(
                    Transfer {
                        from: contract_address_const::<0>(),
                        to: recipient,
                        value: amount,
                    }   
                )
            )
        }
    }
}
Leave a Comment