near-kit 0.7.1

A clean, ergonomic Rust client for NEAR Protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Additional RPC response types for validators, light client, and state changes.

use serde::Deserialize;

use super::rpc::{AccessKeyDetails, ValidatorStakeView};
use super::{AccountId, CryptoHash, NearToken, PublicKey, Signature};

// ============================================================================
// Validators / Epoch types
// ============================================================================

/// Epoch validator info from `validators` RPC.
#[derive(Debug, Clone, Deserialize)]
pub struct EpochValidatorInfo {
    /// Current epoch validators.
    pub current_validators: Vec<CurrentEpochValidatorInfo>,
    /// Next epoch validators.
    pub next_validators: Vec<NextEpochValidatorInfo>,
    /// Current fishermen (deprecated, typically empty).
    #[serde(default)]
    pub current_fishermen: Vec<ValidatorStakeView>,
    /// Next fishermen (deprecated, typically empty).
    #[serde(default)]
    pub next_fishermen: Vec<ValidatorStakeView>,
    /// Current proposals for next epoch.
    #[serde(default)]
    pub current_proposals: Vec<ValidatorStakeView>,
    /// Validators kicked out in previous epoch.
    #[serde(default)]
    pub prev_epoch_kickout: Vec<ValidatorKickoutView>,
    /// Block height when the epoch started.
    pub epoch_start_height: u64,
    /// Epoch height.
    pub epoch_height: u64,
}

/// Current epoch validator information.
#[derive(Debug, Clone, Deserialize)]
pub struct CurrentEpochValidatorInfo {
    /// Validator account ID.
    pub account_id: AccountId,
    /// Validator public key.
    pub public_key: PublicKey,
    /// Whether this validator has been slashed.
    pub is_slashed: bool,
    /// Stake amount.
    pub stake: NearToken,
    /// Shards produced by this validator.
    #[serde(default)]
    pub shards_produced: Vec<u64>,
    /// Number of blocks produced.
    pub num_produced_blocks: u64,
    /// Number of blocks expected.
    pub num_expected_blocks: u64,
    /// Number of chunks produced.
    #[serde(default)]
    pub num_produced_chunks: u64,
    /// Number of chunks expected.
    #[serde(default)]
    pub num_expected_chunks: u64,
    /// Number of produced chunks per shard.
    #[serde(default)]
    pub num_produced_chunks_per_shard: Vec<u64>,
    /// Number of expected chunks per shard.
    #[serde(default)]
    pub num_expected_chunks_per_shard: Vec<u64>,
    /// Number of endorsements produced.
    #[serde(default)]
    pub num_produced_endorsements: u64,
    /// Number of endorsements expected.
    #[serde(default)]
    pub num_expected_endorsements: u64,
    /// Number of endorsements produced per shard.
    #[serde(default)]
    pub num_produced_endorsements_per_shard: Vec<u64>,
    /// Number of endorsements expected per shard.
    #[serde(default)]
    pub num_expected_endorsements_per_shard: Vec<u64>,
    /// Shards endorsed.
    #[serde(default)]
    pub shards_endorsed: Vec<u64>,
}

/// Next epoch validator information.
#[derive(Debug, Clone, Deserialize)]
pub struct NextEpochValidatorInfo {
    /// Validator account ID.
    pub account_id: AccountId,
    /// Validator public key.
    pub public_key: PublicKey,
    /// Stake amount.
    pub stake: NearToken,
    /// Shards to be assigned.
    #[serde(default)]
    pub shards: Vec<u64>,
}

/// Validator kickout information.
#[derive(Debug, Clone, Deserialize)]
pub struct ValidatorKickoutView {
    /// Account ID of the kicked validator.
    pub account_id: AccountId,
    /// Reason for kickout.
    pub reason: ValidatorKickoutReason,
}

/// Reason a validator was kicked out.
#[derive(Debug, Clone, Deserialize)]
pub enum ValidatorKickoutReason {
    /// Slashed (deprecated, unused).
    #[serde(rename = "Slashed")]
    Slashed,
    /// Not enough blocks produced.
    NotEnoughBlocks {
        /// Blocks produced.
        produced: u64,
        /// Blocks expected.
        expected: u64,
    },
    /// Not enough chunks produced.
    NotEnoughChunks {
        /// Chunks produced.
        produced: u64,
        /// Chunks expected.
        expected: u64,
    },
    /// Validator unstaked.
    Unstaked,
    /// Not enough stake.
    NotEnoughStake {
        /// Validator's stake.
        stake: NearToken,
        /// Minimum threshold.
        threshold: NearToken,
    },
    /// Did not get a seat.
    DidNotGetASeat,
    /// Not enough chunk endorsements.
    NotEnoughChunkEndorsements {
        /// Endorsements produced.
        produced: u64,
        /// Endorsements expected.
        expected: u64,
    },
    /// Protocol version too old.
    ProtocolVersionTooOld {
        /// Validator's protocol version.
        version: u32,
        /// Network protocol version.
        network_version: u32,
    },
}

// ============================================================================
// Light client types
// ============================================================================

/// Light client block view from `next_light_client_block` RPC.
#[derive(Debug, Clone, Deserialize)]
pub struct LightClientBlockView {
    /// Previous block hash.
    pub prev_block_hash: CryptoHash,
    /// Next block inner hash.
    pub next_block_inner_hash: CryptoHash,
    /// Inner lite header.
    pub inner_lite: BlockHeaderInnerLiteView,
    /// Hash of inner rest fields.
    pub inner_rest_hash: CryptoHash,
    /// Next epoch block producers (None if unchanged).
    #[serde(default)]
    pub next_bps: Option<Vec<ValidatorStakeView>>,
    /// Approvals after next block.
    #[serde(default)]
    pub approvals_after_next: Vec<Option<Signature>>,
}

/// Light client block lite view.
#[derive(Debug, Clone, Deserialize)]
pub struct LightClientBlockLiteView {
    /// Previous block hash.
    pub prev_block_hash: CryptoHash,
    /// Hash of inner rest fields.
    pub inner_rest_hash: CryptoHash,
    /// Inner lite header.
    pub inner_lite: BlockHeaderInnerLiteView,
}

/// Block header inner lite (for light client proofs).
#[derive(Debug, Clone, Deserialize)]
pub struct BlockHeaderInnerLiteView {
    /// Block height.
    pub height: u64,
    /// Epoch ID.
    pub epoch_id: CryptoHash,
    /// Next epoch ID.
    pub next_epoch_id: CryptoHash,
    /// Previous state root.
    pub prev_state_root: CryptoHash,
    /// Outcome root.
    pub outcome_root: CryptoHash,
    /// Timestamp (legacy, as u64).
    pub timestamp: u64,
    /// Timestamp in nanoseconds.
    pub timestamp_nanosec: String,
    /// Next block producers hash.
    pub next_bp_hash: CryptoHash,
    /// Block merkle root.
    pub block_merkle_root: CryptoHash,
}

// ============================================================================
// State change types
// ============================================================================

/// State change with its cause (from `EXPERIMENTAL_changes` RPC).
#[derive(Debug, Clone, Deserialize)]
pub struct StateChangeWithCauseView {
    /// What caused this state change.
    pub cause: StateChangeCauseView,
    /// The state change value.
    pub value: StateChangeValueView,
}

/// Cause of a state change.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum StateChangeCauseView {
    /// State not writable to disk.
    NotWritableToDisk,
    /// Initial state.
    InitialState,
    /// Transaction processing.
    TransactionProcessing {
        /// Transaction hash.
        tx_hash: CryptoHash,
    },
    /// Action receipt processing started.
    ActionReceiptProcessingStarted {
        /// Receipt hash.
        receipt_hash: CryptoHash,
    },
    /// Action receipt gas reward.
    ActionReceiptGasReward {
        /// Receipt hash.
        receipt_hash: CryptoHash,
    },
    /// Receipt processing.
    ReceiptProcessing {
        /// Receipt hash.
        receipt_hash: CryptoHash,
    },
    /// Postponed receipt.
    PostponedReceipt {
        /// Receipt hash.
        receipt_hash: CryptoHash,
    },
    /// Updated delayed receipts.
    UpdatedDelayedReceipts,
    /// Validator accounts update.
    ValidatorAccountsUpdate,
    /// State migration.
    Migration,
    /// Bandwidth scheduler state update.
    BandwidthSchedulerStateUpdate,
}

/// State change value.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type", content = "change")]
pub enum StateChangeValueView {
    /// Account updated.
    AccountUpdate {
        /// Account ID.
        account_id: AccountId,
        /// Account state fields (flattened: amount, locked, code_hash, etc.).
        #[serde(flatten)]
        account: serde_json::Value,
    },
    /// Account deleted.
    AccountDeletion {
        /// Account ID.
        account_id: AccountId,
    },
    /// Access key updated.
    AccessKeyUpdate {
        /// Account ID.
        account_id: AccountId,
        /// Public key.
        public_key: PublicKey,
        /// New access key.
        access_key: AccessKeyDetails,
    },
    /// Access key deleted.
    AccessKeyDeletion {
        /// Account ID.
        account_id: AccountId,
        /// Public key.
        public_key: PublicKey,
    },
    /// Gas key nonce updated.
    GasKeyNonceUpdate {
        /// Account ID.
        account_id: AccountId,
        /// Public key.
        public_key: PublicKey,
        /// Nonce index.
        index: u16,
        /// Nonce value.
        nonce: u64,
    },
    /// Data updated.
    DataUpdate {
        /// Account ID.
        account_id: AccountId,
        /// Key (base64-encoded).
        #[serde(rename = "key_base64")]
        key: String,
        /// Value (base64-encoded).
        #[serde(rename = "value_base64")]
        value: String,
    },
    /// Data deleted.
    DataDeletion {
        /// Account ID.
        account_id: AccountId,
        /// Key (base64-encoded).
        #[serde(rename = "key_base64")]
        key: String,
    },
    /// Contract code updated.
    ContractCodeUpdate {
        /// Account ID.
        account_id: AccountId,
        /// Code (base64-encoded).
        #[serde(rename = "code_base64")]
        code: String,
    },
    /// Contract code deleted.
    ContractCodeDeletion {
        /// Account ID.
        account_id: AccountId,
    },
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_gas_key_nonce_update_deserialization() {
        let json = serde_json::json!({
            "type": "gas_key_nonce_update",
            "change": {
                "account_id": "alice.near",
                "public_key": "ed25519:6E8sCci9badyRkXb3JoRpBj5p8C6Tw41ELDZoiihKEtp",
                "index": 3,
                "nonce": 42
            }
        });
        let change: StateChangeValueView = serde_json::from_value(json).unwrap();
        match change {
            StateChangeValueView::GasKeyNonceUpdate {
                account_id,
                public_key,
                index,
                nonce,
            } => {
                assert_eq!(account_id.as_str(), "alice.near");
                assert!(public_key.to_string().starts_with("ed25519:"));
                assert_eq!(index, 3);
                assert_eq!(nonce, 42);
            }
            _ => panic!("Expected GasKeyNonceUpdate"),
        }
    }

    #[test]
    fn test_state_change_with_cause_deserialization() {
        let json = serde_json::json!({
            "cause": {
                "type": "transaction_processing",
                "tx_hash": "9FtHUFBQsZ2MG77K3x3MJ9wjX3UT8zE1TczCrhZEcG8U"
            },
            "value": {
                "type": "gas_key_nonce_update",
                "change": {
                    "account_id": "alice.near",
                    "public_key": "ed25519:6E8sCci9badyRkXb3JoRpBj5p8C6Tw41ELDZoiihKEtp",
                    "index": 0,
                    "nonce": 100
                }
            }
        });
        let change: StateChangeWithCauseView = serde_json::from_value(json).unwrap();
        assert!(matches!(
            change.value,
            StateChangeValueView::GasKeyNonceUpdate { .. }
        ));
    }

    #[test]
    fn test_state_change_cause_deserialization() {
        let json = serde_json::json!({
            "type": "transaction_processing",
            "tx_hash": "9FtHUFBQsZ2MG77K3x3MJ9wjX3UT8zE1TczCrhZEcG8U"
        });
        let cause: StateChangeCauseView = serde_json::from_value(json).unwrap();
        assert!(matches!(
            cause,
            StateChangeCauseView::TransactionProcessing { .. }
        ));
    }

    #[test]
    fn test_account_update_flattened_deserialization() {
        let json = serde_json::json!({
            "type": "account_update",
            "change": {
                "account_id": "alice.near",
                "amount": "1000000000000000000000000",
                "locked": "0",
                "code_hash": "11111111111111111111111111111111",
                "storage_usage": 100,
                "storage_paid_at": 0
            }
        });
        let change: StateChangeValueView = serde_json::from_value(json).unwrap();
        match change {
            StateChangeValueView::AccountUpdate {
                account_id,
                account,
            } => {
                assert_eq!(account_id.as_str(), "alice.near");
                assert_eq!(account["amount"], "1000000000000000000000000");
                assert_eq!(account["storage_usage"], 100);
            }
            _ => panic!("expected AccountUpdate"),
        }
    }

    #[test]
    fn test_data_update_base64_field_names() {
        let json = serde_json::json!({
            "type": "data_update",
            "change": {
                "account_id": "alice.near",
                "key_base64": "c3RhdGU=",
                "value_base64": "dGVzdA=="
            }
        });
        let change: StateChangeValueView = serde_json::from_value(json).unwrap();
        match change {
            StateChangeValueView::DataUpdate {
                account_id,
                key,
                value,
            } => {
                assert_eq!(account_id.as_str(), "alice.near");
                assert_eq!(key, "c3RhdGU=");
                assert_eq!(value, "dGVzdA==");
            }
            _ => panic!("expected DataUpdate"),
        }
    }

    #[test]
    fn test_data_deletion_base64_field_name() {
        let json = serde_json::json!({
            "type": "data_deletion",
            "change": {
                "account_id": "alice.near",
                "key_base64": "c3RhdGU="
            }
        });
        let change: StateChangeValueView = serde_json::from_value(json).unwrap();
        assert!(matches!(change, StateChangeValueView::DataDeletion { .. }));
    }

    #[test]
    fn test_contract_code_update_base64_field_name() {
        let json = serde_json::json!({
            "type": "contract_code_update",
            "change": {
                "account_id": "alice.near",
                "code_base64": "AGFzbQEAAAA="
            }
        });
        let change: StateChangeValueView = serde_json::from_value(json).unwrap();
        match change {
            StateChangeValueView::ContractCodeUpdate {
                account_id, code, ..
            } => {
                assert_eq!(account_id.as_str(), "alice.near");
                assert_eq!(code, "AGFzbQEAAAA=");
            }
            _ => panic!("expected ContractCodeUpdate"),
        }
    }
}