-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
stake
function to TokenStaking
contract
Add function that stakes owner's tokens in the staking contract - tracks user's staked funds and emit `Staked` event. The minimum amount is required.
- Loading branch information
1 parent
3448952
commit ad7fd74
Showing
3 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
pragma solidity 0.8.20; | ||
|
||
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
||
contract Token is ERC20 { | ||
constructor() ERC20("Test Token", "TEST") {} | ||
|
||
function mint(address account, uint256 value) external { | ||
_mint(account, value); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers" | ||
import { ethers } from "hardhat" | ||
import { expect } from "chai" | ||
import { Token, TokenStaking } from "../../typechain" | ||
import { WeiPerEther } from "ethers" | ||
import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers" | ||
import { before } from "mocha" | ||
|
||
async function tokenStakingFixture() { | ||
const [deployer, tokenHolder] = await ethers.getSigners() | ||
const StakingToken = await ethers.getContractFactory("Token") | ||
const token = await StakingToken.deploy() | ||
|
||
const amountToMint = WeiPerEther * 10000n | ||
|
||
token.mint(tokenHolder, amountToMint) | ||
|
||
const TokenStaking = await ethers.getContractFactory("TokenStaking") | ||
const tokenStaking = await TokenStaking.deploy(await token.getAddress()) | ||
|
||
return { tokenStaking, token, tokenHolder } | ||
} | ||
|
||
describe("TokenStaking", () => { | ||
let tokenStaking: TokenStaking | ||
let token: Token | ||
let tokenHolder: HardhatEthersSigner | ||
|
||
beforeEach(async () => { | ||
const { | ||
tokenStaking: _tokenStaking, | ||
token: _token, | ||
tokenHolder: _tokenHolder, | ||
} = await loadFixture(tokenStakingFixture) | ||
|
||
tokenStaking = _tokenStaking | ||
token = _token | ||
tokenHolder = _tokenHolder | ||
}) | ||
|
||
describe("staking", () => { | ||
beforeEach(async () => { | ||
// Infinite approval for staking contract. | ||
await token | ||
.connect(tokenHolder) | ||
.approve(await tokenStaking.getAddress(), ethers.MaxUint256) | ||
}) | ||
|
||
it("should stake tokens", async () => { | ||
const tokenHolderAddress = await tokenHolder.getAddress() | ||
const tokenBalance = await token.balanceOf(tokenHolderAddress) | ||
|
||
await expect(tokenStaking.connect(tokenHolder).stake(tokenBalance)) | ||
.to.emit(tokenStaking, "Staked") | ||
.withArgs(tokenHolderAddress, tokenBalance) | ||
expect(await tokenStaking.balanceOf(tokenHolderAddress)).to.be.eq( | ||
tokenBalance, | ||
) | ||
expect(await token.balanceOf(tokenHolderAddress)).to.be.eq(0) | ||
}) | ||
|
||
it("should revert if the staked amount is less than required minimum", async () => { | ||
await expect(tokenStaking.connect(tokenHolder).stake(0)) | ||
.to.be.revertedWith("Amount is less than minimum") | ||
}) | ||
}) | ||
}) |