Skip to content

Commit 0d6d239

Browse files
open-juniusYour Name
andauthored
add staking v2 precompile (#64)
Co-authored-by: Your Name <[email protected]>
1 parent 96800ed commit 0d6d239

File tree

1 file changed

+24
-45
lines changed

1 file changed

+24
-45
lines changed

docs/evm-tutorials/staking-precompile.md

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ In this tutorial you will learn how to interact with staking precompile in two w
2727

2828
```sh
2929
btcli subnet create --network ws://127.0.0.1:9944
30-
btcli subnet register --network ws://127.0.0.1:9944
30+
btcli subnet register --network ws://127.0.0.1:9944
3131
```
3232

33-
3433
3. Save the delegate hotkey address. You will use this in the staking pool use case below.
3534

3635
4. Disable staking rate limits by setting `targetStakesPerInterval` to 1000. Follow these below steps:
@@ -40,13 +39,17 @@ btcli subnet register --network ws://127.0.0.1:9944
4039
- Click on **Submit Transaction** at the bottom right. This will open the **authorize transaction** window.
4140
- On this **authorize transaction** window, make sure the **sign and submit** toggle is ON and click on the **Sign and Submit** on the bottom right.
4241

42+
## Staking V1 and V2.
43+
44+
There are two versions of staking precompile implemenation, V1 and V2. The contract address for V1 is `0x0000000000000000000000000000000000000801`. The address for V2 is `0x0000000000000000000000000000000000000805`. The V1 is deprecated, just being kept for compatiable with old interface. The major difference between V1 and V2 is, the staking amount is fetched from the msg.value in V1. Then precompile transfer the token back to caller. It is misleading and confuses the solidity developers. In V2 implementation, all amount parameters are defined as parameter of transaction.
45+
4346
## Call the staking precompile from another smart contract (staking pool use case)
4447

45-
In this interaction you will compile [`stake.sol`](https://github.com/opentensor/evm-bittensor/blob/main/solidity/stake.sol), a smart contract Solidity code and execute it on the subtensor EVM. This `stake.sol` will, in turn, call the staking precompile that is already deployed in the subtensor EVM.
48+
In this interaction you will compile [`stakeV2.sol`](https://github.com/opentensor/evm-bittensor/blob/main/solidity/stakeV2.sol), a smart contract Solidity code and execute it on the subtensor EVM. This `stakeV2.sol` will, in turn, call the staking precompile that is already deployed in the subtensor EVM.
4649

47-
Before you proceed, familiarize yourself with the Solidity code of the [`stake.sol`](https://github.com/opentensor/evm-bittensor/blob/main/solidity/stake.sol) smart contract.
50+
Before you proceed, familiarize yourself with the Solidity code of the [`stakeV2.sol`](https://github.com/opentensor/evm-bittensor/blob/main/solidity/stakeV2.sol) smart contract.
4851

49-
1. Copy the text of [`stake.sol`](https://github.com/opentensor/evm-bittensor/blob/main/solidity/stake.sol) contract to Remix IDE.
52+
1. Copy the text of [`stakeV2.sol`](https://github.com/opentensor/evm-bittensor/blob/main/solidity/stakeV2.sol) contract to Remix IDE.
5053

5154
2. You will now convert your delegate hotkey ss58 from the above [Setup EVM localnet, subnet and delegate](#setup-evm-localnet-subnet-and-delegate) step into its corresponding public key. Use the [ss58.org](https://ss58.org/) site to obtain the public key for your delegate hotkey ss58.
5255

@@ -64,48 +67,11 @@ Before you proceed, familiarize yourself with the Solidity code of the [`stake.s
6467

6568
In this tutorial, you will interact directly with the staking precompile by using its ABI, and use your Metamask wallet as the source of TAO to stake.
6669

67-
1. Copy this below ABI of staking precompile contract into Remix IDE as a new file:
68-
69-
```json
70-
[
71-
{
72-
"inputs": [
73-
{
74-
"internalType": "bytes32",
75-
"name": "hotkey",
76-
"type": "bytes32"
77-
}
78-
],
79-
"name": "addStake",
80-
"outputs": [],
81-
"stateMutability": "payable",
82-
"type": "function"
83-
},
84-
{
85-
"inputs": [
86-
{
87-
"internalType": "bytes32",
88-
"name": "hotkey",
89-
"type": "bytes32"
90-
},
91-
{
92-
"internalType": "uint256",
93-
"name": "amount",
94-
"type": "uint256"
95-
}
96-
],
97-
"name": "removeStake",
98-
"outputs": [],
99-
"stateMutability": "payable",
100-
"type": "function"
101-
}
102-
]
103-
```
104-
105-
2. Copy staking precompile address `0x0000000000000000000000000000000000000801` to the **At Address** field in Remix IDE, and click **At Address** button.
70+
1. Copy this below ABI from https://github.com/opentensor/subtensor/blob/main/precompiles/src/solidity/stakingV2.abi into Remix IDE as a new file.
10671

107-
3. Remix IDE will find the precompile at the precompile address on the subtensor EVM and show it in the list of deployed contracts. Expand the contract, then expand the `addStake` method, and paste the public key of your delegate hotkey into the `hotkey` field. Then click **transact** and wait for the transaction to be completed.
72+
2. Copy staking precompile address `0x0000000000000000000000000000000000000805` to the **At Address** field in Remix IDE, and click **At Address** button.
10873

74+
3. Remix IDE will find the precompile at the precompile address on the subtensor EVM and show it in the list of deployed contracts. Expand the contract, then expand the `addStake` method, and paste the public key of your delegate hotkey into the `hotkey` field. Then click **transact** and wait for the transaction to be completed.
10975

11076
4. Follow these steps to see that the stake record is updated in [Polkadot JS app](https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944#/chainstate):
11177

@@ -114,3 +80,16 @@ In this tutorial, you will interact directly with the staking precompile by usin
11480
3. Toggle **include option** OFF for the second parameter.
11581
4. Click the **+** button and find the new stake record.
11682

83+
## Questions about Call the staking precompile from another smart contract.
84+
85+
1. which one is the coldkey?
86+
Since the precompile can't get the orginal caller, the precompile takes the contract's address as coldkey.
87+
2. is the token subtracted from contract or original caller if calling the addStake
88+
The contract need to pay the token. so must guarantee the balance of contract is enough to call addStake.
89+
3. any security issue for the contract
90+
Because the token for addStake is subtracted from contract. We must set the transaction as priviledged one.
91+
like the example in [the link will be available after a PR merged in subtensor side]
92+
4. the unit of amount in addStake, removeStake
93+
As the function parameter indicates, all these are aligned with the decimal of TAO
94+
5. If transfer token to contract in msg.value, and trigger the contract to call precompile. should the function in contract do the decimal convert.
95+
Yes. the decimal in msg.value is 18. need conversion like `uint256 amount = msg.value / 1e9`

0 commit comments

Comments
 (0)