Home » Programação » Publishing Smart Contract on Binance Network (BSC) / Ropsten Ethereum with Truffle

Publishing Smart Contract on Binance Network (BSC) / Ropsten Ethereum with Truffle

Compartilhe

Hey guys!

I received some messages after the publication of my other article Creating the first dApp on the blockchain asking how what do you do to publish on an “official” network, for example, Binance Smart Chain (BSC) or Ethereum, well, basically it’s very simple when we use Truffle, come with me and I’ll explain in this post.

Using our previous project https://github.com/over12/hello -worldblockchain let’s instantiate node and install 3 plugins.

Believing you followed the steps in the first article I made, type the following command in the terminal inside your project folder.

$ npm init -y

Next we will install 3 plugins with the following command

$ npm install @truffle/hdwallet-provider fs dotenv

If you cloned the project that is on github, the only thing you need to do in this case is to use the command

$ npm install

first part done.
Let’s now create 2 files in the root folder of your project, a file called .mnemonic and .env the final result of your folder should be the same to the print below.

Inside the .env file you will add the following value

ACCOUNT_MAIN="YOUR_WALLET_METAMASK_ADDRESS"

In the file .mnemonic you will put the 12 words that access your Metamask wallet, this point is important because Truffle will implement your contract using the your account, I don’t know if you remember or if you read the first old one I posted, but in the case when we use localhost (ganache), the first ganache account is automatically used as “owner” of the contract that was implemented, however, when we publish on the networks “official”, we need to have our own real account, and for that, comes this configuration that we are quoting.

Of course, to publish on the network you will need to have money (network currency), for example, Binance uses BNB, Ethereum uses ETH and so on.
The value usually varies, on test networks the value is very low, on the official network it usually varies from 1$ to 2$ (I never paid more than that).

Now, let’s go to the file truffle-config.js, at the beginning of the file paste the following code

require('dotenv').config();
const HDWalletProvider = require('@truffle/hdwallet-provider');
const fs = require('fs');
const mnemonic = fs.readFileSync(".mnemonic").toString().trim();

As I said before, truffle will use the HDWalletProvider plugin to manage our account and publish our contract on the network. Still in the file truffle-config.js let’s now add 3 network configurations, copy and paste the content below the development object

testnet: {
      provider: () => new HDWalletProvider(mnemonic, `https://data-seed-prebsc-1-s1.binance.org:8545`),
      network_id: 97,
      confirmations: 10,
      timeoutBlocks: 200,
      skipDryRun: true,
      from : process.env.ACCOUNT_MAIN
    },
    bsc: {
      provider: () => new HDWalletProvider(mnemonic, `https://bsc-dataseed1.binance.org`),
      network_id: 56,
      confirmations: 10,
      timeoutBlocks: 200,
      skipDryRun: true,
      from : process.env.ACCOUNT_MAIN
    },
    ropsten: {
      provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/${process.env.ROPSTEN_APP_ID}`),
      network_id: 3, // Ropsten's id
      gas: 5500000, // Ropsten has a lower block limit than mainnet
      confirmations: 2, // # of confs to wait between deployments. (default: 0)
      timeoutBlocks: 200, // # of blocks before deployment times out (minimum/default: 50)
      skipDryRun: true, // Skip dry run before migrations? (default: false for public nets )
      from : process.env.ACCOUNT_MAIN
    }

The result of the file would look something like this

Well, about the data of each network there is not much to say because they are “standard” items, the only detail that needs to be taken into account is that it has a property called from, this property indicates which account is implementing the contract and subsequently the owner of the contract.
We haven’t talked about this yet, but the security of your contract depends a lot on the “Owner”, we will talk about this in future articles regarding the development of a “real” dApp.

In the configuration we made we have 3 networks:

  • Testnet : Binance Smart Chain testnet, that is, here is the place for us to test and test our contract several times before thinking about publishing it on the official network.
  • BSC: This is the official Binance network, remember once we publish our contract it is immutable if we need to make any changes or anything like that your contract address will be changed so before publishing make sure everything is working.
  • Ropsten: Ethereum Test Network, to publish you will need to generate an APP ID from Infura, you can get this App ID in this link.

We can publish our contract on as many networks as we want, obviously each network will generate a different contract address, and of course, you will need to pay the fee to deploy the contract on any of the networks.
If you use the Testnet network you get BNB by going to the page https://testnet.binance.org/faucet-smart.

Well, without further ado, to publish to the network you will use the following command

$ truffle migrate --network testnet

Simple, if you want to publish on another network, just change the name “testnet” to “bsc” or “ropsten” (same as the name in the settings).

If everything went well, you will get the following result

Starting migrations...
=========================
> Network name: 'testnet'
> Network id: 97
> Block gas limit: 30000000 (0x1c9c380)


1648481877_hello_world.js
============================

   Deploying 'HelloWorld'
   ----------------------
   > transaction hash: 0x819d944cde0b4862c4a249b93f7b4f5cd5e46c2c154c54bd2c503b856c145495
   > Blocks: 6 Seconds: 18
   > contract address: 0x7478A3fF10A50Fc63400c717e38fa4CC87ee0a82
   > block number: 18013695
   > block timestamp: 1648646336
   > account: 0xC0e30a8518c290797db8dcC79Dd47805eA6b7dF9
   > balance: 6.602444087644871902
   > gas used: 415790 (0x6582e)
   > gas price: 10 gwei
   > value sent: 0 ETH
   > total cost: 0.0041579 ETH

   Pausing for 10 confirmations...
   -------------------------------
   > confirmation number: 2 (block: 18013699)
   > confirmation number: 3 (block: 18013700)
   > confirmation number: 4 (block: 18013701)
   > confirmation number: 6 (block: 18013703)
   > confirmation number: 7 (block: 18013704)
   > confirmation number: 8 (block: 18013705)
   > confirmation number: 10 (block: 18013707)
   > Saving artifacts
   -------------------------------------
   > Total cost: 0.0041579 ETH


Summary
=======
> Total deployments: 1
> Final cost: 0.0041579 ETH

Using the contract address you can consult it on the web page, for example, on testnet you access the address https://testnet.bscscan.com/address/YOUR_CONTRACT_ADDRESS you will see your contract implemented.

At another time I will make an article explaining how you can validate your contract on the network you published via truffle (very easy).

That’s it guys, if you have any questions, leave them in the comment. Until next time!