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
22 changes: 12 additions & 10 deletions src/tests/token/test_erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use openzeppelin::tests::utils::constants::{
ZERO, OWNER, SPENDER, RECIPIENT, NAME, SYMBOL, DECIMALS, SUPPLY, VALUE
};
use openzeppelin::tests::utils;
use openzeppelin::token::erc20::ERC20::Approval;
use openzeppelin::token::erc20::ERC20::ERC20CamelOnlyImpl;
use openzeppelin::token::erc20::ERC20::ERC20Impl;
use openzeppelin::token::erc20::ERC20::InternalImpl;
use openzeppelin::token::erc20::ERC20::Transfer;
use openzeppelin::token::erc20::ERC20;
use option::OptionTrait;
use starknet::ContractAddress;
Expand Down Expand Up @@ -598,10 +596,13 @@ fn test__burn_from_zero() {
//

fn assert_event_approval(owner: ContractAddress, spender: ContractAddress, value: u256) {
let event = utils::pop_log::<Approval>(ZERO()).unwrap();
assert(event.owner == owner, 'Invalid `owner`');
assert(event.spender == spender, 'Invalid `spender`');
assert(event.value == value, 'Invalid `value`');
let event = utils::pop_log::<ERC20::Event>(ZERO()).unwrap();
assert(
event == ERC20::Event::Approval(
ERC20::Approval { owner: owner, spender: spender, value: value }
),
'Invalid `Approval` event'
);
}

fn assert_only_event_approval(owner: ContractAddress, spender: ContractAddress, value: u256) {
Expand All @@ -610,10 +611,11 @@ fn assert_only_event_approval(owner: ContractAddress, spender: ContractAddress,
}

fn assert_event_transfer(from: ContractAddress, to: ContractAddress, value: u256) {
let event = utils::pop_log::<Transfer>(ZERO()).unwrap();
assert(event.from == from, 'Invalid `from`');
assert(event.to == to, 'Invalid `to`');
assert(event.value == value, 'Invalid `value`');
let event = utils::pop_log::<ERC20::Event>(ZERO()).unwrap();
assert(
event == ERC20::Event::Transfer(ERC20::Transfer { from: from, to: to, value: value }),
'Invalid `Transfer` event'
);
}

fn assert_only_event_transfer(from: ContractAddress, to: ContractAddress, value: u256) {
Expand Down
39 changes: 23 additions & 16 deletions src/tests/token/test_erc721.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use openzeppelin::tests::utils::constants::{
};
use openzeppelin::tests::utils;
use openzeppelin::token::erc721::ERC721::{
Approval, ApprovalForAll, ERC721CamelOnlyImpl, ERC721Impl, ERC721MetadataCamelOnlyImpl,
ERC721MetadataImpl, InternalImpl, SRC5CamelImpl, SRC5Impl, Transfer,
ERC721CamelOnlyImpl, ERC721Impl, ERC721MetadataCamelOnlyImpl, ERC721MetadataImpl, InternalImpl,
SRC5CamelImpl, SRC5Impl,
};
use openzeppelin::token::erc721::ERC721;
use openzeppelin::token::erc721;
Expand Down Expand Up @@ -1400,25 +1400,32 @@ fn assert_state_after_mint(recipient: ContractAddress, token_id: u256) {
fn assert_event_approval_for_all(
owner: ContractAddress, operator: ContractAddress, approved: bool
) {
let event = utils::pop_log::<ApprovalForAll>(ZERO()).unwrap();
assert(event.owner == owner, 'Invalid `owner`');
assert(event.operator == operator, 'Invalid `operator`');
assert(event.approved == approved, 'Invalid `approved`');
let event = utils::pop_log::<ERC721::Event>(ZERO()).unwrap();
assert(
event == ERC721::Event::ApprovalForAll(
ERC721::ApprovalForAll { owner: owner, operator: operator, approved: approved }
),
'Invalid `ApprovalForAll` event'
);
utils::assert_no_events_left(ZERO());
}

fn assert_event_approval(owner: ContractAddress, approved: ContractAddress, token_id: u256) {
let event = utils::pop_log::<Approval>(ZERO()).unwrap();
assert(event.owner == owner, 'Invalid `owner`');
assert(event.approved == approved, 'Invalid `approved`');
assert(event.token_id == token_id, 'Invalid `token_id`');
utils::assert_no_events_left(ZERO());
let event = utils::pop_log::<ERC721::Event>(ZERO()).unwrap();
assert(
event == ERC721::Event::Approval(
ERC721::Approval { owner: owner, approved: approved, token_id: token_id }
),
'Invalid `Approval` event'
);
}

fn assert_event_transfer(from: ContractAddress, to: ContractAddress, token_id: u256) {
let event = utils::pop_log::<Transfer>(ZERO()).unwrap();
assert(event.from == from, 'Invalid `from`');
assert(event.to == to, 'Invalid `to`');
assert(event.token_id == token_id, 'Invalid `token_id`');
utils::assert_no_events_left(ZERO());
let event = utils::pop_log::<ERC721::Event>(ZERO()).unwrap();
assert(
event == ERC721::Event::Transfer(
ERC721::Transfer { from: from, to: to, token_id: token_id }
),
'Invalid `Transfer` event'
);
}
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
10 changes: 7 additions & 3 deletions src/token/erc20/erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@ mod ERC20 {
}

#[event]
#[derive(Drop, starknet::Event)]
#[derive(Drop, starknet::Event, PartialEq)]
enum Event {
Transfer: Transfer,
Approval: Approval,
}

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

#[derive(Drop, starknet::Event)]
#[derive(Drop, starknet::Event, PartialEq)]
struct Approval {
#[key]
owner: ContractAddress,
#[key]
spender: ContractAddress,
value: u256
}
Expand Down
16 changes: 12 additions & 4 deletions src/token/erc721/erc721.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,38 @@ mod ERC721 {
}

#[event]
#[derive(Drop, starknet::Event)]
#[derive(Drop, starknet::Event, PartialEq)]
enum Event {
Transfer: Transfer,
Approval: Approval,
ApprovalForAll: ApprovalForAll
}

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

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

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