Staking Precompile
Staking precompile allows Ethereum code to interact with the staking feature of subtensor. For example, by using the staking precompile, the subtensor methods add_stake
or remove_stake
can be called in order to delegate stake to a hotkey or undelegate stake from a hotkey.
In this tutorial you will learn how to interact with staking precompile in two ways:
- Call the staking precompile from another smart contract.
- Use the staking precompile's ABI and your Metamask wallet to call the staking precompile on EVM localnet. You will use Remix IDE for this.
Prerequisites
- You should also be comfortable using Remix IDE.
- Read EVM on Subtensor for a basic understanding of what an ABI is and how to use it.
Setup EVM localnet, subnet and delegate
-
Launch EVM localnet. Also, follow the instructions of running local chain all the way so that you have a Metamask address with some TAO balance.
-
On this EVM localnet create one subnet and a delegate hotkey. The commands below will create a subnet, register a neuron and nominate your hotkey as a delegate, in that order:
btcli subnet create --subtensor.chain_endpoint ws://127.0.0.1:9946
btcli subnet register --subtensor.chain_endpoint ws://127.0.0.1:9946
btcli root nominate --subtensor.chain_endpoint ws://127.0.0.1:9946 -
Save the delegate hotkey address. You will use this in the staking pool use case below.
-
Disable staking rate limits by setting
targetStakesPerInterval
to 1000. Follow these below steps:- Open the Polkadot JS app using this link with encoded transaction.
- Click on Submission tab.
- From the using the selected account field, select ALICE.
- Click on Submit Transaction at the bottom right. This will open the authorize transaction window.
- 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.
Call the staking precompile from another smart contract (staking pool use case)
In this interaction you will compile 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.
Before you proceed, familiarize yourself with the Solidity code of the stake.sol
smart contract.
-
Copy the text of
stake.sol
contract to Remix IDE. -
You will now convert your delegate hotkey ss58 from the above Setup EVM localnet, subnet and delegate step into its corresponding public key. Use the ss58.org site to obtain the public key for your delegate hotkey ss58.
-
In the
stake.sol
text in Remix IDE, replace theHOTKEY
constant on line 9, where it saysbytes32 constant HOTKEY = 0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d;
, with your delegate hotkey's public key. -
Compile it in Remix IDE.
-
Connect Remix IDE to Injected Provider - Metamask and your Metamask address that has TAO balance. You will stake this TAO balance to the delegate hotkey's public key.
-
Execute the Stake contract method
stake_from_this_contract_to_alice
and pass 1e^9 to it (1 TAO). -
Check the stake balance of your delegate hotkey and confirm that it has increased by 1 TAO.
Use the staking precompile's ABI from your user account (staking as an individual use case)
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.
-
Copy this below ABI of staking precompile contract into Remix IDE as a new file:
[
{
"inputs": [
{
"internalType": "bytes32",
"name": "hotkey",
"type": "bytes32"
}
],
"name": "addStake",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hotkey",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "removeStake",
"outputs": [],
"stateMutability": "payable",
"type": "function"
}
] -
Copy staking precompile address
0x0000000000000000000000000000000000000801
to the At Address field in Remix IDE, and click At Address button. -
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 thehotkey
field. Then click transact and wait for the transaction to be completed. -
Follow these steps to see that the stake record is updated in Polkadot JS app:
- Select subtensorModule + stake in the drop-down list.
- Paste the delegate hotkey account ID in the first parameter.
- Toggle include option OFF for the second parameter.
- Click the + button and find the new stake record.