Skip to content

Commit

Permalink
Merge pull request #36 from Darlington02/main
Browse files Browse the repository at this point in the history
chore: upgrade contracts to cairo v2.5.4, snfoundry 0.18
  • Loading branch information
Darlington02 authored Mar 1, 2024
2 parents 06b7d14 + d5b4518 commit 8ad8068
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 229 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
scarb 2.4.3
starknet-foundry 0.14.0
scarb 2.5.4
starknet-foundry 0.18.0
6 changes: 3 additions & 3 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ version = 1

[[package]]
name = "snforge_std"
version = "0.14.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.14.0#e8cbecee4e31ed428c76d5173eaa90c8df796fe3"
version = "0.18.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.18.0#48f909a56b08cbdc5ca6a21a836b0fbc6c36d55b"

[[package]]
name = "token_bound_accounts"
version = "0.1.0"
version = "0.3.0"
dependencies = [
"snforge_std",
]
6 changes: 3 additions & 3 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "token_bound_accounts"
version = "0.1.0"
version = "0.3.0"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest

Expand All @@ -9,8 +9,8 @@ sierra = true
casm = true

[dependencies]
starknet = "2.4.3"
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.14.0" }
starknet = "2.5.4"
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.18.0" }

[tool.snforge]
# exit_first = true
2 changes: 1 addition & 1 deletion src/account.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod account;

use account::AccountComponent;
use account::AccountComponent;
70 changes: 39 additions & 31 deletions src/account/account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,16 @@ mod AccountComponent {

mod Errors {
const LOCKED_ACCOUNT: felt252 = 'Account: account is locked!';
const INV_TX_VERSION: felt252 = 'Account: invalid tx version';
const INV_TX_VERSION: felt252 = 'Account: invalid tx version';
const UNAUTHORIZED: felt252 = 'Account: unauthorized';
const INV_SIG_LEN: felt252 = 'Account: invalid sig length';
const INV_SIGNATURE: felt252 = 'Account: invalid signature';
}

#[embeddable_as(AccountImpl)]
impl Account<
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>
> of IAccount<ComponentState<TContractState>> {
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
> of IAccount<ComponentState<TContractState>> {
/// @notice used for signature validation
/// @param hash The message hash
/// @param signature The signature to be validated
Expand All @@ -85,32 +83,40 @@ mod AccountComponent {
}

fn __validate_deploy__(
self: @ComponentState<TContractState>, class_hash: felt252, contract_address_salt: felt252,
self: @ComponentState<TContractState>,
class_hash: felt252,
contract_address_salt: felt252,
) -> felt252 {
self._validate_transaction()
}

fn __validate_declare__(self: @ComponentState<TContractState>, class_hash: felt252) -> felt252 {
fn __validate_declare__(
self: @ComponentState<TContractState>, class_hash: felt252
) -> felt252 {
self._validate_transaction()
}

/// @notice validate an account transaction
/// @param calls an array of transactions to be executed
fn __validate__(ref self: ComponentState<TContractState>, mut calls: Array<Call>) -> felt252 {
fn __validate__(
ref self: ComponentState<TContractState>, mut calls: Array<Call>
) -> felt252 {
self._validate_transaction()
}

/// @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>> {
fn __execute__(
ref self: ComponentState<TContractState>, mut calls: Array<Call>
) -> Array<Span<felt252>> {
self._assert_only_owner();
let (lock_status, _) = self._is_locked();
assert(!lock_status, Errors::LOCKED_ACCOUNT);

let tx_info = get_tx_info().unbox();
assert(tx_info.version != 0, Errors::INV_TX_VERSION);

let retdata = self._execute_calls(calls.span());
let retdata = self._execute_calls(calls);
let hash = tx_info.transaction_hash;
let response = retdata.span();
self.emit(TransactionExecuted { hash, response });
Expand Down Expand Up @@ -155,7 +161,9 @@ mod AccountComponent {

// @notice check that account supports TBA interface
// @param interface_id interface to be checked against
fn supports_interface(self: @ComponentState<TContractState>, interface_id: felt252) -> bool {
fn supports_interface(
self: @ComponentState<TContractState>, interface_id: felt252
) -> bool {
if (interface_id == TBA_INTERFACE_ID) {
return true;
} else {
Expand All @@ -166,25 +174,26 @@ mod AccountComponent {

#[generate_trait]
impl InternalImpl<
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
> of InternalTrait<TContractState> {

/// @notice initializes the account by setting the initial token conrtact and token id
fn initializer(ref self: ComponentState<TContractState>, token_contract: ContractAddress, token_id: u256) {
fn initializer(
ref self: ComponentState<TContractState>,
token_contract: ContractAddress,
token_id: u256
) {
self.Account_token_contract.write(token_contract);
self.Account_token_id.write(token_id);

self.Account_token_contract.write(token_contract);
self.Account_token_id.write(token_id);

let owner = self._get_owner(token_contract, token_id);
self.emit(AccountCreated { owner });
}
let owner = self._get_owner(token_contract, token_id);
self.emit(AccountCreated { owner });
}

/// @notice check that caller is the token bound account
fn _assert_only_owner(ref self: ComponentState<TContractState>) {
let caller = get_caller_address();
let owner = self._get_owner(self.Account_token_contract.read(), self.Account_token_id.read());
let owner = self
._get_owner(self.Account_token_contract.read(), self.Account_token_id.read());
assert(caller == owner, Errors::UNAUTHORIZED);
}

Expand Down Expand Up @@ -246,7 +255,8 @@ mod AccountComponent {
assert(signature_length == 2_u32, Errors::INV_SIG_LEN);

let caller = get_caller_address();
let owner = self._get_owner(self.Account_token_contract.read(), self.Account_token_id.read());
let owner = self
._get_owner(self.Account_token_contract.read(), self.Account_token_id.read());
if (caller == owner) {
return starknet::VALIDATED;
} else {
Expand All @@ -256,20 +266,18 @@ mod AccountComponent {

/// @notice internal function for executing transactions
/// @param calls An array of transactions to be executed
fn _execute_calls(ref self: ComponentState<TContractState>, mut calls: Span<Call>) -> Array<Span<felt252>> {
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.span()
) {
match call_contract_syscall(call.to, call.selector, call.calldata) {
Result::Ok(mut retdata) => { result.append(retdata); },
Result::Err(revert_reason) => {
panic_with_felt252('multicall_failed');
}
Result::Err(_) => { panic_with_felt252('multicall_failed'); }
}
},
Option::None(_) => { break (); }
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod IAccount;
mod IERC721;
mod IRegistry;
mod IUpgradeable;
mod IUpgradeable;
2 changes: 1 addition & 1 deletion src/presets.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod account;

use account::Account;
use account::Account;
6 changes: 3 additions & 3 deletions src/presets/account.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
////////////////////////////////
// Account contract
////////////////////////////////
#[starknet::contract]
#[starknet::contract(account)]
mod Account {
use starknet::ContractAddress;
use starknet::ClassHash;
Expand Down Expand Up @@ -42,13 +42,13 @@ mod Account {
self.account.initializer(token_contract, token_id);
}

#[external(v0)]
#[abi(embed_v0)]
impl UpgradeableImpl of IUpgradeable<ContractState> {
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
self.account._assert_only_owner();
let (lock_status, _) = self.account._is_locked();
assert(!lock_status, AccountComponent::Errors::LOCKED_ACCOUNT);
self.upgradeable._upgrade(new_class_hash);
}
}
}
}
2 changes: 1 addition & 1 deletion src/registry/registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod Registry {
const CALLER_IS_NOT_OWNER: felt252 = 'Registry: caller is not onwer';
}

#[external(v0)]
#[abi(embed_v0)]
impl IRegistryImpl of IRegistry<ContractState> {
/// @notice deploys a new tokenbound account for an NFT
/// @param implementation_hash the class hash of the reference account
Expand Down
1 change: 0 additions & 1 deletion src/test_helper.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod hello_starknet;
mod account_upgrade;
mod erc721_helper;
mod registry_upgrade;
16 changes: 6 additions & 10 deletions src/test_helper/account_upgrade.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ trait IERC721<TContractState> {
fn token_uri(self: @TContractState, token_id: u256) -> felt252;
}

#[starknet::contract]
#[starknet::contract(account)]
mod UpgradedAccount {
use starknet::{
get_tx_info, get_caller_address, get_contract_address, ContractAddress, account::Call,
Expand Down Expand Up @@ -95,7 +95,7 @@ mod UpgradedAccount {
}


#[external(v0)]
#[abi(embed_v0)]
impl IAccountImpl of super::IUpgradedAccount<ContractState> {
fn get_public_key(self: @ContractState) -> felt252 {
self._public_key.read()
Expand Down Expand Up @@ -134,7 +134,7 @@ mod UpgradedAccount {
let tx_info = get_tx_info().unbox();
assert(tx_info.version != 0, 'invalid tx version');

self._execute_calls(calls.span())
self._execute_calls(calls)
}

fn owner(
Expand Down Expand Up @@ -202,20 +202,16 @@ mod UpgradedAccount {
}
}

fn _execute_calls(ref self: ContractState, mut calls: Span<Call>) -> Array<Span<felt252>> {
fn _execute_calls(ref self: ContractState, 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.span()
) {
match call_contract_syscall(call.to, call.selector, call.calldata) {
Result::Ok(mut retdata) => { result.append(retdata); },
Result::Err(revert_reason) => {
panic_with_felt252('multicall_failed');
}
Result::Err(_) => { panic_with_felt252('multicall_failed'); }
}
},
Option::None(_) => { break (); }
Expand Down
2 changes: 1 addition & 1 deletion src/test_helper/erc721_helper.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ mod ERC721 {
self.symbol.write(_symbol);
}

#[external(v0)]
#[abi(embed_v0)]
impl ERC721Impl of super::IERC721<ContractState> {
fn name(self: @ContractState) -> felt252 {
self.name.read()
Expand Down
2 changes: 1 addition & 1 deletion src/test_helper/hello_starknet.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod HelloStarknet {
balance: felt252,
}

#[external(v0)]
#[abi(embed_v0)]
impl HelloStarknetImpl of super::IHelloStarknet<ContractState> {
fn increase_balance(ref self: ContractState, amount: felt252) {
assert(amount != 0, 'Amount cannot be 0');
Expand Down
Loading

0 comments on commit 8ad8068

Please sign in to comment.