|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { ethers } from 'hardhat'; |
| 3 | +import { loadFixture } from '@nomicfoundation/hardhat-toolbox/network-helpers'; |
| 4 | + |
| 5 | +describe("RIFToken Contract", function () { |
| 6 | + |
| 7 | + async function deployRif() { |
| 8 | + const [owner, addr1, addr2, ...addrs] = await ethers.getSigners(); |
| 9 | + |
| 10 | + // Deploy AddressHelper library |
| 11 | + const addressHelper = await ethers.deployContract("AddressHelper"); |
| 12 | + // Deploy AddressLinker library |
| 13 | + const addressLinker = await ethers.deployContract("AddressLinker", { libraries: { AddressHelper: addressHelper }}); |
| 14 | + |
| 15 | + // Link libraries |
| 16 | + const rifToken = await ethers.deployContract("RIFToken", { |
| 17 | + libraries: { |
| 18 | + AddressLinker: addressLinker, |
| 19 | + AddressHelper: addressHelper, |
| 20 | + }, |
| 21 | + }); |
| 22 | + |
| 23 | + return { |
| 24 | + rifToken, |
| 25 | + owner, |
| 26 | + addr1, |
| 27 | + addr2, |
| 28 | + addrs |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + it("Should assign the initial balance to the contract itself", async function () { |
| 33 | + const { rifToken } = await loadFixture(deployRif) |
| 34 | + const contractBalance = await rifToken.balanceOf(rifToken); |
| 35 | + expect(contractBalance).to.equal(ethers.parseUnits("1000000000", 18)); |
| 36 | + }); |
| 37 | + |
| 38 | + it("Should use validAddress", async function () { |
| 39 | + const { rifToken, addr1 } = await loadFixture(deployRif) |
| 40 | + const addressOne = await addr1.getAddress(); |
| 41 | + const isAddressOneValid = await rifToken.validAddress(addressOne) |
| 42 | + expect(isAddressOneValid).to.be.true; |
| 43 | + |
| 44 | + }); |
| 45 | + |
| 46 | + // Single block to test the entire Transfer flow |
| 47 | + |
| 48 | + let rifFixture: Awaited<ReturnType<typeof deployRif>> |
| 49 | + |
| 50 | + it("Should transfer all the tokens to deployer/owner using setAuthorizedManagerContract", async function() { |
| 51 | + rifFixture = await loadFixture(deployRif) |
| 52 | + const { rifToken, owner } = rifFixture |
| 53 | + await rifToken.setAuthorizedManagerContract(owner) |
| 54 | + |
| 55 | + expect(await rifToken.balanceOf(owner)).to.be.equal(ethers.parseUnits('1000000000', 'ether')) |
| 56 | + }) |
| 57 | + |
| 58 | + it("Should transfer tokens between accounts", async function () { |
| 59 | + const { rifToken, addr1, addr2 } = rifFixture |
| 60 | + // Close distribution |
| 61 | + const latestBlock = await ethers.provider.getBlock('latest') |
| 62 | + |
| 63 | + if (latestBlock) { |
| 64 | + // We must close tokenDistribution to send transactions |
| 65 | + await rifToken.closeTokenDistribution(latestBlock.timestamp) |
| 66 | + |
| 67 | + // Transfer 50 RIF Tokens to address 1 |
| 68 | + await rifToken.transfer(addr1, ethers.parseUnits("50", 'ether')); |
| 69 | + const addr1Balance = await rifToken.balanceOf(addr1); |
| 70 | + expect(addr1Balance).to.equal(ethers.parseUnits("50", 'ether')); |
| 71 | + |
| 72 | + // Transfer 10 RIF Tokens from address 1 to address 2 |
| 73 | + await rifToken.connect(addr1).transfer(addr2, ethers.parseUnits("10", 'ether')); |
| 74 | + const addr2Balance = await rifToken.balanceOf(addr2); |
| 75 | + expect(addr2Balance).to.equal(ethers.parseUnits("10", 'ether')); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + it('Should make sure that the "Transfer" event is emitted', async () => { |
| 80 | + // Also check that the event "Transfer" is emitted |
| 81 | + const { rifToken, addr1, owner } = rifFixture |
| 82 | + |
| 83 | + await expect( |
| 84 | + rifToken.transfer(addr1, 1) |
| 85 | + ).to.emit(rifToken, 'Transfer(address,address,uint256)') |
| 86 | + .withArgs(owner, addr1, 1) |
| 87 | + }) |
| 88 | +}); |
0 commit comments