Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 5 additions & 0 deletions erc20/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
target
.snfoundry_cache/
snfoundry_trace/
coverage/
profile/
24 changes: 24 additions & 0 deletions erc20/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "erc20"
version = "0.1.0"
dependencies = [
"snforge_std",
]

[[package]]
name = "snforge_scarb_plugin"
version = "0.59.0"
source = "registry+https://scarbs.xyz/"
checksum = "sha256:871fba677c03b66a1bf40815dac0ab1b385eb1b9be6e6c3cf2ad9788eeb2b6bb"

[[package]]
name = "snforge_std"
version = "0.59.0"
source = "registry+https://scarbs.xyz/"
checksum = "sha256:3620924fa08bd2d740b2b5b01ef86c8dab3d4b9c2206387c8dbdc8d2ec15133e"
dependencies = [
"snforge_scarb_plugin",
]
52 changes: 52 additions & 0 deletions erc20/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[package]
name = "erc20"
version = "0.1.0"
edition = "2024_07"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
starknet = "2.17.0"

[dev-dependencies]
snforge_std = "0.59.0"
assert_macros = "2.17.0"

[[target.starknet-contract]]
sierra = true

[scripts]
test = "snforge test"

[tool.scarb]
allow-prebuilt-plugins = ["snforge_std"]

# Visit https://foundry-rs.github.io/starknet-foundry/appendix/scarb-toml.html for more information

# [tool.snforge] # Define `snforge` tool section
# exit_first = true # Stop tests execution immediately upon the first failure
# fuzzer_runs = 1234 # Number of runs of the random fuzzer
# fuzzer_seed = 1111 # Seed for the random fuzzer

# [[tool.snforge.fork]] # Used for fork testing
# name = "SOME_NAME" # Fork name
# url = "http://your.rpc.url" # Url of the RPC provider
# block_id.tag = "latest" # Block to fork from (block tag)

# [[tool.snforge.fork]]
# name = "SOME_SECOND_NAME"
# url = "http://your.second.rpc.url"
# block_id.number = "123" # Block to fork from (block number)

# [[tool.snforge.fork]]
# name = "SOME_THIRD_NAME"
# url = "http://your.third.rpc.url"
# block_id.hash = "0x123" # Block to fork from (block hash)

# [profile.dev.cairo] # Configure Cairo compiler
# unstable-add-statements-code-locations-debug-info = true # Should be used if you want to use coverage
# unstable-add-statements-functions-debug-info = true # Should be used if you want to use coverage/profiler
# inlining-strategy = "avoid" # Should be used if you want to use coverage

# [features] # Used for conditional compilation
# enable_for_tests = [] # Feature name and list of other features that should be enabled with it
13 changes: 13 additions & 0 deletions erc20/snfoundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[sncast.default]
account = "erc"
# Visit https://foundry-rs.github.io/starknet-foundry/appendix/snfoundry-toml.html
# and https://foundry-rs.github.io/starknet-foundry/projects/configuration.html for more information

# [sncast.default] # Define a profile name
# url = "https://api.zan.top/public/starknet-sepolia/rpc/v0_10" # Url of the RPC provider
# accounts-file = "../account-file" # Path to the file with the account data
# account = "mainuser" # Account from `accounts_file` or default account file that will be used for the transactions
# keystore = "~/keystore" # Path to the keystore file
# wait-params = { timeout = 300, retry-interval = 10 } # Wait for submitted transaction parameters
# block-explorer = "Voyager" # Block explorer service used to display links to transaction details
# show-explorer-links = true # Print links pointing to pages with transaction details in the chosen block explorer
263 changes: 263 additions & 0 deletions erc20/src/erc20.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
#[starknet::contract]
pub mod ERC20 {
use core::num::traits::Zero;
use starknet::{ContractAddress, get_caller_address};
use starknet::storage::{
Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess,
StoragePointerWriteAccess,
};
use erc20::interfaces::{IERC20, ISpendingControl};
use erc20::events::{
Transfer, Approval, SpenderApproved, SpenderRevoked, SpendingLimitUpdated,
};

const MAX_LIMIT: u256 = 10000;

#[storage]
struct Storage {
name: felt252,
symbol: felt252,
decimals: u8,
total_supply: u256,
balances: Map<ContractAddress, u256>,
allowances: Map<(ContractAddress, ContractAddress), u256>,
admin: ContractAddress,
spending_limit: u256,
approved_spenders: Map<(ContractAddress, ContractAddress), bool>,
}

#[event]
#[derive(Drop, starknet::Event)]
pub enum Event {
Transfer: Transfer,
Approval: Approval,
SpenderApproved: SpenderApproved,
SpenderRevoked: SpenderRevoked,
SpendingLimitUpdated: SpendingLimitUpdated,
}

mod Errors {
pub const APPROVE_FROM_ZERO: felt252 = 'ERC20: approve from 0';
pub const APPROVE_TO_ZERO: felt252 = 'ERC20: approve to 0';
pub const TRANSFER_FROM_ZERO: felt252 = 'ERC20: transfer from 0';
pub const TRANSFER_TO_ZERO: felt252 = 'ERC20: transfer to 0';
pub const BURN_FROM_ZERO: felt252 = 'ERC20: burn from 0';
pub const MINT_TO_ZERO: felt252 = 'ERC20: mint to 0';
pub const EXCEEDS_SPENDING_LIMIT: felt252 = 'ERC20: exceeds limit';
pub const NOT_APPROVED_SPENDER: felt252 = 'ERC20: not approved spender';
pub const CALLER_NOT_ADMIN: felt252 = 'ERC20: caller not admin';
pub const APPROVE_SPENDER_ZERO: felt252 = 'ERC20: approve spender 0';
pub const REVOKE_ZERO_ADDRESS: felt252 = 'ERC20: revoke zero address';
pub const ZERO_AMOUNT: felt252 = 'ERC20: amount is zero';
pub const INSUFFICIENT_BALANCE: felt252 = 'ERC20: insufficient balance';
pub const INSUFFICIENT_ALLOWANCE: felt252 = 'ERC20: insufficient allowance';
pub const ALLOWANCE_UNDERFLOW: felt252 = 'ERC20: allowance underflow';
pub const INVALID_LIMIT: felt252 = 'ERC20: limit must be > 0';
pub const ADMIN_ZERO: felt252 = 'ERC20: admin is zero';
pub const INVALID_NAME: felt252 = 'ERC20: name is zero';
pub const INVALID_SYMBOL: felt252 = 'ERC20: symbol is zero';
pub const SELF_APPROVE: felt252 = 'ERC20: cannot approve self';
}

#[constructor]
fn constructor(
ref self: ContractState,
admin: ContractAddress,
recipient: ContractAddress,
decimals: u8,
initial_supply: u256, ) {
assert(admin.is_non_zero(), Errors::ADMIN_ZERO);
assert(initial_supply > 0, Errors::ZERO_AMOUNT);
self.name.write('Paragon');
self.symbol.write('PGN');
self.decimals.write(decimals);
self.admin.write(admin);
self.spending_limit.write(MAX_LIMIT);
InternalImpl::mint(ref self, recipient, initial_supply);
}

#[abi(embed_v0)]
impl ERC20Impl of IERC20<ContractState> {
fn name(self: @ContractState) -> felt252 {
self.name.read()
}
fn symbol(self: @ContractState) -> felt252 {
self.symbol.read()
}
fn decimals(self: @ContractState) -> u8 {
self.decimals.read()
}
fn total_supply(self: @ContractState) -> u256 {
self.total_supply.read()
}
fn balance_of(self: @ContractState, account: ContractAddress) -> u256 {
self.balances.read(account)
}
fn allowance(
self: @ContractState, owner: ContractAddress, spender: ContractAddress,
) -> u256 {
self.allowances.read((owner, spender))
}

fn transfer(ref self: ContractState, recipient: ContractAddress, amount: u256) {
assert(amount > 0, Errors::ZERO_AMOUNT);
let sender = get_caller_address();
self._transfer(sender, sender, recipient, amount);
}

fn transfer_from(
ref self: ContractState,
sender: ContractAddress,
recipient: ContractAddress,
amount: u256,
) {
assert(amount > 0, Errors::ZERO_AMOUNT);
let caller = get_caller_address();
self.spend_allowance(sender, caller, amount);
self._transfer(caller, sender, recipient, amount);
}

fn approve(ref self: ContractState, spender: ContractAddress, amount: u256) {
let caller = get_caller_address();
assert(caller != spender, Errors::SELF_APPROVE);
self.approve_helper(caller, spender, amount);
}

fn increase_allowance(
ref self: ContractState, spender: ContractAddress, added_value: u256,
) {
assert(added_value > 0, Errors::ZERO_AMOUNT);
let caller = get_caller_address();
assert(caller != spender, Errors::SELF_APPROVE);
self
.approve_helper(
caller, spender, self.allowances.read((caller, spender)) + added_value,
);
}

fn decrease_allowance(
ref self: ContractState, spender: ContractAddress, subtracted_value: u256,
) {
assert(subtracted_value > 0, Errors::ZERO_AMOUNT);
let caller = get_caller_address();
let current = self.allowances.read((caller, spender));
assert(subtracted_value <= current, Errors::ALLOWANCE_UNDERFLOW);
self.approve_helper(caller, spender, current - subtracted_value);
}
}

#[abi(embed_v0)]
impl SpendingControlImpl of ISpendingControl<ContractState> {
fn approve_spender(ref self: ContractState, spender: ContractAddress) {
assert(spender.is_non_zero(), Errors::APPROVE_SPENDER_ZERO);
let caller = get_caller_address();
assert(caller != spender, Errors::SELF_APPROVE);
self.approved_spenders.write((caller, spender), true);
self.emit(SpenderApproved { owner: caller, spender });
}

fn revoke_spender(ref self: ContractState, spender: ContractAddress) {
assert(spender.is_non_zero(), Errors::REVOKE_ZERO_ADDRESS);
let caller = get_caller_address();
self.approved_spenders.write((caller, spender), false);
self.emit(SpenderRevoked { owner: caller, spender });
}

fn update_spending_limit(ref self: ContractState, new_limit: u256) {
assert(get_caller_address() == self.admin.read(), Errors::CALLER_NOT_ADMIN);
assert(new_limit > 0, Errors::INVALID_LIMIT);
let old_limit = self.spending_limit.read();
self.spending_limit.write(new_limit);
self.emit(SpendingLimitUpdated { old_limit, new_limit });
}

fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) {
assert(get_caller_address() == self.admin.read(), Errors::CALLER_NOT_ADMIN);
InternalImpl::mint(ref self, recipient, amount);
}

fn burn(ref self: ContractState, amount: u256) {
let caller = get_caller_address();
assert(caller == self.admin.read(), Errors::CALLER_NOT_ADMIN);
assert(amount > 0, Errors::ZERO_AMOUNT);
assert(self.balances.read(caller) >= amount, Errors::INSUFFICIENT_BALANCE);
self.balances.write(caller, self.balances.read(caller) - amount);
let zero_address = Zero::zero();
self.balances.write(zero_address, self.balances.read(zero_address) + amount);
self.emit(Transfer { from: caller, to: zero_address, value: amount });

}


fn is_approved_spender(
self: @ContractState, owner: ContractAddress, spender: ContractAddress,
) -> bool {
self.approved_spenders.read((owner, spender))
}

fn get_spending_limit(self: @ContractState) -> u256 {
self.spending_limit.read()
}
}

#[generate_trait]
impl InternalImpl of InternalTrait {
fn _transfer(
ref self: ContractState,
caller: ContractAddress,
sender: ContractAddress,
recipient: ContractAddress,
amount: u256,
) {
assert(sender.is_non_zero(), Errors::TRANSFER_FROM_ZERO);
assert(recipient.is_non_zero(), Errors::TRANSFER_TO_ZERO);
assert(sender != recipient, 'ERC20: self-transfer');
assert(amount > 0, Errors::ZERO_AMOUNT);
assert(amount <= self.spending_limit.read(), Errors::EXCEEDS_SPENDING_LIMIT);
assert(
caller == sender || self.approved_spenders.read((sender, caller)),
Errors::NOT_APPROVED_SPENDER,
);
assert(self.balances.read(sender) >= amount, Errors::INSUFFICIENT_BALANCE);
self.balances.write(sender, self.balances.read(sender) - amount);
self.balances.write(recipient, self.balances.read(recipient) + amount);
self.emit(Transfer { from: sender, to: recipient, value: amount });
}

fn spend_allowance(
ref self: ContractState,
owner: ContractAddress,
spender: ContractAddress,
amount: u256,
) {
let allowance = self.allowances.read((owner, spender));
assert(allowance >= amount, Errors::INSUFFICIENT_ALLOWANCE);
self.allowances.write((owner, spender), allowance - amount);
}

fn approve_helper(
ref self: ContractState,
owner: ContractAddress,
spender: ContractAddress,
amount: u256,
) {
assert(owner.is_non_zero(), Errors::APPROVE_FROM_ZERO);
assert(spender.is_non_zero(), Errors::APPROVE_TO_ZERO);
self.allowances.write((owner, spender), amount);
self.emit(Approval { owner, spender, value: amount });
}

fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) {
let caller = get_caller_address();
assert(caller == self.admin.read(), Errors::CALLER_NOT_ADMIN);
assert(recipient.is_non_zero(), Errors::MINT_TO_ZERO);
assert(amount > 0, Errors::ZERO_AMOUNT);
let zero_address = Zero::zero();
self.balances.write(zero_address, self.balances.read(zero_address) + amount);
self.total_supply.write(self.total_supply.read() + amount);
self.balances.write(zero_address, self.balances.read(zero_address) - amount);
self.balances.write(recipient, self.balances.read(recipient) + amount);
self.emit(Transfer { from: zero_address, to: recipient, value: amount });
}
}
}
Loading