From df3db1173a7b35f610a3bc0977453bcfd0c5d08b Mon Sep 17 00:00:00 2001 From: 0xthrpw <0xthrpw@gmail.com> Date: Tue, 13 Apr 2021 23:41:47 -0400 Subject: [PATCH] tests, fixes for ffwd, impersonation --- contracts/test/SuperStaking.test.js | 118 ---------------------------- package.json | 2 +- test/SuperStaking.test.js | 107 +++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 119 deletions(-) delete mode 100644 contracts/test/SuperStaking.test.js create mode 100644 test/SuperStaking.test.js diff --git a/contracts/test/SuperStaking.test.js b/contracts/test/SuperStaking.test.js deleted file mode 100644 index e48e0c9..0000000 --- a/contracts/test/SuperStaking.test.js +++ /dev/null @@ -1,118 +0,0 @@ -//'use strict'; -const hre = require('hardhat') -const { ethers } = require('hardhat') -const { describe, it } = require('mocha') -const { expect } = require('chai') - -const UniswapV2PairABI = require("@uniswap/v2-core/build/UniswapV2Pair.json").abi; - -async function fastForward(amount){ - const currentBlock = await ethers.provider.getBlockNumber() - const block = await ethers.provider.getBlock(currentBlock) - const timestamp = block.timestamp + amount - - await hre.network.provider.request({ - method: 'evm_setNextBlockTimestamp', - params: [timestamp] - }) - await hre.network.provider.send("evm_mine") -} - -const toToken = (count) => { - return count * (10 ** 18) -} - -const fromToken = (count) => { - return parseInt(count) / (10 ** 18) -} - - -SUPER_TOKEN = '0xe53ec727dbdeb9e2d5456c3be40cff031ab40a55'; -SUPER_HOLDER = '0xf8e6E9cEAc3828499DDDf63AC91BBEb42A88e965'; -SUPER_WETH_LP = '0x25647e01bd0967c1b9599fa3521939871d1d0888'; -SUPER_WETH_LP_HOLDER = '0x87ad7b8e02ea527f63051692b9cbd9fd137e8de4'; - - -describe("SuperFarmDAO Liquidity Incentive Program: Tests", function() { - let alice, bob, dev, super_deployer; - let SuperStaking, SuperToken, SuperLP; - - before(async () => { - await hre.network.provider.request({ - method: 'hardhat_impersonateAccount', - params: [SUPER_HOLDER] - }) - super_deployer = await ethers.provider.getSigner(SUPER_HOLDER) - - const signers = await ethers.getSigners(); - const addresses = await Promise.all(signers.map(async signer => signer.getAddress())); - alice = { provider: signers[0].provider, signer: signers[0], address: addresses[0] }; - bob = { provider: signers[1].provider, signer: signers[1], address: addresses[1] }; - dev = { provider: signers[3].provider, signer: signers[3], address: addresses[3] }; - - SuperToken = await ethers.getContractAt('contracts/Token.sol:Token', SUPER_TOKEN); - SuperLP = await ethers.getContractAt(UniswapV2PairABI, SUPER_WETH_LP); - SuperStakingContract = await ethers.getContractFactory('SuperStaking'); - - SuperStaking = await SuperStakingContract.connect(super_deployer).deploy(dev.address, SUPER_TOKEN, SUPER_WETH_LP); - await SuperStaking.deployed(); - - let rewardAmount = await ethers.utils.parseEther('1000000') - await SuperToken.connect(super_deployer).transfer(SuperStaking.address, rewardAmount); - }); - - beforeEach(async () => { - console.log(' -------------------------------------------------------------------------'); - }); - - it('senseless token (in)sanity check', async function () { - let remainingSupply = await ethers.utils.parseEther('1500000') - expect( - await SuperToken.balanceOf(SUPER_HOLDER) - ).to.equal(remainingSupply) - }) - - it("Test Staking Settings", async function() { - await hre.network.provider.request({ - method: 'hardhat_impersonateAccount', - params: [SUPER_WETH_LP_HOLDER] - }) - super_weth_bags = await ethers.provider.getSigner(SUPER_WETH_LP_HOLDER) - let super_weth_bags_balance = await SuperLP.balanceOf(SUPER_WETH_LP_HOLDER) - - let programLength = 2592000; //one month - let rewardAmount = await ethers.utils.parseEther('1000000') - let rewardRate = rewardAmount.div(programLength); - - await SuperStaking.connect(super_deployer).setRewardRate(rewardRate, 1) - expect(await SuperStaking.rewardRate()).to.equal(rewardRate) - - await SuperStaking.connect(super_deployer).notifyRewardAmount(rewardAmount) - - await SuperLP.connect(super_weth_bags).approve(SuperStaking.address, ethers.utils.parseEther('2000000')); - SuperStaking = SuperStaking.connect(super_weth_bags) - let stakingAmount = await ethers.utils.parseEther('20'); - await SuperStaking.stake(stakingAmount); - - expect( - await SuperLP.balanceOf(SuperStaking.address) - ).to.equal(stakingAmount) - - fastForward(programLength); - }); - - it("Check Earnings", async function() { - - const currentBlock = await ethers.provider.getBlockNumber() - const block = await ethers.provider.getBlock(currentBlock) - - await SuperStaking.stake(1); //call updatepool modifier indirectly - expect( - await SuperStaking.earned(SUPER_WETH_LP_HOLDER) - ).to.be.gt(ethers.utils.parseEther('999999')).and.to.be.lt(ethers.utils.parseEther('1000000')) - - let period = await SuperStaking.periodFinish(); - console.log(period.toString(), block.timestamp, "periodFinish"); - }); - -}); diff --git a/package.json b/package.json index ec52286..244ad66 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ }, "homepage": "https://github.com/SuperFarmDAO/SuperFarm-Contracts#readme", "scripts": { - "test": "npx hardhat test; echo 'Executing test cases which require Ganache ...'; npx hardhat --config hardhat-ganache-tests.config.js --network ganache test;", + "test": "npx hardhat test; echo 'Executing test cases which require Ganache ...';", "lint": "npx eslint ./", "validate": "npm-run-all --parallel test lint", "contract-size": "npx hardhat size-contracts" diff --git a/test/SuperStaking.test.js b/test/SuperStaking.test.js new file mode 100644 index 0000000..8721351 --- /dev/null +++ b/test/SuperStaking.test.js @@ -0,0 +1,107 @@ +'use strict'; +// const hre = require('hardhat'); +const { network, ethers } = require('hardhat'); +const { describe, it } = require('mocha'); +const { expect } = require('chai'); +require('dotenv').config(); + +const overrides = { + gasPrice: ethers.utils.parseUnits('0', 'gwei') +}; + +const UniswapV2PairABI = require('@uniswap/v2-core/build/UniswapV2Pair.json').abi; + +async function fastForward (amount) { + const currentBlock = await ethers.provider.getBlockNumber(); + const block = await ethers.provider.getBlock(currentBlock); + const timestamp = block.timestamp + amount; + + await network.provider.request({ + method: 'evm_setNextBlockTimestamp', + params: [timestamp] + }); + await network.provider.send('evm_mine'); +} + +const SUPER_TOKEN = '0xe53ec727dbdeb9e2d5456c3be40cff031ab40a55'; +const SUPER_HOLDER = '0xf8e6E9cEAc3828499DDDf63AC91BBEb42A88e965'; +const SUPER_WETH_LP = '0x25647e01bd0967c1b9599fa3521939871d1d0888'; +const SUPER_WETH_LP_HOLDER = '0x87ad7b8e02ea527f63051692b9cbd9fd137e8de4'; + +describe('SuperFarmDAO Liquidity Incentive Program: Tests', function () { + let superDeployer; + let SuperStaking, SuperToken, SuperLP; + + before(async () => { + await network.provider.request({ + method: 'hardhat_reset', + params: [{ + forking: { + jsonRpcUrl: `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_KEY}`, + blockNumber: 11969707 + } + }] + }); + await network.provider.request({ + method: 'hardhat_impersonateAccount', + params: [SUPER_HOLDER] + }); + + superDeployer = await ethers.provider.getSigner(SUPER_HOLDER); + + SuperToken = await ethers.getContractAt('contracts/Token.sol:Token', SUPER_TOKEN); + + let SuperStakingContract = await ethers.getContractFactory('SuperStaking'); + SuperLP = await ethers.getContractAt(UniswapV2PairABI, SUPER_WETH_LP); + SuperStaking = await SuperStakingContract.connect(superDeployer).deploy(SUPER_HOLDER, SUPER_TOKEN, SUPER_WETH_LP, overrides); + await SuperStaking.deployed(); + + let rewardAmount = await ethers.utils.parseEther('1000000'); + await SuperToken.connect(superDeployer).transfer(SuperStaking.address, rewardAmount, overrides); + }); + + it('Test Staking Settings', async function () { + await network.provider.request({ + method: 'hardhat_impersonateAccount', + params: [SUPER_WETH_LP_HOLDER] + }); + let superWethBags = await ethers.provider.getSigner(SUPER_WETH_LP_HOLDER); + + let programLength = 2592000; // one month + let rewardAmount = ethers.utils.parseEther('1000000'); + let rewardRate = rewardAmount.div(programLength); + + const connectedStake = await SuperStaking.connect(superDeployer); + await connectedStake.setRewardRate(rewardRate, 1, overrides); + expect(await connectedStake.rewardRate()).to.equal(rewardRate); + + await connectedStake.notifyRewardAmount(rewardAmount, overrides); + + await SuperLP.connect(superWethBags).approve(SuperStaking.address, ethers.utils.parseEther('2000000'), overrides); + SuperStaking = SuperStaking.connect(superWethBags); + let stakingAmount = await ethers.utils.parseEther('20'); + await SuperStaking.stake(stakingAmount, overrides); + + expect( + await SuperLP.balanceOf(SuperStaking.address) + ).to.equal(stakingAmount); + + fastForward(programLength); + }); + + it('Check Earnings', async function () { + await SuperStaking.stake(1, overrides); // call updatepool modifier indirectly + expect( + await SuperStaking.earned(SUPER_WETH_LP_HOLDER, overrides) + ).to.be.gt(ethers.utils.parseEther('999999')).and.to.be.lt(ethers.utils.parseEther('1000000')); + // const currentBlock = await ethers.provider.getBlockNumber(); + // const block = await ethers.provider.getBlock(currentBlock); + // let period = await SuperStaking.periodFinish(); + // console.log(period.toString(), block.timestamp, 'periodFinish'); + + await network.provider.request({ + method: 'hardhat_reset', + params: [] + }); + }); +});