相关文章推荐
从容的柚子  ·  如何将多个JSON合并成一个JSON,而不是 ...·  2 年前    · 
卖萌的鸡蛋面  ·  Android ...·  2 年前    · 
腹黑的领带  ·  idea创建动态web项目打开jsp文件报4 ...·  3 年前    · 
聪明的勺子  ·  PHP curl_init函数 | 菜鸟教程·  3 年前    · 
完美的硬盘  ·  TypeScript 仅仅导入声明语法 - 掘金·  3 年前    · 
Code  ›  创建一个基于链上实时数据的动态SVG NFT开发者社区
https://cloud.tencent.com/developer/article/2152770
深情的手术刀
2 年前
Tiny熊

创建一个基于链上实时数据的动态SVG NFT

前往小程序,Get 更优 阅读体验!
立即前往
腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
工具
TVP
最新优惠活动
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
Tiny熊
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
社区首页 > 专栏 > 创建一个基于链上实时数据的动态SVG NFT

创建一个基于链上实时数据的动态SVG NFT

作者头像
Tiny熊
发布 于 2022-11-07 09:51:18
952 0
发布 于 2022-11-07 09:51:18
举报
文章被收录于专栏: 深入浅出区块链技术 深入浅出区块链技术

译文出自:登链翻译计划 [1] 译者:翻译小组 [2] 校对:Tiny 熊 [3]

在过去的一年里,NFT 是一个令人惊讶的突破性使用场景,它使数百万的新用户加入了加密货币和 web3。目前,大多数 NFT 由静态图片组成,有时这些图片由某个预定义规则 揭示 出来(如盲盒形式)。但作为可编程的智能合约,s 能够做得更多。

IPFS 托管 NFT 图像

对 NFT 的一个常见的批评是,它们 只是一个甚至不在区块链上的图片链接 。对于许多著名的项目,如Bored Ape Yacht Club [4] ,的确是如此。

OpenSea上的Bored Ape #969

ERC-721 标准的标准接口 tokenURI()用来返回元数据(metadata),其中包括一个图像链接。在 Bored Apes 的案例中,元数据被存储在 IPFS 上。我们可以通过在Etherscan [5] 上直接查询 Bored Ape 合约的 tokenURI 来看到这一点。

该链接返回 NFT 的完整元数据,包括图片也在IPFS [6] 上。

这个链接也托管在IPFS [7] 上,

一个侧面说法,也是相当哲学的观点:NFT 是收据,而不是图像本身,请看EveryNFTEver [8] ,它有一个很好的简洁解释。

链上 SVG NFT

虽然 IPFS 托管元数据和图像更常见,但存在另一种类型的 NFT,其中数据直接在智能合约中完全存储在链上。代替返回链接,tokenURI 返回一个编码的 json 数据,包含可以在浏览器中呈现的 svg 数据。

SVG NFT 最有名的例子是 Loot:

黑色背景上的白色文字。这个图片不是来自 IPFS,而是一个浏览器可以渲染的编码过的 svg 文件。其完全在链上的,不依赖任何外部链接。完整的合约可以在Etherscan [9] 上找到,但下面是相关部分:

SVG 数据是以编程方式生成、编码并由合约返回。

读取链上数据

Loot 是一个简单的例子,但它说明了与 IPFS 托管图片的区别。因为确定 SVG 的逻辑是在链上执行的,所以它开启了一系列的可能性。

我们可以从其他智能合约中读取数据并将其包含在 SVG 中,每次调用渲染函数时,这些数据都会自动更新读取! 这使得 SVG 图片可以合成,并对链上的数据变化做出反应。

概念验证 BuidlGuidl NFT

作为一个概念证明,我为BuidlGuidl [10] 的成员写了一个简单的动态 SVG NFT。BuidlGuidl NFT 演示-Youtube 视频 [11]

这个想法是一个徽章 NFT,它读取钱包的 ENS 名称、余额和工作流合约余额。并以一种漂亮的简约方式显示它们。

ENS 名称和余额在每次 NFT 被渲染时都会更新,在OpenSea [12] 上查看它。

完整的合约可以在这里 [13] 找到:

代码语言: javascript
复制
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/utils/Base64.sol";
 * @title BuidlGuidl Tabard
 * @author Daniel Khoo
 * @notice A dynamic NFT for BuidlGuidl members. Image is a fully-onchain SVG with tied to the bound address i.e. the minter.
 * Dynamic elements are: ENS reverse resolution, stream and wallet balance updates.
 * @dev Mintable if wallet is toAddress of a BuidlGuidl stream.
contract BuidlGuidlTabard is ERC721 {
    // ENS Reverse Record Contract for address => ENS resolution
    // NOTE: Address of ENS Reverse Record Contract differs across testnets/mainnet
    IReverseRecords ensReverseRecords =
        IReverseRecords(0x3671aE578E63FdF66ad4F3E12CC0c0d71Ac7510C);
    mapping(address => address) public streams; // Store individual stream addresses so they can be referenced post-mint
    constructor() ERC721("BuidlGuidl Tabard", "BGT") {}
    function mintItem(address streamAddress) public {
        // Minimal check that wallet is the recipient of a Stream
        // Someone could deploy a decoy stream to bypass this, but it's easier to just join the BuidlGuidl :)
        ISimpleStream stream = ISimpleStream(streamAddress);
        require(
            msg.sender == stream.toAddress(),
            "You are not the recipient of the stream"
        streams[msg.sender] = streamAddress;
        // Set the token id to the address of minter.
        // Inspired by https://gist.github.com/z0r0z/6ca37df326302b0ec8635b8796a4fdbb
        _mint(msg.sender, uint256(uint160(msg.sender)));
    function tokenURI(uint256 id) public view override returns (string memory) {
        return _buildTokenURI(id);
    // Constructs the encoded svg string to be returned by tokenURI()
    function _buildTokenURI(uint256 id) internal view returns (string memory) {
        bool minted = _exists(id);
        // Bound address from tokenId
        address boundAddress = address(uint160(id));
        string memory streamBalance = "";
        // Don't include stream in URI until token is minted
        if (minted) {
            // Get stream address, to check it's current balance
            address streamAddress = streams[boundAddress];
            ISimpleStream stream = ISimpleStream(streamAddress);
            streamBalance = string(
                abi.encodePacked(
                    unicode'<text x="20" y="305">Stream Ξ',
                    weiToEtherString(stream.streamBalance()),
                    "</text>"
        bytes memory image = abi.encodePacked(
            "data:image/svg+xml;base64,",
            Base64.encode(
                bytes(
                    abi.encodePacked(
                        '<?xml version="1.0" encoding="UTF-8"?>',
                        '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 400 400" preserveAspectRatio="xMidYMid meet">',
                        '<style type="text/css"><![CDATA[text { font-family: monospace; font-size: 21px;} .h1 {font-size: 40px; font-weight: 600;}]]></style>',
                        '<rect width="400" height="400" fill="#ffffff" />',
                        '<text class="h1" x="50" y="70">Knight of the</text>',
                        '<text class="h1" x="80" y="120" >BuidlGuidl</text>',
                        unicode'<text x="70" y="240" style="font-size:100px;">🏗️ 🏰</text>',
                        streamBalance,
                        unicode'<text x="210" y="305">Wallet Ξ',
                        weiToEtherString(boundAddress.balance),
                        "</text>",
                        '<text x="20" y="350" style="font-size:28px;"> ',
                        lookupENSName(boundAddress),
                        "</text>",
                        '<text x="20" y="380" style="font-size:14px;">0x',
                        addressToString(boundAddress),
                        "</text>",
                        "</svg>"
        return
            string(
                abi.encodePacked(
                    "data:application/json;base64,",
                    Base64.encode(
                        bytes(
                            abi.encodePacked(
                                '{"name":"BuidlGuidl Tabard", "image":"',
                                image,
                                unicode'", "description": "This NFT marks the bound address as a member of the BuidlGuidl. The image is a fully-onchain dynamic SVG reflecting current balances of the bound wallet and builder work stream."}'
    /* ========== HELPER FUNCTIONS ========== */
    /// @notice Checks ENS reverse records if address has an ens name, else returns blank string
    function lookupENSName(address addr) public view returns (string memory) {
        address[] memory t = new address[](1 "] memory t = new address[");
        t[0] = addr;
        string[] memory results = ensReverseRecords.getNames(t);
        return results[0];
    /// @notice  Converts wei to ether string with 2 decimal places
    function weiToEtherString(uint256 amountInWei)
        public
        returns (string memory)
        uint256 amountInFinney = amountInWei / 1e15; // 1 finney == 1e15
        return
            string(
                abi.encodePacked(
                    Strings.toString(amountInFinney / 1000), //left of decimal
                    Strings.toString((amountInFinney % 1000) / 100), //first decimal
                    Strings.toString(((amountInFinney % 1000) % 100) / 10) // first decimal
    function addressToString(address x) internal pure returns (string memory) {
        bytes memory s = new bytes(40);
        for (uint256 i = 0; i < 20; i++) {
            bytes1 b = bytes1(uint8(uint256(uint160(x)) / (2**(8 * (19 - i)))));
            bytes1 hi = bytes1(uint8(b) / 16);
            bytes1 lo = bytes1(uint8(b) - 16 * uint8(hi));
            s[2 * i] = char(hi);
            s[2 * i + 1] = char(lo);
        return string(s);
    function char(bytes1 b) internal pure returns (bytes1 c) {
        if (uint8(b) < 10) return bytes1(uint8(b) + 0x30);
        else return bytes1(uint8(b) + 0x57);
/* ========== EXTERNAL CONTRACT INTERFACES ========== */
/// @notice Minimal contract interfaces for dynamic reading of data for SVG
/// @notice SimpleStream that each buidlguidl member has
/// https://github.com/scaffold-eth/scaffold-eth/blob/simple-stream/packages/hardhat/contracts/SimpleStream.sol
interface ISimpleStream {
    function toAddress() external view returns (address);
    function streamBalance() external view returns (uint256);
/// @notice ENS reverse record contract for resolving address to ENS name
/// https://github.com/ensdomains/reverse-records/blob/master/contracts/ReverseRecords.sol
interface IReverseRecords {
 
推荐文章
从容的柚子  ·  如何将多个JSON合并成一个JSON,而不是将结果合并到列表中。
2 年前
卖萌的鸡蛋面  ·  Android Matrix进阶方法详解_android matrix mappoint-CSDN博客
2 年前
腹黑的领带  ·  idea创建动态web项目打开jsp文件报404问题_zjtMeng的博客-CSDN博客
3 年前
聪明的勺子  ·  PHP curl_init函数 | 菜鸟教程
3 年前
完美的硬盘  ·  TypeScript 仅仅导入声明语法 - 掘金
3 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号