Decentralized category intro: NEAR and IPFS

Andrzej Mazur
Sep 7 · 5 min read

I know this comes out quite late as we’re close to the end of the competition already, but it’s better late than never, right? There’s still some time left if you’d like to try adding the decentralized ingredient to your compo entry.

This should give you a good intro as to how you can start with both NEAR and IPFS if you haven’t researched it already, as I know this was a bit of a struggle for those who are unfamiliar with the tech. After reading the contents below you should be able to explore the topic further on your own.

NEAR

The NEAR Protocol offers a JavaScript API. You can link to the file itself from your entry’s index.html file to use it (yes, in the Decentralized category external resources are allowed) - could be either any of the CDN instances:

<script src="https://cdn.jsdelivr.net/gh/nearprotocol/near-api-js/dist/near-api-js.js"></script>

Or our hosted version, up to you:

<script src="https://js13kgames.com/src/near-api-js.js"></script>

After getting the file loaded, the nearApi object in the window scope will be available for us already. The first thing to do with the NEAR API is to connect to the blockchain:

const near = await nearApi.connect({
nodeUrl: “https://rpc.testnet.near.org",
walletUrl: “https://wallet.testnet.near.org",
helperUrl: “https://helper.testnet.near.org",
explorerUrl: “https://explorer.testnet.near.org",
networkId: ‘testnet’,
keyStore: new window.nearApi.keyStores.BrowserLocalStorageKeyStore(window.localStorage, ‘triska’)
});

In this example we’re connecting to a testnet (a network where doing stuff is free). After establishing a successful connection, we can proceed with getting a user’s wallet containing their info:

const walletConnection = new nearApi.WalletConnection(near, ‘triska’)
if(walletConnection.isSignedIn()) {
let account = walletConnection.account();
}

If a user is already signed in, we will get their details in the account object: a username, balance, etc.

From now on you can proceed with your own logic: for example create smart contracts that will enable you to save highscores or user generated levels on the blockchain. You can implement global highscores, NFT prizes, or even an old school arcades using coins. Here are the resources you can follow:

- Near 101
- How to use NEAR in a game
- How to build a Play To Earn game on Near
- Examples and Docs
- NEAR Lands game
- BerryClub game

This should give you a good overview of what’s possible.

IPFS

While NEAR have a single JavaScript API, Protocol Labs offers a whole lot of tools and technologies for you to use. The most known are:

- IPFS - peer-to-peer hypermedia protocol
- Filecoin - decentralized storage network
- libp2p - modular networking stack
- Drand - distributed randomness beacon
- IPLD - decentralized data structure
- Web3 Storage and NFT Storage

And much more. As long as you use any of their tech, like host a game on IPFS, implement FIL coins, communicate using libp2p or add randomness with Drand you will be eligible to join the Protocol Labs challenge in the Decentralized category. All needed code and libraries can be referenced and linked from your entry the same way we did with near-api.js. The most creative implementations will get the most points and the highest chance of winning the challenge.

Make sure to start with this list, where you’ll find intros to IPFS, Filecoin, and more. Notable articles to check first:

- JavaScript IPFS
- Using IPFS in gaming
- Building peer-to-peer games on IPFS - a look at Interplanetary Tag
- Using IPFS distributed file storage for game asset metadata

The easiest thing to do would be to publish your game on IPFS through a service like Pinata, but there are so many other things you could do - see how others implemented decentralized features in the Gamedev.js Jam that happened a few months ago.

Examples from Gamedev.js Jam

Both NEAR and IPFS were implemented in some of the entries submitted to the Decentralized category of the Gamedev.js Jam 2021 in April. Notable examples include:

The game is decentralized, offering the option to log in with your NEAR Protocol wallet, and is being hosted on Protocol Labs’s IPFS.

This game is a multi-user virtual world in which players compete to become owners of the pixel art canvas (which is an actual NFT).

This game is more of a proof-of-concept for a project where players can create their own levels, publish them using NEAR and IPFS, and play others’ levels.

Full list can be found here.

There’s also the collection of games submitted to the ETH Global hackathon which you can look into as well - each and every one have their source code available on GitHub.

Next steps

I know this might be difficult to grasp at first, but we had similar situation with Web Monetization API introduced a few years ago, and now it feels like adding extra features for Coil-enabled users is rather a simple task to most of you. I do hope this will evolve in a similar way and we’ll be getting more and more cool and brilliantly creative entries utilizing decentralized technologies in the future.

Due to unforseen circumstances I wasn’t able to deliver a complete tutorial myself, but I hope the materials above will be enough for you to at least dip your toes in the topic. I do promise I’ll be exploring this further and you can expect more content coming from me in the next weeks.

Don’t hesitate to join our Slack - folks are more than happy to answer all your questions and help you with any issues you might have with your entry!

js13kGames

Js13kGames is a JavaScript coding competition for HTML5…

js13kGames

Js13kGames is a JavaScript coding competition for HTML5 Game Developers. The fun part of the compo is the file size limit set to 13 kilobytes. It runs between August 13th and September 13th, online since 2012.

Andrzej Mazur

Written by

HTML5 Game Developer, Enclave Games indie studio founder, js13kGames competition creator and Gamedev.js Weekly newsletter publisher.

js13kGames

Js13kGames is a JavaScript coding competition for HTML5 Game Developers. The fun part of the compo is the file size limit set to 13 kilobytes. It runs between August 13th and September 13th, online since 2012.