Skip to content

Commit 54be5e0

Browse files
Merge remote-tracking branch 'opentensor/devnet-ready' into fix-subtensor-extrinsics2
2 parents f076799 + 2f41708 commit 54be5e0

File tree

22 files changed

+828
-16
lines changed

22 files changed

+828
-16
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// SPDX-License-Identifier: GPL-3.0
2+
pragma solidity >=0.8.2 <0.9.0;
3+
4+
interface IStaking {
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);
24+
}
25+
26+
contract AlphaPool {
27+
bytes32 public contract_coldkey;
28+
bytes32 public contract_hotkey;
29+
address public constant ISTAKING_V2_ADDRESS =
30+
0x0000000000000000000000000000000000000805;
31+
32+
mapping(address => mapping(uint256 => uint256)) public alphaBalance;
33+
34+
constructor(bytes32 _contract_hotkey) {
35+
contract_hotkey = _contract_hotkey;
36+
}
37+
38+
function setContractColdkey(bytes32 _contract_coldkey) public {
39+
contract_coldkey = _contract_coldkey;
40+
}
41+
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);
58+
59+
bytes memory data = abi.encodeWithSelector(
60+
IStaking.transferStake.selector,
61+
contract_coldkey,
62+
_hotkey,
63+
_netuid,
64+
_netuid,
65+
_alphaAmount
66+
);
67+
(bool success, ) = address(ISTAKING_V2_ADDRESS).delegatecall{
68+
gas: gasleft()
69+
}(data);
70+
require(success, "user deposit alpha call failed");
71+
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+
83+
if (_hotkey != contract_hotkey) {
84+
data = abi.encodeWithSelector(
85+
IStaking.moveStake.selector,
86+
_hotkey,
87+
contract_hotkey,
88+
_netuid,
89+
_netuid,
90+
actualAlphaAmount
91+
);
92+
(success, ) = address(ISTAKING_V2_ADDRESS).call{gas: gasleft()}(
93+
data
94+
);
95+
require(success, "user deposit, move stake call failed");
96+
}
97+
98+
alphaBalance[msg.sender][_netuid] += actualAlphaAmount;
99+
}
100+
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);
112+
113+
alphaBalance[msg.sender][_netuid] -= _alphaAmount;
114+
115+
bytes memory data = abi.encodeWithSelector(
116+
IStaking.transferStake.selector,
117+
_user_coldkey,
118+
contract_hotkey,
119+
_netuid,
120+
_netuid,
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"
131+
);
132+
require(success, "user withdraw alpha call failed");
133+
}
134+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
export const ALPHA_POOL_CONTRACT_ABI = [
2+
{
3+
"inputs": [
4+
{
5+
"internalType": "bytes32",
6+
"name": "_contract_hotkey",
7+
"type": "bytes32"
8+
}
9+
],
10+
"stateMutability": "nonpayable",
11+
"type": "constructor"
12+
},
13+
{
14+
"inputs": [],
15+
"name": "ISTAKING_V2_ADDRESS",
16+
"outputs": [
17+
{
18+
"internalType": "address",
19+
"name": "",
20+
"type": "address"
21+
}
22+
],
23+
"stateMutability": "view",
24+
"type": "function"
25+
},
26+
{
27+
"inputs": [
28+
{
29+
"internalType": "address",
30+
"name": "",
31+
"type": "address"
32+
},
33+
{
34+
"internalType": "uint256",
35+
"name": "",
36+
"type": "uint256"
37+
}
38+
],
39+
"name": "alphaBalance",
40+
"outputs": [
41+
{
42+
"internalType": "uint256",
43+
"name": "",
44+
"type": "uint256"
45+
}
46+
],
47+
"stateMutability": "view",
48+
"type": "function"
49+
},
50+
{
51+
"inputs": [],
52+
"name": "contract_coldkey",
53+
"outputs": [
54+
{
55+
"internalType": "bytes32",
56+
"name": "",
57+
"type": "bytes32"
58+
}
59+
],
60+
"stateMutability": "view",
61+
"type": "function"
62+
},
63+
{
64+
"inputs": [],
65+
"name": "contract_hotkey",
66+
"outputs": [
67+
{
68+
"internalType": "bytes32",
69+
"name": "",
70+
"type": "bytes32"
71+
}
72+
],
73+
"stateMutability": "view",
74+
"type": "function"
75+
},
76+
{
77+
"inputs": [
78+
{
79+
"internalType": "uint256",
80+
"name": "_netuid",
81+
"type": "uint256"
82+
},
83+
{
84+
"internalType": "uint256",
85+
"name": "_alphaAmount",
86+
"type": "uint256"
87+
},
88+
{
89+
"internalType": "bytes32",
90+
"name": "_hotkey",
91+
"type": "bytes32"
92+
}
93+
],
94+
"name": "depositAlpha",
95+
"outputs": [],
96+
"stateMutability": "nonpayable",
97+
"type": "function"
98+
},
99+
{
100+
"inputs": [
101+
{
102+
"internalType": "uint256",
103+
"name": "netuid",
104+
"type": "uint256"
105+
}
106+
],
107+
"name": "getContractStake",
108+
"outputs": [
109+
{
110+
"internalType": "uint256",
111+
"name": "",
112+
"type": "uint256"
113+
}
114+
],
115+
"stateMutability": "view",
116+
"type": "function"
117+
},
118+
{
119+
"inputs": [
120+
{
121+
"internalType": "bytes32",
122+
"name": "_contract_coldkey",
123+
"type": "bytes32"
124+
}
125+
],
126+
"name": "setContractColdkey",
127+
"outputs": [],
128+
"stateMutability": "nonpayable",
129+
"type": "function"
130+
},
131+
{
132+
"inputs": [
133+
{
134+
"internalType": "uint256",
135+
"name": "_netuid",
136+
"type": "uint256"
137+
},
138+
{
139+
"internalType": "uint256",
140+
"name": "_alphaAmount",
141+
"type": "uint256"
142+
},
143+
{
144+
"internalType": "bytes32",
145+
"name": "_user_coldkey",
146+
"type": "bytes32"
147+
}
148+
],
149+
"name": "withdrawAlpha",
150+
"outputs": [],
151+
"stateMutability": "nonpayable",
152+
"type": "function"
153+
}
154+
];
155+
156+
export const ALPHA_POOL_CONTRACT_BYTECODE = "6080604052348015600e575f5ffd5b506040516110d83803806110d88339818101604052810190602e9190606c565b80600181905550506092565b5f5ffd5b5f819050919050565b604e81603e565b81146057575f5ffd5b50565b5f815190506066816047565b92915050565b5f60208284031215607e57607d603a565b5b5f608984828501605a565b91505092915050565b6110398061009f5f395ff3fe608060405234801561000f575f5ffd5b5060043610610086575f3560e01c8063cdcde3e911610059578063cdcde3e914610110578063d67c076114610140578063f0d6bb891461015e578063fdbcdce91461017a57610086565b80632849912d1461008a5780633af975ff146100a657806359948a67146100c4578063bee0bca1146100f4575b5f5ffd5b6100a4600480360381019061009f919061090d565b610198565b005b6100ae610518565b6040516100bb919061096c565b60405180910390f35b6100de60048036038101906100d991906109df565b61051d565b6040516100eb9190610a2c565b60405180910390f35b61010e6004803603810190610109919061090d565b61053d565b005b61012a60048036038101906101259190610a45565b610805565b6040516101379190610a2c565b60405180910390f35b61014861088e565b604051610155919061096c565b60405180910390f35b61017860048036038101906101739190610a70565b610894565b005b61018261089d565b60405161018f9190610aaa565b60405180910390f35b5f5f1b5f54036101dd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101d490610b1d565b60405180910390fd5b5f6101e784610805565b90505f6317ce5f6260e01b5f548487888860405160240161020c959493929190610b3b565b604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff838183161783525050505090505f61080573ffffffffffffffffffffffffffffffffffffffff165a836040516102949190610bde565b5f604051808303818686f4925050503d805f81146102cd576040519150601f19603f3d011682016040523d82523d5f602084013e6102d2565b606091505b5050905080610316576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161030d90610c3e565b60405180910390fd5b5f61032087610805565b9050838111610364576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161035b90610ccc565b60405180910390fd5b5f84826103719190610d17565b905060015486146104ac57631149f65960e01b866001548a8b8560405160240161039f959493929190610b3b565b604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff8381831617835250505050935061080573ffffffffffffffffffffffffffffffffffffffff165a856040516104269190610bde565b5f604051808303815f8787f1925050503d805f8114610460576040519150601f19603f3d011682016040523d82523d5f602084013e610465565b606091505b505080935050826104ab576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104a290610dba565b60405180910390fd5b5b8060025f3373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8a81526020019081526020015f205f8282546105079190610dd8565b925050819055505050505050505050565b5f5481565b6002602052815f5260405f20602052805f5260405f205f91509150505481565b5f5f1b5f5403610582576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161057990610b1d565b60405180910390fd5b8160025f3373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8581526020019081526020015f20541015610611576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161060890610e7b565b60405180910390fd5b5f61061b84610805565b90508260025f3373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8681526020019081526020015f205f8282546106789190610d17565b925050819055505f6317ce5f6260e01b836001548788886040516024016106a3959493929190610b3b565b604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff838183161783525050505090505f61080573ffffffffffffffffffffffffffffffffffffffff165a8360405161072b9190610bde565b5f604051808303815f8787f1925050503d805f8114610765576040519150601f19603f3d011682016040523d82523d5f602084013e61076a565b606091505b505090505f61077887610805565b90508381106107bc576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016107b390610f09565b60405180910390fd5b816107fc576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016107f390610f71565b60405180910390fd5b50505050505050565b5f61080573ffffffffffffffffffffffffffffffffffffffff1663e3b598fa6001545f54856040518463ffffffff1660e01b815260040161084893929190610f8f565b602060405180830381865afa158015610863573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906108879190610fd8565b9050919050565b60015481565b805f8190555050565b61080581565b5f5ffd5b5f819050919050565b6108b9816108a7565b81146108c3575f5ffd5b50565b5f813590506108d4816108b0565b92915050565b5f819050919050565b6108ec816108da565b81146108f6575f5ffd5b50565b5f81359050610907816108e3565b92915050565b5f5f5f60608486031215610924576109236108a3565b5b5f610931868287016108c6565b9350506020610942868287016108c6565b9250506040610953868287016108f9565b9150509250925092565b610966816108da565b82525050565b5f60208201905061097f5f83018461095d565b92915050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6109ae82610985565b9050919050565b6109be816109a4565b81146109c8575f5ffd5b50565b5f813590506109d9816109b5565b92915050565b5f5f604083850312156109f5576109f46108a3565b5b5f610a02858286016109cb565b9250506020610a13858286016108c6565b9150509250929050565b610a26816108a7565b82525050565b5f602082019050610a3f5f830184610a1d565b92915050565b5f60208284031215610a5a57610a596108a3565b5b5f610a67848285016108c6565b91505092915050565b5f60208284031215610a8557610a846108a3565b5b5f610a92848285016108f9565b91505092915050565b610aa4816109a4565b82525050565b5f602082019050610abd5f830184610a9b565b92915050565b5f82825260208201905092915050565b7f636f6e747261637420636f6c646b6579206e6f742073657400000000000000005f82015250565b5f610b07601883610ac3565b9150610b1282610ad3565b602082019050919050565b5f6020820190508181035f830152610b3481610afb565b9050919050565b5f60a082019050610b4e5f83018861095d565b610b5b602083018761095d565b610b686040830186610a1d565b610b756060830185610a1d565b610b826080830184610a1d565b9695505050505050565b5f81519050919050565b5f81905092915050565b8281835e5f83830152505050565b5f610bb882610b8c565b610bc28185610b96565b9350610bd2818560208601610ba0565b80840191505092915050565b5f610be98284610bae565b915081905092915050565b7f75736572206465706f73697420616c7068612063616c6c206661696c656400005f82015250565b5f610c28601e83610ac3565b9150610c3382610bf4565b602082019050919050565b5f6020820190508181035f830152610c5581610c1c565b9050919050565b7f636f6e7472616374207374616b652064656372656173656420616674657220645f8201527f65706f7369740000000000000000000000000000000000000000000000000000602082015250565b5f610cb6602683610ac3565b9150610cc182610c5c565b604082019050919050565b5f6020820190508181035f830152610ce381610caa565b9050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f610d21826108a7565b9150610d2c836108a7565b9250828203905081811115610d4457610d43610cea565b5b92915050565b7f75736572206465706f7369742c206d6f7665207374616b652063616c6c2066615f8201527f696c656400000000000000000000000000000000000000000000000000000000602082015250565b5f610da4602483610ac3565b9150610daf82610d4a565b604082019050919050565b5f6020820190508181035f830152610dd181610d98565b9050919050565b5f610de2826108a7565b9150610ded836108a7565b9250828201905080821115610e0557610e04610cea565b5b92915050565b7f757365722077697468647261772c20696e73756666696369656e7420616c70685f8201527f612062616c616e63650000000000000000000000000000000000000000000000602082015250565b5f610e65602983610ac3565b9150610e7082610e0b565b604082019050919050565b5f6020820190508181035f830152610e9281610e59565b9050919050565b7f636f6e7472616374207374616b6520696e6372656173656420616674657220775f8201527f6974686472617700000000000000000000000000000000000000000000000000602082015250565b5f610ef3602783610ac3565b9150610efe82610e99565b604082019050919050565b5f6020820190508181035f830152610f2081610ee7565b9050919050565b7f7573657220776974686472617720616c7068612063616c6c206661696c6564005f82015250565b5f610f5b601f83610ac3565b9150610f6682610f27565b602082019050919050565b5f6020820190508181035f830152610f8881610f4f565b9050919050565b5f606082019050610fa25f83018661095d565b610faf602083018561095d565b610fbc6040830184610a1d565b949350505050565b5f81519050610fd2816108b0565b92915050565b5f60208284031215610fed57610fec6108a3565b5b5f610ffa84828501610fc4565b9150509291505056fea2646970667358221220a0d4b2eb5f0c7f74a27f987e803ae1c8465e0da35f09c240ddb6bac757ce422164736f6c634300081e0033";

0 commit comments

Comments
 (0)