Skip to content

Commit 10193d5

Browse files
MichaelTrestmanMichael Trestman
andauthored
add script to check emissions for HK (opentensor#515)
* wip * wip * wip --------- Co-authored-by: Michael Trestman <michaeltrestman@mac.mynetworksettings.com>
1 parent 8329317 commit 10193d5

File tree

1 file changed

+88
-25
lines changed

1 file changed

+88
-25
lines changed

docs/dynamic-tao/sdk-cheat-sheet.md

Lines changed: 88 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class DynamicInfo:
7878
## Viewing subnets
7979
Subnets evolve substantially in Dynamic TAO! Each subnet has its own currency, known as its alpha token, and an internal economy comprising a currency reserve of TAO, a reserve of its own alpha token, and a ledger of staked balances, to keep track of all of its stakers&mdash;those who have put TAO into its reserve in exchange for alpha.
8080

81-
### `all_subnets`
81+
#### `all_subnets`
8282
```python
8383
all_subnets(
8484
block_number: Optional[int] = None
@@ -90,7 +90,7 @@ Description: Fetches information about all subnets at a certain block height (or
9090

9191
Returns: A list of DynamicInfo objects (detailed below).
9292

93-
### `get_netuids`
93+
#### `get_netuids`
9494
```python
9595
get_netuids(self, block: Optional[int] = None) -> list[int]
9696

@@ -104,7 +104,7 @@ Returns:
104104
- `list`[int]: A list of network UIDs representing each active subnet.
105105

106106

107-
### `subnet`
107+
#### `subnet`
108108
```python
109109
subnet(
110110
netuid: int,
@@ -117,7 +117,7 @@ Fetches information about a single subnet identified by netuid.
117117
Returns a DynamicInfo object describing the subnet’s current state.
118118

119119

120-
### `metagraph`
120+
#### `metagraph`
121121
```python
122122
metagraph(
123123
netuid: int,
@@ -128,15 +128,15 @@ metagraph(
128128

129129
Returns the metagraph for a specified subnet netuid. The metagraph includes detailed data on the neurons in the subnet.
130130

131-
## Calculating exchange rates
131+
## Exchange rates
132132
You can query the DynamicInfo object for the exchange rates between TAO and alpha tokens.
133133
You can use `all_subnets` or `subnet` to get the DynamicInfo object.
134134

135135
```python
136136
subnet = sub.subnet(netuid=1)
137137
```
138-
139-
### `tao_to_alpha`
138+
### Calculate exhange rates
139+
#### `tao_to_alpha`
140140

141141
```python
142142
tao_to_alpha(self, tao: Union[Balance, float, int]) -> Balance:
@@ -146,15 +146,15 @@ Description: Returns an 'ideal' estimate of how much Alpha a staker would receiv
146146
Parameters:
147147
`tao`: Amount of TAO to stake.
148148

149-
### `alpha_to_tao`
149+
#### `alpha_to_tao`
150150
```python
151151
alpha_to_tao(self, alpha: Union[Balance, float, int]) -> Balance:
152152
```
153153
Description: Returns an 'ideal' estimate of how much TAO would be yielded by unstaking at the current price, *ignoring slippage*.
154154
Parameters:
155155
`alpha`: Amount of Alpha to unstake.
156156

157-
### `tao_to_alpha_with_slippage`
157+
#### `tao_to_alpha_with_slippage`
158158
```python
159159
tao_to_alpha_with_slippage(tao: Union[Balance, float, int], percentage: bool = False) -> Union[tuple[Balance, Balance], float]:
160160
```
@@ -171,7 +171,7 @@ Returns:
171171
OR
172172
Percentage of the slippage as a float
173173

174-
### `alpha_to_tao_with_slippage`
174+
#### `alpha_to_tao_with_slippage`
175175
```python
176176
alpha_to_tao_with_slippage(alpha: Union[Balance, float, int], percentage: bool = False) -> Union[tuple[Balance, Balance], float]:
177177
```
@@ -188,7 +188,7 @@ Returns:
188188
OR
189189
Percentage of the slippage as a float
190190

191-
### Example: Viewing exchange rates
191+
### Display current exchange rates
192192

193193
The following script displays exchange rates for a subnet alpha token, with and without slippage.
194194

@@ -210,7 +210,7 @@ print("alpha_to_tao", subnet.alpha_to_tao(100))
210210

211211
## Managing stake
212212

213-
### `get_stake`
213+
#### `get_stake`
214214
```python
215215
get_stake(
216216
hotkey_ss58: str,
@@ -228,7 +228,7 @@ Parameters:
228228

229229

230230

231-
### `add_stake`
231+
#### `add_stake`
232232

233233
```python
234234
async add_stake(
@@ -246,7 +246,7 @@ Parameters:
246246
- netuid: Unique ID of the subnet on which you want to stake.
247247
- tao_amount: Amount to stake, can be a float, integer, or bittensor.Balance object.
248248

249-
### `unstake`
249+
#### `unstake`
250250
```python
251251
unstake(
252252
wallet,
@@ -266,7 +266,7 @@ Parameters:
266266
- amount: Amount to unstake.
267267

268268

269-
### `get_balance`
269+
#### `get_balance`
270270
```python
271271
get_balance(
272272
address: str,
@@ -282,13 +282,13 @@ Parameters:
282282
- block: Optional block number at which to fetch the balance. Uses the latest block if None.
283283

284284

285-
### `get_current_block`
285+
#### `get_current_block`
286286
```python
287287
get_current_block() -> int
288288

289289
```
290290
Description: Returns the current chain block number.
291-
### `wait_for_block`
291+
#### `wait_for_block`
292292
```python
293293
wait_for_block(
294294
block: Optional[int] = None
@@ -302,9 +302,8 @@ Update: we have added proper nonce protection allowing you to run gather operati
302302
scatter_stake = await asyncio.gather(*[ sub.add_stake( hotkey, coldkey, netuid, amount ) for netuid in range(64) ] )
303303
```
304304

305-
### Example: staking and unstaking
306305

307-
#### Staking
306+
### Staking
308307
The following script incrementally stakes 3 TAO into several subnets over many blocks:
309308

310309
```python
@@ -360,7 +359,7 @@ netuid 5 price τ0.001784484 stake ε11.208213619
360359
...
361360

362361
```
363-
#### Unstaking
362+
### Unstaking
364363

365364
The below script will reverse the effects of the above, by incrementally unstaking alpha tokens from the list of subnets to yield TAO.
366365

@@ -418,11 +417,13 @@ netuid 5 price τ0.001785179 stake ε33.619312896
418417

419418
```
420419

421-
## Register on a subnet
420+
## Subnet registration
421+
422+
### Register
422423

423424
You can register your hotkey on a subnet using the `burned_register` method. This is necessary for staking, mining or validating.
424425

425-
### `burned_register`
426+
#### `burned_register`
426427
```python
427428
burned_register(
428429
wallet,
@@ -454,9 +455,9 @@ reg = sub.burned_register(wallet=wallet, netuid=3)
454455
```
455456

456457

457-
## View your registered subnets
458+
### View registered subnets
458459

459-
### `get_netuids_for_hotkey`
460+
#### `get_netuids_for_hotkey`
460461
```python
461462
get_netuids_for_hotkey(
462463
hotkey: str,
@@ -482,7 +483,7 @@ netuids = sub.get_netuids_for_hotkey(wallet.hotkey.ss58_address)
482483
print(netuids)
483484
```
484485

485-
### `btcli wallet overview`
486+
#### `btcli wallet overview`
486487
You can also use the `btcli` to check subnet registrations using `btcli wallet overview`.
487488
This displays the registrations to subnets by hotkeys controlled by the wallet:
488489

@@ -517,3 +518,65 @@ Subnet: 119: vidac Ⲃ
517518
1 268.38 Ⲃ 0.0094 1.0000 0.0093 0.0094 0.0000 ρ3470929 0.0000
518519

519520
```
521+
522+
523+
## View a hotkey's emissions
524+
525+
This script displays the last day's emissions for a specified hotkey on all subnets on which the hotkey is registered.
526+
527+
This could be useful for a miner to see how much they've been extracting from the different subnets, if they've been mining on several.
528+
529+
Be aware that daily emissions can fluctuate widely. See [Emissions](../emissions.md).
530+
531+
```python
532+
from bittensor.core.async_subtensor import AsyncSubtensor
533+
import sys
534+
import asyncio
535+
536+
# This is the validator HK for Opentensor Foundation, substitute with the hotkey of the miner/validator you wish to inspect
537+
HOTKEY = "5F4tQyWrhfGVcNhoqeiNsR6KjD4wMZ2kfhLj4oHYuyHbZAc3"
538+
539+
# Speicfy which subnets you wish to inspect, by UID
540+
NETUIDS = range(0,64)
541+
BLOCKTIME = 12
542+
543+
subtensor_address = "finney" # or "test" or locally with "ws://127.0.0.1:9944"
544+
545+
async def main():
546+
async with AsyncSubtensor(f"{subtensor_address}") as subtensor:
547+
all_sn_dynamic_info_list = await subtensor.all_subnets()
548+
549+
all_sn_dynamic_info = {info.netuid: info for info in all_sn_dynamic_info_list}
550+
daily_blocks = (60 * 60 * 24) / BLOCKTIME # Number of blocks per day
551+
552+
553+
print(f"Hotkey: {HOTKEY}")
554+
555+
subnets = await asyncio.gather(*[subtensor.subnet_exists(netuid) for netuid in range(1, 8)])
556+
metagraphs = await asyncio.gather(*[ subtensor.metagraph(netuid=id) for id in NETUIDS])
557+
for id in NETUIDS:
558+
print(f"UID: {id}")
559+
560+
metagraph = metagraphs[id]
561+
tempo_multiplier = daily_blocks / metagraph.tempo
562+
563+
subnet_info = all_sn_dynamic_info.get(id)
564+
565+
uid = metagraph.hotkeys.index(HOTKEY) if HOTKEY in metagraph.hotkeys else None
566+
567+
if uid is None:
568+
print(f"Hotkey {HOTKEY} not found in the metagraph")
569+
else:
570+
daily_rewards_alpha = float(metagraph.emission[uid] * tempo_multiplier)
571+
daily_rewards_tao = float(daily_rewards_alpha * subnet_info.price.tao)
572+
alpha_to_tao_with_slippage, slippage = subnet_info.alpha_to_tao_with_slippage(
573+
alpha=daily_rewards_alpha
574+
)
575+
576+
print(f"Daily Rewards Alpha: {daily_rewards_alpha}")
577+
print(f"Daily Rewards Tao: {daily_rewards_tao}")
578+
print(f"Alpha to Tao with Slippage: {alpha_to_tao_with_slippage}")
579+
580+
asyncio.run(main())
581+
582+
```

0 commit comments

Comments
 (0)