forked from Scaffold-Stark/scaffold-stark-2
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update sync Scaffold new depencies starknet-dev, add SC challege1 and…
… types deploy
- Loading branch information
Showing
11 changed files
with
143 additions
and
1,503 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,106 @@ | ||
#[starknet::contract] | ||
mod Challenge1 { | ||
use openzeppelin::token::erc20::ERC20Component; | ||
use starknet::ContractAddress; | ||
use starknet::ContractAddress; | ||
|
||
component!(path: ERC20Component, storage: erc20, event: ERC20Event); | ||
// In order to make contract calls within our Vault, | ||
// we need to have the interface of the remote ERC20 contract defined to import the Dispatcher. | ||
#[starknet::interface] | ||
pub trait IERC20<TContractState> { | ||
fn name(self: @TContractState) -> felt252; | ||
fn symbol(self: @TContractState) -> felt252; | ||
fn decimals(self: @TContractState) -> u8; | ||
fn total_supply(self: @TContractState) -> u256; | ||
fn balance_of(self: @TContractState, account: ContractAddress) -> u256; | ||
fn allowance(self: @TContractState, owner: ContractAddress, spender: ContractAddress) -> u256; | ||
fn transfer(ref self: TContractState, recipient: ContractAddress, amount: u256) -> bool; | ||
fn transfer_from( | ||
ref self: TContractState, sender: ContractAddress, recipient: ContractAddress, amount: u256 | ||
) -> bool; | ||
fn approve(ref self: TContractState, spender: ContractAddress, amount: u256) -> bool; | ||
} | ||
|
||
// ERC20Mixin | ||
#[abi(embed_v0)] | ||
impl ERC20MixinImpl = ERC20Component::ERC20MixinImpl<ContractState>; | ||
impl InternalImpl = ERC20Component::InternalImpl<ContractState>; | ||
#[starknet::interface] | ||
pub trait IChallenge1<TContractState> { | ||
fn deposit(ref self: TContractState, amount: u256); | ||
fn withdraw(ref self: TContractState, shares: u256); | ||
fn get_balance(self: @TContractState, account: ContractAddress) -> u32; | ||
} | ||
|
||
#[starknet::contract] | ||
pub mod Challenge1 { | ||
use super::{IERC20Dispatcher, IERC20DispatcherTrait}; | ||
use starknet::{ContractAddress, get_caller_address, get_contract_address}; | ||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
erc20: ERC20Component::Storage | ||
token: IERC20Dispatcher, | ||
total_supply: u256, | ||
balance_of: LegacyMap<ContractAddress, u256>, | ||
balance: u32, | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
#[flat] | ||
ERC20Event: ERC20Component::Event | ||
#[constructor] | ||
fn constructor(ref self: ContractState, token: ContractAddress) { | ||
self.token.write(IERC20Dispatcher { contract_address: token }); | ||
} | ||
|
||
/// Sets the token `name` and `symbol`. | ||
/// Mints `fixed_supply` tokens to `recipient`. | ||
#[constructor] | ||
fn constructor( | ||
ref self: ContractState, | ||
name: ByteArray, | ||
symbol: ByteArray, | ||
fixed_supply: u256, | ||
recipient: ContractAddress | ||
) { | ||
self.erc20.initializer(name, symbol); | ||
self.erc20._mint(recipient, fixed_supply); | ||
#[generate_trait] | ||
impl PrivateFunctions of PrivateFunctionsTrait { | ||
fn _mint(ref self: ContractState, to: ContractAddress, shares: u256) { | ||
self.total_supply.write(self.total_supply.read() + shares); | ||
self.balance_of.write(to, self.balance_of.read(to) + shares); | ||
} | ||
|
||
fn _burn(ref self: ContractState, from: ContractAddress, shares: u256) { | ||
self.total_supply.write(self.total_supply.read() - shares); | ||
self.balance_of.write(from, self.balance_of.read(from) - shares); | ||
} | ||
} | ||
|
||
#[abi(embed_v0)] | ||
impl Challenge1 of super::IChallenge1<ContractState> { | ||
fn deposit(ref self: ContractState, amount: u256) { | ||
// a = amount | ||
// B = balance of token before deposit | ||
// T = total supply | ||
// s = shares to mint | ||
// | ||
// (T + s) / T = (a + B) / B | ||
// | ||
// s = aT / B | ||
let caller = get_caller_address(); | ||
let this = get_contract_address(); | ||
|
||
let mut shares = 0; | ||
if self.total_supply.read() == 0 { | ||
shares = amount; | ||
} else { | ||
let balance = self.token.read().balance_of(this); | ||
shares = (amount * self.total_supply.read()) / balance; | ||
} | ||
|
||
PrivateFunctions::_mint(ref self, caller, shares); | ||
self.token.read().transfer_from(caller, this, amount); | ||
} | ||
|
||
fn withdraw(ref self: ContractState, shares: u256) { | ||
// a = amount | ||
// B = balance of token before withdraw | ||
// T = total supply | ||
// s = shares to burn | ||
// | ||
// (T - s) / T = (B - a) / B | ||
// | ||
// a = sB / T | ||
let caller = get_caller_address(); | ||
let this = get_contract_address(); | ||
|
||
let balance = self.token.read().balance_of(this); | ||
let amount = (shares * balance) / self.total_supply.read(); | ||
PrivateFunctions::_burn(ref self, caller, shares); | ||
self.token.read().transfer(caller, amount); | ||
} | ||
|
||
fn get_balance(self: @ContractState, account: ContractAddress) -> u32 { | ||
self.balance.read() | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.