Deployment (To the chain!)

Filip Koprivec
8 min readJan 16, 2023

--

Having contracts on the local blockchain is nice, but we want to deploy them to the real chain and make them accessible to the world.

We will deploy our contracts to the Coston testnet, which is a public testnet for Songbird that is used by many projects. Since Flare/Songbird/Coston is running an EVM, everything we do here can be done on Ethereum as well. You should first switch your metamask to the Coston testnet. Head over to the faucet and get some testnet CFLR.

Once everything is set, just input your address and click on the button to get some CFLR and wait for the transaction to be mined confirmed.

The configuration for Coston network is already included in the hardhat config, and there is also a link to the faucet in the comments.

Web-based deployment

Open remix and create our token contract. Compile the token, open the deploy tab and select Metamask injected environment.

Select the account that you want to deploy from (the account will need some funds) and select the contract you want to deploy Token - contracts/Token.sol. Input the desired initial supply and click on the transact button. A metamask popup will open, where you have to check and confirm the transaction. NEVER confirm a transaction you don't understand or are not sure what it is doing

Once you confirm, the transaction will deploy the contract to the chain and you will be able to interact with it (this might take up to a minute).

Go on the blockscout, find your address and open the transaction (The easiest way is to just click on the transaction in metamask and use the link to view it on block explorer), that should look somewhat like the image.

The to field denotes the address of the contract we just created, click on the address to see it on etherscan.

The to field denotes the address of the contract we just created, click on the address to see it on the block explorer.

Yay, we deployed our first contract to the chain! But we can’t interact with it, because we don’t have the ABI (Application Binary Interface) of the contract. Well, we do, but the etherscan (or anyone else) doesn’t. Although we wrote the contract in solidity, it is compiled to EVM bytecode, which is what is actually deployed to the chain. To make it more accessible, we will publish the full source code of the contract on the chain, so that anyone can verify it. The block explorer will also check, that the source code we pasted matches the compiled bytecode on the chain and generate helper methods to interact with it.

Select the contract tab and click on Verify & Publish button. Select Via flattened source code and click next. Once a new tab opens, input contract name (Token), select the correct compiler version (check the remix settings), license check, that the optimization setting and optimizer runs match those in the remix settings (if you didn't make any changes in the remix, click No on optimizator config).

We now have to copy the source code of the contract and paste it into the Contract Source Code field. But our contract contains an import, so we will have to flatten it before compiling it. Go over to remix, right-click on the Token.sol file and select Flatten. This will inline all the imports and create a single file, that we can copy and paste into the Contract Source Code field (Exercise: is this always possible?). Once you paste the code, click on the Verify button and wait for the verification to finish.

Now we can interact with the contract directly from the block explorer. Read contract tab contains pure and view functions, that can be called without any transaction, go check your balance. The write contract tab contains all the functions that can change the state of the contract, so we can transfer some tokens to another account (but we also have to pay a transaction cost).

Head over to the write contract tab and transfer some tokens to a friend (or your other account). You will need to confirm the transaction in metamask and wait for it to be mined (you should first connect to web3). Once it is mined, you can check the balance of the recipient on the read contract tab, but you can also import the token directly on in metamask and check the balance there.

Open metamask, head over to Assets click on the Add Token button. Copy the contract address, come up with a nice token name and set decimals to 0. Metmask will now fetch the token information from the chain and you will be able to see your balance.

Hurray, we deployed our first contract to the chain and interacted with it directly from the browser!

CLI deployment and interaction

DANGER

We are now going to prepare the deployment script, which will deploy the contract to the chain. To pay for the transaction, we will have to use the private key of the account that we got the CFLR from and use the blockscout api to connect to the chain.

TAKE GREAT CARE WITH THE FOLLOWING AND MAKE SURE THAT NO ONE SEES YOUR PRIVATE KEY AND API KEYS.

Rename .env.sample to .env (if you haven't done that in the first step of the tutorial) and fill in the values:

API_URL: Create a new project app on alchemy and copy the API key from there. You will only need this if you want to deploy on ethereum tests networks. If you want to deploy on Flare, you don't need it, as a good deal of requests are on us; go build stuff :)

ETHERSCAN_API_URL: Login to etherscan.io create a new API key and paste it here. This is also only needed on Ethereum chains.

PRIVATE_KEY: Export the private key from metmask, following the instructions on https://metamask.zendesk.com/hc/en-us/articles/360015289632-How-to-export-an-account-s-private-key.

Coston API is free to use for a small number of transactions, so we won’t need any API key for it.

LESS DANGER

Manual deployment and interaction with contracts using the blockscout is nice, but it is not very convenient if you want to interact with the contract from your own application. Let’s deploy the contract using the CLI and interact with it using RPC (remote procedure call) protocol.

Create a new file simpleTokenDeploy.ts in the scripts folder and paste the following code

The script should be pretty self-explanatory, we are just deploying the contract (the same way as we did in tests) and transferring some tokens to another account. Another important thing to note is the Token.at method, which enables us to interact with an existing contract using the address.

Now we just have to run yarn hardhat run scripts/simpleTokenDeploy.ts --network coston and wait for the transaction to be confirmed. And TADA, we deployed our contract to the chain and interacted with it using the CLI. The process is practically the same as with tests, just the underlying network is changed to the real one. You did change the secondAddress to something that makes sense, did you?

We just need to verify the contract on etherscan, so that we can interact with it directly from the browser. We will use the verify plugin for this. Run npx hardhat verify --network coston TOKEN_ADDRESS "1000" (where you insert a correct token address instead of TOKEN_ADDRESS) and wait a few seconds for verification to finish. The number 1000 is exactly the same number that was provided as a constructor argument.

Now do all this for coston2. Just replace coston with coston2 in cli commands and you are done.

We already mentioned that Flare is EVM-compatible, so anything we did here can easily be replicated on Ethereum testnets. If you have set up API keys in the previous steps, just get some tesnet eth and run the whole bunch of CLI commands using --network goerli.

Extra

Most of the time, you will want a token that is able to provide some additional metadata for a nicer presentation and better user experience. One of the available extensions is the extended ERC20 standard, which allows you to add a name, symbol and decimals to your token. Use IERC20Metadata from the openzeppelin library to add the metadata to your token.

A possible solution where we inherit from the original Token and just add additional metadata

Openzeppelin library also provides an implementation of the extended ERC20 standard, so you can just set up some hooks and use the library implementation that does most of the work for you.

Deploy your own token with metadata and verify it on the block explorer. Then add this new token to your metamask wallet and check how you can track the transfers of tokens.

Additional exercises

Smart contracts offer a whole new world of possibilities when transactions are executed. A transfer transaction can do a lot more than just transfer tokens from one account to another. Here are a few possible exercises that you can try out (try to use ERC20 already implemented in openzeppelin and provide appropriate hooks).

  • Taxation: Add a tax to every transfer transaction, so that on every transaction a specified percentage of tokens is sent to the contract owner.
  • Burn: Add a burn function that will allow users to burn their tokens (take a look at how openzeppelin implements the burn function).
  • Blacklist: Add a blacklist that will prevent users from transferring tokens to addresses that are on the blacklist, user should be able to add and remove addresses from the by burning a specified amount of tokens. Advanced: Make the cost dynamic so that adding the user to a blacklist will increase exponentially.

Woohoo, you made it this far and successfully released the token. Stay tuned for Flare specific goodies.

--

--