The first starcoin & move hacksong source code analysis - P (a)

Starcoin 2021-09-15 08:20:28

Overall introduction

P(A) It's the first Move Hacker song NFT A project of the track , from MemeX Team designed . This year, there are many NFT project , From functional integrity 、 Project completion 、 Creative 、 Comprehensive consideration of display degree and other aspects ,P(A) It's a good one NFT project .NFT It's a very popular track at present , and Move stay NFT The field has great advantages .P(A) Use Move Realization , It can be said that it combines well Move Characteristics of .

Here we analyze the contract source code , Let's learn how to use Move Implement a simple one NFT. Let's see first One Next P(A) The overall design drawing of the project :

You can see some important information from the figure above :

  1. Core data structure :
  • NFT
  • NFT_Info
  • Market
  1. Core function :
  • init_market
  • mint

Logically speaking , Simple and direct , contract Owner adopt init_market Initialize a Market, Ordinary users can mint To buy NFT. Next, let's analyze the source code .

Core data structure

P(A) Contains multiple data structures :Art、NFT、UniqList、NFT_INFO、MARKET, The relationship between them is shown in the figure below :

1. Art

struct Art has store, copy, drop { prob_a: u8, prob_b: u8, param_1: vector<u64>, param_2: vector<u64>}

This is a NFT Metadata information required for rendering , No, key Of ability, Means you can copy 、 Can discard 、 Energy storage . Here's the advice NFT Metadata cannot be copied or discarded .

  1. NFT
struct NFT has store { id: u8,// Unique identification next_nft_id: u8,// Put... In the form of a linked list NFT Identification string , Easy to track next_nft_owner: address,// Put... In the form of a linked list address String together , Easy to track data: Art,// NFT Metadata sell_status: bool,// state price: u128,// Price }

This is a NFT Specific data , Only storage capacity , It means that you can't copy and discard . Very convenient to play Move The advantages of , Use virtual machines to ensure NFT The integrity of 、 Uniqueness, etc .

  1. UniqList
struct UniqList has key { data: vector<NFT> // Store user owned NFT list }

UniqList Although an array is used , But don't worry about large arrays here , Because each user's NFT It's stored under your own account , It won't affect all users .

struct NFT_INFO has store, drop { id: u8,// NFT identification next_nft_id: u8, next_nft_owner: address, data: Art, price: u128,// Price }

This is a NFT Generated information , For display . Yes NFT Of id 了 ,Art It's a little redundant here .

  1. Market
struct MARKET has key { head: address, cur_num: u8,// Current quantity min_price: u128,// Minimum price market_nft_info: vector<NFT_INFO>// be-all NFT Information }

All the... Are stored here NFT Information . There seems to be a problem with large arrays , because P(A) It limits the maximum number of 100 individual NFT, So don't worry here . however , Lists like this show data , Recommended by Event The way , Polymerize under the chain .

Function Definition

  1. init_market function
public(script) fun init_market(account: signer)

This is the contract initialization entry , In the contract Owner Initialize a... Under the account Market. The function uses public(script) visibility .

  1. mint function
public(script) fun mint(account: signer, amount: u128) acquires UniqList, MARKET

This is what users buy NFT Entrance , It's also public(script) visibility .acquires Indicates that the current Module Defined UniqList and MARKET Two data structures .

reflection & summary

P(A) In terms of contract implementation details , The code is relatively simple , Make the most of it Move stay NFT The advantages of the scene , It saves a lot of unnecessary security checks , For example, to prevent NFT Lost... Etc . Let's summarize the advantages and disadvantages of contract code implementation .

  1. advantage :
  • NFT Is a hot direction , It's also Move A typical application scenario to give full play to its advantages
  • part Struct In the definition, it skillfully applies Move Of ability To ensure safety and reliability , for example NFT, Cannot copy and discard
  • All functions are public(script) visibility , There is no excess entrance exposed
  • NFT The data is stored under the user's own account ,NFT There will be no problem with large arrays
  • The design of linked list is used , Easy to track
  1. Can further optimize :
  • NFT internal data Art It should also not be copied and discarded , Avoid producing the same content NFT
  • Key data status changes should be defined Event, Convenient offline monitoring
  • Market The array can be stored under the chain , Avoid possible large array problems
  • Some details are optimized , for example Struct The name is in the same case 、 Redundant data, etc
  • Can add richer functions , such as NFT Transfer and so on

in general ,P(A) Cleverly applied Move The advantages of , stay NFT Made a good attempt in the direction of , here Check out the full code .

Please bring the original link to reprint ,thank
Similar articles