Skip to content
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

Add indexed keys to token events #746

Merged
merged 17 commits into from
Sep 29, 2023
Merged
14 changes: 7 additions & 7 deletions src/tests/account/test_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn test_constructor() {
let mut state = STATE();
Account::constructor(ref state, PUBLIC_KEY);

let event = testing::pop_log::<OwnerAdded>(ZERO()).unwrap();
let event = utils::pop_log::<OwnerAdded>(ZERO()).unwrap();
assert(event.new_owner_guid == PUBLIC_KEY, 'Invalid owner key');
utils::assert_no_events_left(ZERO());

Expand Down Expand Up @@ -452,10 +452,10 @@ fn test_public_key_setter_and_getter() {
// Set key
PublicKeyImpl::set_public_key(ref state, NEW_PUBKEY);

let event = testing::pop_log::<OwnerRemoved>(ACCOUNT_ADDRESS()).unwrap();
let event = utils::pop_log::<OwnerRemoved>(ACCOUNT_ADDRESS()).unwrap();
assert(event.removed_owner_guid == 0, 'Invalid old owner key');

let event = testing::pop_log::<OwnerAdded>(ACCOUNT_ADDRESS()).unwrap();
let event = utils::pop_log::<OwnerAdded>(ACCOUNT_ADDRESS()).unwrap();
assert(event.new_owner_guid == NEW_PUBKEY, 'Invalid new owner key');
utils::assert_no_events_left(ACCOUNT_ADDRESS());

Expand Down Expand Up @@ -493,10 +493,10 @@ fn test_public_key_setter_and_getter_camel() {
// Set key
PublicKeyCamelImpl::setPublicKey(ref state, NEW_PUBKEY);

let event = testing::pop_log::<OwnerRemoved>(ACCOUNT_ADDRESS()).unwrap();
let event = utils::pop_log::<OwnerRemoved>(ACCOUNT_ADDRESS()).unwrap();
assert(event.removed_owner_guid == 0, 'Invalid old owner key');

let event = testing::pop_log::<OwnerAdded>(ACCOUNT_ADDRESS()).unwrap();
let event = utils::pop_log::<OwnerAdded>(ACCOUNT_ADDRESS()).unwrap();
assert(event.new_owner_guid == NEW_PUBKEY, 'Invalid new owner key');
utils::assert_no_events_left(ACCOUNT_ADDRESS());

Expand Down Expand Up @@ -526,7 +526,7 @@ fn test_initializer() {
let mut state = STATE();
Account::InternalImpl::initializer(ref state, PUBLIC_KEY);

let event = testing::pop_log::<OwnerAdded>(ZERO()).unwrap();
let event = utils::pop_log::<OwnerAdded>(ZERO()).unwrap();
assert(event.new_owner_guid == PUBLIC_KEY, 'Invalid owner key');
utils::assert_no_events_left(ZERO());

Expand Down Expand Up @@ -582,7 +582,7 @@ fn test__set_public_key() {
let mut state = STATE();
Account::InternalImpl::_set_public_key(ref state, PUBLIC_KEY);

let event = testing::pop_log::<OwnerAdded>(ZERO()).unwrap();
let event = utils::pop_log::<OwnerAdded>(ZERO()).unwrap();
assert(event.new_owner_guid == PUBLIC_KEY, 'Invalid owner key');
utils::assert_no_events_left(ZERO());

Expand Down
3 changes: 1 addition & 2 deletions src/tests/upgrades/test_upgradeable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use starknet::ContractAddress;
use starknet::Felt252TryIntoClassHash;
use starknet::class_hash_const;
use starknet::contract_address_const;
use starknet::testing;
use traits::TryInto;

const VALUE: felt252 = 123;
Expand Down Expand Up @@ -58,7 +57,7 @@ fn test_upgraded_event() {
let v1 = deploy_v1();
v1.upgrade(V2_CLASS_HASH());

let event = testing::pop_log::<Upgraded>(v1.contract_address).unwrap();
let event = utils::pop_log::<Upgraded>(v1.contract_address).unwrap();
assert(event.class_hash == V2_CLASS_HASH(), 'Invalid class hash');
}

Expand Down
7 changes: 6 additions & 1 deletion src/tests/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ fn deploy(contract_class_hash: felt252, calldata: Array<felt252>) -> ContractAdd

/// Pop the earliest unpopped logged event for the contract as the requested type
/// and checks there's no more data left on the event, preventing unaccounted params.
/// Indexed event members are currently not supported, so they are ignored.
/// This function also removes the hashed event-name key to support indexed event
/// members.
martriay marked this conversation as resolved.
Show resolved Hide resolved
fn pop_log<T, impl TDrop: Drop<T>, impl TEvent: starknet::Event<T>>(
address: ContractAddress
) -> Option<T> {
let (mut keys, mut data) = testing::pop_log_raw(address)?;

// Remove the event ID from the keys.
keys.pop_front();

let ret = starknet::Event::deserialize(ref keys, ref data);
assert(data.is_empty(), 'Event has extra data');
ret
Expand Down
4 changes: 4 additions & 0 deletions src/token/erc20/erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ mod ERC20 {

#[derive(Drop, starknet::Event)]
struct Transfer {
#[key]
from: ContractAddress,
#[key]
to: ContractAddress,
value: u256
}

#[derive(Drop, starknet::Event)]
struct Approval {
#[key]
owner: ContractAddress,
#[key]
spender: ContractAddress,
value: u256
}
Expand Down
8 changes: 8 additions & 0 deletions src/token/erc721/erc721.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,29 @@ mod ERC721 {

#[derive(Drop, starknet::Event)]
struct Transfer {
#[key]
from: ContractAddress,
#[key]
to: ContractAddress,
#[key]
token_id: u256
}

#[derive(Drop, starknet::Event)]
struct Approval {
#[key]
owner: ContractAddress,
#[key]
approved: ContractAddress,
#[key]
token_id: u256
}

#[derive(Drop, starknet::Event)]
struct ApprovalForAll {
#[key]
owner: ContractAddress,
#[key]
operator: ContractAddress,
approved: bool
}
Expand Down