Skip to content

Instantly share code, notes, and snippets.

@harry830622
Last active February 27, 2022 06:30
Show Gist options
  • Save harry830622/1c171b18ce3f1a4c5ecccce553f5359c to your computer and use it in GitHub Desktop.
Save harry830622/1c171b18ce3f1a4c5ecccce553f5359c to your computer and use it in GitHub Desktop.
// NFT 代表的就是一個 moment,也就是一張卡片
// 它實作官方 NFT contract 裡的 INFT interface
// 以及 MetadataViews.Resolver interface
pub resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver {
// 每一張卡片都有獨特的 ID 代表,
// 並且這也是實作 NonFungibleToken.INFT 必須要有的
pub let id: UInt64
pub let data: MomentData
// 創建 struct 時候的初始化 function
// 在這裡就是卡片被 mint 出來的時候,
// 主要就是初始化上述提到的那些 variables
// 並且發出 mint 出新卡片的 event
init(serialNumber: UInt32, playID: UInt32, setID: UInt32) {
// 將總供應量 +1
TopShot.totalSupply = TopShot.totalSupply + UInt64(1)
self.id = TopShot.totalSupply
self.data = MomentData(setID: setID, playID: playID, serialNumber: serialNumber)
emit MomentMinted(momentID: self.id, playID: playID, setID: self.data.setID, serialNumber: self.data.serialNumber)
}
// 卡片被銷毀時候會發出某張卡片被銷毀的 event
destroy() {
emit MomentDestroyed(id: self.id)
}
pub fun name(): String {
let fullName: String = TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "FullName") ?? ""
let playType: String = TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "PlayType") ?? ""
return fullName
.concat(" ")
.concat(playType)
}
pub fun description(): String {
let setName: String = TopShot.getSetName(setID: self.data.setID) ?? ""
let serialNumber: String = self.data.serialNumber.toString()
let seriesNumber: String = TopShot.getSetSeries(setID: self.data.setID)?.toString() ?? ""
return "A series "
.concat(seriesNumber)
.concat(" ")
.concat(setName)
.concat(" moment with serial number ")
.concat(serialNumber)
}
// 這是 MetadataViews.Resolver 必須的實作 function,
// 代表 metadata 可以有幾種 view type,也就是 metadata 的資料結構,
// 第一個當然是原本 MetadataViews 內建的 Display type,會儲存 name, description
// 第二個就是本合約自己定義的 TopShotMomentMetadataView Type 則是儲存上面提到的那些卡片資訊
pub fun getViews(): [Type] {
return [
Type<MetadataViews.Display>(),
Type<TopShotMomentMetadataView>()
]
}
// 這也是 MetadataViews.Resolver 必須的實作 function,
// 目的是讓外部能夠傳入 view type 來存取到想要的 metadata 形式,
// 所以下面可以看到如果是傳入預設的 MetadataViews.Display type ,就會回傳 name, description, thumbnail
// 而如果是傳入 TopShotMomentMetadataView Type 就會回傳球員名字、所屬隊伍等等卡片資訊
pub fun resolveView(_ view: Type): AnyStruct? {
switch view {
case Type<MetadataViews.Display>():
return MetadataViews.Display(
name: self.name(),
description: self.description(),
thumbnail: MetadataViews.HTTPFile("")
)
case Type<TopShotMomentMetadataView>():
return TopShotMomentMetadataView(
fullName: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "FullName"),
firstName: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "FirstName"),
lastName: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "LastName"),
birthdate: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "Birthdate"),
birthplace: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "Birthplace"),
jerseyNumber: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "JerseyNumber"),
draftTeam: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "DraftTeam"),
draftYear: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "DraftYear"),
draftSelection: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "DraftSelection"),
draftRound: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "DraftRound"),
teamAtMomentNBAID: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "TeamAtMomentNBAID"),
teamAtMoment: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "TeamAtMoment"),
primaryPosition: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "PrimaryPosition"),
height: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "Height"),
weight: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "Weight"),
totalYearsExperience: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "TotalYearsExperience"),
nbaSeason: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "NbaSeason"),
dateOfMoment: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "DateOfMoment"),
playCategory: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "PlayCategory"),
playType: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "PlayType"),
homeTeamName: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "HomeTeamName"),
awayTeamName: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "AwayTeamName"),
homeTeamScore: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "HomeTeamScore"),
awayTeamScore: TopShot.getPlayMetaDataByField(playID: self.data.playID, field: "AwayTeamScore"),
seriesNumber: TopShot.getSetSeries(setID: self.data.setID),
setName: TopShot.getSetName(setID: self.data.setID),
serialNumber: self.data.serialNumber,
playID: self.data.playID,
setID: self.data.setID,
numMomentsInEdition: TopShot.getNumMomentsInEdition(setID: self.data.setID, playID: self.data.playID)
)
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment