contracts-imtbl/test/testSimpleStake.ts
2025-01-13 17:42:21 +08:00

101 lines
3.6 KiB
TypeScript

import { expect } from 'chai'
import hre from "hardhat";
import {
ZeroAddress,
getBytes,
solidityPackedKeccak256,
formatEther,
JsonRpcProvider,
} from 'ethers'
import {
loadFixture,
} from "@nomicfoundation/hardhat-toolbox/network-helpers";
import { expandDecimals, increaseTime, mineBlock, reportGasUsed } from './shared/utilities';
const secondsOneDay = BigInt(24 * 60 * 60);
const secondsPerYear = 365 * 24 * 60 * 60;
// 1.5 / (365*24*60*60)
const rewardPerSecond = BigInt(1.5 * 10 ** 18) / BigInt(secondsPerYear) ;
// 47564688000
console.log('rewardPerSecond: ', rewardPerSecond.toString())
const blockTime = async (provider: any) => {
const block = await provider.getBlock('latest')
return block ? block.timestamp : 0
}
describe('SimpleStake', function() {
async function deployOneContract() {
// Contracts are deployed using the first signer/account by default
const [owner, user0, user1, user2] = await hre.ethers.getSigners();
const chainId = hre.network.config.chainId
const Cec = await hre.ethers.getContractFactory("MintableBaseToken");
const cec = await Cec.deploy("test cec", "cec");
const SimpleStake = await hre.ethers.getContractFactory("SimpleStake");
const startTime = await blockTime(owner.provider)
const endTime = startTime + 14 * 24 * 60 * 60
const maxStakeAmount = expandDecimals(1000, 18)
const simpleStake = await SimpleStake.deploy(
cec.target,
startTime,
endTime,
maxStakeAmount
);
await cec.setMinter(owner.address, true)
return { owner, user0, user1, user2, chainId, cec, simpleStake, startTime, endTime };
}
describe("Staking CEC", function () {
it("stake unstake", async () => {
const {owner, user0, user1, user2, cec, simpleStake, startTime, endTime} = await loadFixture(deployOneContract);
const wallet = owner
const provider = wallet.provider;
await cec.setMinter(wallet.address, true)
await cec.mint(user0.address, expandDecimals(1500, 18))
expect(await cec.balanceOf(user0.address)).eq(expandDecimals(1500, 18))
// @ts-ignore
await cec.connect(user0).approve(simpleStake.target, expandDecimals(1001, 18))
// @ts-ignore
await expect(simpleStake.connect(user0).stake(expandDecimals(1100, 18)))
.to.be.revertedWith("Amount exceeds max stake amount")
// @ts-ignore
await simpleStake.connect(user0).stake(expandDecimals(800, 18))
let _startTime = await blockTime(provider)
console.log('startTime: ', _startTime)
expect(await cec.balanceOf(user0.address)).eq(expandDecimals(700, 18))
// @ts-ignore
await expect(simpleStake.connect(user0).stake(expandDecimals(300, 18)))
.to.be.revertedWith("Exceeds max stake amount")
// @ts-ignore
let stakeOne = await simpleStake.connect(user0).stakes(user0.address, 0)
console.log('stakes: ', stakeOne)
// @ts-ignore
let stakes = await simpleStake.connect(user0).userStakes(user0.address)
console.log('stakes: ', stakes)
let nowTime = await blockTime(provider)
console.log('nowTime: ', nowTime)
// @ts-ignore
await expect(simpleStake.connect(user0).unstake())
.to.be.revertedWith("Unlock time not reached")
const period = endTime - startTime
await increaseTime(provider, period )
await mineBlock(provider)
// @ts-ignore
await simpleStake.connect(user0).unstake()
expect(await cec.balanceOf(user0.address)).eq(expandDecimals(1500, 18))
// @ts-ignore
await expect(simpleStake.connect(user0).unstake())
.to.be.revertedWith("No stakes to unstake")
})
})
})