Skip to content

Commit

Permalink
add ability to stake
Browse files Browse the repository at this point in the history
  • Loading branch information
pysel committed Nov 25, 2024
1 parent 7aa02a2 commit c27e3c1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/ClvrHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { ClvrIntentPool } from "./ClvrIntentPool.sol";
import { ClvrModel } from "./ClvrModel.sol";
import {PoolSwapTest} from "@uniswap/v4-core/src/test/PoolSwapTest.sol";

import {ClvrStake} from "./ClvrStake.sol";

import {console} from "forge-std/console.sol";


contract ClvrHook is BaseHook {
contract ClvrHook is BaseHook, ClvrStake {
using SafeCast for *;
using StateLibrary for IPoolManager;
using BalanceDeltaLibrary for BalanceDelta;
Expand All @@ -43,7 +46,7 @@ contract ClvrHook is BaseHook {

address private constant BATCH = address(0);

mapping(PoolId => SwapParamsExtended[]) public swapParams;
mapping(PoolId => SwapParamsExtended[]) public swapParams; // per pool scheduled swaps (their params)

ClvrModel private model;
PoolSwapTest swapRouter;
Expand Down Expand Up @@ -93,8 +96,10 @@ contract ClvrHook is BaseHook {
address recepient = abi.decode(data, (address));

if (recepient == BATCH) {
// TODO: make sure this can't easily be called
// perform the swap right away
if (!isStakedScheduler(key, sender)) {
revert("Only staked schedulers can schedule swaps");
}

return (
BaseHook.beforeSwap.selector,
BeforeSwapDeltaLibrary.ZERO_DELTA,
Expand Down
33 changes: 33 additions & 0 deletions src/ClvrStake.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {PoolId} from "@uniswap/v4-core/src/types/PoolId.sol";

contract ClvrStake {

uint256 public constant STAKE_AMOUNT = 1 ether;

mapping(PoolId => mapping(address => bool)) public stakedSchedulers; // per pool addresses that can schedule swaps

modifier onlyStakedScheduler(PoolKey calldata key) {
require(isStakedScheduler(key, msg.sender), "Only staked schedulers can call this function");
_;
}

constructor() {}

function stake(PoolKey calldata key, address scheduler) payable public {
require(msg.value == STAKE_AMOUNT, "Must stake 1 ETH to stake");
stakedSchedulers[key.toId()][scheduler] = true;
}

function unstake(PoolKey calldata key, address scheduler) external onlyStakedScheduler(key) {
stakedSchedulers[key.toId()][scheduler] = false;
payable(msg.sender).transfer(STAKE_AMOUNT);
}

function isStakedScheduler(PoolKey calldata key, address scheduler) internal view returns (bool) {
return stakedSchedulers[key.toId()][scheduler];
}
}

0 comments on commit c27e3c1

Please sign in to comment.