From d3945917a2957767e268ef49b773371862334f23 Mon Sep 17 00:00:00 2001 From: EasonC13 Date: Thu, 28 Nov 2024 09:36:05 -0500 Subject: [PATCH 1/3] add event (emit not implemented yet) --- zksend/zk_bag/sources/zk_bag.move | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/zksend/zk_bag/sources/zk_bag.move b/zksend/zk_bag/sources/zk_bag.move index 652b593..ef5aa26 100644 --- a/zksend/zk_bag/sources/zk_bag.move +++ b/zksend/zk_bag/sources/zk_bag.move @@ -34,6 +34,29 @@ const EClaimAddressNotExists: u64 = 6; /// Claims an item that does not exist. const EItemNotExists: u64 = 7; + +public struct BagCreatedEvent { + bag_id: UID, + creator: address, +} + +public struct BagItemAddedEvent { + bag_id: UID, + creator: address, +} + +public struct BagItemClaimedEvent { + bag_id: UID, + creator: address, + receiver: address, +} + +public struct BagClaimedEvent { + bag_id: ID, + creator: address, + receiver: address, +} + /// A store that holds all the bags to prevent needing /// the objectId in the URL of requests. /// From 0cc1c8b37e5079a53234a55b92cdbb0bcc7d032f Mon Sep 17 00:00:00 2001 From: EasonC13 Date: Thu, 28 Nov 2024 09:46:38 -0500 Subject: [PATCH 2/3] add: emit events and pass all tests --- zksend/zk_bag/sources/zk_bag.move | 82 +++++++++++++++++++++++---- zksend/zk_bag/tests/zk_bag_tests.move | 3 + 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/zksend/zk_bag/sources/zk_bag.move b/zksend/zk_bag/sources/zk_bag.move index ef5aa26..b873c98 100644 --- a/zksend/zk_bag/sources/zk_bag.move +++ b/zksend/zk_bag/sources/zk_bag.move @@ -13,6 +13,7 @@ module zk_bag::zk_bag; use sui::table::{Self, Table}; use sui::transfer::Receiving; use sui::vec_set::{Self, VecSet}; +use sui::event::{Self}; /// Capping this at 50 items as a limit based on business requirements. /// WARNING: DO NOT EXCEED THE MAXIMUM INPUTs IN A PTB LIMIT, @@ -35,28 +36,44 @@ const EClaimAddressNotExists: u64 = 6; const EItemNotExists: u64 = 7; -public struct BagCreatedEvent { - bag_id: UID, +/// Event emitted when a new ZkBag is created +public struct BagCreatedEvent has copy, drop { + bag_id: ID, creator: address, } -public struct BagItemAddedEvent { - bag_id: UID, +/// Event emitted when an item of type T is added to a ZkBag +public struct BagItemAddedEvent has copy, drop { + bag_id: ID, creator: address, } -public struct BagItemClaimedEvent { - bag_id: UID, +/// Event emitted when an item of type T is claimed from a ZkBag +public struct BagItemClaimedEvent has copy, drop { + bag_id: ID, creator: address, receiver: address, } -public struct BagClaimedEvent { +/// Event emitted when all items in a ZkBag are claimed +public struct BagClaimedEvent has copy, drop { bag_id: ID, creator: address, receiver: address, } +/// Event emitted when ownership of a ZkBag is transferred to a new address +public struct BagOwnerUpdatedEvent has copy, drop { + bag_id: ID, + old_owner: address, + new_owner: address, +} + +/// Event emitted when a ZkBag is destroyed after all items are claimed +public struct BagDestroyedEvent has copy, drop { + bag_id: ID, +} + /// A store that holds all the bags to prevent needing /// the objectId in the URL of requests. /// @@ -92,15 +109,22 @@ fun init(ctx: &mut TxContext) { public fun new(store: &mut BagStore, receiver: address, ctx: &mut TxContext) { assert!(!store.items.contains(receiver), EClaimAddressAlreadyExists); + let zk_bag = ZkBag { + id: object::new(ctx), + owner: ctx.sender(), + item_ids: vec_set::empty(), + }; + + event::emit(BagCreatedEvent { + bag_id: object::id(&zk_bag), + creator: ctx.sender(), + }); + store .items .add( receiver, - ZkBag { - id: object::new(ctx), - owner: ctx.sender(), - item_ids: vec_set::empty(), - }, + zk_bag, ); } @@ -125,6 +149,11 @@ public fun add( // all items in a single-go. bag.item_ids.insert(object::id_address(&item)); + event::emit(BagItemAddedEvent { + bag_id: object::id(bag), + creator: ctx.sender(), + }); + // TTO (Transfer to Object) the item to the bag. transfer::public_transfer(item, object::id_address(bag)); } @@ -143,6 +172,12 @@ public fun init_claim( bag_id: object::id(&bag), }; + event::emit(BagClaimedEvent { + bag_id: object::id(&bag), + creator: bag.owner, + receiver: receiver, + }); + (bag, claim_proof) } @@ -161,6 +196,12 @@ public fun reclaim( bag_id: bag.id.to_inner(), }; + event::emit(BagClaimedEvent { + bag_id: bag.id.to_inner(), + creator: bag.owner, + receiver: receiver, + }); + (bag, claim_proof) } @@ -177,6 +218,12 @@ public fun update_receiver( let bag = store.items.remove(from); // validate that the sender is the owner of the bag. assert!(bag.owner == ctx.sender(), EUnauthorized); + + event::emit(BagOwnerUpdatedEvent { + bag_id: object::id(&bag), + old_owner: from, + new_owner: to, + }); store.items.add(to, bag); } @@ -188,6 +235,7 @@ public fun claim( bag: &mut ZkBag, claim: &BagClaim, receiving: Receiving, + ctx: &mut TxContext, ): T { assert!(bag.is_valid_claim_object(claim), EUnauthorizedProof); @@ -202,6 +250,12 @@ public fun claim( bag.item_ids.remove(&object::id_address(&item)); + event::emit(BagItemClaimedEvent { + bag_id: object::id(bag), + creator: bag.owner, + receiver: ctx.sender(), + }); + item } @@ -212,6 +266,10 @@ public fun finalize(bag: ZkBag, claim: BagClaim) { let BagClaim { bag_id: _ } = claim; + event::emit(BagDestroyedEvent { + bag_id: object::id(&bag), + }); + let ZkBag { id, owner: _, diff --git a/zksend/zk_bag/tests/zk_bag_tests.move b/zksend/zk_bag/tests/zk_bag_tests.move index 88dcab2..c027b22 100644 --- a/zksend/zk_bag/tests/zk_bag_tests.move +++ b/zksend/zk_bag/tests/zk_bag_tests.move @@ -33,6 +33,7 @@ fun flow() { let item: TestItem = bag.claim( &claim_proof, ts::receiving_ticket_by_id(item_ids.pop_back()), + scenario.ctx(), ); sui::transfer::transfer(item, USER_TWO); }; @@ -142,6 +143,7 @@ fun tries_to_claim_non_existing_id() { let _item: TestItem = bag.claim( &claim_proof, ts::receiving_ticket_by_id(random_item_id), + scenario.ctx(), ); abort 1337 @@ -164,6 +166,7 @@ fun try_to_claim_with_invalid_proof() { let _item: TestItem = bag.claim( &claim_proof_two, ts::receiving_ticket_by_id(vector::pop_back(&mut item_ids)), + scenario.ctx(), ); abort 1337 From 130373e4b2c82234bef5509d3d074f379fb43bae Mon Sep 17 00:00:00 2001 From: EasonC13 Date: Thu, 28 Nov 2024 14:20:14 -0500 Subject: [PATCH 3/3] update: event fields and remove destroy event --- zksend/zk_bag/sources/zk_bag.move | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/zksend/zk_bag/sources/zk_bag.move b/zksend/zk_bag/sources/zk_bag.move index b873c98..ed80799 100644 --- a/zksend/zk_bag/sources/zk_bag.move +++ b/zksend/zk_bag/sources/zk_bag.move @@ -45,16 +45,10 @@ public struct BagCreatedEvent has copy, drop { /// Event emitted when an item of type T is added to a ZkBag public struct BagItemAddedEvent has copy, drop { bag_id: ID, + item_id: ID, creator: address, } -/// Event emitted when an item of type T is claimed from a ZkBag -public struct BagItemClaimedEvent has copy, drop { - bag_id: ID, - creator: address, - receiver: address, -} - /// Event emitted when all items in a ZkBag are claimed public struct BagClaimedEvent has copy, drop { bag_id: ID, @@ -69,10 +63,6 @@ public struct BagOwnerUpdatedEvent has copy, drop { new_owner: address, } -/// Event emitted when a ZkBag is destroyed after all items are claimed -public struct BagDestroyedEvent has copy, drop { - bag_id: ID, -} /// A store that holds all the bags to prevent needing /// the objectId in the URL of requests. @@ -151,6 +141,7 @@ public fun add( event::emit(BagItemAddedEvent { bag_id: object::id(bag), + item_id: object::id(&item), creator: ctx.sender(), }); @@ -250,12 +241,6 @@ public fun claim( bag.item_ids.remove(&object::id_address(&item)); - event::emit(BagItemClaimedEvent { - bag_id: object::id(bag), - creator: bag.owner, - receiver: ctx.sender(), - }); - item } @@ -266,10 +251,6 @@ public fun finalize(bag: ZkBag, claim: BagClaim) { let BagClaim { bag_id: _ } = claim; - event::emit(BagDestroyedEvent { - bag_id: object::id(&bag), - }); - let ZkBag { id, owner: _,