Created
May 20, 2022 13:04
-
-
Save gracelungu/4a0e1a2fea90c3677fe41fcb974795c0 to your computer and use it in GitHub Desktop.
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 { Contract } from "ethers"; | |
import { ethers } from "hardhat"; | |
describe("LockSave", function () { | |
let lockSave: Contract; | |
beforeEach(async () => { | |
// Deploy the contract | |
const LockSave = await ethers.getContractFactory("LockSave"); | |
lockSave = await LockSave.deploy(); | |
await lockSave.deployed(); | |
}); | |
type Saving = { | |
address: string; | |
value: string; | |
timestamp: number; | |
withdrawTimestamp: number; | |
}; | |
it("Should save and retrieve the savings data", async function () { | |
// Get the signer | |
const [sender] = await ethers.getSigners(); | |
// Amount to save | |
const ethAmount = "0.001"; | |
const weiAmount = ethers.utils.parseEther(ethAmount); | |
const transaction = { | |
value: weiAmount, | |
}; | |
// Withdraw timestamp | |
const withdrawTime = Date.now() + 1000; | |
// Save the amount | |
await lockSave.save(withdrawTime, transaction); | |
// Get savings | |
const savings = await lockSave.getSavings(); | |
// Extract savings values | |
const saving = savings.values().next().value; | |
const [owner, value, timestamp, withdrawTimestamp] = saving; | |
const currentSaving: Saving = { | |
address: sender.address, | |
value: weiAmount.toString(), | |
timestamp: Date.now(), | |
withdrawTimestamp: withdrawTime, | |
}; | |
const expectedSaving: Saving = { | |
address: owner, | |
value: value.toString(), | |
timestamp: timestamp.toNumber(), | |
withdrawTimestamp: withdrawTimestamp.toNumber(), | |
}; | |
// Assert saving values | |
expect(currentSaving.address).to.equal(expectedSaving.address); | |
expect(currentSaving.value).to.equal(currentSaving.value); | |
expect( | |
currentSaving.withdrawTimestamp - expectedSaving.timestamp | |
).to.be.greaterThan(0); | |
expect(currentSaving.withdrawTimestamp).to.equal( | |
expectedSaving.withdrawTimestamp.toString() | |
); | |
}); | |
it("Should withdraw the savings", async function () { | |
// Amount to save | |
const ethAmount = "0.001"; | |
const weiAmount = ethers.utils.parseEther(ethAmount); | |
const transaction = { | |
value: weiAmount, | |
}; | |
// Withdraw timestamp | |
const withdrawTime = Date.now() + 1; | |
// Save the amount | |
await lockSave.save(withdrawTime, transaction); | |
// Get savings | |
const savings = await lockSave.getSavings(); | |
// Extract the timestamp | |
const saving = savings.values().next().value; | |
const [, , timestamp] = saving; | |
// Withdraw the amount | |
await lockSave.withdraw(timestamp); | |
// Get savings | |
const ownerSavings = await lockSave.getSavings(); | |
expect(ownerSavings.length).to.be.eq(0); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment