Move语言社区

【Move进阶】NFT协议V4

address 0x2 {
module NFTExample4 {
    use 0x1::Vector;
    use 0x1::Event;

    struct NFT<T: store> has key, store { name: T }

    struct UniqIdList<T: store + drop> has key, store {
        data: vector<T>,
	nft_events: Event::EventHandle<NFTEvent<T>>,
    }

    struct NFTEvent<T: store + drop> has drop, store {
        name: T,
    }

    public fun initialize<T: store + drop>(account: &signer) {
        move_to(account, UniqIdList {data: Vector::empty<T>(), nft_events: Event::new_event_handle<NFTEvent<T>>(account)});
    }

    public fun new<T: store + copy + drop>(_account: &signer, account_address:address, name: T): NFT<T> acquires UniqIdList {
        let exist = Vector::contains<T>(&borrow_global<UniqIdList<T>>(account_address).data, &name);
        assert(!exist, 1);
        let id_list = borrow_global_mut<UniqIdList<T>>(account_address);
        Vector::push_back<T>(&mut id_list.data, copy name);
	let new_name = copy name;
        Event::emit_event(&mut id_list.nft_events, NFTEvent { name:new_name });
        NFT { name }
    }
}
}