Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jung/staking #69

Open
wants to merge 158 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 68 commits
Commits
Show all changes
158 commits
Select commit Hold shift + click to select a range
1be8e65
Add staking contracts with interfaces
Frenchkebab Aug 23, 2024
c8dedb3
add Enumerable Set for tokens
Frenchkebab Aug 28, 2024
67b6c38
add getter for stake amount
Frenchkebab Aug 28, 2024
bf7fdb6
add contract inheritance
Frenchkebab Aug 29, 2024
27fafca
add add/remove token feature
Frenchkebab Aug 29, 2024
f4441fd
add supportedSignature feature
Frenchkebab Aug 29, 2024
3b6dbca
implement stake/operatorSelfStake logic
Frenchkebab Aug 29, 2024
215b33c
add interfaces
Frenchkebab Aug 29, 2024
78f02d0
add getter for signature support
Frenchkebab Aug 29, 2024
9936d1c
add getters for stake
Frenchkebab Aug 29, 2024
9126919
add unstake
Frenchkebab Aug 29, 2024
5f873f6
add userStakeInfo mapping
Frenchkebab Aug 29, 2024
657119c
add token transfer logic
Frenchkebab Aug 29, 2024
170567c
todos
Frenchkebab Aug 29, 2024
c8928f1
add check logic for submitSnapshot
Frenchkebab Aug 29, 2024
4ba1ee4
implement submitSnapshot logic
Frenchkebab Aug 29, 2024
e3a947a
add mapping for slash result submission
Frenchkebab Aug 30, 2024
33c92e8
fix return fix logic for getters
Frenchkebab Aug 30, 2024
335deeb
add Snapshot submission logic
Frenchkebab Aug 30, 2024
7193efb
add operator snapshot update logic
Frenchkebab Aug 30, 2024
1291204
add vault snapshot logic
Frenchkebab Aug 30, 2024
98499ee
add slash result submission logic
Frenchkebab Aug 30, 2024
b27093d
refactor code
Frenchkebab Aug 30, 2024
4d8461e
finish submitSlashResultData logic
Frenchkebab Aug 30, 2024
42a6c02
fix typo
Frenchkebab Aug 30, 2024
933809d
fix typo
Frenchkebab Aug 30, 2024
0a277b7
add upgradeability to StakingManager
Frenchkebab Aug 31, 2024
40e0b01
add logic for jobCreation and stake lock
Frenchkebab Sep 1, 2024
2463e2c
implement unlock stake
Frenchkebab Sep 2, 2024
1d90243
fix compile erorrs
Frenchkebab Sep 3, 2024
2964e1d
remove slashjob
Frenchkebab Sep 3, 2024
40e4eee
add lockPool to IKalypsoStaking
Frenchkebab Sep 3, 2024
eb8a9af
add core feature in JobManager
Frenchkebab Sep 10, 2024
a65ea70
create SymbioticStakingReward contract
Frenchkebab Sep 12, 2024
5890845
add upgradeable contracts inheritance
Frenchkebab Sep 12, 2024
1738b0f
add logic
Frenchkebab Sep 13, 2024
e239d01
add _stakeToken and _rewardToken
Frenchkebab Sep 13, 2024
a10c4c3
import Math, EnumerableSet
Frenchkebab Sep 13, 2024
d6ebec6
add active stake and more update logic
Frenchkebab Sep 13, 2024
c788c51
refactor reward logic
Frenchkebab Sep 13, 2024
467efcd
refactor admin functions
Frenchkebab Sep 13, 2024
6525aa5
refactor code
Frenchkebab Sep 13, 2024
c2060cb
create NativeStakingReward contract
Frenchkebab Sep 14, 2024
5c00314
remove everything related to jobManager
Frenchkebab Sep 14, 2024
59e6209
add initialize
Frenchkebab Sep 14, 2024
3fe574e
add rewardPerToken logic
Frenchkebab Sep 14, 2024
46369f6
fix mappings structure
Frenchkebab Sep 15, 2024
66a646c
add update logic
Frenchkebab Sep 16, 2024
dac1008
remove lock getting deducted in RewardDistributors
Frenchkebab Sep 17, 2024
230b8ba
fix signature for lockStake
Frenchkebab Sep 18, 2024
dfc1a8b
fix jobCreation logic
Frenchkebab Sep 18, 2024
97d4152
add unlock logic
Frenchkebab Sep 18, 2024
7dab574
resolve cimpile error
Frenchkebab Sep 18, 2024
842c686
fix NativeStaking stake logic
Frenchkebab Sep 19, 2024
10d3ab6
hardhat config for compile
Frenchkebab Sep 19, 2024
3cc6914
fix withdrawStake logic
Frenchkebab Sep 19, 2024
5bb69e6
reflect review comments
Frenchkebab Sep 23, 2024
a464606
fix mask from bytes4 to bytes32
Frenchkebab Sep 23, 2024
fe4a8dc
move lockStake logic from StsakingManager to NativeStaking
Frenchkebab Sep 24, 2024
9f54798
refactor _selectToken logic
Frenchkebab Sep 24, 2024
2b5a282
move lockStake logic to SymbioticStaking from StakingManager
Frenchkebab Sep 24, 2024
a766094
refactor code
Frenchkebab Sep 24, 2024
8a1e5c2
fix SD, TC to be more verbose name
Frenchkebab Sep 25, 2024
f00b55a
minor fix
Frenchkebab Sep 25, 2024
cd62599
add comments
Frenchkebab Sep 25, 2024
2c6cb90
comment out unnecessary variables
Frenchkebab Sep 25, 2024
a2c92ed
force Snapshot submission to be sequential
Frenchkebab Sep 25, 2024
8d20006
consolidate OperatorSnapshot into VaultSnapshot
Frenchkebab Sep 25, 2024
6d0c15f
remove unnecessary comment
Frenchkebab Sep 25, 2024
2c8a0bc
fix typo
Frenchkebab Sep 25, 2024
c67c32b
fix transmitterComission to baseTransmitterComission
Frenchkebab Sep 25, 2024
b202fe8
fix IKalysoStaking to IStakingPool
Frenchkebab Sep 25, 2024
193a86b
init jobManager
Frenchkebab Sep 25, 2024
a3d605e
remove unused struct
Frenchkebab Sep 25, 2024
1f4afc1
remove minStake from StakingManager
Frenchkebab Sep 25, 2024
dac7f80
remove isEnabledPool mapping
Frenchkebab Sep 25, 2024
2262916
remove unlockEpoch
Frenchkebab Sep 25, 2024
8d86dd3
fix submitProof logic
Frenchkebab Sep 25, 2024
ffea6b6
remove operatorSet
Frenchkebab Sep 25, 2024
245beb7
fix lockStake logic
Frenchkebab Sep 26, 2024
d265222
fix account to msg.sender
Frenchkebab Sep 26, 2024
819c13f
remove selfstake
Frenchkebab Sep 26, 2024
f31ae90
remove account param
Frenchkebab Sep 26, 2024
2dce130
withdraw checks operatorActiveStakeAmount
Frenchkebab Sep 26, 2024
3195325
add todo comment
Frenchkebab Sep 26, 2024
fae46b1
remove minStakeAmount and fix typo
Frenchkebab Sep 26, 2024
948ea0e
remove locked token address from StakingManager
Frenchkebab Sep 26, 2024
e60d824
add deadline logic
Frenchkebab Sep 26, 2024
2575a1f
remove supported signature
Frenchkebab Sep 26, 2024
cd47892
add refundFee
Frenchkebab Sep 26, 2024
2204958
add upgradeability
Frenchkebab Sep 26, 2024
8c2cbe0
fix param variable
Frenchkebab Sep 26, 2024
c934123
resolve TODOs for StakingManager
Frenchkebab Sep 26, 2024
683e429
remove onlyAdmin
Frenchkebab Sep 26, 2024
f4df6e1
add missing admin functions
Frenchkebab Sep 26, 2024
a421c16
remove unnecessary functions
Frenchkebab Sep 26, 2024
f8422ec
add admin function
Frenchkebab Sep 26, 2024
96ffa9f
refactor code
Frenchkebab Sep 26, 2024
fb50a1c
add missing admin functions
Frenchkebab Sep 26, 2024
3c2e6fa
add upgradeability
Frenchkebab Sep 26, 2024
2a2c043
remove onlyAdmin
Frenchkebab Sep 26, 2024
a04883b
remove transmitter from setShare
Frenchkebab Sep 26, 2024
a5a761d
add unlockStake logic
Frenchkebab Sep 26, 2024
082b2e3
implement completeSubmission logic
Frenchkebab Sep 26, 2024
4d0357f
fix setShare logic
Frenchkebab Sep 26, 2024
4412bf6
update unlockStake logic
Frenchkebab Sep 26, 2024
42aba9f
implement slashing logic
Frenchkebab Sep 27, 2024
4bd8823
move struct to Struct.sol
Frenchkebab Sep 27, 2024
c716bd0
remove lock/unlock from SymbioticStakingReward
Frenchkebab Sep 28, 2024
6db8ede
complete reward distribution logic
Frenchkebab Sep 30, 2024
66ca214
refactor code
Frenchkebab Sep 30, 2024
2ca8ae2
refactor code
Frenchkebab Sep 30, 2024
9513350
logic when operatorStakeAmount < operatorLockAmount
Frenchkebab Sep 30, 2024
4b3dd14
add logic for inflation reward emission
Frenchkebab Oct 1, 2024
359f637
refactor reward distribution logic
Frenchkebab Oct 1, 2024
9671f96
refactor _update logic
Frenchkebab Oct 1, 2024
5ea15b5
update reward claim logic
Frenchkebab Oct 1, 2024
dec69fd
fix reward distribution logic
Frenchkebab Oct 2, 2024
d3d7f24
refactor code
Frenchkebab Oct 5, 2024
25a981f
reimplement symbioticStakingReward
Frenchkebab Oct 5, 2024
2696f61
fix symbiotic reward logic
Frenchkebab Oct 5, 2024
06276b8
fix compile errors
Frenchkebab Oct 5, 2024
9339d7d
fix transmitter comission rate logic
Frenchkebab Oct 5, 2024
a1798aa
add comments
Frenchkebab Oct 5, 2024
18c98a6
add getter for pendingInflationReward
Frenchkebab Oct 5, 2024
c2f1192
fix variable name
Frenchkebab Oct 5, 2024
5b75efa
temporarily comment out
Frenchkebab Oct 5, 2024
29fa07d
temporarily comment out
Frenchkebab Oct 5, 2024
a5d9da4
add comments
Frenchkebab Oct 5, 2024
99b3d3c
format code
Frenchkebab Oct 7, 2024
cc59e38
add 2step withdrawal
Frenchkebab Oct 7, 2024
e74888c
fix typo
Frenchkebab Oct 7, 2024
38fed2d
resolve compile error
Frenchkebab Oct 7, 2024
c6caeb0
remove totalFeeStored
Frenchkebab Oct 7, 2024
0d11aa9
symbioticStakingReward pulls reward
Frenchkebab Oct 8, 2024
5f8a4d3
initialize foundry
Frenchkebab Oct 8, 2024
77ca2da
add comment
Frenchkebab Oct 8, 2024
696b8e7
remove internal change to add public
Frenchkebab Oct 8, 2024
fb9aed3
refactor code
Frenchkebab Oct 8, 2024
974df18
refactor SymbioticStakingLock
Frenchkebab Oct 8, 2024
4b74dbd
remove epoch array and replace with single variable
Frenchkebab Oct 8, 2024
d1109ca
add InflationRewardManager contract
Frenchkebab Oct 9, 2024
106fc42
send comission to operator
Frenchkebab Oct 9, 2024
82442e3
add interaction with InflationRewardManager
Frenchkebab Oct 10, 2024
7ef70c5
add transmitter comission feature
Frenchkebab Oct 10, 2024
f6f8231
fix timestampIdx logic
Frenchkebab Oct 10, 2024
1376f23
implement weight based token selection
Frenchkebab Oct 10, 2024
7424f80
add gaps
Frenchkebab Oct 10, 2024
57ac65d
foundry test setup
Frenchkebab Oct 10, 2024
a245830
consider the case when inflation reward is not enough
Frenchkebab Oct 10, 2024
9a6019f
minor fix
Frenchkebab Oct 11, 2024
30e11a2
test setup
Frenchkebab Oct 11, 2024
22ddb9d
fix token selection algorithm in NativeStaking
Frenchkebab Oct 11, 2024
a4b58a2
minor fix
Frenchkebab Oct 11, 2024
ae2ac28
test operator self stake
Frenchkebab Oct 11, 2024
d1fba22
fix unexpected logic
Frenchkebab Oct 14, 2024
95f7fcb
e2e test
Frenchkebab Oct 14, 2024
45d17de
format import orders
Frenchkebab Oct 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contracts/interfaces/IL2Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
pragma solidity ^0.8.0;

interface IL2Staking {
function stake(address generatorAddress, uint256 amount) external returns (uint256);

function intendToReduceStake(uint256 stakeToReduce) external;

function stake(address generatorAddress, uint256 amount) external returns (uint256);

function unstake(address receiver) external;
}
23 changes: 23 additions & 0 deletions contracts/interfaces/staking/IKalypsoStaking.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

interface IKalypsoStaking {
function isSupportedToken(address _token) external view returns (bool);

// function getPoolStake(address _operator, address _token) external view returns (uint256);

function lockStake(uint256 _jobId, address _operator) external; // Staking Manager only

function unlockStake(uint256 _jobId) external; // Staking Manager only

struct PoolLockInfo {
address token;
uint256 amount;
address transmitter;
}

// struct NativeStakingLock {
// address token;
// uint256 amount;
// }
}
19 changes: 19 additions & 0 deletions contracts/interfaces/staking/INativeStaking.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import {IKalypsoStaking} from "../staking/IKalypsoStaking.sol";

interface INativeStaking is IKalypsoStaking {

// TODO: check if timestamp is needed
event Staked(address indexed account, address indexed operator, address indexed token, uint256 amount, uint256 timestamp);
event SelfStaked(address indexed operator, address indexed token, uint256 amount, uint256 timestamp);
event StakeWithdrawn(address indexed account, address indexed operator, address indexed token, uint256 amount, uint256 timestamp);
event SelfStakeWithdrawn(address indexed operator, address indexed token, uint256 amount, uint256 timestamp);

// function stakeOf(address _operator, address _token) external view returns (uint256);

// function stakesOf(address _operator) external view returns (address[] memory _tokens, uint256[] memory _amounts);

// function isSupportedSignature(bytes4 sig) external view returns (bool);
}
7 changes: 7 additions & 0 deletions contracts/interfaces/staking/INativeStakingReward.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.26;

interface INativeStakingReward {
function update(address account, address _stakeToken, address _operator) external;
}
15 changes: 15 additions & 0 deletions contracts/interfaces/staking/IStakingManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.26;

interface IStakingManager {
function onJobCreation(uint256 jobId, address operator) external;

function onJobCompletion(uint256 jobId, address token) external;

function submitProofs(uint256[] memory jobIds, bytes[] calldata proofs) external;

function submitProof(uint256 jobId, bytes calldata proof) external;

function setStakingManager(address _stakingManager) external;
}
47 changes: 47 additions & 0 deletions contracts/interfaces/staking/ISymbioticStaking.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

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

interface ISymbioticStaking is IKalypsoStaking {
// function stakeOf(address _operator, address _token) external view returns (uint256);

struct SnapshotTxCountInfo {
uint256 idxToSubmit; // idx of pratial snapshot tx to submit
uint256 numOfTxs; // total number of txs for the snapshot
}

struct VaultSnapshot {
address operator;
address vault;
address token;
uint256 stake;
}

struct SlashResultData {
uint256 jobId;
SlashResult slashResult;
}

struct SlashResult {
uint256 slashAmount;
address rewardAddress; // address that transmitted slash reqeust to L1 Vault
}

struct ConfirmedTimestamp {
uint256 capturedTimestamp;
uint256 rewardShare; // TODO
address transmitter;
}

// event OperatorSnapshotSubmitted

// event VaultSnapshotSubmitted

// event SlashResultSubmitted

// event SubmissionCompleted

/// @notice Returns the captureTimestamp of latest completed snapshot submission
function lastConfirmedTimestamp() external view returns (uint256);
}
68 changes: 68 additions & 0 deletions contracts/staking/l2_contracts/JobManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import {IStakingManager} from "../../interfaces/staking/IStakingManager.sol";

/*
JobManager contract is responsible for creating and managing jobs.
Staking Manager contract is responsible for locking/unlocking tokens and distributing rewards.
*/
contract JobManager {
uint256 constant JOB_DURATION = 1 days;
Frenchkebab marked this conversation as resolved.
Show resolved Hide resolved

IStakingManager stakingManager;

struct JobInfo {
address operator;
address lockToken;
uint256 lockedAmount; // this will go to slasher if the proof is not submitted before deadline
uint256 deadline;
address dataTransmitter; //
}

mapping(uint256 => JobInfo) public jobs;

// TODO: check paramter for job details
function createJob(uint256 jobId, address operator) external {
// TODO: called only from Kalypso Protocol

// TODO: create a job and record StakeData Transmitter who submitted capture timestamp


// TODO: call creation function in StakingManager
stakingManager.onJobCreation(jobId, operator); // lock stake
}

/**
* @notice Submit Single Proof
*/
function submitProof(uint256 jobId, bytes calldata proof) public {
_verifyProof(jobId, proof);
Frenchkebab marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @notice Submit Multiple proofs in single transaction
*/
function submitProofs(uint256[] calldata jobIds, bytes[] calldata proofs) external {
require(jobIds.length == proofs.length, "Invalid Length");

// TODO: close job and distribute rewards

uint256 len = jobIds.length;
for (uint256 idx = 0; idx < len; idx++) {
_verifyProof(jobIds[idx], proofs[idx]);

uint256 jobId;
Frenchkebab marked this conversation as resolved.
Show resolved Hide resolved
stakingManager.onJobCompletion(jobId, jobs[jobId].lockToken); // unlock stake
}

}

function _verifyProof(uint256 jobId, bytes calldata proof) internal {
// TODO: verify proof
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slash functions need to be added

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this supposed to be automatically slashed for not submitting proof?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user can manually slash if they want to. Only native staking is slashed(they are still stored with contract to be given to slash_transmitter) in that case and user gets their tokens for the job back.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[discuss]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. slash by user (NativeStaking)
  2. slash by slash trasmitter (Symbiotic Staking)

if manual)
the anyone(most likely fee payer) calls JobManager.slash() and this

  • refund the fee paid

  1. if manual slash -> 2step
    step1) manual fee refund
    step2) unlock by slash result transmission

  2. 1 step
    slash result submission unlocks the stake(operator's locked stake will be given to the slashRequest transmitter)

  • if fee is not refunded then refund

what's needed
: manual fee refund mechanism (will need to store the amount of fee paid when job creation)

function setStakingManager(address _stakingManager) external {
stakingManager = IStakingManager(_stakingManager);
}
}
Loading