How to test a BEP20 token with Hardhat and not get SCAMmed
Tags: beginners, blockchain, solidity, tutorial
Published: 2021-06-30 17:53:39

---
title: How to test a BEP20 token with Hardhat and not get SCAMmed
published: true
date: 2021-06-30 03:31:28 UTC
tags: beginner,blockchain,solidity,tutorial
canonical_url: https://medium.com/coinmonks/how-to-test-a-bep20-token-with-hardhat-and-not-get-scammed-ea655774b006
---

![](https://cdn-images-1.medium.com/max/1024/1*fzh8CKBANEADXHS7Tu4njw.png)

In this post, you will learn how to test a custom BEP20 token with [Hardhat](https://hardhat.org/) and [ethers.js](https://docs.ethers.io/v5/) NPM package.

The BEP20 token we will be testing today is the $SCAM token.

SCAM is a BEP20 token created on Binance Smart Chain. $SCAM has a 100k marketcap currently (350k at its/BNB’s ATH) and a limited supply of 100 million tokens, a lot of which was distributed through multiple free airdrops on reddit that helped grow the community into 200+ members on telegram and 700+ on [reddit](https://www.reddit.com/r/scam_coin/).

$SCAM follows a bare-bone ERC20 token implementation deployed on the BSC Mainnet with basically no admin functions or centralized control — no blacklist, burn or freeze functions and also no additional tokens can be minted.

[Find out more with this post.](https://www.reddit.com/r/scam_coin/comments/n4oomy/all_you_need_to_know_about_safe_crypto_and_money/)

[You can also visit their website.](https://www.scam-coin.org/#about)

I hope you already have solidity development environment ready at your machine.

Otherwise, please refer to my previous post [How to make a fullstack dapp with React, Hardhat and ethers.js](https://dev.to/steadylearner/how-to-make-a-fullstack-dapp-with-react-hardhat-and-ether-js-with-examples-4fi2/) with examples.

### Table of contents

1. Why was SCAM token chosen for this post?
2. Inspect the SCAM contract source code
3. Prepare tests for it
4. Conclusion

[You can find the entire code used for this post here.](https://github.com/steadylearner/blockchain/tree/main/real/scam)

### 1. Why was SCAM token chosen for this post?

![](https://cdn-images-1.medium.com/max/880/0*v46iBcpnHEsjEIy5.png)

I came across Safe Crypto and Money ($SCAM) randomly on the dev.to website through the [developer’s story](https://dev.to/otter/a-deep-dive-into-creating-a-shitcoin-1af5) about how learning about ERC20 tokens and programming practice on Solidity became a passion project leading to this coin.

I was just restarting blockchain development again and I found the [story](https://www.reddit.com/r/scam_coin/comments/nei371/scam_my_deep_dive_into_creating_a_shitcoin/) of the author very interesting.

What started out as a meme token, as can be understood from the name itself, organically evolved into an awesome friendly community where beginners and developers alike could learn more about cryptocurrency and DeFi.

They’ve already launched a website and implemented a fully decentralized liquidity rewards contract, and are currently [working](https://www.reddit.com/r/scam_coin/comments/nq9d8d/oh_shit_its_a_roadmap/) on decentralized governance and treasury, as well as a faucet.

The community itself has become a place of healthy conversation, discussion and ideation, and a launching platform for various voluntary projects taken up by members such as a coin-listing site, NFTs, merchandise, and marketing campaigns.

First, I thought it was just a joke but I was intrigued by the post. I found the project funny and wanted to be more involved with the project and found [its Telegram group](https://t.me/SCAM_Coin_Community) also.

Different from my first expectation, the members of the group were very helpful. I could find [a clue to set up and deploy a smart contract code](https://testnet.bscscan.com/address/0xF26c1f9Ac029629d8e1dD0D15738EB0929CF2D04#code) that one of my client wanted to test.

I also inspired myself to write [How to make a fullstack dapp with React, Hardhat and ethers.js with example](https://dev.to/steadylearner/how-to-make-a-fullstack-dapp-with-react-hardhat-and-ether-js-with-examples-4fi2) by participating in the group.

I think it helped me a lot in getting further involved with blockchain stuffs and find useful information.

It is important to have someone help you get familiar with cryptocurrency and its relevant technologies. Otherwise, it is very easy to get scammed and lose money.

If you want to know more about SCAM after this post, you can participate in [its Telegram group](https://t.me/SCAM_Coin_Community).

Currently, [the faucet contract](https://bscscan.com/address/0xfcf2774cd61743ff7c34607fadf3c84fc4762029#code) to get SCAM free is also working so you can hold it free. I think it can be a good opportunity to have your first cryptocurrency.

[For more details, you can read this post for the faucet also.](https://www.reddit.com/r/scam_coin/comments/o3ebuc/new_scam_faucet_out_now/)

The website for it is also ready at [https://faucet.scam-coin.org/](https://faucet.scam-coin.org/) page.

![](https://cdn-images-1.medium.com/max/880/0*qZ3ZwAqXkKa0XM9L.png)

You can use any wallet in the list below.

![](https://cdn-images-1.medium.com/max/566/0*XL0CdLM4oZawq80N.png)

Then, connect it to the website.

![](https://cdn-images-1.medium.com/max/706/0*p30mFYK2HdKWgo2s.png)

Finally, follow its instruction to get your first $SCAM token.

![](https://cdn-images-1.medium.com/max/880/0*Asw587L6nQrrFq6N.png)

If you have any doubt, please join the group and ask the members in there for help. They will help you to learn about SCAM, cryptocurrency, smart contracts and DeFi in general and other blockchain relevant stuffs.

### 2. Inspect the SCAM contract source code

![](https://cdn-images-1.medium.com/max/880/0*AY239aUjwG8KXbro.png)

Before you start, BEP20 token specification is equal to ERC20 equivalent, so if you are not familiar with that, please read [the documentation for it](https://docs.openzeppelin.com/contracts/2.x/api/token/erc20) first.

[You can see that SCAM was verified by bscscan with the code snippet similar to this.](https://bscscan.com/address/0xdb78fcbb4f1693fdbf7a85e970946e4ce466e2a9#code) I use js extension instead of sol to easily read the code snippet with a gist.

{% gist https://gist.github.com/steadylearner/038f7ba2db2e7b26d5d91601fd64774c.js %}

We will inspect each part of it. This will help you find what this contract does and write tests in the next part to confirm that.

**1.** This is where the owner of the contract can define BEP20 token name, symbol and the decimal for it. It is just following the ERC20 standard.

**2.** Here, the total supply of the token is defined. There are no mint or burn function for SCAM. You can see that there is a fixed amount of SCAM tokens(100000000).

Then, the owner of the contract will take 20000000 of it and 1000000 will be used with airdropTokens function.

**3.** These functions until **4.** is just some parts of [IERC20 standard functions](https://docs.openzeppelin.com/contracts/2.x/api/token/erc20#IERC20). It was the intention of its author.

**4.** This is where airdrop of the SCAM token happens, you can see it is sent from the contract to receiver. (100000000–20000000 = 80000000 in the constructor part)

**5.** This part is not relevant to the SCAM token logic. It is just a fallback function that extracts the BNB balance from the contract.

I spoke with the author and he told me that he included it to receive donation for the contract.

It is not relevant to SCAM token logic. But, we will include the test to see if it affects the balance of a SCAM holders in the next part.

**6.** These are some functions from [SafeMath OpenZeppelin library](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol) code.

You can read their documentation for more information.

### 3. Prepare tests for it

![](https://cdn-images-1.medium.com/max/880/0*M6HlAiNdn1V1x7Bs.png)

In the previous part, we inspected the contract code for SCAM and see what each part of it does.

We will write simple tests for it to find that they all work ok.

I will assume you are using the source code link I shared at the beginning of this post.

Before you update your project, please edit your **hardhat.config.js** and use the solidity compiler version between >=0.4.22 <0.6.0 that you prefer.

```
module.exports = {
  defaultNetwork: "hardhat",
  paths: {
    artifacts: './src/artifacts',
  },
  networks: {
    // $npx hardhat run scripts/deploy.js --network localhost
    // https://hardhat.org/config/
    hardhat: {
      chainId: 1337,
    },
  },
  solidity: "0.5.6", // This is where you should edit.
};
```

Use $yarn compile (npx hardhat compile) to see if the contract code really compiles and it should show a result similar to this.

```
Creating Typechain artifacts in directory typechain for target ethers-v5
Successfully generated Typechain artifacts!
```

Use $yarn serve (npx hardhat node) in a console to set up local solidity dev env with hardhat.

Then, verify that everything is ok with your **scripts/deploy.js** file.

{% gist https://gist.github.com/steadylearner/8ee74292d20be085a25aa85229a552a4.js %}

If you could make it to this part, we can finally write tests for the SCAM contract.

You can see the **test/scam-test.js** file in the repository similar to this.

{% gist https://gist.github.com/steadylearner/2f0b5daa7c6499d7b36aeeeca57d3e69.js %}

The smart contract code for SCAM is very simple and we don’t need a lot of lines of code to test it also.

**1.** There are very big numbers are involved for BEP20 token contract. We need their hex values to test them with hardhat and ethers.js package.

Therefore, I manually included them. For that, you can use your Python console with **hex** function.

```
$python
>>> hex(1000000000000000000000000)
'0xd3c21bcecceda1000000'
>>> hex(1000000000000000000000000000000)
'0xc9f2c9cd04674edea40000000'
>>> hex(1000000000000000000000000000000000)
'0x314dc6448d9338c15b0a00000000'
>>> hex(1000000000000000000000000)
'0xd3c21bcecceda1000000'
>>> 10 ** 26
100000000000000000000000000
>>> hex(_)
'0x52b7d2dcc80cd2e4000000'
```

You can also refer to [How to use Python in JavaScript with examples](https://dev.to/steadylearner/how-to-use-python-in-javascript-4bnm) post to include that to your JavaScript code.

Otherwise, you can find a JavaScript function to find hex value of the big number also. But, that will be unnecessary if you know how to use Python also.

**2.** We test here to see contract name, symbol and the decimal is set correctly when the contract is deployed. You can also verify if the total supply of the token is set and preMine amount is correctly saved to the address of the owner of the contract.

**3.** In this part, we first check if other users (not owners) can call airdropTokens or not.

You can see if the caller were firstComer or secondComer (not owners), the contract shouldn’t allow to use airdropTokens function. In the contract, there is no error message set, so we use unspecified api from **@openzeppelin/test-helpers** package to test them.

You can also see the receiver have airDropSize amount of token “1000000000000000000000000” after that. This can be confusing comparing to the smart contract part but you can see the decimal value is “18”. If we consider that, the large number will be 1000000 of SCAM token and sent to the receiver.

**4.** Tests below this will be help you find how some of ERC20 standard functions included for this contract work.

You can see transfer, approve, allowance, transferFrom will work all ok with them.

**5.** There were fallback function and releaseBNB in the contract code for donation.

```
function releaseBNB() external {
  require(msg.sender == owner_);
  owner_.transfer(address(this).balance);
}

// Fallback function
function() external payable {}
```

We can test that calling this function will not have any effect for not owners.

You can see the balance for the firstComer is equal after calling releaseBNB function in the test.

You could write more tests here if you want.

Test all of them really work at your machine with $yarn test (npx hardhat test) and you will see every test passes with the result similar to this.

```
$yarn test
Creating Typechain artifacts in directory typechain for target ethers-v5
Successfully generated Typechain artifacts!

SCAMToken state and transactions
    ✓ Should test 'totalSupply' and other default values.
    ✓ Should test 'airdropTokens' and the contract and receiver balance change.
    ✓ Should test 'transfer' from the owner to firstComer.
    ✓ Should test 'approve' and 'allowance' from the owner to firstComer.
    ✓ Should test 'approve', 'allowance' and 'transferFrom'.
    ✓ Should test 'releaseBNB' and it shouldn't affect the balance of other users (not owners)
```

Hope you could make all tests pass.

### 4. Conclusion

In this post, we learnt how to test a custom BEP20 token with [SCAM](https://scam-coin.org/). I introduced it because I found its community very helpful. Hope the code snippet used here can be a starting point to test other BEP20 or ERC20 tokens also before you buy them.

ERC20 and BEP20 token are almost identical and you will be able to use the code snippet used here for the former as well.

If you liked the post, please share it with others. I am plan to share more blockchain relevant stuffs. I am interested in ETH and POLKADOT.

[If you need to hire a developer, you can contact me.](https://t.me/steadylearner)

[I can write a full stack dapp.](https://dev.to/steadylearner/how-to-make-a-fullstack-dapp-with-react-hardhat-and-ether-js-with-examples-4fi2)

[I can also clone, set up, update and deploy another blockchain project if you want.](https://testnet.bscscan.com/address/0xF26c1f9Ac029629d8e1dD0D15738EB0929CF2D04#code)

Thanks.

> Join [Coinmonks Telegram Channel](https://t.me/coincodecap) and learn about crypto trading and investing

#### Also, Read

- [What is Margin Trading](https://blog.coincodecap.com/margin-trading) | [Dollar-Cost Averaging](https://blog.coincodecap.com/dca)
- [BigONE Exchange Review](https://medium.com/coinmonks/bigone-exchange-review-64705d85a1d4) | [Grid Trading Bot](https://blog.coincodecap.com/grid-trading)
- [3Commas Review](https://medium.com/coinmonks/3commas-review-an-excellent-crypto-trading-bot-2020-1313a58bec92) | [Pionex Review](https://medium.com/coinmonks/pionex-review-exchange-with-crypto-trading-bot-1e459d0191ea) | [Coinrule review](https://medium.com/coinmonks/coinrule-review-2021-a-beginner-friendly-crypto-trading-bot-daf0504848ba)
- [Ledger vs Ngrave](https://medium.com/coinmonks/ledger-vs-ngrave-zero-7e40f0c1d694) | [Ledger nano s vs x](https://medium.com/coinmonks/ledger-nano-s-vs-x-battery-hardware-price-storage-59a6663fe3b0) | [Binance Review](https://medium.com/coinmonks/binance-review-ee10d3bf3b6e)
- [Bybit Exchange Review](https://medium.com/coinmonks/bybit-exchange-review-dbd570019b71) | [Bityard Review](https://medium.com/coinmonks/bityard-review-7d104239be35) | [CoinSpot Review](https://blog.coincodecap.com/coinspot-review)
- [3Commas vs Cryptohopper](https://medium.com/coinmonks/3commas-vs-pionex-vs-cryptohopper-best-crypto-bot-6a98d2baa203) | [Earn crypto interest](https://medium.com/coinmonks/earn-crypto-interest-b10b810fdda3)
- The Best Bitcoin [Hardware wallet](https://medium.com/coinmonks/the-best-cryptocurrency-hardware-wallets-of-2020-e28b1c124069?source=friends_link&sk=324dd9ff8556ab578d71e7ad7658ad7c) | [BitBox02 Review](https://medium.com/coinmonks/bitbox02-review-your-swiss-bitcoin-hardware-wallet-c36c88fff29)
- [BlockFi vs Celsius](https://medium.com/coinmonks/blockfi-vs-celsius-vs-hodlnaut-8a1cc8c26630) | [Hodlnaut Review](https://medium.com/coinmonks/hodlnaut-review-best-way-to-hodl-is-to-earn-interest-on-your-bitcoin-6658a8c19edf) | [KuCoin Review](https://blog.coincodecap.com/kucoin-review)

* * *