Skip to main content

KagePool Contract

The KagePool contract is the core component of our staking system, managing multiple staking pools with different parameters and handling all staking operations.

Contract Overview

Core Features

Multi-Pool Support

  • Create and manage multiple staking pools
  • Each pool has unique parameters:
    • Capacity limits
    • Minimum stake amounts
    • Lock durations
    • APY rates
    • Join time windows

Staking Operations

  • Deposit tokens
  • Withdraw tokens
  • Early withdrawal with penalties
  • Revenue allocation
  • Reward calculations

Contract API

Pool Management

kageAddPool

Creates a new staking pool with specified parameters.

function kageAddPool(
uint128 _cap,
uint128 _minStaked,
uint128 _lockDuration,
uint128 _startJoinTime,
uint128 _endJoinTime,
uint64 _APY
) external onlyOwner returns (uint256)

Parameters:

  • _cap: Maximum tokens that can be staked
  • _minStaked: Minimum tokens required to stake
  • _lockDuration: Duration tokens must be locked
  • _startJoinTime: Start time for joining pool
  • _endJoinTime: End time for joining pool
  • _APY: Annual Percentage Yield in basis points

Events:

event KagePoolCreated(
uint256 indexed poolId,
uint128 cap,
uint128 minStaked,
uint128 lockDuration,
uint64 APY
);

Staking Operations

kageDeposit

Deposits tokens into a specified pool.

function kageDeposit(
uint256 _poolId,
uint128 _amount
) external nonReentrant whenNotPaused

Parameters:

  • _poolId: Target pool ID
  • _amount: Amount of tokens to stake

Requirements:

  • Pool must exist and be active
  • Amount must meet minimum stake requirement
  • Pool must not be full
  • User must have sufficient tokens

Events:

event KageDeposit(
uint256 indexed poolId,
address indexed user,
uint128 amount
);

Withdrawal Operations

kageWithdraw

Withdraws tokens after lock period.

function kageWithdraw(
uint256 _poolId,
uint128 _amount
) external nonReentrant

Parameters:

  • _poolId: Target pool ID
  • _amount: Amount to withdraw

kageEarlyWithdrawAll

Withdraws all tokens before lock period with penalty.

function kageEarlyWithdrawAll(
uint256 _poolId
) external nonReentrant

Revenue Distribution

kageAllocateRevenue

Allocates revenue across specified pools.

function kageAllocateRevenue(
uint256[] calldata _poolIds,
uint128[] calldata _amounts,
bool _accumulative
) external onlyRewardDistributor

Examples

Creating a New Pool

// Create a 30-day staking pool with 10% APY
await kagePool.kageAddPool(
ethers.utils.parseEther("1000000"), // 1M tokens cap
ethers.utils.parseEther("100"), // 100 tokens minimum
30 days, // 30 day lock
Math.floor(Date.now() / 1000), // Start now
Math.floor(Date.now() / 1000) + 90 days, // End in 90 days
1000 // 10% APY (basis points)
);

Staking Tokens

// Approve tokens first
await token.approve(kagePool.address, amount);

// Stake tokens
await kagePool.kageDeposit(0, amount);

Withdrawing Tokens

// Regular withdrawal (after lock period)
await kagePool.kageWithdraw(0, amount);

// Early withdrawal (with penalty)
await kagePool.kageEarlyWithdrawAll(0);

Gas Optimization

The contract implements several gas optimization techniques:

  1. Storage Packing
struct KagePoolInfo {
uint128 cap; // Slot 1
uint128 minStaked; // Slot 1
uint128 totalStaked; // Slot 2
uint128 lockDuration; // Slot 2
// ... more packed variables
}

  1. Efficient Mappings
// O(1) lookups for user info
mapping(uint256 => mapping(address => UserInfo)) private userInfo;
  1. Batch Operations
function kageAllocateRevenue(
uint256[] calldata _poolIds,
uint128[] calldata _amounts,
bool _accumulative
) external {
// Process multiple pools in one transaction
}

Security Features

Access Control

  • Owner-only pool management
  • Reward distributor authorization
  • Emergency pause mechanism

Safety Checks

modifier nonReentrant() {
require(_notEntered, "ReentrancyGuard: reentrant call");
_notEntered = false;
_;
_notEntered = true;
}

Emergency Functions

function pause() external onlyOwner {
_pause();
}

function unpause() external onlyOwner {
_unpause();
}

Events

event KagePoolCreated(uint256 indexed poolId, ...);
event KageDeposit(uint256 indexed poolId, address indexed user, uint128 amount);
event KageWithdraw(uint256 indexed poolId, address indexed user, uint128 amount);
event KageEarlyWithdraw(uint256 indexed poolId, address indexed user, uint128 amount, uint128 penalty);
event RevenueAllocated(uint256 indexed poolId, uint128 amount, bool accumulative);

Error Codes

error InvalidPool();
error PoolFull();
error InsufficientAmount();
error LockPeriodNotEnded();
error NoStakedAmount();
error InvalidWithdrawAmount();

Testing

For detailed test cases and coverage, see our Test Cases documentation.

Audits

The contract has undergone thorough security audits. Key findings and remediations are documented in our Security Features section.