Skip to content

Commit

Permalink
Merge branch 'feat/grand-rename' into improve-balance_of-syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
sgc-code authored Jun 26, 2024
2 parents 5826460 + 29fc3a4 commit 75ceaca
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface AccountConstructorArguments {

export interface Gift extends AccountConstructorArguments {
factory: string;
class_hash: string;
escrow_class_hash: string;
}

export function buildGiftCallData(gift: Gift) {
Expand Down
4 changes: 2 additions & 2 deletions lib/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function deposit(depositParams: {
depositParams.overrides?.escrowAccountClassHash || (await factory.get_latest_escrow_class_hash());
const gift: Gift = {
factory: factoryAddress,
class_hash: escrowAccountClassHash,
escrow_class_hash: escrowAccountClassHash,
sender: deployer.address,
gift_token: giftTokenAddress,
gift_amount: giftAmount,
Expand Down Expand Up @@ -123,7 +123,7 @@ export function calculateEscrowAddress(gift: Gift): string {

const escrowAddress = hash.calculateContractAddressFromHash(
0,
gift.class_hash,
gift.escrow_class_hash,
CallData.compile({
...constructorArgs,
gift_amount: uint256.bnToUint256(gift.gift_amount),
Expand Down
11 changes: 10 additions & 1 deletion scripts/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ for (const { giftTokenContract, unit } of tokens) {
},
});

const { gift: claimExternalGift, giftPrivateKey: giftPrivateKeyExternal } = await defaultDepositTestSetup({
factory,
useTxV3,
overrides: {
giftPrivateKey: 43n,
giftTokenAddress: giftTokenContract.address,
},
});

await profiler.profile(`Gifting ${unit} (FeeToken: ${manager.tokens.unitTokenContract(useTxV3)})`, txReceipt);

await profiler.profile(
Expand All @@ -60,7 +69,7 @@ for (const { giftTokenContract, unit } of tokens) {

await profiler.profile(
`Claiming external ${unit} (FeeToken: ${manager.tokens.unitTokenContract(useTxV3)})`,
await claimExternal({ gift, receiver, giftPrivateKey }),
await claimExternal({ gift: claimExternalGift, receiver, giftPrivateKey: giftPrivateKeyExternal }),
);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/contracts/escrow_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct AccountConstructorArguments {
#[starknet::contract(account)]
mod EscrowAccount {
use argent_gifting::contracts::escrow_library::{
IEscrowLibraryLibraryDispatcher as IEscrowLibraryDelegateDispatcher, IEscrowLibraryDispatcherTrait
IEscrowLibraryLibraryDispatcher, IEscrowLibraryDispatcherTrait
};
use argent_gifting::contracts::gift_data::GiftData;
use argent_gifting::contracts::gift_factory::{IGiftFactory, IGiftFactoryDispatcher, IGiftFactoryDispatcherTrait};
Expand Down Expand Up @@ -130,14 +130,14 @@ mod EscrowAccount {
let (gift, receiver): (GiftData, ContractAddress) = full_deserialize(*calldata)
.expect('gift-acc/invalid-calldata');
let library_class_hash: ClassHash = IGiftFactoryDispatcher { contract_address: gift.factory }
.get_escrow_lib_class_hash(gift.class_hash);
IEscrowLibraryDelegateDispatcher { class_hash: library_class_hash }.claim_internal(gift, receiver)
.get_escrow_lib_class_hash(gift.escrow_class_hash);
IEscrowLibraryLibraryDispatcher { class_hash: library_class_hash }.claim_internal(gift, receiver)
}

fn is_valid_signature(self: @ContractState, hash: felt252, signature: Array<felt252>) -> felt252 {
let mut signature_span = signature.span();
let gift: GiftData = Serde::deserialize(ref signature_span).expect('gift-acc/invalid-gift');
IEscrowLibraryDelegateDispatcher { class_hash: get_validated_lib(gift) }
IEscrowLibraryLibraryDispatcher { class_hash: get_validated_lib(gift) }
.is_valid_account_signature(gift, hash, signature_span)
}

Expand All @@ -154,7 +154,7 @@ mod EscrowAccount {
let mut calldata_span = calldata.span();
let gift: GiftData = Serde::deserialize(ref calldata_span).expect('gift-acc/invalid-gift');
let library_class_hash = get_validated_lib(gift);
IEscrowLibraryDelegateDispatcher { class_hash: library_class_hash }
IEscrowLibraryLibraryDispatcher { class_hash: library_class_hash }
.execute_action(library_class_hash, selector, calldata.span())
}
}
Expand All @@ -166,7 +166,7 @@ mod EscrowAccount {
) -> Array<Span<felt252>> {
let gift: GiftData = Serde::deserialize(ref signature).expect('gift-acc/invalid-gift');
let library_class_hash = get_validated_lib(gift);
IEscrowLibraryDelegateDispatcher { class_hash: library_class_hash }
IEscrowLibraryLibraryDispatcher { class_hash: library_class_hash }
.execute_from_outside_v2(gift, outside_execution, signature)
}

Expand All @@ -177,7 +177,7 @@ mod EscrowAccount {

fn get_validated_lib(gift: GiftData) -> ClassHash {
assert_valid_claim(gift);
IGiftFactoryDispatcher { contract_address: gift.factory }.get_escrow_lib_class_hash(gift.class_hash)
IGiftFactoryDispatcher { contract_address: gift.factory }.get_escrow_lib_class_hash(gift.escrow_class_hash)
}

fn assert_valid_claim(gift: GiftData) {
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/gift_data.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use starknet::{ContractAddress, ClassHash};

/// @notice Struct representing the data required for a claiming a gift
/// @param factory The address of the factory
/// @param class_hash The class hash of the escrow account
/// @param escrow_class_hash The class hash of the escrow account
/// @param sender The address of the sender
/// @param gift_token The ERC-20 token address of the gift
/// @param gift_amount The amount of the gift
Expand All @@ -13,7 +13,7 @@ use starknet::{ContractAddress, ClassHash};
#[derive(Serde, Drop, Copy)]
pub struct GiftData {
pub factory: ContractAddress,
pub class_hash: ClassHash,
pub escrow_class_hash: ClassHash,
pub sender: ContractAddress,
pub gift_token: ContractAddress,
pub gift_amount: u256,
Expand Down
22 changes: 11 additions & 11 deletions src/contracts/gift_factory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ pub trait IGiftFactory<TContractState> {
gift_pubkey: felt252
);

/// @notice Retrieves the current class_hash used for creating an escrow account
/// @notice Retrieves the current escrow_class_hash used for creating an escrow account
fn get_latest_escrow_class_hash(self: @TContractState) -> ClassHash;

/// @notice Retrieves the current class_hash of the escrow account's library
/// @notice Retrieves the current escrow_class_hash of the escrow account's library
fn get_escrow_lib_class_hash(self: @TContractState, escrow_class_hash: ClassHash) -> ClassHash;

/// @notice Get the address of the escrow account contract given all parameters
/// @param class_hash The class hash
/// @param escrow_class_hash The class hash
/// @param sender The address of the sender
/// @param gift_token The ERC-20 token address of the gift
/// @param gift_amount The amount of the gift
Expand All @@ -36,7 +36,7 @@ pub trait IGiftFactory<TContractState> {
/// @param gift_pubkey The public key associated with the gift
fn get_escrow_address(
self: @TContractState,
class_hash: ClassHash,
escrow_class_hash: ClassHash,
sender: ContractAddress,
gift_token: ContractAddress,
gift_amount: u256,
Expand Down Expand Up @@ -118,7 +118,7 @@ mod GiftFactory {
escrow_address: ContractAddress,
#[key] // Find all gifts from a specific sender
sender: ContractAddress,
class_hash: ClassHash,
escrow_class_hash: ClassHash,
gift_token: ContractAddress,
gift_amount: u256,
fee_token: ContractAddress,
Expand Down Expand Up @@ -155,13 +155,13 @@ mod GiftFactory {

let sender = get_caller_address();
// TODO We could manually serialize for better performance but then we loose the type safety
let class_hash = self.escrow_class_hash.read();
assert(class_hash == escrow_class_hash, 'gift-fac/invalid-class-hash');
let escrow_class_hash_storage = self.escrow_class_hash.read();
assert(escrow_class_hash_storage == escrow_class_hash, 'gift-fac/invalid-class-hash');
let constructor_arguments = AccountConstructorArguments {
sender, gift_token, gift_amount, fee_token, fee_amount, gift_pubkey
};
let (escrow_contract, _) = deploy_syscall(
class_hash, // class_hash
escrow_class_hash, // escrow_class_hash
0, // salt
serialize(@constructor_arguments).span(), // constructor data
false // deploy_from_zero
Expand All @@ -172,7 +172,7 @@ mod GiftFactory {
GiftCreated {
escrow_address: escrow_contract,
sender,
class_hash,
escrow_class_hash,
gift_token,
gift_amount,
fee_token,
Expand Down Expand Up @@ -205,7 +205,7 @@ mod GiftFactory {

fn get_escrow_address(
self: @ContractState,
class_hash: ClassHash,
escrow_class_hash: ClassHash,
sender: ContractAddress,
gift_token: ContractAddress,
gift_amount: u256,
Expand All @@ -216,7 +216,7 @@ mod GiftFactory {
calculate_escrow_account_address(
GiftData {
factory: get_contract_address(),
class_hash,
escrow_class_hash,
sender,
gift_amount,
gift_token,
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn calculate_escrow_account_address(gift: GiftData) -> ContractAddress {
};
calculate_contract_address_from_deploy_syscall(
0, // salt
gift.class_hash, // class_hash
gift.escrow_class_hash, // escrow_class_hash
serialize(@constructor_arguments).span(), // constructor_data
gift.factory
)
Expand Down
7 changes: 3 additions & 4 deletions src/mocks/reentrant_erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use starknet::{ClassHash, ContractAddress};
#[derive(Serde, Drop, Copy, starknet::Store, Debug)]
struct TestGiftData {
factory: ContractAddress,
class_hash: ClassHash,
escrow_class_hash: ClassHash,
sender: ContractAddress,
gift_token: ContractAddress,
gift_amount: u256,
Expand Down Expand Up @@ -41,8 +41,7 @@ mod ReentrantERC20 {
get_caller_address, ContractAddress, get_contract_address, contract_address_const,
syscalls::call_contract_syscall
};
use super::IMalicious;
use super::TestGiftData;
use super::{IMalicious, TestGiftData};


component!(path: ERC20Component, storage: erc20, event: ERC20Event);
Expand Down Expand Up @@ -113,7 +112,7 @@ mod ReentrantERC20 {
// let test_gift: TestGiftData = self.gift.read();
// let gift = GiftData {
// factory: test_gift.factory,
// class_hash: test_gift.class_hash,
// escrow_class_hash: test_gift.escrow_class_hash,
// sender: test_gift.sender,
// gift_token: test_gift.gift_token,
// gift_amount: test_gift.gift_amount,
Expand Down
2 changes: 1 addition & 1 deletion tests-integration/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("Test Core Factory Functions", function () {
const { gift } = await defaultDepositTestSetup({ factory });

const escrowAddress = await factory.get_escrow_address(
gift.class_hash,
gift.escrow_class_hash,
deployer.address,
gift.gift_token,
gift.gift_amount,
Expand Down

0 comments on commit 75ceaca

Please sign in to comment.