Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions zksend/zk_bag/sources/zk_bag.move
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,6 +35,35 @@ const EClaimAddressNotExists: u64 = 6;
/// Claims an item that does not exist.
const EItemNotExists: u64 = 7;


/// Event emitted when a new ZkBag is created
public struct BagCreatedEvent has copy, drop {
bag_id: ID,
creator: address,
}

/// Event emitted when an item of type T is added to a ZkBag
public struct BagItemAddedEvent<phantom T> has copy, drop {
bag_id: ID,
item_id: ID,
creator: address,
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this event is useful without the id of the added item. In practice items won't be added to links outside the creating transaction, so this is effectively just a duplicate of the create event that let's you count the number of items in the link


/// 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,
}


/// A store that holds all the bags to prevent needing
/// the objectId in the URL of requests.
///
Expand Down Expand Up @@ -69,15 +99,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,
);
}

Expand All @@ -102,6 +139,12 @@ public fun add<T: key + store>(
// all items in a single-go.
bag.item_ids.insert(object::id_address(&item));

event::emit(BagItemAddedEvent<T> {
bag_id: object::id(bag),
item_id: object::id(&item),
creator: ctx.sender(),
});

// TTO (Transfer to Object) the item to the bag.
transfer::public_transfer(item, object::id_address(bag));
}
Expand All @@ -120,6 +163,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)
}

Expand All @@ -138,6 +187,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)
}

Expand All @@ -154,6 +209,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);
}
Expand All @@ -165,6 +226,7 @@ public fun claim<T: key + store>(
bag: &mut ZkBag,
claim: &BagClaim,
receiving: Receiving<T>,
ctx: &mut TxContext,
): T {
assert!(bag.is_valid_claim_object(claim), EUnauthorizedProof);

Expand Down
3 changes: 3 additions & 0 deletions zksend/zk_bag/tests/zk_bag_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down