Move语言社区

【Move进阶】NFT协议V2

address 0x2 {
module NFTExample2 {
    use 0x1::Signer;
    use 0x1::Vector;

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

    struct UniqIdList<T: store> has key, store {
        data: vector<T>
    }

    public fun initialize(account: &signer) {
        move_to(account, UniqIdList {data: Vector::empty<vector<u8>>()});
    }

    public fun new(account: &signer, name: vector<u8>): NFT<vector<u8>> acquires UniqIdList {
        let account_address = Signer::address_of(account);
        let exist = Vector::contains<vector<u8>>(&borrow_global<UniqIdList<vector<u8>>>(account_address).data, &name);
        assert(!exist, 1);
        let id_list = borrow_global_mut<UniqIdList<vector<u8>>>(account_address);
        Vector::push_back<vector<u8>>(&mut id_list.data, copy name);
        NFT { name }
    }
}
}