Skip to content

Commit

Permalink
Update staking logic
Browse files Browse the repository at this point in the history
Add min and max staking amount. Currently, functions that return these
values have hardcoded values, but in the future we want to pull this
param from "parameters" contract that stores governable params.
  • Loading branch information
r-czajkowski committed Oct 30, 2023
1 parent 3ba358d commit 63736e1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
18 changes: 17 additions & 1 deletion core/contracts/staking/TokenStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,24 @@ contract TokenStaking is IReceiveApproval {
_stake(msg.sender, amount);
}

/// @notice Returns minimum amount of staking tokens to participate in
/// protocol.
function minimumStake() public pure returns (uint256) {
// TODO: Fetch this param from "parameters" contract that stores
// governable params.
return 1;
}

/// @notice Returns maximum amount of staking tokens.
function maximumStake() public pure returns (uint256) {
// TODO: Fetch this param from "parameters" contract that stores
// governable params.
return 100 ether;
}

function _stake(address account, uint256 amount) private {
require(amount > 0, "Amount is less than minimum");
require(amount >= minimumStake(), "Amount is less than minimum");
require(amount <= maximumStake(), "Amount is greater than maxium");
require(account != address(0), "Can not be the zero address");

balanceOf[account] += amount;
Expand Down
36 changes: 26 additions & 10 deletions core/test/staking/TokenStaking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ describe("TokenStaking", () => {
})

describe("staking", () => {
const amountToStake = WeiPerEther * 10n

describe("when staking via staking contract directly", () => {
beforeEach(async () => {
// Infinite approval for staking contract.
Expand All @@ -48,41 +50,55 @@ describe("TokenStaking", () => {

it("should stake tokens", async () => {
const tokenHolderAddress = await tokenHolder.getAddress()
const tokenBalance = await token.balanceOf(tokenHolderAddress)
const tokenBalanceBeforeStake =
await token.balanceOf(tokenHolderAddress)

await expect(tokenStaking.connect(tokenHolder).stake(tokenBalance))
await expect(tokenStaking.connect(tokenHolder).stake(amountToStake))
.to.emit(tokenStaking, "Staked")
.withArgs(tokenHolderAddress, tokenBalance)
.withArgs(tokenHolderAddress, amountToStake)
expect(await tokenStaking.balanceOf(tokenHolderAddress)).to.be.eq(
tokenBalance,
amountToStake,
)
expect(await token.balanceOf(tokenHolderAddress)).to.be.eq(
tokenBalanceBeforeStake - amountToStake,
)
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")
})

it("should revert if the staked amount is grater than maxium stake amount", async () => {
const maxAmount = await tokenStaking.maximumStake()

await expect(
tokenStaking.connect(tokenHolder).stake(maxAmount + 1n),
).to.be.revertedWith("Amount is greater than maxium")
})
})

describe("when staking via staking token using approve and call pattern", () => {
it("should stake tokens", async () => {
const tokenHolderAddress = await tokenHolder.getAddress()
const tokenBalance = await token.balanceOf(tokenHolderAddress)
const tokenBalanceBeforeStake =
await token.balanceOf(tokenHolderAddress)
const tokenStakingAddress = await tokenStaking.getAddress()

await expect(
token
.connect(tokenHolder)
.approveAndCall(tokenStakingAddress, tokenBalance, "0x"),
.approveAndCall(tokenStakingAddress, amountToStake, "0x"),
)
.to.emit(tokenStaking, "Staked")
.withArgs(tokenHolderAddress, tokenBalance)
.withArgs(tokenHolderAddress, amountToStake)
expect(await tokenStaking.balanceOf(tokenHolderAddress)).to.be.eq(
tokenBalance,
amountToStake,
)
expect(await token.balanceOf(tokenHolderAddress)).to.be.eq(
tokenBalanceBeforeStake - amountToStake,
)
expect(await token.balanceOf(tokenHolderAddress)).to.be.eq(0)
})
})
})
Expand Down

0 comments on commit 63736e1

Please sign in to comment.