mirror of
				https://github.com/Instadapp/dsa-governance.git
				synced 2024-07-29 22:27:52 +00:00 
			
		
		
		
	Add tests
This commit is contained in:
		
							parent
							
								
									2c30d27685
								
							
						
					
					
						commit
						0e8b863a09
					
				
							
								
								
									
										0
									
								
								.env.example
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								.env.example
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							|  | @ -3,3 +3,5 @@ node_modules | |||
| #Hardhat files | ||||
| cache | ||||
| artifacts | ||||
| 
 | ||||
| .env | ||||
|  | @ -1,6 +1,8 @@ | |||
| pragma solidity ^0.7.0; | ||||
| pragma experimental ABIEncoderV2; | ||||
| 
 | ||||
| import "hardhat/console.sol"; | ||||
| 
 | ||||
| import { TokenDelegateStorageV1, TokenEvents} from "./TokenInterfaces.sol"; | ||||
| import { SafeMath } from "./SafeMath.sol"; | ||||
| 
 | ||||
|  | @ -34,7 +36,6 @@ contract TokenDelegate is TokenDelegateStorageV1, TokenEvents { | |||
|       */ | ||||
|     function initialize(address account, address minter_, uint mintingAllowedAfter_, bool transferPaused_) public { | ||||
|         require(mintingAllowedAfter == 0, "Token::initialize: can only initialize once"); | ||||
|         require(minter == address(0), "Token::initialize: can only initialize once"); | ||||
|         require(mintingAllowedAfter_ >= block.timestamp, "Token::constructor: minting can only begin after deployment"); | ||||
|         require(msg.sender == minter, "Token::initialize: admin only"); | ||||
|         require(account != address(0) && minter_ != address(0), "Token::initialize: invalid address"); | ||||
|  | @ -46,6 +47,9 @@ contract TokenDelegate is TokenDelegateStorageV1, TokenEvents { | |||
|         mintingAllowedAfter = mintingAllowedAfter_; | ||||
|         transferPaused = transferPaused_; | ||||
| 
 | ||||
|         // console.log("Account: ", account); | ||||
|         // console.log("Balance: ", balances[account]); | ||||
| 
 | ||||
|         if (transferPaused) { | ||||
|             emit TransferPaused(msg.sender); | ||||
|         } else { | ||||
|  | @ -335,6 +339,9 @@ contract TokenDelegate is TokenDelegateStorageV1, TokenEvents { | |||
|         require(src != address(0), "Tkn::_transferTokens: cannot transfer from the zero address"); | ||||
|         require(dst != address(0), "Tkn::_transferTokens: cannot transfer to the zero address"); | ||||
| 
 | ||||
|         // console.log("Balance: ", balances[src]); | ||||
|         // console.log("Account (Transfer): ", src); | ||||
| 
 | ||||
|         balances[src] = sub96(balances[src], amount, "Tkn::_transferTokens: transfer amount exceeds balance"); | ||||
|         balances[dst] = add96(balances[dst], amount, "Tkn::_transferTokens: transfer amount overflows"); | ||||
|         emit Transfer(src, dst, amount); | ||||
|  |  | |||
|  | @ -9,7 +9,8 @@ contract TokenDelegator is TokenDelegatorStorage, TokenEvents { | |||
|         address minter_, | ||||
|         address implementation_, | ||||
|         uint mintingAllowedAfter_, | ||||
|         uint changeImplementationAfter_ | ||||
|         uint changeImplementationAfter_, | ||||
|         bool transferPaused_ | ||||
|     ) { | ||||
|         // Admin set to msg.sender for initialization | ||||
|         minter = msg.sender; | ||||
|  | @ -17,10 +18,11 @@ contract TokenDelegator is TokenDelegatorStorage, TokenEvents { | |||
|         delegateTo( | ||||
|             implementation_, | ||||
|             abi.encodeWithSignature( | ||||
|                 "initialize(address,address,uint256)", | ||||
|                 "initialize(address,address,uint256,bool)", | ||||
|                 account, | ||||
|                 minter_, | ||||
|                 mintingAllowedAfter_ | ||||
|                 mintingAllowedAfter_, | ||||
|                 transferPaused_ | ||||
|             ) | ||||
|         ); | ||||
| 
 | ||||
|  |  | |||
|  | @ -44,6 +44,14 @@ contract TokenDelegatorStorage { | |||
|     /// @notice The timestamp after which implementation maybe change | ||||
|     uint public changeImplementationAfter; | ||||
| 
 | ||||
|     /// @notice EIP-20 token name for this token | ||||
|     string public name = "<Token Name>"; // TODO - Replace it | ||||
| 
 | ||||
|     /// @notice EIP-20 token symbol for this token | ||||
|     string public symbol = "<TKN>"; // TODO - Replace it | ||||
| 
 | ||||
|     /// @notice Total number of tokens in circulation | ||||
|     uint public totalSupply = 10000000e18; // TODO - Replace it | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  | @ -53,18 +61,12 @@ contract TokenDelegatorStorage { | |||
|  * TokenDelegateStorageVX. | ||||
|  */ | ||||
| contract TokenDelegateStorageV1 is TokenDelegatorStorage { | ||||
|     /// @notice EIP-20 token name for this token | ||||
|     string public name = "<Token Name>"; // TODO - Replace it | ||||
| 
 | ||||
|     /// @notice EIP-20 token symbol for this token | ||||
|     string public symbol = "<TKN>"; // TODO - Replace it | ||||
| 
 | ||||
|     /// @notice Total number of tokens in circulation | ||||
|     uint public totalSupply = 10000000e18; // TODO - Replace it | ||||
| 
 | ||||
|     /// @notice The timestamp after which minting may occur | ||||
|     uint public mintingAllowedAfter; | ||||
| 
 | ||||
|     /// @notice token transfer pause state | ||||
|     bool public transferPaused; | ||||
| 
 | ||||
|     // Allowance amounts on behalf of others | ||||
|     mapping (address => mapping (address => uint96)) internal allowances; | ||||
| 
 | ||||
|  | @ -88,7 +90,4 @@ contract TokenDelegateStorageV1 is TokenDelegatorStorage { | |||
| 
 | ||||
|     /// @notice A record of states for signing / validating signatures | ||||
|     mapping (address => uint) public nonces; | ||||
| 
 | ||||
|     /// @notice token transfer pause state | ||||
|     bool public transferPaused; | ||||
| } | ||||
|  | @ -1,14 +1,9 @@ | |||
| require("@nomiclabs/hardhat-ethers"); | ||||
| require("@nomiclabs/hardhat-waffle"); | ||||
| require("@nomiclabs/hardhat-etherscan"); | ||||
| 
 | ||||
| // This is a sample Hardhat task. To learn how to create your own go to
 | ||||
| // https://hardhat.org/guides/create-task.html
 | ||||
| task("accounts", "Prints the list of accounts", async () => { | ||||
|   const accounts = await ethers.getSigners(); | ||||
| 
 | ||||
|   for (const account of accounts) { | ||||
|     console.log(account.address); | ||||
|   } | ||||
| }); | ||||
| require("dotenv").config(); | ||||
| const ALCHEMY_ID = process.env.ALCHEMY_ID; | ||||
| 
 | ||||
| // You need to export an object to set up your config
 | ||||
| // Go to https://hardhat.org/config/ to learn more
 | ||||
|  | @ -17,6 +12,19 @@ task("accounts", "Prints the list of accounts", async () => { | |||
|  * @type import('hardhat/config').HardhatUserConfig | ||||
|  */ | ||||
| module.exports = { | ||||
|   defaultNetwork: "hardhat", | ||||
|   solidity: "0.7.3", | ||||
|   // networks: {
 | ||||
|   //   hardhat: {
 | ||||
|   //     forking: {
 | ||||
|   //       url: `https://eth-mainnet.alchemyapi.io/v2/${ALCHEMY_ID}`,
 | ||||
|   //       blockNumber: 12070498,
 | ||||
|   //     },
 | ||||
|   //     blockGasLimit: 12000000,
 | ||||
|   //   },
 | ||||
|   // },
 | ||||
|   etherscan: { | ||||
|     apiKey: process.env.ETHERSCAN | ||||
|   } | ||||
| }; | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										16247
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										16247
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							|  | @ -17,5 +17,9 @@ | |||
|     "ethers": "^5.0.32", | ||||
|     "hardhat": "^2.1.1", | ||||
|     "solc": "^0.7.0" | ||||
|   }, | ||||
|   "dependencies": { | ||||
|     "@nomiclabs/hardhat-etherscan": "^2.1.1", | ||||
|     "dotenv": "^8.2.0" | ||||
|   } | ||||
| } | ||||
|  |  | |||
|  | @ -1,32 +0,0 @@ | |||
| // We require the Hardhat Runtime Environment explicitly here. This is optional 
 | ||||
| // but useful for running the script in a standalone fashion through `node <script>`.
 | ||||
| //
 | ||||
| // When running the script with `hardhat run <script>` you'll find the Hardhat
 | ||||
| // Runtime Environment's members available in the global scope.
 | ||||
| const hre = require("hardhat"); | ||||
| 
 | ||||
| async function main() { | ||||
|   // Hardhat always runs the compile task when running scripts with its command
 | ||||
|   // line interface.
 | ||||
|   //
 | ||||
|   // If this script is run directly using `node` you may want to call compile 
 | ||||
|   // manually to make sure everything is compiled
 | ||||
|   // await hre.run('compile');
 | ||||
| 
 | ||||
|   // We get the contract to deploy
 | ||||
|   const Greeter = await hre.ethers.getContractFactory("Greeter"); | ||||
|   const greeter = await Greeter.deploy("Hello, Hardhat!"); | ||||
| 
 | ||||
|   await greeter.deployed(); | ||||
| 
 | ||||
|   console.log("Greeter deployed to:", greeter.address); | ||||
| } | ||||
| 
 | ||||
| // We recommend this pattern to be able to use async/await everywhere
 | ||||
| // and properly handle errors.
 | ||||
| main() | ||||
|   .then(() => process.exit(0)) | ||||
|   .catch(error => { | ||||
|     console.error(error); | ||||
|     process.exit(1); | ||||
|   }); | ||||
|  | @ -1,14 +0,0 @@ | |||
| const { expect } = require("chai"); | ||||
| 
 | ||||
| describe("Greeter", function() { | ||||
|   it("Should return the new greeting once it's changed", async function() { | ||||
|     const Greeter = await ethers.getContractFactory("Greeter"); | ||||
|     const greeter = await Greeter.deploy("Hello, world!"); | ||||
|      | ||||
|     await greeter.deployed(); | ||||
|     expect(await greeter.greet()).to.equal("Hello, world!"); | ||||
| 
 | ||||
|     await greeter.setGreeting("Hola, mundo!"); | ||||
|     expect(await greeter.greet()).to.equal("Hola, mundo!"); | ||||
|   }); | ||||
| }); | ||||
							
								
								
									
										101
									
								
								test/token.test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								test/token.test.js
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,101 @@ | |||
| const hre = require("hardhat"); | ||||
| const { expect } = require("chai"); | ||||
| const { ethers, network } = hre; | ||||
| 
 | ||||
| describe("Token", function() { | ||||
|   let tokenDelegate, tokenDelegator, token, minter, account1, account2, account3, allowedAfter, ethereum | ||||
|   before(async () => { | ||||
|     accounts = await ethers.getSigners() | ||||
|     minter = accounts[0] | ||||
| 
 | ||||
|     account1 = accounts[1] | ||||
|     account2 = accounts[2] | ||||
|     account3 = accounts[3] | ||||
| 
 | ||||
|     const TokenDelegate = await ethers.getContractFactory("TokenDelegate") | ||||
|     tokenDelegate = await TokenDelegate.deploy() | ||||
| 
 | ||||
|     await tokenDelegate.deployed() | ||||
| 
 | ||||
|     // console.log((await tokenDelegate.totalSupply()))
 | ||||
|     // console.log((await tokenDelegate.symbol()))
 | ||||
|     // console.log((await tokenDelegate.decimals()))
 | ||||
|     // console.log((await tokenDelegate.transferPaused()))
 | ||||
| 
 | ||||
|     allowedAfter = 1622505601 // June 1 2021
 | ||||
| 
 | ||||
|     const TokenDelegator = await ethers.getContractFactory("TokenDelegator") | ||||
|     tokenDelegator = await TokenDelegator.deploy(minter.address, minter.address, tokenDelegate.address, allowedAfter, allowedAfter, false) | ||||
| 
 | ||||
|     await tokenDelegator.deployed() | ||||
| 
 | ||||
|     token = await ethers.getContractAt("TokenDelegate", tokenDelegator.address) | ||||
| 
 | ||||
|     // console.log((await tokenDelegate.balanceOf(minter.address)))
 | ||||
| 
 | ||||
|     // console.log((await token.balanceOf(minter.address)))
 | ||||
| 
 | ||||
|     await token.transfer(account1.address, ethers.utils.parseEther("10000")) | ||||
|     await token.transfer(account2.address, ethers.utils.parseEther("20000")) | ||||
| 
 | ||||
|     ethereum = network.provider | ||||
|   }) | ||||
| 
 | ||||
|   it("Should match the deployed", async () => { | ||||
|     const minter_ = await token.minter() | ||||
|     const totalSupply_ = await token.totalSupply() | ||||
|     const transferPaused_ = await token.transferPaused() | ||||
|     const name_ = await token.name() | ||||
|     const symbol_ = await token.symbol() | ||||
|     expect(minter_).to.be.equal(minter.address) | ||||
|     expect(totalSupply_).to.be.equal(ethers.utils.parseEther("10000000")) | ||||
|     expect(transferPaused_).to.be.equal(false) | ||||
|     expect(name_).to.be.equal("<Token Name>") | ||||
|     expect(symbol_).to.be.equal("<TKN>") | ||||
|   }) | ||||
| 
 | ||||
|   it("Should pause transfer", async () => { | ||||
|     await token.pauseTransfer() | ||||
|     const transferPaused_ = await token.transferPaused() | ||||
|     expect(transferPaused_).to.be.equal(true) | ||||
|     await expect(token.transfer(account1.address, ethers.utils.parseEther("10000"))) | ||||
|       .to.be.revertedWith("Tkn::_transferTokens: transfer paused") | ||||
|   }) | ||||
| 
 | ||||
|   it("Should unpause transfer", async () => { | ||||
|     await token.unpauseTransfer() | ||||
|     const transferPaused_ = await token.transferPaused() | ||||
|     expect(transferPaused_).to.be.equal(false) | ||||
|     await token.transfer(account3.address, ethers.utils.parseEther("10")) | ||||
|     const balance = await token.balanceOf(account3.address) | ||||
|     expect(balance).to.be.equal(ethers.utils.parseEther("10")) | ||||
|   }) | ||||
| 
 | ||||
|   it("Should change name", async () => { | ||||
|     await token.changeName("Awesome Token") | ||||
|     const name_ = await token.name() | ||||
|     expect(name_).to.be.equal("Awesome Token") | ||||
|   }) | ||||
| 
 | ||||
|   it("Should change symbol", async () => { | ||||
|     await token.changeSymbol("AWT") | ||||
|     const name_ = await token.symbol() | ||||
|     expect(name_).to.be.equal("AWT") | ||||
|   }) | ||||
| 
 | ||||
|   it("Should delegate", async () => { | ||||
|     await token.connect(account1).delegate(account3.address) | ||||
|     const delegate_ = await token.delegates(account1.address) | ||||
|     expect(delegate_).to.be.equal(account3.address) | ||||
|   }) | ||||
| 
 | ||||
|   it("Should mint after allowed time", async () => { | ||||
|     await ethereum.send("evm_setNextBlockTimestamp", [allowedAfter+100]) | ||||
|     await ethereum.send("evm_mine", []) | ||||
| 
 | ||||
|     await token.mint(account2.address, ethers.utils.parseEther("1000")) | ||||
| 
 | ||||
|     const totalSupply_ = await token.totalSupply() | ||||
|     expect(totalSupply_).to.be.equal(ethers.utils.parseEther("10001000")) | ||||
|   }) | ||||
| }) | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Mubaris NK
						Mubaris NK