import { ethers , waffle } from "hardhat" ; import { expect } from "chai" ; import { Address } from "cluster" ; import { FirstCoin , FirstCoin__factory } from "../types" ; import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers" ; import { BigNumber } from "@ethersproject/bignumber" ; describe ( "FirstCoin" , function ( ) { it ( "Should return the total coins = owners coins" , async function ( ) { const [ owner , addr1 ] = await ethers . getSigners ( ) ; const FirstCoin = await ethers . getContractFactory ( "FirstCoin" ) ; const firstCoin = await FirstCoin . deploy ( 1000 ) ; await firstCoin . deployed ( ) ; expect ( await firstCoin . totalSupply ( ) ) . to . equal ( 1000 ) ; expect ( await firstCoin . balanceOf ( await owner . getAddress ( ) ) ) . to . equal ( 1000 ) ; } ) ; it ( "Should transfer coins correctly" , async function ( ) { const [ owner , addr1 ] = await ethers . getSigners ( ) ; const FirstCoin = await ethers . getContractFactory ( "FirstCoin" ) ; const firstCoin = await FirstCoin . deploy ( 1000 ) ; await firstCoin . deployed ( ) ; await firstCoin . transfer ( await addr1 . getAddress ( ) , 10 ) ; expect ( await firstCoin . balanceOf ( await owner . getAddress ( ) ) ) . to . equal ( 990 ) ; expect ( await firstCoin . balanceOf ( await addr1 . getAddress ( ) ) ) . to . equal ( 10 ) ; } ) ; } ) ; describe ( "Token contract" , function ( ) { let Token ; let hardhatToken ; let owner ; let addr1 ; let addr2 ; let addrs ; beforeEach ( async function ( ) { Token = await ethers . getContractFactory ( "FirstCoin" ) ; [ owner , addr1 , addr2 , ... addrs ] = await ethers . getSigners ( ) ; hardhatToken = await Token . deploy ( 10000 ) ; } ) ; describe ( "Deployment" , function ( ) { it ( "Should set the right owner" , async function ( ) { expect ( await hardhatToken . owner ( ) ) . to . equal ( owner . address ) ; } ) ; it ( "Should assign the total supply of tokens to the owner" , async function ( ) { const ownerBalance = await hardhatToken . balanceOf ( owner . address ) ; expect ( await hardhatToken . totalSupply ( ) ) . to . equal ( ownerBalance ) ; } ) ; } ) ; describe ( "Transactions" , function ( ) { it ( "Should transfer tokens between accounts" , async function ( ) { await hardhatToken . transfer ( addr1 . address , 50 ) ; const addr1Balance = await hardhatToken . balanceOf ( addr1 . address ) ; expect ( addr1Balance ) . to . equal ( 50 ) ; await hardhatToken . connect ( addr1 ) . transfer ( addr2 . address , 50 ) ; const addr2Balance = await hardhatToken . balanceOf ( addr2 . address ) ; expect ( addr2Balance ) . to . equal ( 50 ) ; } ) ; it ( "Should fail if sender doesn’t have enough tokens" , async function ( ) { const initialOwnerBalance = await hardhatToken . balanceOf ( owner . address ) ; await expect ( hardhatToken . connect ( addr1 ) . transfer ( owner . address , 1 ) ) . to . be . revertedWith ( "Not enough tokens" ) ; expect ( await hardhatToken . balanceOf ( owner . address ) ) . to . equal ( initialOwnerBalance ) ; } ) ; it ( "Should update balances after transfers" , async function ( ) { const initialOwnerBalance = await hardhatToken . balanceOf ( owner . address ) ; await hardhatToken . transfer ( addr1 . address , 100 ) ; await hardhatToken . transfer ( addr2 . address , 50 ) ; const finalOwnerBalance = await hardhatToken . balanceOf ( owner . address ) ; expect ( finalOwnerBalance ) . to . equal ( initialOwnerBalance . toNumber ( ) - 150 ) ; const addr1Balance = await hardhatToken . balanceOf ( addr1 . address ) ; expect ( addr1Balance ) . to . equal ( 100 ) ; const addr2Balance = await hardhatToken . balanceOf ( addr2 . address ) ; expect ( addr2Balance ) . to . equal ( 50 ) ; } ) ; } ) ; } ) ;