22pragma solidity >= 0.8.2 < 0.9.0 ;
33
44interface IStaking {
5- function transferStake (bytes32 coldkey , bytes32 hotkey , uint256 netuid1 , uint256 netuid2 , uint256 amount ) external ;
6- function moveStake (bytes32 hotkey1 , bytes32 hotkey2 , uint256 netuid1 , uint256 netuid2 , uint256 amount ) external ;
5+ function transferStake (
6+ bytes32 coldkey ,
7+ bytes32 hotkey ,
8+ uint256 netuid1 ,
9+ uint256 netuid2 ,
10+ uint256 amount
11+ ) external ;
12+ function moveStake (
13+ bytes32 hotkey1 ,
14+ bytes32 hotkey2 ,
15+ uint256 netuid1 ,
16+ uint256 netuid2 ,
17+ uint256 amount
18+ ) external ;
19+ function getStake (
20+ bytes32 hotkey ,
21+ bytes32 coldkey ,
22+ uint256 netuid
23+ ) external view returns (uint256 );
724}
825
926contract AlphaPool {
10- bytes32 public contract_coldkey;
11- bytes32 public contract_hotkey;
12- address public constant ISTAKING_V2_ADDRESS = 0x0000000000000000000000000000000000000805 ;
27+ bytes32 public contract_coldkey;
28+ bytes32 public contract_hotkey;
29+ address public constant ISTAKING_V2_ADDRESS =
30+ 0x0000000000000000000000000000000000000805 ;
1331
14- mapping (address => mapping (uint256 => uint256 )) public alphaBalance;
32+ mapping (address => mapping (uint256 => uint256 )) public alphaBalance;
1533
1634 constructor (bytes32 _contract_hotkey ) {
1735 contract_hotkey = _contract_hotkey;
@@ -21,51 +39,96 @@ contract AlphaPool {
2139 contract_coldkey = _contract_coldkey;
2240 }
2341
24- function depositAlpha (uint256 _netuid , uint256 _alphyAmount , bytes32 _hotkey ) public {
25- require (contract_hotkey != 0x00 , "contract coldkey not set " );
42+ function getContractStake (uint256 netuid ) public view returns (uint256 ) {
43+ return
44+ IStaking (ISTAKING_V2_ADDRESS).getStake (
45+ contract_hotkey,
46+ contract_coldkey,
47+ netuid
48+ );
49+ }
50+
51+ function depositAlpha (
52+ uint256 _netuid ,
53+ uint256 _alphaAmount ,
54+ bytes32 _hotkey
55+ ) public {
56+ require (contract_coldkey != 0x00 , "contract coldkey not set " );
57+ uint256 contractStake = getContractStake (_netuid);
2658
2759 bytes memory data = abi.encodeWithSelector (
2860 IStaking.transferStake.selector ,
2961 contract_coldkey,
3062 _hotkey,
3163 _netuid,
3264 _netuid,
33- _alphyAmount
65+ _alphaAmount
3466 );
35- (bool success , ) = address (ISTAKING_V2_ADDRESS).delegatecall {gas: gasleft ()}(data);
67+ (bool success , ) = address (ISTAKING_V2_ADDRESS).delegatecall {
68+ gas: gasleft ()
69+ }(data);
3670 require (success, "user deposit alpha call failed " );
3771
72+ uint256 newContractStake = getContractStake (_netuid);
73+
74+ require (
75+ newContractStake > contractStake,
76+ "contract stake decreased after deposit "
77+ );
78+
79+ // use the increased stake as the actual alpha amount, for the swap fee in the move stake call
80+ // the contract will take it and get compensated by laster emission of alpha
81+ uint256 actualAlphaAmount = newContractStake - contractStake;
82+
3883 if (_hotkey != contract_hotkey) {
3984 data = abi.encodeWithSelector (
4085 IStaking.moveStake.selector ,
4186 _hotkey,
4287 contract_hotkey,
4388 _netuid,
4489 _netuid,
45- _alphyAmount
90+ actualAlphaAmount
91+ );
92+ (success, ) = address (ISTAKING_V2_ADDRESS).call {gas: gasleft ()}(
93+ data
4694 );
47- (success, ) = address (ISTAKING_V2_ADDRESS).call {gas: gasleft ()}(data);
4895 require (success, "user deposit, move stake call failed " );
4996 }
5097
51- alphaBalance[msg .sender ][_netuid] += _alphyAmount ;
98+ alphaBalance[msg .sender ][_netuid] += actualAlphaAmount ;
5299 }
53100
54- function withdrawAlpha (uint256 _netuid , uint256 _alphyAmount , bytes32 _user_coldkey ) public {
55- require (contract_hotkey != 0x00 , "contract coldkey not set " );
56- require (alphaBalance[msg .sender ][_netuid] >= _alphyAmount, "user withdraw, insufficient alpha balance " );
101+ function withdrawAlpha (
102+ uint256 _netuid ,
103+ uint256 _alphaAmount ,
104+ bytes32 _user_coldkey
105+ ) public {
106+ require (contract_coldkey != 0x00 , "contract coldkey not set " );
107+ require (
108+ alphaBalance[msg .sender ][_netuid] >= _alphaAmount,
109+ "user withdraw, insufficient alpha balance "
110+ );
111+ uint256 contractStake = getContractStake (_netuid);
57112
58- alphaBalance[msg .sender ][_netuid] -= _alphyAmount ;
113+ alphaBalance[msg .sender ][_netuid] -= _alphaAmount ;
59114
60115 bytes memory data = abi.encodeWithSelector (
61116 IStaking.transferStake.selector ,
62117 _user_coldkey,
63118 contract_hotkey,
64119 _netuid,
65120 _netuid,
66- _alphyAmount
121+ _alphaAmount
122+ );
123+ (bool success , ) = address (ISTAKING_V2_ADDRESS).call {gas: gasleft ()}(
124+ data
125+ );
126+
127+ uint256 newContractStake = getContractStake (_netuid);
128+ require (
129+ newContractStake < contractStake,
130+ "contract stake increased after withdraw "
67131 );
68- (bool success , ) = address (ISTAKING_V2_ADDRESS).call {gas: gasleft ()}(data);
69132 require (success, "user withdraw alpha call failed " );
70133 }
71134}
0 commit comments