BNB Smart Chain: BEP-1155 contract with Truffle and OpenZeppelin
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.
- Interact with the deployed contract.
Prerequisites
- Chainstack account to deploy a BNB Smart Chain node.
- Truffle Suite to create and deploy contracts.
- OpenZeppelin Contracts to use the audited ERC-1155 libraries to create your BEP-1155 contract.
Overview
To get from zero to a deployed BEP-1155 contract on the BNB Smart Chain testnet, do the following:
- With Chainstack, create a public chain project.
- With Chainstack, join the BNB Smart Chain testnet.
- With Chainstack, access your BNB Smart Chain node credentials.
- With OpenZeppelin, create a BEP-1155 contract.
- With Truffle, compile and deploy the contract through your BNB Smart Chain node.
Step-by-step
Create a public chain project
See Create a project.
Join the BNB Smart Chain testnet
Get your BNB Smart Chain node access and credentials
See View node access and credentials.
Install OpenZeppelin Contracts
Install Truffle Suite
See Truffle Suite: Installation.
Create the contract
-
In your contract directory, initialize Truffle:
truffle init
This will generate the Truffle boilerplate structure:
. ├── contracts │ └── .gitkeep ├── migrations │ └── .gitkeep ├── test │ └── .gitkeep └── truffle-config.js
-
Go to the
contracts
directory. In the directory, create your BEP-1155 contract:BNBSmartChain1155.sol
.//SPDX-License-Identifier: MIT pragma solidity ^0.8; import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; contract BNBSmartChain1155 is ERC1155 { uint256 public constant FUNGIBLE = 0; uint256 public constant NON_FUNGIBLE = 1; constructor() ERC1155("JSON_URI") { _mint(msg.sender, FUNGIBLE, 100, ""); _mint(msg.sender, NON_FUNGIBLE, 1, ""); } }
The contract implementation is the following:
- The contract uses OpenZeppelin audited ERC-1155 contract templates.
- The contract creates two tokens: 100 fungible units of the currency called
FUNGIBLE
and 1 non-fungible unit calledNON-FUNGIBLE
. In the BEP-1155 standard, setting a token issuance to1
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 themigrations
directory.module.exports = function(deployer) { var BNBSmartChain1155 = artifacts.require("./BNBSmartChain1155.sol"); deployer.deploy(BNBSmartChain1155); };
This will create the contract deployment instructions for Truffle.
Compile and deploy the contract
-
Install
HDWalletProvider
.HDWalletProvider is Truffle's separate npm package used to sign transactions.
Run:
npm install @truffle/hdwallet-provider
-
Edit
truffle-config.js
to add:-
HDWalletProvider
-
Your BNB Smart Chain node access and credentials
-
Your BNB Smart Chain account that you will use to deploy the contract
const HDWalletProvider = require("@truffle/hdwallet-provider"); const private_key = 'PRIVATE_KEY'; module.exports = { networks: { testnet: { provider: () => new HDWalletProvider(private_key, "YOUR_CHAINSTACK_ENDPOINT"), network_id: 97, confirmations: 3, timeoutBlocks: 200, skipDryRun: true } }, compilers: { solc: { version: "0.8.2", } } };
where
testnet
— any network name that you will pass to thetruffle 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.
- YOUR_CHAINSTACK_ENDPOINT — your Chainstack node HTTPS endpoint. See also View node access and credentials and BNB Smart Chain tooling.
network_id
— the network ID of the BNB Smart Chain network: mainnet is56
, testnet is97
.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 intruffle-config.js
.
Interact with the contract
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.
Flatten your contract code
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.
-
Install Truffle Flattener.
Run:
npm install truffle-flattener
-
Flatten the contract.
In the
contracts
directory, run:npx truffle-flattener BNBSmartChain1155.sol > FlatBNBSmartChain1155.sol
-
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.
Verify the deployed contract on BscScan
At this point, you have your flattened and cleaned-up contract ready for the BscScan verification.
- Go to BscScan testnet.
- 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.
- In Open Source License Type, select MIT License (MIT).
- 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.
Check the balance
Now that your BEP-1155 contract is verified, you can check your balance of the issued tokens on BscScan.
- On BscScan, on your contract, click Contract.
- Click Read Contract.
- Scroll to the balanceOf field.
- In the account field, provide the address of the account you used to deploy the contract.
- In the id field, put
0
to check your fungible balance and put1
to check your non-fungible balance.
Conclusion
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.
About the author
Updated 8 months ago