Skip to content

Dodging-Turtis/Turtis-API

 
 

Repository files navigation

Turtle API

A Custom API which returns random Turtle images along with some metadata through an IPFS Hash

About the API

The API is built using NodeJS and ExpressJS.

The API endpoint takes in 2 query parameters namely characterId(ID of the Turtle in the smart contract) and score(new high score that the user has achieved in the game).

The speed of the new Turtle NFT generated will depend on the new high score achieved and is scaled accordingly inside the game. The speed is also randomized to a certain extent.

The name of the new Turtle is being generated using the characterId.

The most interesting part of the API is the Random Turtle image generation part. The turtle is divided into 4 components such as Head, Legs, Shell and Tail. Everytime the API is called, the image-blend-randomize.js file returns a PNG which is a randomized combination of image collection in each component. The components are stored in the folder components. See the image below to understand it better.

Turtle

The NFT metadata is a JSON file which comprises of a name, description, image URL and some traits(speed of the Turtle in this case). This metadata is stored in Filecoin and IPFS using nft.storage. It is a free decentralized off-chain storage of NFT data. After uploading the JSON to nft.storage, an IPFS hash is returned which is returned by the API.

Chainlink Oracles are used to fetch the data using API calls. The smart contracts are deployed to Polygon Mumbai testnet and the suitable Oracle which is used to GET bytes32 data on-chain is Linkriver. Since bytes32 variable can only hold 32 characters of data, the first endpoint returns the first 31 characters of the IPFS hash. We had to optimize the API to create a second endpoint which contains the next 28 characters of the IPFS hash.

By this way, we can solve the problem of getting the IPFS hash from off-chain into on-chain using Chainlink by making 2 API calls which can further be used to set the Token URI for the NFT in the smart contract.

Finally, the API is hosted using Heroku which is a cloud platform. The base URL for the hosted API is https://images-blend.herokuapp.com/

Built With

  • nft.storge - Free Storage for NFTs
  • NodeJS - Node.js is an open-source, cross-platform, back-end JavaScript runtime environment
  • ExpressJS - Express.js is a back end web application framework for Node.js
  • Heroku - Heroku is a cloud platform which is used to host the API

Getting Started

These instructions will get you a copy of the project up and running on your local machine for testing purposes

Prerequisites

Please make sure you've already installed NPM package.

Installing

A step by step guide to locally run the API

  • First, get a free API key from nft.storage
  • Clone this repository:
git clone https://github.com/DineshBS44/images-blend
  • Install all the required packages using the following command
npm install
  • Create a .env file and store the following content in it and include API key of nft.storage:
NFT_STORAGE_API_KEY="<nft.storage API Key>"
  • To test the API endpoint locally, use the following command:
node server.js

Libraries/services used

  • nft.storage - To store NFT's metadata to Filecoin and IPFS
  • images - Used to blend different image components to return a new PNG file
  • express - backend web framework for Node JS
  • dotenv - To manage data in .env files
  • node-fetch - A light weight module that brings Fetch API to Node JS

Developers

License

Licensed under MIT License : https://opensource.org/licenses/MIT



About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%