Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mint and burn function #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/bwc_erc20_token.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ trait IERC20<TContractState> {
fn get_name(self: @TContractState) -> felt252;
fn get_symbol(self: @TContractState) -> felt252;
fn get_decimals(self: @TContractState) -> u8;
//fn totalSupply(ref self: TContractState, to: ContractAddress, amount: u256);
fn get_total_supply(self: @TContractState) -> u256;
fn balance_of(self: @TContractState, account: ContractAddress) -> u256;
fn allowance(self: @TContractState, owner: ContractAddress, spender: ContractAddress) -> u256;
Expand All @@ -16,6 +17,10 @@ trait IERC20<TContractState> {
fn decrease_allowance(
ref self: TContractState, spender: ContractAddress, subtracted_value: u256
);

fn mint(ref self: TContractState, recipient: ContractAddress, amount: u256) -> bool;

fn burn(ref self: TContractState, to: ContractAddress, amount: u256) -> bool;
}

#[starknet::contract]
Expand All @@ -31,6 +36,7 @@ mod BWCERC20Token {
#[storage]
struct Storage {
name: felt252,
owner: ContractAddress,
symbol: felt252,
decimals: u8,
total_supply: u256,
Expand Down Expand Up @@ -67,6 +73,7 @@ mod BWCERC20Token {
#[constructor]
fn constructor(
ref self: ContractState,
_owner: ContractAddress,
_name: felt252,
_symbol: felt252,
_decimal: u8,
Expand All @@ -75,6 +82,8 @@ mod BWCERC20Token {
) {
// The .is_zero() method here is used to determine whether the address type recipient is a 0 address, similar to recipient == address(0) in Solidity.
assert(!recipient.is_zero(), 'transfer to zero address');
assert(!_owner.is_zero(), 'owner cant be zero addr');
self.owner.write(_owner);
self.name.write(_name);
self.symbol.write(_symbol);
self.decimals.write(_decimal);
Expand All @@ -90,6 +99,7 @@ mod BWCERC20Token {
);
}


#[external(v0)]
impl IERC20Impl of IERC20<ContractState> {
fn get_name(self: @ContractState) -> felt252 {
Expand Down Expand Up @@ -118,6 +128,21 @@ mod BWCERC20Token {
self.allowances.read((owner, spender))
}


fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) -> bool {
let owner = self.owner.read();
let caller = get_caller_address();
assert(owner == caller, 'caller not owner');
assert(!recipient.is_zero(), 'ERC20: Adddress zero');
assert(self.balances.read(recipient) >= amount, 'Insufficient fund');
self.balances.write(recipient, self.balances.read(recipient) + amount);
self.total_supply.write(self.total_supply.read() - amount);
// call tranfer
// Transfer(Zeroable::zero(), recipient, amount);

true
}

fn transfer(ref self: ContractState, recipient: ContractAddress, amount: u256) {
let caller = get_caller_address();
self.transfer_helper(caller, recipient, amount);
Expand Down Expand Up @@ -163,6 +188,22 @@ mod BWCERC20Token {
caller, spender, self.allowances.read((caller, spender)) - subtracted_value
);
}

fn burn(ref self: ContractState, to: ContractAddress, amount: u256) -> bool {
let owner = self.owner.read();
let msg_sender = get_caller_address();
assert(owner == msg_sender, 'msg_sender not owner');

assert(self.balances.read(to) >= amount, 'Insufficient fund');

self.balances.write(msg_sender, self.balances.read(msg_sender) - amount);

// call transfer

// Transfer(Zeroable::zero(), to, amount);

true
}
}

#[generate_trait]
Expand Down