BNB Smart Chain: BEP-1155 with Truffle & OpenZeppelin
Learn to create BEP-1155 multi-token contracts with Truffle and OpenZeppelin on BNB Smart Chain. Deploy fungible and non-fungible tokens using Chainstack nodes.
TLDR:
You’ll create a BEP-1155 contract combining fungible and non-fungible tokens in one deployment.
You’ll rely on OpenZeppelin libraries and Truffle to compile, deploy, and verify your tokens on BNB Smart Chain testnet via Chainstack.
You’ll flatten your code for a smooth verification on BscScan.
By the end, you’ll have fungible tokens (0) and NFT tokens (1) all in a single smart contract.
BEP-1155 is the multi-token standard for smart contracts that combines the fungibility of BEP-20 and the non-fungibility of BEP-721 in one contract.With a single BEP-1155 contract, you can deploy an ecosystem that has both fungible tokens (currency) and non-fungible tokens (NFTs).In this tutorial, you will:
Create a BEP-1155 contract that has a supply of fungible tokens and one non-fungible token.
Deploy the contract on the BNB Smart Chain testnet through a node deployed with Chainstack.
The contract creates two tokens: 100 fungible units of the currency called FUNGIBLE and 1 non-fungible unit called NON-FUNGIBLE. In the BEP-1155 standard, setting a token issuance to 1 makes it non-fungible.
The contract also has JSON_URI which is a locator for the metadata of your tokens hosted externally. For example, <https://token-cdn-domain/{id}.json>. See EIP-1155 for details.
Create 2_deploy_contracts.js in the migrations directory.
testnet — any network name that you will pass to the truffle migrate --network command.
HDWalletProvider — Truffle’s custom provider to sign transactions.
PRIVATE_KEY — the private key of your BNB Smart Chain account that will deploy the contract. The account must have enough BNB funds to run the deployment. See also BNB Smart Chain Faucet.
network_id — the network ID of the BNB Smart Chain network: mainnet is 56, testnet is 97.
solc — the Solidity compiler version that Truffle must use. OpenZeppelin contracts have a higher version Solidity compiler requirement than the default Truffle installation, hence you must provide a specific compiler version.
Run:
truffle migrate --network testnet
This will engage 2_deploy_contracts.js and deploy the contract to the BNB Smart Chain testnet as specified in truffle-config.js.
Once your contract is deployed, you can view it online at BscScan testnet.Network explorers, including BscScan, do not display the NFT standards by default, so you will have to perform additional steps to check the balance of your issued tokens. Namely, you must verify the contract on BscScan to interact with it online.
Since your BEP-1155 contract uses imported OpenZeppelin libraries, you must put all the imports into one .sol file to make BscScan be able to verify it.
Clean up the licensing information.The flattened contract will have the same licensing note imported from each of the files. Multiple licensing notes in one file break the BscScan verification, so you have to leave one licensing note for the entirety of the flattened contract.The easiest way to clean up is to search for the SPDX mentions in the file and remove all of them except for the very first one.
Find your deployed contract. The address of your contract should have been printed by Truffle at the end of the deployment in the contract address field.
On the contract page on BscScan, click Contract > Verify and Publish.
In Compiler Type, select Solidity (Single file).
In Compiler Version, select v0.8.2. This is the version this tutorial used to compile the contract.
Click Continue.
Keep the Optimization option set to No as Truffle does not use optimization by default.
Paste the entirety of your flattened .sol contract in the Enter the Solidity Contract Code below field.
Click Verify and Publish.
BscScan will take a few seconds to compile your contract, verify, and publish it.
This tutorial guided you through the basics of creating and deploying a contract in the BEP-1155 multi-token standard. The BEP-1155 is useful in that it can deploy an ecosystem of currencies and NFTs in one go—you can add however many fungible and non-fungible tokens to your contract.You also verified your deployed contract online and interacted with it.This tutorial uses testnet, however, the exact same instructions and sequence will work on the mainnet as well.