当前位置:网站首页>NFT without IPFs and completely on the chain?
NFT without IPFs and completely on the chain?
2022-07-03 19:23:00 【chinadefi】
No, IPFS And completely on the chain NFT? Yes , There's a way !


Encryption and NFT A big problem that the encryption field tries to solve is centralization . Blockchain is an important part . Without blockchain , We wouldn't have millions of dollars DAO Or like CryptoPunks Such a project . however , Most projects still have a problem :“ If IPFS What about the descent ?”
IPFS It has been very decentralized , So don't worry . But some collectors may prefer the pure chain data of their tokens . Metadata and Art . thankfully , We can do it in Solidity To do this . It is with OpenSea compatible .
How to store images on the chain ?
We can store anything on the chain , The problem is how to store it . We are likely to want to base64 code , And prefix it / Add more information to the suffix , So that the computer knows what it is looking at . This is it. OpenSea How it works , I've tested .
Intelligent contract ( Or library ) It can be used to encode data as on chain base64. I used this library . This is a Loopring What the team did .
Now? , Aimed at NFT In our smart contract , We hope to be able to SVG The data is encoded as base64, And add the correct information prefix .
function svgToImageURI(string memory _source) public pure returns (string memory) {
string memory baseURL = "data:image/svg+xml;base64,";
string memory svgBase64Encoded = Base64.encode(bytes(string(abi.encodePacked(_source))));
return string(abi.encodePacked(baseURL, svgBase64Encoded));
}
This function will be later safeMint Function is used in another function of , But in essence, it will SVG ’ string ' Encoded as base64, And encode it and return it to Solidity For further processing .
SVG It's not a string , They are images , Right ?
No , They can be regarded as images , But they are just written in code , Very similar to HTML.
How does the image look ?
Because I have tested a potential client before , So I also found a simple SVG File to test . It's in ordinary SVG It looks like this in :
base64 Code it to look like this :
data:image/svg+xml;base64,PHN2ZyB3aWR0aD0nNTAwJyBoZWlnaHQ9JzUwMCcgdmlld0JveD0nMCAwIDI4NSAzNTAnIGZpbGw9J25vbmUnIHhtbG5zPSdodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2Zyc+PHBhdGggZmlsbD0nYmxhY2snIGQ9J00xNTAsMCxMNzUsMjAwLEwyMjUsMjAwLFonPjwvcGF0aD48L3N2Zz4=
We can copy the encoded string and paste it into the address bar of the browser , To see the actual parsed SVG( At least in the Chrome It's like this ). That's what we're doing , We make it readable for browsers , In addition, we need to make it accessible to browsers , Blockchain is why we use abi.encodePacked() The reason for further coding it .
Notice the first of the strings ( bold ). That is to say data It's a imagefromsvg+xml Type of coding base64.

There is no problem for the browser to read it
Yes TokenURI Encoding
TokenURI( Usually ) It refers to JSON Link to file . however , Like a picture , We can also JSON Document carried out base64 code , And use the correct prefix to make it readable in the browser . We didn't use SVG Prefix , But use data:application/json;base64 To let the browser know that it is a base64 Coded json file .
We can also do that Solidity Create the entire file . We code it , Recode , Recode . Although this code looks interesting , But it's neat , But it applies to OpenSea.
function formatTokenURI(string memory _imageURI, string memory _name, string memory _description, string memory _properties) public pure returns (string memory) {
return string(
abi.encodePacked(
"data:application/json;base64,",
Base64.encode(
bytes(
abi.encodePacked(
'{"name":"', _name,
'", "description": "', _description, '"',
', "attributes": ', _properties,
', "image":"', _imageURI, '"}'
)
)
)
)
);
}
As a parameter , I use tokenURI Have different properties , And use encodepack As a way to paste all strings together ( because JSON Can be used as a string ).
Be careful :_properties Value is a string that can be an array , such as “[1,2,3]”.
We use the imported library to finally JSON Encoded as base64, And use... Again encodepack Prefix it , Make it available to browsers , Like it's a routine URL equally .
ContractURI?
In addition to giving each token information TokenURI outside , We also have ContractURI. It provides overall information about the collection ,OpenSea also ( Up to a point ) Support this . Except for the contract address , We don't need to be in OpenSea Add anything manually in .
I code it with the following code . It is similar to TokenURI, But there are fewer options . I just put the whole ContractURI JSON Pass as a string .
function setContractURI(string memory contractURI_) public onlyOwner() {
_contractURI = string(abi.encodePacked(
"data:application/json;base64,",
Base64.encode(
bytes(
contractURI_
)
)
));
}
Be careful :OpenSea Information will not be collected from blockchain updates , So add it to OpenSea It needs to be set once before !
Forge new tokens
Because everything is on the chain , So more information is really needed to forge new tokens . We also need to set all the information ( Or later setTokenURI() Function to set ), Instead of simply checking the price or creating a new token .
function safeMint(string memory _source, string memory _name, string memory _description, string memory _properties) public onlyOwner() {
_safeMint(msg.sender, _tokenIdCounter.current());
string memory imageURI = svgToImageURI(_source);
_setTokenURI(_tokenIdCounter.current(), formatTokenURI(imageURI, _name, _description, _properties));
emit tokenChanged(_tokenIdCounter.current());
_tokenIdCounter.increment();
}
therefore , We sent more information and safeMint function , Such as _description、_name、_source (SVG) and _properties, Then we all use the functions described above to code them .
Conclusion
Although creating an online collection looks cool , But be careful , For those who will make tokens , It's very cheap . We must submit more data to the blockchain , This will consume a lot of gas.
Source:https://medium.com/quick-programming/how-to-implement-fully-on-chain-nft-contracts-8c409acc98b7
About
ChinaDeFi - ChinaDeFi.com It's a research driven DeFi Innovation organizations , We are also a blockchain development team . From all over the world every day 500 Close to a good source of information 900 In the content , Looking for deeper thinking 、 Sort out more systematic content , Provide decision-making assistant materials to the Chinese market at the fastest speed .
Layer 2 friends sharing same hobby - Welcome to Layer 2 Interested blockchain technology enthusiasts 、 Study and analyze people and Gavin( WeChat : chinadefi) contact , Discuss together Layer 2 Landing opportunities . Please pay attention to our official account of WeChat “ Decentralized financial community ”.

边栏推荐
- Pecan — Overview
- 2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)
- Analyse du Code du planificateur ego bspline Section Optimizer (1)
- Zhang Fei hardware 90 day learning notes - personal records on day 4, please see my personal profile / homepage for the complete
- Bad mentality leads to different results
- 利用可视化结果,点击出现对应的句子
- EGO Planner代碼解析bspline_optimizer部分(1)
- Luogu-p1107 [bjwc2008] Lei Tao's kitten
- Zhang Fei hardware 90 day learning notes - personal records on day 2, please see my personal profile / homepage for the complete
- How does if ($variable) work? [repeat] - how exactly does if ($variable) work? [duplicate]
猜你喜欢

If the warehouse management communication is not in place, what problems will occur?

What does a really excellent CTO look like in my eyes

Detailed explanation of shuttle unity interworking principle

第一章:求所有阶乘和数,大奖赛现场统分程序设计,三位阶乘和数,图形点扫描,递归求n的阶乘n!,求n的阶乘n!,舍罕王失算

Ego planner code parsing Bspline_ Optimizer section (3)

Chapter 1: extend the same code decimal sum s (D, n)

These problems should be paid attention to in the production of enterprise promotional videos

Common text labels

我们做了一个智能零售结算平台

我們做了一個智能零售結算平臺
随机推荐
[water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]
Chapitre 1: le roi de shehan a mal calculé
【LeetCode】【SQL】刷题笔记
Pecan — Overview
Analyse du Code du planificateur ego bspline Section Optimizer (1)
Why should the gradient be manually cleared before back propagation in pytorch?
Use unique_ PTR forward declaration? [repetition] - forward declaration with unique_ ptr? [duplicate]
Redis master-slave synchronization, clustering, persistence
How to build an efficient information warehouse
The earliest record
Zhang Fei hardware 90 day learning notes - personal record on day 6. Please see my personal profile / homepage for the complete record
Comments on flowable source code (37) asynchronous job processor
Pecan - route
04 -- QT OpenGL two sets of shaders draw two triangles
Streaming media server (16) -- figure out the difference between live broadcast and on-demand
第一章:求n的阶乘n!
第一章: 舍罕王失算
【数学建模】基于matlab船舶三自由度MMG模型【含Matlab源码 1925期】
Free sharing | linefriends hand account inner page | horizontal grid | not for sale
Buuctf's different flags and simplerev