TLDR:
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:
To get from zero to a deployed BEP-1155 contract on the BNB Smart Chain testnet, do the following:
With Chainstack, create a .
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.
See Create a project.
See View node access and credentials.
See Truffle Suite: Installation.
In your contract directory, initialize Truffle:
This will generate the Truffle boilerplate structure:
Go to the contracts
directory. In the directory, create your BEP-1155 contract: BNBSmartChain1155.sol
.
The contract implementation is the following:
FUNGIBLE
and 1 non-fungible unit called NON-FUNGIBLE
. In the BEP-1155 standard, setting a token issuance to 1
makes it non-fungible.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.
This will create the contract deployment instructions for Truffle.
Install HDWalletProvider
.
HDWalletProvider is Truffle’s separate npm package used to sign transactions.
Run:
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
where
testnet
— any network name that you will pass to the truffle migrate --network
command.HDWalletProvider
— Truffle’s custom provider to sign transactions.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:
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.
Flatten the contract.
In the contracts
directory, run:
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.
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.
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.
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 put 1
to check your non-fungible balance.
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.
TLDR:
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:
To get from zero to a deployed BEP-1155 contract on the BNB Smart Chain testnet, do the following:
With Chainstack, create a .
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.
See Create a project.
See View node access and credentials.
See Truffle Suite: Installation.
In your contract directory, initialize Truffle:
This will generate the Truffle boilerplate structure:
Go to the contracts
directory. In the directory, create your BEP-1155 contract: BNBSmartChain1155.sol
.
The contract implementation is the following:
FUNGIBLE
and 1 non-fungible unit called NON-FUNGIBLE
. In the BEP-1155 standard, setting a token issuance to 1
makes it non-fungible.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.
This will create the contract deployment instructions for Truffle.
Install HDWalletProvider
.
HDWalletProvider is Truffle’s separate npm package used to sign transactions.
Run:
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
where
testnet
— any network name that you will pass to the truffle migrate --network
command.HDWalletProvider
— Truffle’s custom provider to sign transactions.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:
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.
Flatten the contract.
In the contracts
directory, run:
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.
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.
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.
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 put 1
to check your non-fungible balance.
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.