Skip to content

Commit

Permalink
Merge pull request #58 from aptos-labs/j/update
Browse files Browse the repository at this point in the history
use module event and rename
  • Loading branch information
0xaptosj authored Mar 14, 2024
2 parents 0dda067 + 5f45e9e commit 9519210
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
2 changes: 1 addition & 1 deletion frontend/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NEXT_PUBLIC_CONTRACT_ADDRESS="0xcab918f5f28bab478e237cd15c3750b3fa3f95ec0505510a24aa663efb348dd3"
NEXT_PUBLIC_CONTRACT_ADDRESS="0xa3527451930153f0da31da246da3837c5c6ee3156389a075af4bf6465c91aef1"
NEXT_PUBLIC_BODY_OPTIONS=5
NEXT_PUBLIC_EAR_OPTIONS=6
NEXT_PUBLIC_FACE_OPTIONS=4
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/hooks/useGetAptogotchiCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ export function useGetAptogotchiCollection() {
try {
setLoading(true);

const aptogotchiCollectionIDResponse = (await aptosClient.view({
const aptogotchiCollectionAddressResponse = (await aptosClient.view({
payload: {
function: `${NEXT_PUBLIC_CONTRACT_ADDRESS}::main::get_aptogotchi_collection_id`,
function: `${NEXT_PUBLIC_CONTRACT_ADDRESS}::main::get_aptogotchi_collection_address`,
},
})) as [`0x${string}`];

const collectionIDAddr = padAddressIfNeeded(
aptogotchiCollectionIDResponse[0]
const collectionAddress = padAddressIfNeeded(
aptogotchiCollectionAddressResponse[0]
);

const collectionResponse: CollectionResponse =
await aptosClient.queryIndexer({
query: {
query: queryAptogotchiCollection,
variables: {
collection_id: collectionIDAddr,
collection_id: collectionAddress,
},
},
});
Expand Down
48 changes: 22 additions & 26 deletions move/sources/aptogotchi.move
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module aptogotchi::main {
use aptos_framework::account;
use aptos_framework::event;
use aptos_framework::object;
use aptos_framework::timestamp;
Expand Down Expand Up @@ -46,19 +45,21 @@ module aptogotchi::main {
burn_ref: token::BurnRef,
}

struct MintAptogotchiEvents has key {
mint_aptogotchi_events: event::EventHandle<MintAptogotchiEvent>,
}

#[event]
struct MintAptogotchiEvent has drop, store {
token_name: String,
aptogotchi_name: String,
parts: AptogotchiParts,
}

// Tokens require a signer to create, so this is the signer for the collection
struct CollectionCapability has key {
extend_ref: ExtendRef,
// We need a contract signer as the creator of the aptogotchi collection and aptogotchi token
// Otherwise we need admin to sign whenever a new aptogotchi token is minted which is inconvenient
struct ObjectController has key {
// This is the extend_ref of the app object, not the extend_ref of collection object or token object
// app object is the creator and owner of aptogotchi collection object
// app object is also the creator of all aptogotchi token (NFT) objects
// but owner of each token object is aptogotchi owner (i.e. user who mints aptogotchi)
app_extend_ref: ExtendRef,
}

const APP_OBJECT_SEED: vector<u8> = b"APTOGOTCHI";
Expand All @@ -82,12 +83,8 @@ module aptogotchi::main {
let extend_ref = object::generate_extend_ref(&constructor_ref);
let app_signer = &object::generate_signer(&constructor_ref);

move_to(account, MintAptogotchiEvents {
mint_aptogotchi_events: account::new_event_handle<MintAptogotchiEvent>(account),
});

move_to(app_signer, CollectionCapability {
extend_ref,
move_to(app_signer, ObjectController {
app_extend_ref: extend_ref,
});

create_aptogotchi_collection(app_signer);
Expand All @@ -97,8 +94,8 @@ module aptogotchi::main {
object::create_object_address(&@aptogotchi, APP_OBJECT_SEED)
}

fun get_app_signer(): signer acquires CollectionCapability {
object::generate_signer_for_extending(&borrow_global<CollectionCapability>(get_app_signer_addr()).extend_ref)
fun get_app_signer(): signer acquires ObjectController {
object::generate_signer_for_extending(&borrow_global<ObjectController>(get_app_signer_addr()).app_extend_ref)
}

// Create the collection that will hold all the Aptogotchis
Expand All @@ -123,7 +120,7 @@ module aptogotchi::main {
body: u8,
ear: u8,
face: u8,
) acquires CollectionCapability, MintAptogotchiEvents {
) acquires ObjectController {
assert!(string::length(&name) <= NAME_UPPER_BOUND, error::invalid_argument(ENAME_LIMIT));
assert!(
body >= 0 && body <= BODY_MAX_VALUE,
Expand Down Expand Up @@ -173,8 +170,7 @@ module aptogotchi::main {
move_to(&token_signer, gotchi);

// Emit event for minting Aptogotchi token
event::emit_event<MintAptogotchiEvent>(
&mut borrow_global_mut<MintAptogotchiEvents>(@aptogotchi).mint_aptogotchi_events,
event::emit<MintAptogotchiEvent>(
MintAptogotchiEvent {
token_name,
aptogotchi_name: name,
Expand Down Expand Up @@ -249,9 +245,10 @@ module aptogotchi::main {
token_address
}

// Get collection ID of aptogotchi collection
// Get collection address (also known as collection ID) of aptogotchi collection
// Collection itself is an object, that's why it has an address
#[view]
public fun get_aptogotchi_collection_id(): (address) {
public fun get_aptogotchi_collection_address(): (address) {
let collection_name = string::utf8(APTOGOTCHI_COLLECTION_NAME);
let creator_addr = get_app_signer_addr();
collection::create_collection_address(&creator_addr, &collection_name)
Expand All @@ -261,9 +258,8 @@ module aptogotchi::main {
#[view]
public fun has_aptogotchi(owner_addr: address): (bool) {
let token_address = get_aptogotchi_address(owner_addr);
let has_gotchi = exists<Aptogotchi>(token_address);

has_gotchi
exists<Aptogotchi>(token_address)
}

// Returns all fields for this Aptogotchi (if found)
Expand Down Expand Up @@ -304,7 +300,7 @@ module aptogotchi::main {
aptos: &signer,
account: &signer,
creator: &signer
) acquires CollectionCapability, MintAptogotchiEvents {
) acquires ObjectController {
setup_test(aptos, account, creator);

create_aptogotchi(creator, utf8(b"test"), 1, 1, 1);
Expand Down Expand Up @@ -333,7 +329,7 @@ module aptogotchi::main {
aptos: &signer,
account: &signer,
creator: &signer
) acquires CollectionCapability, MintAptogotchiEvents, Aptogotchi {
) acquires ObjectController, Aptogotchi {
setup_test(aptos, account, creator);
let creator_address = signer::address_of(creator);
create_aptogotchi(creator, utf8(b"test"), 1, 1, 1);
Expand All @@ -357,7 +353,7 @@ module aptogotchi::main {
aptos: &signer,
account: &signer,
creator: &signer
) acquires CollectionCapability, MintAptogotchiEvents {
) acquires ObjectController {
setup_test(aptos, account, creator);

create_aptogotchi(creator, utf8(b"test"), 1, 1, 1);
Expand Down

0 comments on commit 9519210

Please sign in to comment.