Skip to content

Commit

Permalink
Update sync Scaffold new depencies starknet-dev, add SC challege1 and…
Browse files Browse the repository at this point in the history
… types deploy
  • Loading branch information
Nadai2010 committed Apr 12, 2024
1 parent 956ab1c commit 1b582c7
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 1,503 deletions.
939 changes: 41 additions & 898 deletions packages/nextjs/contracts/deployedContracts.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/nextjs/scaffold.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type ScaffoldConfig = {
};

const scaffoldConfig = {
targetNetworks: [chains.devnet],
targetNetworks: [chains.sepolia],
// Only show the Burner Wallet when running on devnet
onlyLocalBurnerWallet: false,
rpcProviderUrl: process.env.NEXT_PUBLIC_PROVIDER_URL || "",
Expand Down
62 changes: 0 additions & 62 deletions packages/snfoundry/contracts/src/challenge0.cairo

This file was deleted.

125 changes: 96 additions & 29 deletions packages/snfoundry/contracts/src/challenge1.cairo
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()
}
}
}
}
54 changes: 0 additions & 54 deletions packages/snfoundry/contracts/src/challenge2.cairo

This file was deleted.

35 changes: 0 additions & 35 deletions packages/snfoundry/contracts/src/challenge3.cairo

This file was deleted.

30 changes: 0 additions & 30 deletions packages/snfoundry/contracts/src/helloStarknet.cairo

This file was deleted.

Loading

0 comments on commit 1b582c7

Please sign in to comment.