> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainstack.com/llms.txt
> Use this file to discover all available pages before exploring further.

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://docs.chainstack.com/feedback

```json
{
  "path": "/docs/bsc-tutorial-bep-1155-contract-with-truffle-and-openzeppelin",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# 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.

## Main article

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](https://console.chainstack.com/) to deploy a [reliable BNB Smart Chain RPC endpoint](https://chainstack.com/build-better-with-bnb-smart-chain/).
* [Truffle Suite](https://trufflesuite.com/) to create and deploy contracts.
* [OpenZeppelin Contracts](https://docs.openzeppelin.com/contracts/4.x/) to use the audited [ERC-1155 libraries](https://docs.openzeppelin.com/contracts/4.x/erc1155) 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:

1. With Chainstack, create a <Tooltip tip="public chain project- a project to join public networks">public chain project</Tooltip>.

2. With Chainstack, join the BNB Smart Chain testnet.

3. With Chainstack, access your BNB Smart Chain node credentials.

4. With OpenZeppelin, create a BEP-1155 contract.

5. 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](/docs/manage-your-project#create-a-project).

### Join the BNB Smart Chain testnet

See [Join a public network](/docs/manage-your-networks).

### Get your BNB Smart Chain node access and credentials

See [View node access and credentials](/docs/manage-your-node#view-node-access-and-credentials).

### Install OpenZeppelin Contracts

See [OpenZeppelin Contracts](https://docs.openzeppelin.com/contracts/4.x/).

### Install Truffle Suite

See [Truffle Suite: Installation](https://trufflesuite.com/docs/truffle/how-to/install/).

### Create the contract

1. In your contract directory, initialize Truffle:

   <CodeGroup>
     ```bash Shell theme={"system"}
     truffle init
     ```
   </CodeGroup>

   This will generate the Truffle boilerplate structure:

   <CodeGroup>
     ```bash Shell theme={"system"}
     .
     ├── contracts
     │   └── .gitkeep
     ├── migrations
     │   └── .gitkeep
     ├── test
     │   └── .gitkeep
     └── truffle-config.js
     ```
   </CodeGroup>

2. Go to the `contracts` directory. In the directory, create your BEP-1155 contract: `BNBSmartChain1155.sol`.

   <CodeGroup>
     ```solidity solidity theme={"system"}
     //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, "");
       }
     }
     ```
   </CodeGroup>

   The contract implementation is the following:

   * The contract uses OpenZeppelin audited [ERC-1155 contract templates](https://docs.openzeppelin.com/contracts/4.x/erc1155).
   * 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](https://github.com/ethereum/eips/issues/1155) for details.

3. Create `2_deploy_contracts.js` in the `migrations` directory.

   <CodeGroup>
     ```js JavaScript theme={"system"}
     module.exports = function(deployer) {
     var BNBSmartChain1155 = artifacts.require("./BNBSmartChain1155.sol");
       deployer.deploy(BNBSmartChain1155);
     };
     ```
   </CodeGroup>

   This will create the contract deployment instructions for Truffle.

### Compile and deploy the contract

1. Install `HDWalletProvider`.

   [HDWalletProvider](https://github.com/trufflesuite/truffle/tree/develop/packages/hdwallet-provider) is Truffle's separate npm package used to sign transactions.

   Run:

   <CodeGroup>
     ```bash Shell theme={"system"}
     npm install @truffle/hdwallet-provider
     ```
   </CodeGroup>

2. 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

     <CodeGroup>
       ```js JavaScript theme={"system"}
       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",
          }
        }
       };
       ```
     </CodeGroup>

   where

   * `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](https://faucet.chainstack.com/bnb-testnet-faucet).
   * YOUR\_CHAINSTACK\_ENDPOINT — your Chainstack node HTTPS endpoint. See also [View node access and credentials](/docs/manage-your-node#view-node-access-and-credentials) and [BNB Smart Chain tooling](/docs/bsc-tooling).
   * `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.

3. Run:

   <CodeGroup>
     ```bash Shell theme={"system"}
     truffle migrate --network testnet
     ```
   </CodeGroup>

   This will engage `2_deploy_contracts.js` and deploy the contract to the BNB Smart Chain testnet as specified in `truffle-config.js`.

### Interact with the contract

Once your contract is deployed, you can view it online at [BscScan testnet](https://testnet.bscscan.com/).

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.

1. Install [Truffle Flattener](https://www.npmjs.com/package/truffle-flattener).

   Run:

   <CodeGroup>
     ```bash Shell theme={"system"}
     npm install truffle-flattener
     ```
   </CodeGroup>

2. Flatten the contract.

   In the `contracts` directory, run:

   <CodeGroup>
     ```bash Shell theme={"system"}
     npx truffle-flattener BNBSmartChain1155.sol > FlatBNBSmartChain1155.sol
     ```
   </CodeGroup>

3. 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.

1. Go to [BscScan testnet](https://testnet.bscscan.com/).

2. 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.

3. On the contract page on BscScan, click **Contract** > **Verify and Publish**.

4. In **Compiler Type**, select **Solidity (Single file)**.

5. In **Compiler Version**, select **v0.8.2**. This is the version this tutorial used to compile the contract.

6. Click **Continue**.

7. Keep the **Optimization** option set to **No** as Truffle does not use optimization by default.

8. Paste the entirety of your flattened `.sol` contract in the **Enter the Solidity Contract Code below** field.

9. 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.

1. On BscScan, on your contract, click **Contract**.

2. Click **Read Contract**.

3. Scroll to the **balanceOf** field.

4. In the **account** field, provide the address of the account you used to deploy the contract.

5. In the **id** field, put `0` to check your fungible balance and put `1` 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

<CardGroup>
  <Card title="Ake">
    <img src="https://mintcdn.com/chainstack/UN3rP7zhB69idvnC/images/docs/profile_images/1719912994363326464/8_Bi4fdM_400x400.jpg?fit=max&auto=format&n=UN3rP7zhB69idvnC&q=85&s=792a24ab1b4682406fa589c0ecd88e5d" alt="Ake" style={{width: '80px', height: '80px', borderRadius: '50%', objectFit: 'cover', display: 'block', margin: '0 auto'}} noZoom width="400" height="400" data-path="images/docs/profile_images/1719912994363326464/8_Bi4fdM_400x400.jpg" />

    <Icon icon="code" iconType="solid" /> Director of Developer Experience @ Chainstack
    <br /><Icon icon="screwdriver-wrench" iconType="solid" /> Talk to me all things Web3
    <br />20 years in technology | 8+ years in Web3 full time years experience

    <div style={{display: "flex", justifyContent: "center", gap: "12px"}}>
      <a href="https://github.com/akegaviar/" style={{textDecoration: "none", borderBottom: "none"}}>
        <Icon icon="github" iconType="brands" />
      </a>

      <a href="https://twitter.com/akegaviar" style={{textDecoration: "none", borderBottom: "none"}}>
        <Icon icon="twitter" iconType="brands" />
      </a>

      <a href="https://www.linkedin.com/in/ake/" style={{textDecoration: "none", borderBottom: "none"}}>
        <Icon icon="linkedin" iconType="brands" />
      </a>
    </div>
  </Card>
</CardGroup>
