乐趣区

关于ipfs:如何使用IPFS和Filecoin创建NFT

挑战 1 – 部署一个 NFT 合约

工具应用

- Node.js, version 16
- NPM, version 7
- HardHat - ethereum development framework & blockchain simulator
    - https://hardhat.org
- OpenZeppelin Contracts - base Solidity contracts
    - https://docs.openzeppelin.com/contracts/4.x

步骤
1. 为我的项目新建一个目录并输出

mkdir nft-challenge
cd nft-challenge

2. 应用默认设置开启一个新的 NPM 我的项目

npm init -y

3. 装置 HardHat and OpenZeppelin 合约作为开发依赖项

npm install --save-dev hardhat @openzeppelin/contracts

4. 应用 HardHat 生成我的项目 scaffold

npx hardhat

当提醒装置依赖项时,承受默认值。最初你会看到如下信息:

✨ Project created ✨

Try running some of the following tasks:
  npx hardhat accounts
  npx hardhat compile
  npx hardhat test
  npx hardhat node
  node scripts/sample-script.js
  npx hardhat help

并且目录下会创立一些新文件

ls
contracts         node_modules      package.json      test
hardhat.config.js package-lock.json scripts

5.(可选)应用 Git 来治理你的代码

git init .
git add .
git commit -m 'solidity boilerplate'

6. 用你最善于的 IDE 并关上我的项目目录

code .

点击 contracts 目录,而后关上 Greeter.sol,你将看到一个“hello world”合约。

7. 在合约文件中创立一个名为‘GameItem.sol’的新文件,把这个粘贴进:
Source: https://docs.openzeppelin.com…

// contracts/GameItem.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract GameItem is ERC721URIStorage {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() ERC721("GameItem", "ITM") {}

    function awardItem(address player, string memory tokenURI)
        public
        returns (uint256)
    {_tokenIds.increment();

        uint256 newItemId = _tokenIds.current();
        _mint(player, newItemId);
        _setTokenURI(newItemId, tokenURI);

        return newItemId;
    }
}

8. 编译智能合约

npx hardhat compile

9. 从 HardHat 框架中关上示例脚本
在你的编辑器中关上 scripts/sample-script.js
将 Greeter 重命名为 GameItemgreeter重命名为 gameItem。移除该参数 到 GameItem.deploy()

最终后果中的 main 函数应该是这样的:

async function main() {
  // Hardhat always runs the compile task when running scripts with its command
  // line interface.
  //
  // If this script is run directly using `node` you may want to call compile
  // manually to make sure everything is compiled
  // await hre.run('compile');

  // We get the contract to deploy
  const GameItem = await hre.ethers.getContractFactory("GameItem");
  const gameItem = await GameItem.deploy();

  await gameItem.deployed();

  console.log("GameItem deployed to:", gameItem.address);
}

10. 应用 hardhat 命令运行该脚本:

npx hardhat run scripts/sample-script.js
GameItem deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3

提醒说它曾经部署在这个地址上了!是不是看上去咱们曾经实现了?
不,还没完。默认状况下,HardHat 应用 ephermeral blockhain 模拟器作为它的指标环境,这对于编写自动化测试很有用,然而对互动式编程不太敌对。
接下来咱们将启动一个本地节点,并且通知 HardHat 应用它。

11. 在我的项目目录中关上一个新的终端,并且运行 hardhat 节点

npx hardhat node

12. 返回原始终端,将 HARDHAT_NETWORK 环境变量设置为 localhost,并且从新运行该脚本

export HARDHAT_NETWORK=localhost
npx hardhat run scripts/sample-script.js

脚本输入的后果大抵是雷同的,然而如果你查看其余的终端,你将在 hardhat 的输入中看到一些交易信息。

挑战 2

1. 在你的编辑器里再次关上 scripts/sample-script.js 
在合约部署实现后,尝试调用一下 awardItem 函数

const accounts = await hre.ethers.getSigners()
const player = accounts[0].address

const cid = 'a-fake-cid'
const tx = await gameItem.awardItem(player, `ipfs://${cid}`)
const receipt = await tx.wait()
for (const event of receipt.events) {if (event.event !== 'Transfer') {continue}
  console.log('token id:', event.args.tokenId.toString())
  console.log('owner:', event.args.to)
}

const uri = await gameItem.tokenURI(1)
console.log('token uri:', uri)

2. 运行脚本

npx hardhat run scripts/sample-script.js

挑战 3

1. 装置 nft.storage 客户端

npm install nft.storage

2. 在你的编辑器中再次关上 scripts/sample-script.js 
导入 client package 和 File constructor,援用 node.js 内置的 readFile

const {readFile} = require('fs/promises')
const {NFTStorage, File} = require('nft.storage')

3. 创立一个新的函数来加载一张图片并保留它

async function storeNFTData() {
  const apiKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJnaXRodWJ8Njc4NzE1IiwiaXNzIjoibmZ0LXN0b3JhZ2UiLCJpYXQiOjE2MTYwNzM2MjMxMTksIm5hbWUiOiJkZWZhdWx0In0.RJj_mR2zwTJ1_4gpAapGdkQcw3gSUz1SMmIHgr10Xng'
  const client = new NFTStorage({token: apiKey})

  const filename = '../files/nft.jpg'
  const data = await readFile(filename)
  const file = new File([data], 'nft.jpg', {type: 'image/jpeg'})

  console.log('sending data to nft.storage...')
  const metadata = await client.store({
    name: 'Encode Club Demo NFT',
    description: 'Hello Encode Club!',
    image: file
  })

  console.log('stored NFT data! metadata URL:', metadata.url)
  return metadata.url
}

4. 应用 storeNFTData 办法取得 metadata URL,而后创立该 token,例如上面所示:
在 main函数中:

const metadataURI = await storeNFTData()
const tx = await gameItem.awardItem(player, metadataURI)

作者:Eason
原文链接:https://hackmd.io/E-pMngLbReq…

退出移动版