DApp设计与开发 课程笔记(六):NFT交易市场后端开发

笔记对应课程内容为成都信息工程大学区块链产业学院老师梁培利DApp 设计与开发 17-18 课

笔记中提到的名词不做过多解释 不懂就搜!

tokenuri 对应一个metadata的 json 数据

上传一个图片,将图片上传到IPFS,获得一个cid,然后将json格式的metadata上传到IPFS,然后给用户发送一个NFT

代码:https://github.com/liangpeili/nft-market-backend

app.js

import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import fileUpload from 'express-fileupload';
import { addFileToIPFS, addJSONToIPFS } from './ipfs-uploader.js';
import { mint } from './nft-minter.js';
import dotenv from 'dotenv';
dotenv.config("./.env");

const app = express()

app.set('view engine','ejs')
app.use(bodyParser.urlencoded({extended:true}))
app.use(fileUpload())
app.use(cors())

app.get('/', (req,res) => {
    res.render('home')
})

app.post('/upload', (req, res) => {
    if (!req.files || Object.keys(req.files).length === 0) {
        return res.status(400).json({ message: 'No files were uploaded.' });
    }

    const file = req.files.file;
    const fileName = file.name
    const filePath = 'files/' + fileName

    const title = req.body.title
    const description = req.body.description
    const address = req.body.address

    console.log(title, description, address)

    file.mv(filePath, async (err) => {
        if (err) {
            console.log('error: failed to download the file.')
            return res.status(500).send(err)
        }

        const fileResult = await addFileToIPFS(filePath)
        console.log('File added to IPFS:', fileResult.cid.toString());

        const metadata = {
            title,
            description,
            image: 'http://127.0.0.1:8080/ipfs/' + fileResult.cid.toString() + '/' + fileName
        }
        const jsonResult = await addJSONToIPFS(metadata)
        console.log('Metadata added to IPFS:', jsonResult.cid.toString());

        const userAddress = address || process.env.ADDRESS;
        await mint(userAddress, 'http://127.0.0.1:8080/ipfs/' + jsonResult.cid.toString())

        res.json({ 
            message: 'File uploaded successfully.', 
            metadata
        });
    });
});

const HOST = process.env.HOST 
const PORT = process.env.PORT

app.listen(PORT, HOST, () => {
    console.log(`Server is running on port ${PORT}`)
})

ipfs-uploader.js

import { create } from 'kubo-rpc-client';
import fs from 'fs';
import dotenv from 'dotenv';
dotenv.config("./.env");

const ipfs = create(new URL(process.env.IPFS_URL))

export async function addJSONToIPFS(json) {
  try {
      const result = await ipfs.add(JSON.stringify(json));
      return result;
  } catch (error) {
      console.error('Failed to add JSON to IPFS:', error);
  }
}

export async function addFileToIPFS(filePath) {
  try {
      const file = fs.readFileSync(filePath);
      const result = await ipfs.add({ path: filePath, content: file });
      return result;
  } catch (error) {
      console.error('Failed to add file to IPFS:', error);
  }
}

nft-minter.js

import { ethers, JsonRpcProvider } from "ethers";
import fs from 'fs';
import dotenv from 'dotenv';
dotenv.config("./.env");

export async function mint(address, uri) {
    const provider = new JsonRpcProvider(process.env.RPC);
    const signer = await provider.getSigner()

    const MyNFTAbi = JSON.parse(fs.readFileSync('./abis/MyNFT.json'));
    const MyNFTContract = new ethers.Contract(process.env.NFT, MyNFTAbi, signer);

    const result = await MyNFTContract.safeMint(address, uri);
    console.log(result.hash);
}

// mint('0x70997970C51812dc3A010C7d01b50e0d17dc79C8', 'https://ipfs.io/ipfs/QmZ4tj')
posted @ 2024-07-02 22:21  孤飞  阅读(24)  评论(0编辑  收藏  举报