Gaming Lifehack: How to integrate an ERC-20 into the ChainSafe Gaming SDK
Check out our previous Gaming Lifehack series blog post on NFT multicalls here.
The ChainSafe Gaming SDK is an open-source library that connects games built with the Unity game development engine to the blockchain. The library currently supports WebGL and iOS/Android (experimental), and is compatible with most EVM-based chains such as Ethereum, Polygon, and Binance Smart Chain. It is the first fully agnostic SDK in the Web3 gaming paradigm. Dive into the code in our Github repo, read more from our official announcement, and join the thriving conversation over in the #🕹gaming-general channel in our Discord.
In the following introduction and tutorial, you will learn how to create a standard ERC-20 based token using Remix (an Ethereum-based online editor) and integrate it into the ChainSafe Gaming SDK. For context, we’ll start with a deeper look at what an ERC-20 token is.
ERC-20, a short history
First implemented by Fabian Vogelsteller and Vitalik Buterin in 2015, The ERC-20 token standard is the base utility that governs the Ethereum ecosystem. ERC-20 allows for a standard API or application programming interface. If other developers follow this implementation, they will be recognized by the Ethereum blockchain and other ERC-20’s. There are, of course, other token standards, but ERC-20 is the most widely used — giving wallets, exchanges, and developers a simplified entryway into the blockchain ecosystem.
What’s an ERC-20 token?
The Ethereum system is based on the concept of tokens that can be bought, sold, and traded. Tokens can be created to perform any utility within the ecosystem. In essence, Ethereum tokens are smart contracts that live on the Ethereum blockchain.
The most famous example of a token implementation is the aforementioned ERC-20. But there are other standards, including the similarly famous ERC-721 and ERC-1155 standards, which underpin most NFT’s today.
The best way to think about ERC-20 is as a blueprint. If other developers follow this blueprint, their ERC-20 tokens will be interchangeable. Therefore, anyone who follows the standard has a consistent general-purpose implementation across the ecosystem.
The ERC-20 blueprint
There are over 400,000 (!) ERC-20-compatible tokens within the Ethereum ecosystem. Developers of ERC-20 tokens need to implement a particular set of six standards for the token to be ERC-20 compliant, as listed and explained below:
- totalSupply = This returns the total token supply of the implemented ERC-20.
- balanceOf = This returns the account balance of another account with address _owner, which means the user with the current balance.
- allowance = This returns the amount which _spender is still allowed to withdraw from _owner.
- transfer = This transfers _value amount of tokens to address _to and MUST fire the Transfer event. The function SHOULD throw if the message caller’s account balance does not have enough tokens to spend.
- approve = This allows _spender to withdraw from your account multiple times, up to the _value amount. If this function is called, again, it overwrites the current allowance with _value.
- transferFrom = This transfers _value amount of tokens from address _from to address _to, and MUST fire the Transfer event.
These functions ensure that all Ethereum tokens are consistent in format and performance across the ecosystem. Learning how ERC-20 tokens work is therefore fundamental to developing on Ethereum.
So, how might a blockchain game use an ERC-20 token? Consider the example of a platform game like Super Mario. As you complete a level, you collect coins, which then get sent to your wallet at the end as a play-to-earn reward mechanism.
In this instance, you could also create in-game (or cross-game) economics by allowing a user to take these rewards and purchase NFT’s (ERC-721 or ERC-1155 tokens) in the form of new abilities, items, skins, or whatever strikes your fancy.
Creating an ERC-20 smart contract and integrating it into the ChainSafe Gaming SDK
Luckily for us, the fantastic team at OpenZeppelin has done the hard work of implementing the ERC-20 standard in composable smart contracts. They‘ve provided interfaces and even wizards to get us started with a base contract. In this case, a lot is going on under the hood, but that’s out of scope for this article.
However, in the following video, we will show you how to use OpenZeppelin’s wizard to get to base ERC-20. We will then compile the smart contract, access its contract address, integrate it into the ChainSafe Gaming SDK, and transfer the tokens to another account!
We hope this intro tutorial is helpful for gaming developers looking to integrate the ERC-20 token standard into their blockchain game using the ChainSafe Gaming SDK!
Got further questions? Hop into our Discord and join an active community of web3 game devs in the #gaming-general channel. Or, if you’ve got a project you’d like to show off, make sure to share it on our #gaming-showcase channel!
About ChainSafe
ChainSafe is a leader in blockchain development and infrastructure solutions for Web3. We’re working toward a more decentralized future by building client implementations for Ethereum, Filecoin, Polkadot, and Mina. We’re also maintaining the Web3.js library, working on a Gaming SDK that connects NFTs to Unity, building a distributed cloud storage application, and bootstrapping a multi-chain bridge. To learn more, click here.
Want to help build the foundations of Web3? Join us!
Have a question, comment, or suggestion? Drop into our Discord and join the conversation! We’re also always looking for talented people. Check out our open positions and get in touch ➡️➡️ careers@chainsafe.io