铸造NFT交易系统开发功能介绍

112 阅读3分钟

 nft指的是数字作品。NFT一般指数字作品。【18I链上合约-259l开发系统3365】就是将图片、文字、视频、音频等数字化的内容上传到区块链上,生成一串独一无二,不可互换,不能篡改的代码。NFT全称为Non-FungibleToken,翻译过来是非同质化通证。NFT就是一种在区块链中注册的独特数字证书,可以用来记录你的艺术品或者收藏品等资产的所有权。

NFT名词解释

NFT的评价有主观和客观两个维度。NFT为非同质化代币,与非同质化代币NFT相对应的就是同质化代币FT,NFT是区块链的一个条目,而区块链是类似于比特币等加密货币的去中心化数字账本技术。现在,艺术家、音乐家、影响者和体育特许经营公司都在使用NFT将以前廉价或免费的数字商品货币化。

该技术还响应了艺术界在日益数字化的世界中对认证和出处的需求,将一个数字文件与其创作者永久地联系起来。NFT完全在链上铸造,如Avastars、Aavegotchis和ArtBlocksdrop,仅依赖于各自的以太坊智能合约而存在。另一方面,一些NFT项目通过使其NFT依赖于外部的链外提供商来选择简单性和灵活性。

Dapp是一种应用程序。18I链上合约-259l开发系统3365,DApp就是D+App。Dapp(去中心化应用程序)是一种在网络上公开运行的软件应用程序,他们与普通的应用程序没有什么区别,都拥有一样的功能,但不同的是Dapp是在P2P网络上运行。

  App我们都知道是客户端应用,是application的简称。DApp就是D+App,D是英文单词 理解 tokenURI() 让我们解释一下 ERC721 tokenURI()函数的实现:

tokenURI()是 ERC721 标准的一个元数据函数,在 OpenZeppelin 文档中 :

tokenURI(uint256 tokenId) → string 返回 tokenId 代币的统一资源标识符(URI)。

通常 tokenURI 会返回一个 URI。我们可以通过连接 baseURI 和 tokenId 来获得每个 token 的 URI 结果。

在我们的tokenURI()中,将 URI 作为一个 base64 编码的对象返回。

首先,构建对象。对象中的 svg 图片也是 base64 编码的:

{ "name":"Badge #1", "description":"Badge NFT with on-chain SVG image." "image":"data:image/svg+xml;base64,[svg base64 encoded]" } 复制 然后我们返回 base64 编码的对象:

data:application/json;base64,(object base64 encoded) 复制 Webapp 可以通过调用tokenURI(tokenId)获得 URI,并解码以获得名称、描述和 SVG 图片。

这里的 SVG 图片由 LOOT 项目改编的。非常简单,就是在图片中显示 tokenId:

.base { fill: white; font-family: serif; font-size: 300px; } 1 // test/BadgeToken.test.ts import { expect } from "chai" import { Signer } from "ethers" import { ethers } from "hardhat" import { BadgeToken } from "../typechain"

const base64 = require( "base-64")

const _name='BadgeToken' const _symbol='BADGE'

describe("BadgeToken", function () { let badge:BadgeToken let account0:Signer,account1:Signer

beforeEach(async function () { [account0, account1] = await ethers.getSigners() const BadgeToken = await ethers.getContractFactory("BadgeToken") badge = await BadgeToken.deploy(_name,_symbol) })

it("Should has the correct name and symbol ", async function () { expect(await badge.name()).to.equal(_name) expect(await badge.symbol()).to.equal(_symbol) })

it("Should tokenId start from 1 and auto increment", async function () { const address1=await account1.getAddress() await badge.mintTo(address1) expect(await badge.ownerOf(1)).to.equal(address1)

await badge.mintTo(address1)
expect(await badge.ownerOf(2)).to.equal(address1)
expect(await badge.balanceOf(address1)).to.equal(2)

})

it("Should mint a token with event", async function () { const address1=await account1.getAddress() await expect(badge.mintTo(address1)) .to.emit(badge, 'Transfer') .withArgs(ethers.constants.AddressZero,address1, 1) })

it("Should mint a token with desired tokenURI (log result for inspection)", async function () { const address1=await account1.getAddress() await badge.mintTo(address1)

const tokenUri = await badge.tokenURI(1)
// console.log("tokenURI:")
// console.log(tokenUri)

const tokenId = 1
const data = base64.decode(tokenUri.slice(29))
const itemInfo = JSON.parse(data)
expect(itemInfo.name).to.be.equal('Badge #'+String(tokenId))
expect(itemInfo.description).to.be.equal('Badge NFT with on-chain SVG image.')

const svg = base64.decode(itemInfo.image.slice(26))
const idInSVG = svg.slice(256,-13)
expect(idInSVG).to.be.equal(String(tokenId))
// console.log("SVG image:")
// console.log(svg)

})

it("Should mint 10 token with desired tokenURI", async function () { const address1=await account1.getAddress()

for(let i=1;i<=10;i++){
  await badge.mintTo(address1)
  const tokenUri = await badge.tokenURI(i)

  const data = base64.decode(tokenUri.slice(29))
  const itemInfo = JSON.parse(data)
  expect(itemInfo.name).to.be.equal('Badge #'+String(i))
  expect(itemInfo.description).to.be.equal('Badge NFT with on-chain SVG image.')

  const svg = base64.decode(itemInfo.image.slice(26))
  const idInSVG = svg.slice(256,-13)
  expect(idInSVG).to.be.equal(String(i))
}

expect(await badge.balanceOf(address1)).to.equal(10)

}) })