Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SigismundSchlomo committed Jun 26, 2024
1 parent 9249cc0 commit 319995c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 11 deletions.
2 changes: 1 addition & 1 deletion contracts/staking/token/IPoolsManager.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

interface IPoolsMsanager {
interface IPoolsManager {
//OWNER METHODS
function createPool(address token_, uint interest_, uint minStakeValue) external returns (address);
function deactivatePool(address pool_) external;
Expand Down
20 changes: 10 additions & 10 deletions contracts/staking/token/PoolsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ contract PoolsManager is Ownable, IPoolsManager {

RewardsBank public bank;

mapping(address => address) public pools; //maps the address of the token to the address of the pool
mapping(address => address) public tokenToPool; //maps the address of the token to the address of the pool
mapping(address => address) public poolToToken; //maps the address of the pool to the address of the token

constructor(RewardsBank bank_) Ownable() {
bank = bank_;
Expand All @@ -20,49 +21,48 @@ contract PoolsManager is Ownable, IPoolsManager {

function createPool(address token_, uint interest_, uint minStakeValue) public onlyOwner() returns (address) {
TokenPool pool = new TokenPool(token_, bank, interest_, minStakeValue);
pools[address(pool)] = address(pool);
tokenToPool[token_] = address(pool);
poolToToken[address(pool)] = token_;
emit PoolCreated(address(pool), token_, interest_, minStakeValue);
pool.activate();
emit PoolActivated(address(pool));
return address(pool);
}

function deactivatePool(address pool_) public onlyOwner() {
require(pools[pool_] != address(0), "Pool does not exist");
require(poolToToken[pool_] != address(0), "Pool does not exist");
TokenPool pool = TokenPool(pool_);
pool.deactivate();
emit PoolDeactivated(pool_);
}

function activatePool(address pool_) public onlyOwner() {
require(pools[pool_] != address(0), "Pool does not exist");
require(poolToToken[pool_] != address(0), "Pool does not exist");
TokenPool pool = TokenPool(pool_);
pool.activate();
emit PoolActivated(pool_);
}

function setInterest(address pool_, uint interest_) public onlyOwner() {
require(pools[pool_] != address(0), "Pool does not exist");
require(poolToToken[pool_] != address(0), "Pool does not exist");
TokenPool pool = TokenPool(pool_);
pool.setInterest(interest_);
}

function setMinStakeValue(address pool_, uint minStakeValue_) public onlyOwner() {
require(pools[pool_] != address(0), "Pool does not exist");
require(poolToToken[pool_] != address(0), "Pool does not exist");
TokenPool pool = TokenPool(pool_);
pool.setMinStakeValue(minStakeValue_);
}

function grantBackendRole(address pool_, address backend_) public onlyOwner() {
require(pools[pool_] != address(0), "Pool does not exist");
require(poolToToken[pool_] != address(0), "Pool does not exist");
TokenPool pool = TokenPool(pool_);
pool.grantRole(pool.BACKEND_ROLE(), backend_);
}

// VIEW METHODS

function getPool(address token_) public view returns (address) {
return pools[token_];
return tokenToPool[token_];
}

function getPoolInfo(address pool_) public view returns (address token, uint interest, uint minStakeValue, uint totalStake, uint totalShare,bool) {
Expand Down
87 changes: 87 additions & 0 deletions test/staking/token/PoolsManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { ethers, upgrades } from "hardhat";

import {
PoolsManager,
RewardsBank,
AirBond__factory,
RewardsBank__factory,
PoolsManager__factory,
} from "../../../typechain-types";

import { expect } from "chai";

describe("PoolsManager", function () {
let poolsManager: PoolsManager;
let rewardsBank: RewardsBank;
let tokenAddr: string;

async function deploy() {
const [owner] = await ethers.getSigners();

const rewardsBank = await new RewardsBank__factory(owner).deploy();
const airBond = await new AirBond__factory(owner).deploy(owner.address);

const poolsManager = await new PoolsManager__factory(owner).deploy(rewardsBank.address);

await (await rewardsBank.grantRole(await rewardsBank.DEFAULT_ADMIN_ROLE(), poolsManager.address)).wait();
const tokenAddr = airBond.address;

return { poolsManager, rewardsBank, tokenAddr };
}

beforeEach(async function () {
({ poolsManager, rewardsBank, tokenAddr } = await loadFixture(deploy));
});

describe("Pool Management", function () {
it("Should allow the owner to create a pool", async function () {
const interest = 100000; // 10%
const minStakeValue = 10;

const tx = await poolsManager.createPool(tokenAddr, interest, minStakeValue);
const receipt = await tx.wait();
const poolAddress = receipt.events![2].args!.pool;

expect(await poolsManager.getPool(tokenAddr)).to.equal(poolAddress);
});

it("Should activate and deactivate a pool", async function () {
const interest = 100000; // 10%
const minStakeValue = 10;

const tx = await poolsManager.createPool(tokenAddr, interest, minStakeValue);
const receipt = await tx.wait();
console.log(receipt.events);
const poolAddress = receipt.events![2].args!.pool;

await poolsManager.deactivatePool(poolAddress);
expect(await poolsManager.getPoolInfo(poolAddress)).to.include(false); // Pool should be inactive

await poolsManager.activatePool(poolAddress);
expect(await poolsManager.getPoolInfo(poolAddress)).to.include(true); // Pool should be active
});

it("Should allow the owner to set interest and min stake value", async function () {
const interest = 100000; // 10%
const minStakeValue = 10;

const tx = await poolsManager.createPool(tokenAddr, interest, minStakeValue);
const receipt = await tx.wait();
const poolAddress = receipt.events![2].args![0];

const newInterest = 300000; // 30%
await poolsManager.setInterest(poolAddress, newInterest);
const [,interestSet,,,] = await poolsManager.getPoolInfo(poolAddress);
expect(interestSet).to.equal(newInterest);

const newMinStakeValue = 20;
await poolsManager.setMinStakeValue(poolAddress, newMinStakeValue);
const [,,minStakeValueSet,,,] = await poolsManager.getPoolInfo(poolAddress);
expect(minStakeValueSet).to.equal(newMinStakeValue);
});

});

});

Empty file added test/staking/token/TokenPool.ts
Empty file.

0 comments on commit 319995c

Please sign in to comment.