Vรญ dแปฅ: Tแบกo Rare Animal NFTs at the Thao Cam Vien Zoo ๐ฅบ
Trong vรญ dแปฅ nร y mรฌnh sแบฝ viแบฟt mแปt module wild_NFT. ฤรขy lร  module ฤฦฐแปฃc thiแบฟt kแบฟ quแบฃn lรฝ cรกc NFT ฤแปng vแบญt quรฝ hiแบฟm แป sแป thรบ
Sau ฤรขy lร phแบงn giแปi thiแปu vแป cรกc cแบฅu trรบc chรญnh:
- Animals struct:
public struct AnimalInfo has key , store {
        id: UID,
        name: String, 
        species: String,
        habitat: String, 
        status: u64,
        image_url: Url
  }
 
public struct Animals has key, store {
    id: UID,
    animal_infos: Table<u64, AnimalInfo>,
}
 
 
//Nhรขn viรชn mแปi sแบฝ cรณ quyแปn ฤแป cung cแบฅp thรชm thรดng tin ฤแปng vแบญt
 public struct NFTAdminCap has key{
      id: UID
  }
 Hร m (Function):
- Thรชm thรดng tin ฤแปng vแบญt (Quyแปn Admin) Admin cรณ thแป thรชm thรดng tin ฤแปng vแบญt mแปi vร o struct Animals:
public fun add_animal(
    _: &NFTAdminCap,
    animals: &mut Animals,
    name: String,
    species: String,
    habitat: String,
    status: u64,
    image_url: String,
    ctx: &mut TxContext,
) {
    let new_animal = AnimalInfo {
        id: object::new(ctx),
        name,
        species,
        habitat,
        status,
        image_url,
    };
    let key = table::length(&animals.animal_infos);
    table::add(&mut animals.animal_infos, key, new_animal);
}Vรญ dแปฅ sau khi mรฌnh add thร nh cรดng:
[warning] Client/Server api version mismatch, client api version : 1.39.2, server api version : 1.39.3
โญโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ objectId      โ  0xb9235f4a2b07d6c6f071c7a42bdc58ec1ca1ab040dbcdb44d4844d8709bc2b99                                                                                                      โ
โ version       โ  289571540                                                                                                                                                               โ
โ digest        โ  3bicPMZ8jYBS6oGjPm2dcaiZQia9WLqzzrdJ6KmEi2Zv                                                                                                                            โ
โ objType       โ  0xf5e12e4451771fb23acca10a10b23ecb797d52c11ad42e363616062e9120e6c6::animal::Animals                                                                                     โ
โ owner         โ โญโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ                                                                                  โ
โ               โ โ AddressOwner โ  0x915c2d19ee5fde257693f25e6c2cabb04c25e7ae03932817d52e122258c88ddb  โ                                                                                  โ
โ               โ โฐโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ                                                                                  โ
โ prevTx        โ  63Rni1y3Ms6g6B89Aq1utKgZpNmbSLeEEBeV6rLWfw7x                                                                                                                            โ
โ storageRebate โ  1596000                                                                                                                                                                 โ
โ content       โ โญโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ               โ โ dataType          โ  moveObject                                                                                                                                      โ โ
โ               โ โ type              โ  0xf5e12e4451771fb23acca10a10b23ecb797d52c11ad42e363616062e9120e6c6::animal::Animals                                                             โ โ
โ               โ โ hasPublicTransfer โ  true                                                                                                                                            โ โ
โ               โ โ fields            โ โญโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ โ
โ               โ โ                   โ โ animal_infos โ โญโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ โ โ
โ               โ โ                   โ โ              โ โ type   โ  0x2::table::Table<u64, 0xf5e12e4451771fb23acca10a10b23ecb797d52c11ad42e363616062e9120e6c6::animal::AnimalInfo>  โ โ โ โ
โ               โ โ                   โ โ              โ โ fields โ โญโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ                         โ โ โ โ
โ               โ โ                   โ โ              โ โ        โ โ id   โ โญโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ                         โ โ โ โ
โ               โ โ                   โ โ              โ โ        โ โ      โ โ id โ  0x60c9569eb12e3c466c9894ad61ec7add6a0f6791334074a83e3db6c446aebc35  โ โ                         โ โ โ โ
โ               โ โ                   โ โ              โ โ        โ โ      โ โฐโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ                         โ โ โ โ
โ               โ โ                   โ โ              โ โ        โ โ size โ  0                                                                            โ                         โ โ โ โ
โ               โ โ                   โ โ              โ โ        โ โฐโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ                         โ โ โ โ
โ               โ โ                   โ โ              โ โฐโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ โ โ
โ               โ โ                   โ โ id           โ โญโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ                                                 โ โ โ
โ               โ โ                   โ โ              โ โ id โ  0xb9235f4a2b07d6c6f071c7a42bdc58ec1ca1ab040dbcdb44d4844d8709bc2b99  โ                                                 โ โ โ
โ               โ โ                   โ โ              โ โฐโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ                                                 โ โ โ
โ               โ โ                   โ โฐโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ โ
โ               โ โฐโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โNgoร i ra bแบกn cรณ thแป cรณ thรชm quyแปn update thรดng tin cแปงa ฤแปng vแบญt ฤรณ ( chแป cรณ admin mแปi lร m ฤฦฐแปฃc) :
public fun update_animal(
    _: &NFTAdminCap,
    animals: &mut Animals,
    key: u64,
    new_name: String,
    new_status: u64,
    ctx: &mut TxContext,
) {
		// kiแปm tra id cแปงa ฤแปng vแบญt ฤรณ lร  stt cแปงa ฤแปng vแบญt ฤรณ
    //assert!(table::contains(&animals.animal_infos, key), ERR_KEY_DOES_NOT_EXIST);
    
    let animal_info = table::borrow_mut(&mut animals.animal_infos, key);
    animal_info.name = new_name;
    animal_info.status = new_status;
}
 Tiแบฟp theo mรฌnh sแบฝ lร m sรขu hฦกn vแป cรกc phแบงn lร m viแปc vแปi NFT nhฦฐ lร Buy NFT:
public fun mint_nft(
    animals: &Animals,
    key: u64,
    inputcoin: Coin<SUI>,
    recipient: address,
    ctx: &mut TxContext,
) {
 
    let animal_info = &animals.animal_infos[key];
    let new_nft = AnimalNFT {
        id: object::new(ctx),
        name: animal_info.name,
        animal_id: key,
        species: animal_info.species,
        habitat: animal_info.habitat,
        adopted_by: recipient,
        image_url: animal_info.image_url,
    };
    transfer::public_transfer(new_nft, recipient);
}