|
1 | 1 | # bt-decode
|
2 | 2 | A python wrapper around the rust scale-codec crate for fast scale-decoding of Bittensor data structures.
|
| 3 | + |
| 4 | +## Usage |
| 5 | + |
| 6 | +### DelegateInfo |
| 7 | +#### get_delegates |
| 8 | +```python |
| 9 | +import bittensor |
| 10 | +from bt_decode import DelegateInfo |
| 11 | + |
| 12 | +# Setup subtensor connection |
| 13 | +subtensor = bittensor.subtensor() |
| 14 | +# Grab result from RuntimeAPI |
| 15 | +hex_bytes_result = sub.query_runtime_api( |
| 16 | + runtime_api="DelegateInfoRuntimeApi", |
| 17 | + method="get_delegates", |
| 18 | + params=[ ] |
| 19 | +) |
| 20 | +# Decode scale-encoded DelegateInfo |
| 21 | +delegates_info: List[DelegateInfo] = DelegateInfo.decode_vec( |
| 22 | + bytes.fromhex( |
| 23 | + hex_bytes_result |
| 24 | +)) |
| 25 | +``` |
| 26 | +#### get_delegated |
| 27 | +```python |
| 28 | +import bittensor |
| 29 | +from bt_decode import DelegateInfo |
| 30 | + |
| 31 | +validator_key = bittensor.Keypair(ss58_address="5E9fVY1jexCNVMjd2rdBsAxeamFGEMfzHcyTn2fHgdHeYc5p") |
| 32 | + |
| 33 | +# Setup subtensor connection |
| 34 | +subtensor = bittensor.subtensor() |
| 35 | +# Grab result from RuntimeAPI |
| 36 | +hex_bytes_result = sub.query_runtime_api( |
| 37 | + runtime_api="DelegateInfoRuntimeApi", |
| 38 | + method="get_delegated", |
| 39 | + params=[list( validator_key.public_key )] |
| 40 | +) |
| 41 | +# Decode scale-encoded (DelegateInfo, take) |
| 42 | +delegated_info: List[Tuple[DelegateInfo, int]] = DelegateInfo.decode_delegated( |
| 43 | + bytes.fromhex( |
| 44 | + hex_bytes_result |
| 45 | +)) |
| 46 | +``` |
| 47 | + |
| 48 | +### StakeInfo |
| 49 | +#### get_stake_info_for_coldkey |
| 50 | +```python |
| 51 | +import bittensor |
| 52 | +from bt_decode import StakeInfo |
| 53 | + |
| 54 | +validator_key = bittensor.Keypair(ss58_address="5HBtpwxuGNL1gwzwomwR7sjwUt8WXYSuWcLYN6f9KpTZkP4k") |
| 55 | + |
| 56 | +# Setup subtensor connection |
| 57 | +subtensor = bittensor.subtensor() |
| 58 | +encoded_coldkey = list( validator_key.public_key ) |
| 59 | +# Grab result from RuntimeAPI |
| 60 | +hex_bytes_result = sub.query_runtime_api( |
| 61 | + runtime_api="StakeInfoRuntimeApi", |
| 62 | + method="get_stake_info_for_coldkey", |
| 63 | + params=[encoded_coldkey] |
| 64 | +) |
| 65 | +# Decode scale-encoded StakeInfo |
| 66 | +stake_info: List[StakeInfo] = StakeInfo.decode_vec( |
| 67 | + bytes.fromhex( |
| 68 | + hex_bytes_result |
| 69 | +)) |
| 70 | +``` |
| 71 | + |
| 72 | +#### get_stake_info_for_coldkeys |
| 73 | +```python |
| 74 | +import bittensor |
| 75 | +from bt_decode import StakeInfo |
| 76 | + |
| 77 | +validator_key_0 = bittensor.Keypair(ss58_address="5GcCZ2BPXBjgG88tXJCEtkbdg2hNrPbL4EFfbiVRvBZdSQDC") |
| 78 | +validator_key_1 = bittensor.Keypair(ss58_address="5HBtpwxuGNL1gwzwomwR7sjwUt8WXYSuWcLYN6f9KpTZkP4k") |
| 79 | + |
| 80 | +encoded_coldkeys = [ |
| 81 | + list( validator_key_0.public_key ), |
| 82 | + list( validator_key_1.public_key ) |
| 83 | +] |
| 84 | + |
| 85 | +# Setup subtensor connection |
| 86 | +subtensor = bittensor.subtensor() |
| 87 | +# Grab result from RuntimeAPI |
| 88 | +hex_bytes_result = sub.query_runtime_api( |
| 89 | + runtime_api="StakeInfoRuntimeApi", |
| 90 | + method="get_stake_info_for_coldkeys", |
| 91 | + params=[encoded_coldkeys] |
| 92 | +) |
| 93 | +# Decode scale-encoded (AccountId, StakeInfo) |
| 94 | +stake_info: List[Tuple[bytes, List["StakeInfo"]]] = StakeInfo.decode_vec_tuple_vec( |
| 95 | + bytes.fromhex( |
| 96 | + hex_bytes_result |
| 97 | +)) |
| 98 | +``` |
0 commit comments