Created
November 21, 2023 15:17
-
-
Save SilviaMargaritaOcegueda/428986ec47c59397a1b4f25983809716 to your computer and use it in GitHub Desktop.
Hardhat deployment script and unit testing file from Base Camp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {HardhatRuntimeEnvironment} from 'hardhat/types'; | |
import { DeployFunction } from 'hardhat-deploy/types'; | |
import { ethers } from 'hardhat'; | |
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { | |
const { deploy } = hre.deployments; | |
const { deployer } = await hre.getNamedAccounts(); | |
const VALUE_LOCKED = hre.ethers.parseEther("0.01"); | |
const UNLOCK_TIME = 10000; | |
const blockNumber = await ethers.provider.getBlockNumber(); | |
const lastBlockTimeStamp = (await ethers.provider.getBlock(blockNumber))?.timestamp as number; | |
await deploy("Lock", { | |
from: deployer, | |
args: [lastBlockTimeStamp + UNLOCK_TIME], | |
value: VALUE_LOCKED.toString() | |
}) | |
}; | |
export default func; | |
func.tags = ["DeployAll"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {expect} from 'chai'; | |
import { ethers, deployments } from 'hardhat'; | |
import { time } from "@nomicfoundation/hardhat-network-helpers"; | |
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"; | |
import { Lock } from '../typechain-types'; | |
import { Lock__factory } from '../typechain-types/factories'; | |
describe('Lock', function () { | |
const UNLOCK_TIME = 10000; | |
const VALUE_LOCKED = ethers.parseEther('0.01'); | |
let lastBlockTimeStamp: number; | |
let lockInstance: Lock; | |
let ownerSigner: SignerWithAddress; | |
let otherUserSigner: SignerWithAddress; | |
before(async () => { | |
lastBlockTimeStamp = await time.latest(); | |
const signers = await ethers.getSigners(); | |
ownerSigner = signers[0]; | |
otherUserSigner = signers[1]; | |
await deployments.fixture(['DeployAll']); | |
const lockDeployment = await deployments.get('Lock'); | |
lockInstance = Lock__factory.connect(lockDeployment.address, ownerSigner); | |
}); | |
it('Should get the unlockTime value', async () => { | |
const unlockTime = await lockInstance.unlockTime(); | |
expect(unlockTime).to.equal(lastBlockTimeStamp + UNLOCK_TIME); | |
}); | |
it('Should have the right Ether balance', async () => { | |
const lockInstanceAddress = await lockInstance.getAddress(); | |
const lockInstanceBalance = await ethers.provider.getBalance(lockInstanceAddress); | |
expect(lockInstanceBalance).to.equal(VALUE_LOCKED); | |
}); | |
it('Should have the right owner', async () => { | |
const lockInstanceOwner = await lockInstance.owner(); | |
expect(lockInstanceOwner).to.equal(ownerSigner.address); | |
}); | |
it('Shouldn"t alow to withdraw before unlock time',async () => { | |
await expect(lockInstance.withdraw()).to.be.revertedWith("You can't withdraw yet"); | |
}); | |
it('Shouldn"t allow to withdraw a non owner', async () => { | |
const newLastBlockTimestamp = await time.latest(); | |
await time.setNextBlockTimestamp(newLastBlockTimestamp + UNLOCK_TIME); | |
const lockInstanceUsingAnotherSigner = lockInstance.connect(otherUserSigner); | |
await expect(lockInstanceUsingAnotherSigner.withdraw()).to.be.revertedWith("You aren't the owner"); | |
}) | |
it('Should allow to withdraw a owner', async () => { | |
const newLastBlockTimestamp = await time.latest(); | |
await time.setNextBlockTimestamp(newLastBlockTimestamp + UNLOCK_TIME); | |
await lockInstance.withdraw(); | |
const balanceAfter = await ethers.provider.getBalance(lockInstance.getAddress()); | |
expect(balanceAfter).to.equal(0); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment