Skip to content

Commit

Permalink
implemented mock tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
casweeney committed Oct 15, 2024
1 parent 0c0299a commit c65e84a
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/interfaces/ierc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ pub trait IERC20<TContractState> {
fn approve(ref self: TContractState, spender: ContractAddress, amount: u256) -> bool;
fn transfer(ref self: TContractState, recipient: ContractAddress, amount: u256) -> bool;
fn transfer_from(ref self: TContractState, sender: ContractAddress, recipient: ContractAddress, amount: u256) -> bool;

fn mint(ref self: TContractState, recipient: ContractAddress, amount: u256) -> bool;
}
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod interfaces;
pub mod mocks;

#[starknet::contract]
mod StakingRewards {
Expand Down
2 changes: 2 additions & 0 deletions src/mocks.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod mock_reward_token;
pub mod mock_staking_token;
144 changes: 144 additions & 0 deletions src/mocks/mock_reward_token.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#[starknet::contract]
pub mod RewardToken {
use starknet::event::EventEmitter;
use starknet::{ContractAddress, get_caller_address};
use core::starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess, Map, StoragePathEntry};
use synthetix_staking::interfaces::ierc20::IERC20;
use core::num::traits::Zero;

#[storage]
pub struct Storage {
balances: Map<ContractAddress, u256>,
allowances: Map<(ContractAddress, ContractAddress), u256>, // Mapping<(owner, spender), amount>
token_name: ByteArray,
symbol: ByteArray,
decimal: u8,
total_supply: u256,
owner: ContractAddress,
}

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

#[derive(Drop, starknet::Event)]
pub struct Transfer {
#[key]
from: ContractAddress,
#[key]
to: ContractAddress,
amount: u256,
}

#[derive(Drop, starknet::Event)]
pub struct Approval {
#[key]
owner: ContractAddress,
#[key]
spender: ContractAddress,
value: u256
}

#[constructor]
fn constructor(ref self: ContractState) {
self.token_name.write("Reward Token");
self.symbol.write("RWT");
self.decimal.write(18);
self.owner.write(get_caller_address());
}

#[abi(embed_v0)]
impl RewardTokenImpl of IERC20<ContractState> {
fn total_supply(self: @ContractState) -> u256 {
self.total_supply.read()
}

fn balance_of(self: @ContractState, account: ContractAddress) -> u256 {
let balance = self.balances.entry(account).read();

balance
}

fn allowance(self: @ContractState, owner: ContractAddress, spender: ContractAddress) -> u256 {
let allowance = self.allowances.entry((owner, spender)).read();

allowance
}

fn transfer(ref self: ContractState, recipient: ContractAddress, amount: u256) -> bool {
let sender = get_caller_address();

let sender_prev_balance = self.balances.entry(sender).read();
let recipient_prev_balance = self.balances.entry(recipient).read();

assert(sender_prev_balance >= amount, 'Insufficient amount');

self.balances.entry(sender).write(sender_prev_balance - amount);
self.balances.entry(recipient).write(recipient_prev_balance + amount);

assert(self.balances.entry(recipient).read() > recipient_prev_balance, 'Transaction failed');

self.emit(Transfer { from: sender, to: recipient, amount });

true
}

fn transfer_from(ref self: ContractState, sender: ContractAddress, recipient: ContractAddress, amount: u256) -> bool {
let spender = get_caller_address();

let spender_allowance = self.allowances.entry((sender, spender)).read();
let sender_balance = self.balances.entry(sender).read();
let recipient_balance = self.balances.entry(recipient).read();

assert(amount <= spender_allowance, 'amount exceeds allowance');
assert(amount <= sender_balance, 'amount exceeds balance');

self.allowances.entry((sender, spender)).write(spender_allowance - amount);
self.balances.entry(sender).write(sender_balance - amount);
self.balances.entry(recipient).write(recipient_balance + amount);

self.emit(Transfer { from: sender, to: recipient, amount });

true
}

fn approve(ref self: ContractState, spender: ContractAddress, amount: u256) -> bool {
let caller = get_caller_address();

self.allowances.entry((caller, spender)).write(amount);

self.emit(Approval { owner: caller, spender, value: amount });

true
}

fn name(self: @ContractState) -> ByteArray {
self.token_name.read()
}

fn symbol(self: @ContractState) -> ByteArray {
self.symbol.read()
}

fn decimals(self: @ContractState) -> u8 {
self.decimal.read()
}

fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) -> bool {
let previous_total_supply = self.total_supply.read();
let previous_balance = self.balances.entry(recipient).read();

self.total_supply.write(previous_total_supply + amount);
self.balances.entry(recipient).write(previous_balance + amount);

let zero_address = Zero::zero();

self.emit(Transfer { from: zero_address, to: recipient, amount });

true
}
}
}
144 changes: 144 additions & 0 deletions src/mocks/mock_staking_token.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#[starknet::contract]
pub mod StakingToken {
use starknet::event::EventEmitter;
use starknet::{ContractAddress, get_caller_address};
use core::starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess, Map, StoragePathEntry};
use synthetix_staking::interfaces::ierc20::IERC20;
use core::num::traits::Zero;

#[storage]
pub struct Storage {
balances: Map<ContractAddress, u256>,
allowances: Map<(ContractAddress, ContractAddress), u256>, // Mapping<(owner, spender), amount>
token_name: ByteArray,
symbol: ByteArray,
decimal: u8,
total_supply: u256,
owner: ContractAddress,
}

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

#[derive(Drop, starknet::Event)]
pub struct Transfer {
#[key]
from: ContractAddress,
#[key]
to: ContractAddress,
amount: u256,
}

#[derive(Drop, starknet::Event)]
pub struct Approval {
#[key]
owner: ContractAddress,
#[key]
spender: ContractAddress,
value: u256
}

#[constructor]
fn constructor(ref self: ContractState) {
self.token_name.write("Staking Token");
self.symbol.write("SKT");
self.decimal.write(18);
self.owner.write(get_caller_address());
}

#[abi(embed_v0)]
impl StakingTokenImpl of IERC20<ContractState> {
fn total_supply(self: @ContractState) -> u256 {
self.total_supply.read()
}

fn balance_of(self: @ContractState, account: ContractAddress) -> u256 {
let balance = self.balances.entry(account).read();

balance
}

fn allowance(self: @ContractState, owner: ContractAddress, spender: ContractAddress) -> u256 {
let allowance = self.allowances.entry((owner, spender)).read();

allowance
}

fn transfer(ref self: ContractState, recipient: ContractAddress, amount: u256) -> bool {
let sender = get_caller_address();

let sender_prev_balance = self.balances.entry(sender).read();
let recipient_prev_balance = self.balances.entry(recipient).read();

assert(sender_prev_balance >= amount, 'Insufficient amount');

self.balances.entry(sender).write(sender_prev_balance - amount);
self.balances.entry(recipient).write(recipient_prev_balance + amount);

assert(self.balances.entry(recipient).read() > recipient_prev_balance, 'Transaction failed');

self.emit(Transfer { from: sender, to: recipient, amount });

true
}

fn transfer_from(ref self: ContractState, sender: ContractAddress, recipient: ContractAddress, amount: u256) -> bool {
let spender = get_caller_address();

let spender_allowance = self.allowances.entry((sender, spender)).read();
let sender_balance = self.balances.entry(sender).read();
let recipient_balance = self.balances.entry(recipient).read();

assert(amount <= spender_allowance, 'amount exceeds allowance');
assert(amount <= sender_balance, 'amount exceeds balance');

self.allowances.entry((sender, spender)).write(spender_allowance - amount);
self.balances.entry(sender).write(sender_balance - amount);
self.balances.entry(recipient).write(recipient_balance + amount);

self.emit(Transfer { from: sender, to: recipient, amount });

true
}

fn approve(ref self: ContractState, spender: ContractAddress, amount: u256) -> bool {
let caller = get_caller_address();

self.allowances.entry((caller, spender)).write(amount);

self.emit(Approval { owner: caller, spender, value: amount });

true
}

fn name(self: @ContractState) -> ByteArray {
self.token_name.read()
}

fn symbol(self: @ContractState) -> ByteArray {
self.symbol.read()
}

fn decimals(self: @ContractState) -> u8 {
self.decimal.read()
}

fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) -> bool {
let previous_total_supply = self.total_supply.read();
let previous_balance = self.balances.entry(recipient).read();

self.total_supply.write(previous_total_supply + amount);
self.balances.entry(recipient).write(previous_balance + amount);

let zero_address = Zero::zero();

self.emit(Transfer { from: zero_address, to: recipient, amount });

true
}
}
}
5 changes: 0 additions & 5 deletions tests/test_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ fn deploy_contract(name: ByteArray) -> ContractAddress {
contract_address
}

// const ONE_E18: u256 = u256 { low: 1000000000000000000_u128, high: 0_u128 };
const ONE_E18: u256 = 1000000000000000000_u256;


#[test]
fn test_increase_balance() {
Expand All @@ -30,6 +27,4 @@ fn test_increase_balance() {


assert(1 == 1, 'wrong number');

println!("{}", ONE_E18);
}

0 comments on commit c65e84a

Please sign in to comment.