From 1b582c741a5d9f76da1d3a59bbd134e406e6b6a4 Mon Sep 17 00:00:00 2001 From: Nadai2010 Date: Fri, 12 Apr 2024 15:44:16 +0100 Subject: [PATCH] Update sync Scaffold new depencies starknet-dev, add SC challege1 and types deploy --- .../nextjs/contracts/deployedContracts.ts | 939 +----------------- packages/nextjs/scaffold.config.ts | 2 +- .../snfoundry/contracts/src/challenge0.cairo | 62 -- .../snfoundry/contracts/src/challenge1.cairo | 125 ++- .../snfoundry/contracts/src/challenge2.cairo | 54 - .../snfoundry/contracts/src/challenge3.cairo | 35 - .../contracts/src/helloStarknet.cairo | 30 - packages/snfoundry/contracts/src/lib.cairo | 127 +-- .../contracts/src/simpleStorage.cairo | 29 - packages/snfoundry/contracts/src/vote.cairo | 171 ---- packages/snfoundry/scripts_js/deploy.js | 72 +- 11 files changed, 143 insertions(+), 1503 deletions(-) delete mode 100644 packages/snfoundry/contracts/src/challenge0.cairo delete mode 100644 packages/snfoundry/contracts/src/challenge2.cairo delete mode 100644 packages/snfoundry/contracts/src/challenge3.cairo delete mode 100644 packages/snfoundry/contracts/src/helloStarknet.cairo delete mode 100644 packages/snfoundry/contracts/src/simpleStorage.cairo delete mode 100644 packages/snfoundry/contracts/src/vote.cairo diff --git a/packages/nextjs/contracts/deployedContracts.ts b/packages/nextjs/contracts/deployedContracts.ts index c7bca93d6..ed24c182f 100644 --- a/packages/nextjs/contracts/deployedContracts.ts +++ b/packages/nextjs/contracts/deployedContracts.ts @@ -5,14 +5,14 @@ const deployedContracts = { devnet: { - Challenge0: { + Challenge1: { address: - "0x064a129d88d4a54e467afd5fb138fec224259bc3fdfb1239b8b4605bad6d8546", + "0x035c2a1f8854fe9ca04a77fa855ac98fff3a80ae61cb47f73b3eff8ac70baf8d", abi: [ { type: "impl", - name: "Challenge0Impl", - interface_name: "contracts::challenge0::IChallenge0", + name: "Challenge1", + interface_name: "contracts::challenge1::IChallenge1", }, { type: "struct", @@ -30,577 +30,15 @@ const deployedContracts = { }, { type: "interface", - name: "contracts::challenge0::IChallenge0", + name: "contracts::challenge1::IChallenge1", items: [ { type: "function", - name: "mint_item", - inputs: [ - { - name: "recipient", - type: "core::starknet::contract_address::ContractAddress", - }, - ], - outputs: [ - { - type: "core::integer::u256", - }, - ], - state_mutability: "external", - }, - { - type: "function", - name: "mint_id", - inputs: [ - { - name: "recipient", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "id", - type: "core::integer::u256", - }, - ], - outputs: [], - state_mutability: "external", - }, - ], - }, - { - type: "impl", - name: "ERC721MixinImpl", - interface_name: "openzeppelin::token::erc721::interface::ERC721ABI", - }, - { - type: "struct", - name: "core::array::Span::", - members: [ - { - name: "snapshot", - type: "@core::array::Array::", - }, - ], - }, - { - type: "enum", - name: "core::bool", - variants: [ - { - name: "False", - type: "()", - }, - { - name: "True", - type: "()", - }, - ], - }, - { - type: "struct", - name: "core::byte_array::ByteArray", - members: [ - { - name: "data", - type: "core::array::Array::", - }, - { - name: "pending_word", - type: "core::felt252", - }, - { - name: "pending_word_len", - type: "core::integer::u32", - }, - ], - }, - { - type: "interface", - name: "openzeppelin::token::erc721::interface::ERC721ABI", - items: [ - { - type: "function", - name: "balance_of", - inputs: [ - { - name: "account", - type: "core::starknet::contract_address::ContractAddress", - }, - ], - outputs: [ - { - type: "core::integer::u256", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "owner_of", - inputs: [ - { - name: "token_id", - type: "core::integer::u256", - }, - ], - outputs: [ - { - type: "core::starknet::contract_address::ContractAddress", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "safe_transfer_from", - inputs: [ - { - name: "from", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "to", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "token_id", - type: "core::integer::u256", - }, - { - name: "data", - type: "core::array::Span::", - }, - ], - outputs: [], - state_mutability: "external", - }, - { - type: "function", - name: "transfer_from", - inputs: [ - { - name: "from", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "to", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "token_id", - type: "core::integer::u256", - }, - ], - outputs: [], - state_mutability: "external", - }, - { - type: "function", - name: "approve", - inputs: [ - { - name: "to", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "token_id", - type: "core::integer::u256", - }, - ], - outputs: [], - state_mutability: "external", - }, - { - type: "function", - name: "set_approval_for_all", - inputs: [ - { - name: "operator", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "approved", - type: "core::bool", - }, - ], - outputs: [], - state_mutability: "external", - }, - { - type: "function", - name: "get_approved", - inputs: [ - { - name: "token_id", - type: "core::integer::u256", - }, - ], - outputs: [ - { - type: "core::starknet::contract_address::ContractAddress", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "is_approved_for_all", - inputs: [ - { - name: "owner", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "operator", - type: "core::starknet::contract_address::ContractAddress", - }, - ], - outputs: [ - { - type: "core::bool", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "supports_interface", - inputs: [ - { - name: "interface_id", - type: "core::felt252", - }, - ], - outputs: [ - { - type: "core::bool", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "name", - inputs: [], - outputs: [ - { - type: "core::byte_array::ByteArray", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "symbol", - inputs: [], - outputs: [ - { - type: "core::byte_array::ByteArray", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "token_uri", - inputs: [ - { - name: "token_id", - type: "core::integer::u256", - }, - ], - outputs: [ - { - type: "core::byte_array::ByteArray", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "balanceOf", - inputs: [ - { - name: "account", - type: "core::starknet::contract_address::ContractAddress", - }, - ], - outputs: [ - { - type: "core::integer::u256", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "ownerOf", - inputs: [ - { - name: "tokenId", - type: "core::integer::u256", - }, - ], - outputs: [ - { - type: "core::starknet::contract_address::ContractAddress", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "safeTransferFrom", - inputs: [ - { - name: "from", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "to", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "tokenId", - type: "core::integer::u256", - }, - { - name: "data", - type: "core::array::Span::", - }, - ], - outputs: [], - state_mutability: "external", - }, - { - type: "function", - name: "transferFrom", - inputs: [ - { - name: "from", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "to", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "tokenId", - type: "core::integer::u256", - }, - ], - outputs: [], - state_mutability: "external", - }, - { - type: "function", - name: "setApprovalForAll", - inputs: [ - { - name: "operator", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "approved", - type: "core::bool", - }, - ], - outputs: [], - state_mutability: "external", - }, - { - type: "function", - name: "getApproved", - inputs: [ - { - name: "tokenId", - type: "core::integer::u256", - }, - ], - outputs: [ - { - type: "core::starknet::contract_address::ContractAddress", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "isApprovedForAll", - inputs: [ - { - name: "owner", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "operator", - type: "core::starknet::contract_address::ContractAddress", - }, - ], - outputs: [ - { - type: "core::bool", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "tokenURI", - inputs: [ - { - name: "tokenId", - type: "core::integer::u256", - }, - ], - outputs: [ - { - type: "core::byte_array::ByteArray", - }, - ], - state_mutability: "view", - }, - ], - }, - { - type: "constructor", - name: "constructor", - inputs: [], - }, - { - type: "event", - name: "openzeppelin::token::erc721::erc721::ERC721Component::Transfer", - kind: "struct", - members: [ - { - name: "from", - type: "core::starknet::contract_address::ContractAddress", - kind: "key", - }, - { - name: "to", - type: "core::starknet::contract_address::ContractAddress", - kind: "key", - }, - { - name: "token_id", - type: "core::integer::u256", - kind: "key", - }, - ], - }, - { - type: "event", - name: "openzeppelin::token::erc721::erc721::ERC721Component::Approval", - kind: "struct", - members: [ - { - name: "owner", - type: "core::starknet::contract_address::ContractAddress", - kind: "key", - }, - { - name: "approved", - type: "core::starknet::contract_address::ContractAddress", - kind: "key", - }, - { - name: "token_id", - type: "core::integer::u256", - kind: "key", - }, - ], - }, - { - type: "event", - name: "openzeppelin::token::erc721::erc721::ERC721Component::ApprovalForAll", - kind: "struct", - members: [ - { - name: "owner", - type: "core::starknet::contract_address::ContractAddress", - kind: "key", - }, - { - name: "operator", - type: "core::starknet::contract_address::ContractAddress", - kind: "key", - }, - { - name: "approved", - type: "core::bool", - kind: "data", - }, - ], - }, - { - type: "event", - name: "openzeppelin::token::erc721::erc721::ERC721Component::Event", - kind: "enum", - variants: [ - { - name: "Transfer", - type: "openzeppelin::token::erc721::erc721::ERC721Component::Transfer", - kind: "nested", - }, - { - name: "Approval", - type: "openzeppelin::token::erc721::erc721::ERC721Component::Approval", - kind: "nested", - }, - { - name: "ApprovalForAll", - type: "openzeppelin::token::erc721::erc721::ERC721Component::ApprovalForAll", - kind: "nested", - }, - ], - }, - { - type: "event", - name: "openzeppelin::introspection::src5::SRC5Component::Event", - kind: "enum", - variants: [], - }, - { - type: "event", - name: "contracts::challenge0::Challenge0::Event", - kind: "enum", - variants: [ - { - name: "ERC721Event", - type: "openzeppelin::token::erc721::erc721::ERC721Component::Event", - kind: "flat", - }, - { - name: "SRC5Event", - type: "openzeppelin::introspection::src5::SRC5Component::Event", - kind: "flat", - }, - ], - }, - ], - }, - }, - mainnet: { - HelloStarknet: { - address: - "0x0750aa622d77436d69d58c91dbc73a6d0d25db0b7076c1377cdda15e13fd632b", - abi: [ - { - type: "impl", - name: "HelloStarknetImpl", - interface_name: "contracts::helloStarknet::IHelloStarknet", - }, - { - type: "interface", - name: "contracts::helloStarknet::IHelloStarknet", - items: [ - { - type: "function", - name: "increase_balance", + name: "deposit", inputs: [ { name: "amount", - type: "core::integer::u32", + type: "core::integer::u256", }, ], outputs: [], @@ -608,63 +46,11 @@ const deployedContracts = { }, { type: "function", - name: "get_balance", - inputs: [], - outputs: [ - { - type: "core::integer::u32", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "get_balance_increment", - inputs: [ - { - name: "amount", - type: "core::integer::u32", - }, - ], - outputs: [ - { - type: "core::integer::u32", - }, - ], - state_mutability: "view", - }, - ], - }, - { - type: "event", - name: "contracts::helloStarknet::HelloStarknet::Event", - kind: "enum", - variants: [], - }, - ], - }, - }, - sepolia: { - HelloStarknet: { - address: - "0x07e6136fbfaae09a09f491c97c127c661b9e2edb553be28e1966d1ac1724d98c", - abi: [ - { - type: "impl", - name: "HelloStarknetImpl", - interface_name: "contracts::helloStarknet::IHelloStarknet", - }, - { - type: "interface", - name: "contracts::helloStarknet::IHelloStarknet", - items: [ - { - type: "function", - name: "increase_balance", + name: "withdraw", inputs: [ { - name: "amount", - type: "core::integer::u32", + name: "shares", + type: "core::integer::u256", }, ], outputs: [], @@ -673,21 +59,10 @@ const deployedContracts = { { type: "function", name: "get_balance", - inputs: [], - outputs: [ - { - type: "core::integer::u32", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "get_balance_increment", inputs: [ { - name: "amount", - type: "core::integer::u32", + name: "account", + type: "core::starknet::contract_address::ContractAddress", }, ], outputs: [ @@ -699,286 +74,89 @@ const deployedContracts = { }, ], }, - { - type: "event", - name: "contracts::helloStarknet::HelloStarknet::Event", - kind: "enum", - variants: [], - }, - ], - }, - SimpleStorage: { - address: - "0x072c40ba7d070bfd6aaa7532743f635cc432748ab1f7e8e4254c4238975858f3", - abi: [ - { - type: "impl", - name: "SimpleStorageImpl", - interface_name: "contracts::simpleStorage::ISimpleStorage", - }, - { - type: "interface", - name: "contracts::simpleStorage::ISimpleStorage", - items: [ - { - type: "function", - name: "get_name", - inputs: [], - outputs: [ - { - type: "core::felt252", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "set_name", - inputs: [ - { - name: "name", - type: "core::felt252", - }, - ], - outputs: [], - state_mutability: "external", - }, - ], - }, { type: "constructor", name: "constructor", inputs: [ { - name: "name", - type: "core::felt252", + name: "token", + type: "core::starknet::contract_address::ContractAddress", }, ], }, { type: "event", - name: "contracts::simpleStorage::SimpleStorage::Event", + name: "contracts::challenge1::Challenge1::Event", kind: "enum", variants: [], }, ], }, - Vote: { + }, + sepolia: { + Challenge1: { address: - "0x04c394c4fc87a9a62f04205ac456f6811b6bcb0465e339f95308332cbff7364f", + "0x00055f8f2a4041be203f2c47a99f5e6c026713f9e7a144ab09d40a9694c0f8a8", abi: [ { type: "impl", - name: "VoteImpl", - interface_name: "contracts::vote::VoteTrait", + name: "Challenge1", + interface_name: "contracts::challenge1::IChallenge1", }, { - type: "enum", - name: "core::bool", - variants: [ + type: "struct", + name: "core::integer::u256", + members: [ { - name: "False", - type: "()", + name: "low", + type: "core::integer::u128", }, { - name: "True", - type: "()", + name: "high", + type: "core::integer::u128", }, ], }, { type: "interface", - name: "contracts::vote::VoteTrait", + name: "contracts::challenge1::IChallenge1", items: [ { type: "function", - name: "get_vote_status", - inputs: [], - outputs: [ - { - type: "(core::integer::u8, core::integer::u8, core::integer::u8, core::integer::u8)", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "voter_can_vote", - inputs: [ - { - name: "user_address", - type: "core::starknet::contract_address::ContractAddress", - }, - ], - outputs: [ - { - type: "core::bool", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "is_voter_registered", - inputs: [ - { - name: "address", - type: "core::starknet::contract_address::ContractAddress", - }, - ], - outputs: [ - { - type: "core::bool", - }, - ], - state_mutability: "view", - }, - { - type: "function", - name: "vote", + name: "deposit", inputs: [ { - name: "vote", - type: "core::integer::u8", + name: "amount", + type: "core::integer::u256", }, ], outputs: [], state_mutability: "external", }, - ], - }, - { - type: "constructor", - name: "constructor", - inputs: [ - { - name: "voter_1", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "voter_2", - type: "core::starknet::contract_address::ContractAddress", - }, - { - name: "voter_3", - type: "core::starknet::contract_address::ContractAddress", - }, - ], - }, - { - type: "event", - name: "contracts::vote::Vote::VoteCast", - kind: "struct", - members: [ - { - name: "voter", - type: "core::starknet::contract_address::ContractAddress", - kind: "data", - }, - { - name: "vote", - type: "core::integer::u8", - kind: "data", - }, - ], - }, - { - type: "event", - name: "contracts::vote::Vote::UnauthorizedAttempt", - kind: "struct", - members: [ - { - name: "unauthorized_address", - type: "core::starknet::contract_address::ContractAddress", - kind: "data", - }, - ], - }, - { - type: "event", - name: "contracts::vote::Vote::Event", - kind: "enum", - variants: [ - { - name: "VoteCast", - type: "contracts::vote::Vote::VoteCast", - kind: "nested", - }, - { - name: "UnauthorizedAttempt", - type: "contracts::vote::Vote::UnauthorizedAttempt", - kind: "nested", - }, - ], - }, - ], - }, - Ownable: { - address: - "0x07b0a115eb4111e34607418ff37015e8f60149541d3af39b094d19eea2ec5ad4", - abi: [ - { - type: "impl", - name: "OwnableDataImpl", - interface_name: "contracts::IData", - }, - { - type: "interface", - name: "contracts::IData", - items: [ - { - type: "function", - name: "get_data", - inputs: [], - outputs: [ - { - type: "core::integer::u64", - }, - ], - state_mutability: "view", - }, { type: "function", - name: "set_data", + name: "withdraw", inputs: [ { - name: "new_value", - type: "core::integer::u64", + name: "shares", + type: "core::integer::u256", }, ], outputs: [], state_mutability: "external", }, - ], - }, - { - type: "impl", - name: "OwnableImpl", - interface_name: "contracts::IOwnable", - }, - { - type: "interface", - name: "contracts::IOwnable", - items: [ { type: "function", - name: "transfer_ownership", + name: "get_balance", inputs: [ { - name: "new_owner", + name: "account", type: "core::starknet::contract_address::ContractAddress", }, ], - outputs: [], - state_mutability: "external", - }, - { - type: "function", - name: "owner", - inputs: [], outputs: [ { - type: "core::starknet::contract_address::ContractAddress", + type: "core::integer::u32", }, ], state_mutability: "view", @@ -990,51 +168,16 @@ const deployedContracts = { name: "constructor", inputs: [ { - name: "initial_owner", - type: "core::starknet::contract_address::ContractAddress", - }, - ], - }, - { - type: "event", - name: "contracts::ownable_component::OwnershipTransferred", - kind: "struct", - members: [ - { - name: "previous_owner", - type: "core::starknet::contract_address::ContractAddress", - kind: "key", - }, - { - name: "new_owner", + name: "token", type: "core::starknet::contract_address::ContractAddress", - kind: "data", - }, - ], - }, - { - type: "event", - name: "contracts::ownable_component::Event", - kind: "enum", - variants: [ - { - name: "OwnershipTransferred", - type: "contracts::ownable_component::OwnershipTransferred", - kind: "nested", }, ], }, { type: "event", - name: "contracts::Ownable::Event", + name: "contracts::challenge1::Challenge1::Event", kind: "enum", - variants: [ - { - name: "OwnableEvent", - type: "contracts::ownable_component::Event", - kind: "nested", - }, - ], + variants: [], }, ], }, diff --git a/packages/nextjs/scaffold.config.ts b/packages/nextjs/scaffold.config.ts index 9d0109107..b57feb3a0 100644 --- a/packages/nextjs/scaffold.config.ts +++ b/packages/nextjs/scaffold.config.ts @@ -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 || "", diff --git a/packages/snfoundry/contracts/src/challenge0.cairo b/packages/snfoundry/contracts/src/challenge0.cairo deleted file mode 100644 index 80e0abe6c..000000000 --- a/packages/snfoundry/contracts/src/challenge0.cairo +++ /dev/null @@ -1,62 +0,0 @@ -use starknet::ContractAddress; - -#[starknet::interface] -pub trait IChallenge0 { - fn mint_item(ref self: T, recipient: ContractAddress) -> u256; - fn mint_id(ref self: T, recipient: ContractAddress, id: u256); -} -#[starknet::contract] -mod Challenge0 { - use super::IChallenge0; - use openzeppelin::introspection::src5::SRC5Component; - use openzeppelin::token::erc721::ERC721Component; - use starknet::ContractAddress; - - component!(path: ERC721Component, storage: erc721, event: ERC721Event); - component!(path: SRC5Component, storage: src5, event: SRC5Event); - - // ERC721Mixin - #[abi(embed_v0)] - impl ERC721MixinImpl = ERC721Component::ERC721MixinImpl; - //pub impl ERC721MetadataImpl = ERC721Component::ERC721MetadataImpl; - impl ERC721InternalImpl = ERC721Component::InternalImpl; - - #[storage] - struct Storage { - #[substorage(v0)] - erc721: ERC721Component::Storage, - #[substorage(v0)] - src5: SRC5Component::Storage - } - - #[event] - #[derive(Drop, starknet::Event)] - enum Event { - #[flat] - ERC721Event: ERC721Component::Event, - #[flat] - SRC5Event: SRC5Component::Event - } - - #[constructor] - fn constructor(ref self: ContractState) { - let name: ByteArray = "YourCollectible"; - let symbol: ByteArray = "YCB"; - let base_uri: ByteArray = - "https://ipfs.io/ipfs/QmfVMAmNM1kDEBYrC2TPzQDoCRFH6F5tE1e9Mr4FkkR5Xr"; // bison nft - - self.erc721.initializer(name, symbol, base_uri); - } - - #[abi(embed_v0)] - pub impl Challenge0Impl of IChallenge0 { - fn mint_item(ref self: ContractState, recipient: ContractAddress) -> u256 { - let id: u256 = 1; - self.erc721._mint(recipient, id); // _mint include _setTokenURI() - id - } - fn mint_id(ref self: ContractState, recipient: ContractAddress, id: u256) { - self.erc721._mint(recipient, id); - } - } -} diff --git a/packages/snfoundry/contracts/src/challenge1.cairo b/packages/snfoundry/contracts/src/challenge1.cairo index 3b367d74e..164bab035 100644 --- a/packages/snfoundry/contracts/src/challenge1.cairo +++ b/packages/snfoundry/contracts/src/challenge1.cairo @@ -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 { + 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; - impl InternalImpl = ERC20Component::InternalImpl; +#[starknet::interface] +pub trait IChallenge1 { + 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, + 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 { + 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() + } } -} \ No newline at end of file +} diff --git a/packages/snfoundry/contracts/src/challenge2.cairo b/packages/snfoundry/contracts/src/challenge2.cairo deleted file mode 100644 index 050ae7c83..000000000 --- a/packages/snfoundry/contracts/src/challenge2.cairo +++ /dev/null @@ -1,54 +0,0 @@ -#[starknet::contract] -mod Challenge2 { - use openzeppelin::introspection::src5::SRC5Component; - use openzeppelin::token::erc1155::ERC1155Component; - use starknet::ContractAddress; - - component!(path: ERC1155Component, storage: erc1155, event: ERC1155Event); - component!(path: SRC5Component, storage: src5, event: SRC5Event); - - // ERC1155 - #[abi(embed_v0)] - impl ERC1155Impl = ERC1155Component::ERC1155Impl; - #[abi(embed_v0)] - impl ERC1155MetadataURIImpl = - ERC1155Component::ERC1155MetadataURIImpl; - #[abi(embed_v0)] - impl ERC1155Camel = ERC1155Component::ERC1155CamelImpl; - impl ERC1155InternalImpl = ERC1155Component::InternalImpl; - - // SRC5 - #[abi(embed_v0)] - impl SRC5Impl = SRC5Component::SRC5Impl; - - #[storage] - struct Storage { - #[substorage(v0)] - erc1155: ERC1155Component::Storage, - #[substorage(v0)] - src5: SRC5Component::Storage - } - - #[event] - #[derive(Drop, starknet::Event)] - enum Event { - #[flat] - ERC1155Event: ERC1155Component::Event, - #[flat] - SRC5Event: SRC5Component::Event - } - - #[constructor] - fn constructor( - ref self: ContractState, - base_uri: ByteArray, - recipient: ContractAddress, - token_ids: Span, - values: Span - ) { - self.erc1155.initializer(base_uri); - self - .erc1155 - .batch_mint_with_acceptance_check(recipient, token_ids, values, array![].span()); - } -} \ No newline at end of file diff --git a/packages/snfoundry/contracts/src/challenge3.cairo b/packages/snfoundry/contracts/src/challenge3.cairo deleted file mode 100644 index 0ce542562..000000000 --- a/packages/snfoundry/contracts/src/challenge3.cairo +++ /dev/null @@ -1,35 +0,0 @@ -#[starknet::contract(account)] -mod Challenge3 { - use openzeppelin::account::AccountComponent; - use openzeppelin::introspection::src5::SRC5Component; - - component!(path: AccountComponent, storage: account, event: AccountEvent); - component!(path: SRC5Component, storage: src5, event: SRC5Event); - - // AccountMixin - #[abi(embed_v0)] - impl AccountMixinImpl = AccountComponent::AccountMixinImpl; - impl AccountInternalImpl = AccountComponent::InternalImpl; - - #[storage] - struct Storage { - #[substorage(v0)] - account: AccountComponent::Storage, - #[substorage(v0)] - src5: SRC5Component::Storage - } - - #[event] - #[derive(Drop, starknet::Event)] - enum Event { - #[flat] - AccountEvent: AccountComponent::Event, - #[flat] - SRC5Event: SRC5Component::Event - } - - #[constructor] - fn constructor(ref self: ContractState, public_key: felt252) { - self.account.initializer(public_key); - } -} \ No newline at end of file diff --git a/packages/snfoundry/contracts/src/helloStarknet.cairo b/packages/snfoundry/contracts/src/helloStarknet.cairo deleted file mode 100644 index dd1408813..000000000 --- a/packages/snfoundry/contracts/src/helloStarknet.cairo +++ /dev/null @@ -1,30 +0,0 @@ -#[starknet::interface] -pub trait IHelloStarknet { - fn increase_balance(ref self: TContractState, amount: u32); - fn get_balance(self: @TContractState) -> u32; - fn get_balance_increment(self: @TContractState, amount: u32) -> u32; -} - -#[starknet::contract] -mod HelloStarknet { - #[storage] - struct Storage { - balance: u32, - } - - #[abi(embed_v0)] - impl HelloStarknetImpl of super::IHelloStarknet { - fn increase_balance(ref self: ContractState, amount: u32) { - assert(amount !=0, 'amount cannot be 0'); - self.balance.write(self.balance.read() + amount); - } - - fn get_balance(self: @ContractState) -> u32 { - self.balance.read() - } - - fn get_balance_increment(self: @ContractState, amount: u32) -> u32 { - self.balance.read() + amount - } - } -} diff --git a/packages/snfoundry/contracts/src/lib.cairo b/packages/snfoundry/contracts/src/lib.cairo index 4290cbfd6..e829f7df1 100644 --- a/packages/snfoundry/contracts/src/lib.cairo +++ b/packages/snfoundry/contracts/src/lib.cairo @@ -1,126 +1 @@ -mod helloStarknet; -mod simpleStorage; -mod vote; -mod challenge0; -mod challenge1; -mod challenge2; -mod challenge3; - -use starknet::ContractAddress; - -#[starknet::interface] -trait IData { - fn get_data(self: @T) -> u64; - fn set_data(ref self: T, new_value: u64); -} - -#[starknet::interface] -trait IOwnable { - fn transfer_ownership(ref self: T, new_owner: ContractAddress); - fn owner(self: @T) -> ContractAddress; -} - -#[starknet::component] -pub mod ownable_component { - use super::{ContractAddress, IOwnable}; - use starknet::get_caller_address; - use core::num::traits::Zero; - - #[storage] - struct Storage { - owner: ContractAddress - } - - #[event] - #[derive(Drop, starknet::Event)] - pub enum Event { - OwnershipTransferred: OwnershipTransferred - } - - #[derive(Drop, starknet::Event)] - struct OwnershipTransferred { - #[key] - previous_owner: ContractAddress, - new_owner: ContractAddress, - } - - #[embeddable_as(Ownable)] - impl OwnableImpl< - TContractState, +HasComponent - > of IOwnable> { - fn transfer_ownership( - ref self: ComponentState, new_owner: ContractAddress - ) { - self.only_owner(); - self._transfer_ownership(new_owner); - } - fn owner(self: @ComponentState) -> ContractAddress { - self.owner.read() - } - } - - #[generate_trait] - pub impl InternalImpl< - TContractState, +HasComponent - > of InternalTrait { - fn only_owner(self: @ComponentState) { - let owner: ContractAddress = self.owner.read(); - let caller: ContractAddress = get_caller_address(); - assert(caller.is_non_zero(), 'ZERO_ADDRESS_CALLER'); - assert(caller == owner, 'NOT_OWNER'); - } - - fn _transfer_ownership( - ref self: ComponentState, new_owner: ContractAddress - ) { - let previous_owner: ContractAddress = self.owner.read(); - self.owner.write(new_owner); - self - .emit( - OwnershipTransferred { previous_owner: previous_owner, new_owner: new_owner } - ); - } - } -} - -#[starknet::contract] -mod Ownable { - use contracts::ownable_component; - use super::{ContractAddress, IData}; - - component!(path: ownable_component, storage: ownable, event: OwnableEvent); - - #[abi(embed_v0)] - impl OwnableImpl = ownable_component::Ownable; - - impl OwnableInternalImpl = ownable_component::InternalImpl; - - #[storage] - struct Storage { - data: u64, - #[substorage(v0)] - ownable: ownable_component::Storage - } - - #[event] - #[derive(Drop, starknet::Event)] - enum Event { - OwnableEvent: ownable_component::Event - } - - #[constructor] - fn constructor(ref self: ContractState, initial_owner: ContractAddress) { - self.ownable.owner.write(initial_owner); - self.data.write(1); - } - #[abi(embed_v0)] - impl OwnableDataImpl of IData { - fn get_data(self: @ContractState) -> u64 { - self.data.read() - } - fn set_data(ref self: ContractState, new_value: u64) { - self.ownable.only_owner(); - self.data.write(new_value); - } - } -} +mod challenge1; \ No newline at end of file diff --git a/packages/snfoundry/contracts/src/simpleStorage.cairo b/packages/snfoundry/contracts/src/simpleStorage.cairo deleted file mode 100644 index 08f061530..000000000 --- a/packages/snfoundry/contracts/src/simpleStorage.cairo +++ /dev/null @@ -1,29 +0,0 @@ -#[starknet::interface] -trait ISimpleStorage { - fn get_name(self: @T) -> felt252; - fn set_name(ref self: T, name: felt252); -} - -#[starknet::contract] -mod SimpleStorage { - #[storage] - struct Storage { - name: felt252, - } - - #[constructor] - fn constructor(ref self: ContractState, name: felt252) { - self.name.write(name); - } - - #[abi(embed_v0)] - impl SimpleStorageImpl of super::ISimpleStorage { - fn get_name(self: @ContractState) -> felt252 { - self.name.read() - } - - fn set_name(ref self: ContractState, name: felt252) { - self.name.write(name); - } - } -} diff --git a/packages/snfoundry/contracts/src/vote.cairo b/packages/snfoundry/contracts/src/vote.cairo deleted file mode 100644 index ba5d19dfb..000000000 --- a/packages/snfoundry/contracts/src/vote.cairo +++ /dev/null @@ -1,171 +0,0 @@ -/// @dev Core Library Imports for the Traits outside the Starknet Contract -use starknet::ContractAddress; - -/// @dev Trait defining the functions that can be implemented or called by the Starknet Contract -#[starknet::interface] -trait VoteTrait { - /// @dev Function that returns the current vote status - fn get_vote_status(self: @T) -> (u8, u8, u8, u8); - /// @dev Function that checks if the user at the specified address is allowed to vote - fn voter_can_vote(self: @T, user_address: ContractAddress) -> bool; - /// @dev Function that checks if the specified address is registered as a voter - fn is_voter_registered(self: @T, address: ContractAddress) -> bool; - /// @dev Function that allows a user to vote - fn vote(ref self: T, vote: u8); -} - -/// @dev Starknet Contract allowing three registered voters to vote on a proposal -#[starknet::contract] -mod Vote { - use super::ContractAddress; - use starknet::get_caller_address; - - const YES: u8 = 1_u8; - const NO: u8 = 0_u8; - - /// @dev Structure that stores vote counts and voter states - #[storage] - struct Storage { - yes_votes: u8, - no_votes: u8, - can_vote: LegacyMap::, - registered_voter: LegacyMap::, - } - - /// @dev Contract constructor initializing the contract with a list of registered voters and 0 vote count - #[constructor] - fn constructor( - ref self: ContractState, - voter_1: ContractAddress, - voter_2: ContractAddress, - voter_3: ContractAddress - ) { - // Register all voters by calling the _register_voters function - self._register_voters(voter_1, voter_2, voter_3); - } - - /// @dev Event that gets emitted when a vote is cast - #[event] - #[derive(Drop, starknet::Event)] - enum Event { - VoteCast: VoteCast, - UnauthorizedAttempt: UnauthorizedAttempt, - } - - /// @dev Represents a vote that was cast - #[derive(Drop, starknet::Event)] - struct VoteCast { - voter: ContractAddress, - vote: u8, - } - - /// @dev Represents an unauthorized attempt to vote - #[derive(Drop, starknet::Event)] - struct UnauthorizedAttempt { - unauthorized_address: ContractAddress, - } - - /// @dev Implementation of VoteTrait for ContractState - #[abi(embed_v0)] - impl VoteImpl of super::VoteTrait { - /// @dev Returns the voting results - fn get_vote_status(self: @ContractState) -> (u8, u8, u8, u8) { - let (n_yes, n_no) = self._get_voting_result(); - let (yes_percentage, no_percentage) = self._get_voting_result_in_percentage(); - (n_yes, n_no, yes_percentage, no_percentage) - } - - /// @dev Check whether a voter is allowed to vote - fn voter_can_vote(self: @ContractState, user_address: ContractAddress) -> bool { - self.can_vote.read(user_address) - } - - /// @dev Check whether an address is registered as a voter - fn is_voter_registered(self: @ContractState, address: ContractAddress) -> bool { - self.registered_voter.read(address) - } - - /// @dev Submit a vote - fn vote(ref self: ContractState, vote: u8) { - assert(vote == NO || vote == YES, 'VOTE_0_OR_1'); - let caller: ContractAddress = get_caller_address(); - self._assert_allowed(caller); - self.can_vote.write(caller, false); - - if (vote == NO) { - self.no_votes.write(self.no_votes.read() + 1_u8); - } - if (vote == YES) { - self.yes_votes.write(self.yes_votes.read() + 1_u8); - } - - self.emit(VoteCast { voter: caller, vote: vote, }); - } - } - - /// @dev Internal Functions implementation for the Vote contract - #[generate_trait] - impl InternalFunctions of InternalFunctionsTrait { - /// @dev Registers the voters and initializes their voting status to true (can vote) - fn _register_voters( - ref self: ContractState, - voter_1: ContractAddress, - voter_2: ContractAddress, - voter_3: ContractAddress - ) { - self.registered_voter.write(voter_1, true); - self.can_vote.write(voter_1, true); - - self.registered_voter.write(voter_2, true); - self.can_vote.write(voter_2, true); - - self.registered_voter.write(voter_3, true); - self.can_vote.write(voter_3, true); - } - } - - /// @dev Asserts implementation for the Vote contract - #[generate_trait] - impl AssertsImpl of AssertsTrait { - // @dev Internal function that checks if an address is allowed to vote - fn _assert_allowed(ref self: ContractState, address: ContractAddress) { - let is_voter: bool = self.registered_voter.read((address)); - let can_vote: bool = self.can_vote.read((address)); - - if (!can_vote) { // can_vote == false - self.emit(UnauthorizedAttempt { unauthorized_address: address, }); - } - - assert(is_voter, 'USER_NOT_REGISTERED'); - assert(can_vote, 'USER_ALREADY_VOTED'); - } - } - - /// @dev Implement the VotingResultTrait for the Vote contract - #[generate_trait] - impl VoteResultFunctionsImpl of VoteResultFunctionsTrait { - // @dev Internal function to get the voting results (yes and no vote counts) - fn _get_voting_result(self: @ContractState) -> (u8, u8) { - let n_yes: u8 = self.yes_votes.read(); - let n_no: u8 = self.no_votes.read(); - - (n_yes, n_no) - } - - // @dev Internal function to calculate the voting results in percentage - fn _get_voting_result_in_percentage(self: @ContractState) -> (u8, u8) { - let n_yes: u8 = self.yes_votes.read(); - let n_no: u8 = self.no_votes.read(); - - let total_votes: u8 = n_yes + n_no; - - if (total_votes == 0_u8) { - return (0, 0); - } - let yes_percentage: u8 = (n_yes * 100_u8) / (total_votes); - let no_percentage: u8 = (n_no * 100_u8) / (total_votes); - - (yes_percentage, no_percentage) - } - } -} diff --git a/packages/snfoundry/scripts_js/deploy.js b/packages/snfoundry/scripts_js/deploy.js index 2cee62532..8290f4b0d 100644 --- a/packages/snfoundry/scripts_js/deploy.js +++ b/packages/snfoundry/scripts_js/deploy.js @@ -125,80 +125,16 @@ const deployContract = async ( }; const deployScript = async () => { - const { - classHash: helloStarknetClassHash, - abi: helloStarknetAbi, - address: ContractAddress, - } = await deployContract(null, "HelloStarknet"); // can pass another argument for the exported contract name - await deployContract( + const { } = await deployContract( { - name: 1, + token: "0x049D36570D4E46F48E99674BD3FCC84644DDD6B96F7C741B1562B82F9E004DC7", }, - "SimpleStorage" + "Challenge1" ); - - // await deployContract( - // { - // name: "MARQUIS", - // symbol: "MARQ", - // recipient: deployer.address, - // fixed_supply: 100, - // }, - // "Challenge1" - // ); - - // await deployContract( - // { - // name: 1, - // symbol: 2, - // fixed_supply: 10, - // recipient: - // "0x06072Bb27d275a0bC1deBf1753649b8721CF845B681A48443Ac46baF45769f8E", - // }, - // "Challenge1" - // ); - - // await deployContract( - // { - // base_uri: CallData.byteArrayFromString("https://example.com/"), - // // recipient: - // // "0x06072Bb27d275a0bC1deBf1753649b8721CF845B681A48443Ac46baF45769f8E", - // // token_ids: 2, - // // values: 100, - // }, - // "Challenge2" - // ); - - // await deployContract( - // { - // public_key: - // "0x6e4fd4f9d6442e10cf8e20a799be3533be3756c5ea4d13e16a297d7d2717039", - // }, - // "Challenge3" - // ); - - // await deployContract( - // { - // voter_1: - // "0x06072Bb27d275a0bC1deBf1753649b8721CF845B681A48443Ac46baF45769f8E", - // voter_2: - // "0x06072Bb27d275a0bC1deBf1753649b8721CF845B681A48443Ac46baF45769f8E", - // voter_3: - // "0x06072Bb27d275a0bC1deBf1753649b8721CF845B681A48443Ac46baF45769f8E", - // }, - // "Vote" - // ); - // await deployContract( - // { - // initial_owner: - // "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691", - // }, - // "Ownable" - // ); // simple storage receives an argument in the constructor }; deployScript() .then(() => { console.log("All Setup Done"); }) - .catch(console.error); + .catch(console.error); \ No newline at end of file