Impluse News
  • Home
  • News
  • Politics
  • Business
  • National
  • Culture
  • Opinion
  • Lifestyle
  • Sports
No Result
View All Result
  • Home
  • News
  • Politics
  • Business
  • National
  • Culture
  • Opinion
  • Lifestyle
  • Sports
No Result
View All Result
Impluse Logo
No Result
View All Result
Home NFT

Step-by-step instructions on how to create an NFT collection on Golang + Ethereum

by Impluse News Desk
February 12, 2022
in NFT
0
nft
7.9k
SHARES
788.7k
VIEWS
Share on FacebookShare on Twitter

In my subjective opinion, the main message of NFT tokens is that no one can ever change the token and its contents, and accordingly, during development, we should be as abstract as possible from centralized systems. That is why we will publish all our files in decentralized storage – IPFS. 

So, to ensure that our content stays persisted (pinned), we need to run our own IPFS nodes. Of course, we can set up IPFS nodes ourselves, but it is much more convenient to use a ready-made service, such as Pinata. The following article will focus on how to work with the Pinata service for publishing NFT media files.

According to the documentation, we need to call the pinFileToIPFS endpoint to pin the files. Let’s write the code that actually does this:

const (
	pinFileURL = "https://api.pinata.cloud/pinning/pinFileToIPFS"
)

func (s *service) pinFile(fileName string, data []byte, wrapWithDirectory bool) (string, error) {
   type pinataResponse struct {
       IPFSHash  string `json:"IpfsHash"`
       PinSize   int    `json:"PinSize"`
       Timestamp string `json:"Timestamp"`
   }
 
   bodyBuf := &bytes.Buffer{}
   bodyWriter := multipart.NewWriter(bodyBuf)
 
   // this step is very important
   fileWriter, err := bodyWriter.CreateFormFile("file", fileName)
   if err != nil {
       return "", err
   }
   if _, err := fileWriter.Write(data); err != nil {
       return "", err
   }
 
   // Wrap your content inside of a directory when adding to IPFS. 
   // This allows users to retrieve content via a filename instead of just a hash.
   if wrapWithDirectory {
       fileWriter, err = bodyWriter.CreateFormField("pinataOptions")
       if err != nil {
           return "", err
       }
       if _, err := fileWriter.Write([]byte(`{"wrapWithDirectory": true}`)); err != nil {
           return "", err
       }
   }
 
   contentType := bodyWriter.FormDataContentType()
   bodyWriter.Close()
 
   req, err := http.NewRequest("POST", pinFileURL, bodyBuf)
   if err != nil {
       return "", err
   }
 
   req.Header.Set("Content-Type", contentType)
   req.Header.Set("pinata_api_key", s.params.APIKey)
   req.Header.Set("pinata_secret_api_key", s.params.SecretKey)
 
   // Do request.
   var (
       retries = 3
       resp    *http.Response
   )
   for retries > 0 {
       resp, err = s.client.Do(req)
       if err != nil {
           retries -= 1
       } else {
           break
       }
   }
   if resp == nil {
       return "", fmt.Errorf("Failed to upload files to ipfs, err: %v", err)
   }
   defer resp.Body.Close()
   if resp.StatusCode != http.StatusOK {
       errMsg := make([]byte, resp.ContentLength)
       _, _ = resp.Body.Read(errMsg)
       return "", fmt.Errorf("Failed to upload file, response code %d, msg: %s", resp.StatusCode, string(errMsg))
   }
   body, err := ioutil.ReadAll(resp.Body)
   if err != nil {
       return "", err
   }
   pinataResp := pinataResponse{}
   err = json.NewDecoder(bytes.NewReader(body)).Decode(&pinataResp)
   if err != nil {
       return "", fmt.Errorf("Failed to decode json, err: %v", err)
   }
   if len(pinataResp.IPFSHash) == 0 {
       return "", errors.New("Ipfs hash not found in the response body")
   }
   return pinataResp.IPFSHash, nil
}

Looking at the code in detail, you can see that in order to call pinata endpoints, you need to get an API key + secret key.

On a successfully uploaded file, we will get an IPFS hash of the file, of the following form: QmPbxeGcXhYQQNgsC6a36dDyYUcHgMLnGKnF8pVFmGsvqi

Now that we have a method for uploading files to IPFS – pinFilewe need to create another method that will:

  1. read *.png file from the specified directory
  2. upload *.png file to IPFS
  3. read *.json file with a description of attributes (traits)
  4. create final *.json file with the description of ERC-721 metadata

So let’s get started writing the code:

Tags: create NFTnftnft news

Impluse News Desk

Related Posts

NFT
NFT

NFT pictures will be hacked at The Standoff

February 15, 2022
Analysis of the tasks of the contest for hacking NFT “The Standoff Digital Art”
NFT

Analysis of the tasks of the contest for hacking NFT “The Standoff Digital Art”

February 14, 2022
NFT
NFT

Step-by-step instructions on how to create an NFT collection on Golang + Ethereum (part 1)

February 13, 2022
Next Post
cryptocurrency

Authorities estimate the domestic cryptocurrency market at $214 billion

NFT

Step-by-step instructions on how to create an NFT collection on Golang + Ethereum (part 1)

A few words about Solana and ed25519

A few words about Solana and ed25519

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow Us

Recommended

Blockchain

Blockchain compatibility: a little background

5 months ago
Zhivoder

Karateka Zhivoder accused of beating a 9-month-old baby

5 months ago
Yagudin

“You are a young father, you need strength. And I’m in the sauna! ”: Yagudin mocked 48-year-old Averbukh

5 months ago
Mishustin: In the next 4 years, 150 billion ruble

Mishustin: In the next 4 years, 150 billion rubles will be allocated for the modernization of housing and communal services

5 months ago

Instagram

    Please install/update and activate JNews Instagram plugin.

Categories

  • Business
  • Crypto
  • Culture
  • Featured
  • Lifestyle
  • National
  • News
  • NFT
  • Opinion
  • Politics
  • Sports
  • Travel
  • Uncategorized
  • World

Topics

Beijing Beijing Olympics bejing Blockchain crypto Budget 2022 centralization crypto nft corona coronavirus covid covid 19 crypto crypto currency crypto news crypto stored defi blockchain Dmitry game news ice hockey implusenews impluse news Krasnodar moscow Nepryaeva news nft nft and metaverse nft news nft project nft token nft use olympic olympics omicron Poklonskaya Putin \ russia Russian scientist skaters st petersburg ukraine Ukrainian United States Ural dumplings Zakharova
No Result
View All Result

Highlights

कामगारों के लिए ई-श्रम पोर्टल पर रजिस्ट्रेशन की हुई शुरूआत, जानिए कैसे कर सकते हैं रजिस्ट्रेशन

Savor The Cryptocurrency Experience Fully With Crypto Mufasa.

Technology, business, and why freelancers have emerged

Williemgc exceeds 1 million subscribers on YouTube

Cryptocurrency vindication. Arbitrage practice

NFT pictures will be hacked at The Standoff

Trending

Soni Mehta: Acing The World of Dance and Entrepreneurship.
Lifestyle

Soni Mehta: Acing The World of Dance and Entrepreneurship.

by Himanshu Mahawar
May 7, 2022
0

Soni Mehta is a young influencer, professional dancer, choreographer and entrepreneur from Punjab. She has been staying...

ForexGlo

ForexGlo: The young musical artist has shown his A-game through each of his tracks.

April 11, 2022
Shabaz Zamani

Spell bounding millions of listeners around the world with his melodious voice and catchy songs is Shabaz Zamani.

April 6, 2022
कामगारों के लिए ई-श्रम पोर्टल पर रजिस्ट्रेशन की हुई शुरूआत, जानिए कैसे कर सकते हैं रजिस्ट्रेशन

कामगारों के लिए ई-श्रम पोर्टल पर रजिस्ट्रेशन की हुई शुरूआत, जानिए कैसे कर सकते हैं रजिस्ट्रेशन

March 9, 2022
Savor The Cryptocurrency Experience Fully With Crypto Mufasa.

Savor The Cryptocurrency Experience Fully With Crypto Mufasa.

February 28, 2022

Impluse News

Impluse reports news on the latest technology, science, and business developments, their interactions with other industries.

Recent News

  • Soni Mehta: Acing The World of Dance and Entrepreneurship. May 7, 2022
  • ForexGlo: The young musical artist has shown his A-game through each of his tracks. April 11, 2022
  • Spell bounding millions of listeners around the world with his melodious voice and catchy songs is Shabaz Zamani. April 6, 2022

Categories

  • Business
  • Crypto
  • Culture
  • Featured
  • Lifestyle
  • National
  • News
  • NFT
  • Opinion
  • Politics
  • Sports
  • Travel
  • Uncategorized
  • World
  • About Us
  • Contact
  • Privacy Policy
  • Terms and Conditions

© 2022 IMPLUSE NEWS

No Result
View All Result
  • Home
  • Politics
  • News
  • Business
  • Culture
  • National
  • Sports
  • Lifestyle
  • Travel
  • Opinion

© 2022 IMPLUSE NEWS