-
Notifications
You must be signed in to change notification settings - Fork 3
Feat: implement up-gradable and access control #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 contains hidden or 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 contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // Compatible with OpenZeppelin Contracts for Cairo 2.0.0-alpha.1 | ||
|
|
||
| #[starknet::contract(account)] | ||
| mod Account { | ||
| use account::interfaces::Iaccount::Iaccount; | ||
| use openzeppelin::account::AccountComponent; | ||
| use openzeppelin::account::extensions::SRC9Component; | ||
| use openzeppelin::introspection::src5::SRC5Component; | ||
| use openzeppelin::upgrades::UpgradeableComponent; | ||
| use openzeppelin::upgrades::interface::IUpgradeable; | ||
| use starknet::storage::{ | ||
| Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess, | ||
| StoragePointerWriteAccess, | ||
| }; | ||
| use starknet::{ClassHash, ContractAddress, get_caller_address}; | ||
|
|
||
|
|
||
| component!(path: AccountComponent, storage: account, event: AccountEvent); | ||
| component!(path: SRC5Component, storage: src5, event: SRC5Event); | ||
| component!(path: SRC9Component, storage: src9, event: SRC9Event); | ||
| component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent); | ||
|
|
||
| // External | ||
| #[abi(embed_v0)] | ||
| impl AccountMixinImpl = AccountComponent::AccountMixinImpl<ContractState>; | ||
| #[abi(embed_v0)] | ||
| impl OutsideExecutionV2Impl = | ||
| SRC9Component::OutsideExecutionV2Impl<ContractState>; | ||
|
|
||
| // Internal | ||
| impl AccountInternalImpl = AccountComponent::InternalImpl<ContractState>; | ||
| impl OutsideExecutionInternalImpl = SRC9Component::InternalImpl<ContractState>; | ||
| impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>; | ||
|
|
||
| #[storage] | ||
| struct Storage { | ||
| #[substorage(v0)] | ||
| account: AccountComponent::Storage, | ||
| #[substorage(v0)] | ||
| src5: SRC5Component::Storage, | ||
| #[substorage(v0)] | ||
| src9: SRC9Component::Storage, | ||
| #[substorage(v0)] | ||
| upgradeable: UpgradeableComponent::Storage, | ||
| // Custom storage for SyncPayment functionality | ||
| fiat_balance: Map<(ContractAddress, felt252), u128>, // (user, currency) => balance | ||
| token_address: Map<felt252, ContractAddress>, // symbol => token_address | ||
| default_fiat_currency: felt252, | ||
| liquidity_bridge: ContractAddress, | ||
| initialized: bool, | ||
| } | ||
|
|
||
| #[event] | ||
| #[derive(Drop, starknet::Event)] | ||
| enum Event { | ||
| #[flat] | ||
| AccountEvent: AccountComponent::Event, | ||
| #[flat] | ||
| SRC5Event: SRC5Component::Event, | ||
| #[flat] | ||
| SRC9Event: SRC9Component::Event, | ||
| #[flat] | ||
| UpgradeableEvent: UpgradeableComponent::Event, | ||
| } | ||
|
|
||
| #[constructor] | ||
| fn constructor(ref self: ContractState, public_key: felt252) { | ||
| self.account.initializer(public_key); | ||
| self.src9.initializer(); | ||
| } | ||
|
|
||
| // | ||
| // Upgradeable | ||
| // | ||
|
|
||
| #[abi(embed_v0)] | ||
| impl UpgradeableImpl of IUpgradeable<ContractState> { | ||
| fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { | ||
| self.account.assert_only_self(); | ||
| self.upgradeable.upgrade(new_class_hash); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // contract impl | ||
| #[abi(embed_v0)] | ||
| impl AccountImpl of Iaccount<ContractState> { | ||
| fn increase_balance(ref self: ContractState, amount: felt252) { | ||
| assert(amount != 0, 'Amount cannot be 0'); | ||
| let caller = get_caller_address(); | ||
| let currency = self.default_fiat_currency.read(); | ||
| let current_balance = self.fiat_balance.read((caller, currency)); | ||
|
|
||
| self | ||
| .fiat_balance | ||
| .write((caller, currency), current_balance + amount.try_into().unwrap()); | ||
| } | ||
|
|
||
| fn get_balance(self: @ContractState) -> felt252 { | ||
| let caller = get_caller_address(); | ||
| let currency = self.default_fiat_currency.read(); | ||
| self.fiat_balance.read((caller, currency)).into() | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #[starknet::interface] | ||
| pub trait Iaccount<TContractState> { | ||
| fn increase_balance(ref self: TContractState, amount: felt252); | ||
| fn get_balance(self: @TContractState) -> felt252; | ||
| } |
This file contains hidden or 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,32 +1,7 @@ | ||
| /// Interface representing `HelloContract`. | ||
| /// This interface allows modification and retrieval of the contract balance. | ||
| #[starknet::interface] | ||
| pub trait IHelloStarknet<TContractState> { | ||
| /// Increase contract balance. | ||
| fn increase_balance(ref self: TContractState, amount: felt252); | ||
| /// Retrieve contract balance. | ||
| fn get_balance(self: @TContractState) -> felt252; | ||
| pub mod contract { | ||
| pub mod account; | ||
| } | ||
|
|
||
| /// Simple contract for managing balance. | ||
| #[starknet::contract] | ||
| mod HelloStarknet { | ||
| use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; | ||
|
|
||
| #[storage] | ||
| struct Storage { | ||
| balance: felt252, | ||
| } | ||
|
|
||
| #[abi(embed_v0)] | ||
| impl HelloStarknetImpl of super::IHelloStarknet<ContractState> { | ||
| fn increase_balance(ref self: ContractState, amount: felt252) { | ||
| assert(amount != 0, 'Amount cannot be 0'); | ||
| self.balance.write(self.balance.read() + amount); | ||
| } | ||
|
|
||
| fn get_balance(self: @ContractState) -> felt252 { | ||
| self.balance.read() | ||
| } | ||
| } | ||
| pub mod interfaces { | ||
| pub mod Iaccount; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub mod test_contract; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add this to storage:
// Custom storage for SyncPayment functionality
fiat_balance: Map<(ContractAddress, felt252), u128>, // (user, currency) => balance
token_address: Map<felt252, ContractAddress>, // symbol => token_address
default_fiat_currency: felt252,
liquidity_bridge: ContractAddress,
initialized: bool,