-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: executable as standalone component
- Loading branch information
1 parent
0923398
commit 14628c4
Showing
12 changed files
with
165 additions
and
129 deletions.
There are no files selected for viewing
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 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 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
mod accountV3; |
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 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
mod executable; |
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// ************************************************************************* | ||
// EXECUTABLE COMPONENT | ||
// ************************************************************************* | ||
#[starknet::component] | ||
mod UpgradeableComponent { | ||
use starknet::SyscallResultTrait; | ||
|
||
use token_bound_accounts::interfaces::IExecutable; | ||
|
||
// ************************************************************************* | ||
// STORAGE | ||
// ************************************************************************* | ||
#[storage] | ||
struct Storage {} | ||
|
||
// ************************************************************************* | ||
// EVENT | ||
// ************************************************************************* | ||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
TransactionExecuted: TransactionExecuted | ||
} | ||
|
||
/// @notice Emitted when the account executes a transaction | ||
/// @param hash The transaction hash | ||
/// @param response The data returned by the methods called | ||
#[derive(Drop, starknet::Event)] | ||
struct TransactionExecuted { | ||
#[key] | ||
hash: felt252, | ||
#[key] | ||
account_address: ContractAddress, | ||
response: Span<Span<felt252>> | ||
} | ||
|
||
// ************************************************************************* | ||
// ERRORS | ||
// ************************************************************************* | ||
mod Errors { | ||
const UNAUTHORIZED: felt252 = 'Account: unauthorized'; | ||
} | ||
|
||
// ************************************************************************* | ||
// EXTERNAL FUNCTIONS | ||
// ************************************************************************* | ||
#[embeddable_as(AccountExecutable)] | ||
impl Account< | ||
TContractState, +HasComponent<TContractState>, +Drop<TContractState> | ||
> of IExecutable<ComponentState<TContractState>> { | ||
/// @notice executes a transaction | ||
/// @param calls an array of transactions to be executed | ||
fn _execute( | ||
ref self: ComponentState<TContractState>, mut calls: Array<Call> | ||
) -> Array<Span<felt252>> { | ||
let caller = get_caller_address(); | ||
assert(self._is_valid_signer(caller), Errors::UNAUTHORIZED); | ||
|
||
let tx_info = get_tx_info().unbox(); | ||
assert(tx_info.version != 0, Errors::INV_TX_VERSION); | ||
|
||
let retdata = self._execute_calls(calls); | ||
let hash = tx_info.transaction_hash; | ||
let response = retdata.span(); | ||
self.emit( | ||
TransactionExecuted { | ||
hash, | ||
account_address: get_contract_address(), | ||
response | ||
}); | ||
retdata | ||
} | ||
} | ||
|
||
// ************************************************************************* | ||
// PRIVATE FUNCTIONS | ||
// ************************************************************************* | ||
#[generate_trait] | ||
impl InternalImpl< | ||
TContractState, +HasComponent<TContractState>, +Drop<TContractState> | ||
> of InternalTrait<TContractState> { | ||
/// @notice internal function for executing transactions | ||
/// @param calls An array of transactions to be executed | ||
fn _execute_calls( | ||
ref self: ComponentState<TContractState>, mut calls: Array<Call> | ||
) -> Array<Span<felt252>> { | ||
let mut result: Array<Span<felt252>> = ArrayTrait::new(); | ||
let mut calls = calls; | ||
|
||
loop { | ||
match calls.pop_front() { | ||
Option::Some(call) => { | ||
match call_contract_syscall(call.to, call.selector, call.calldata) { | ||
Result::Ok(mut retdata) => { result.append(retdata); }, | ||
Result::Err(_) => { panic(array!['multicall_failed']); } | ||
} | ||
}, | ||
Option::None(_) => { break (); } | ||
}; | ||
}; | ||
result | ||
} | ||
} | ||
} |
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 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
Oops, something went wrong.