nado-sdk 0.3.6

Official Rust SDK for the Nado Protocol API
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
#![allow(non_snake_case)]

use crate::bindings::{endpoint, offchain_exchange};
use crate::isolated::is_isolated_subaccount;
use crate::math::{mul_x18, ONE_X18, ONE_X6};
use crate::serialize_utils::{
    deserialize_bytes20, deserialize_bytes32, deserialize_i128, deserialize_u128, deserialize_u64,
    deserialize_vec_bytes32, serialize_bytes20, serialize_bytes32, serialize_i128, serialize_u128,
    serialize_u64, serialize_vec_bytes32,
};
use ethers::prelude::*;
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
use serde::{Deserialize, Serialize};
use std::cmp::{max, min};
use std::fmt;
use std::fmt::Debug;

#[derive(
    Archive,
    RkyvDeserialize,
    RkyvSerialize,
    Serialize,
    Deserialize,
    Debug,
    Clone,
    Eip712,
    EthAbiType,
    Default,
)]
#[eip712()]
#[archive(check_bytes)]
#[allow(non_snake_case)]
pub struct Order {
    // #[ts(type = "string")]
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    #[serde(
        serialize_with = "serialize_i128",
        deserialize_with = "deserialize_i128"
    )]
    // #[ts(type = "BigNumberish")]
    pub priceX18: i128,
    #[serde(
        serialize_with = "serialize_i128",
        deserialize_with = "deserialize_i128"
    )]
    // #[ts(type = "BigNumberish")]
    pub amount: i128, // positive: bid

    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub expiration: u64,
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub nonce: u64,
    #[serde(
        serialize_with = "serialize_u128",
        deserialize_with = "deserialize_u128"
    )]
    pub appendix: u128,
}

#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum OrderType {
    #[default]
    Default,
    ImmediateOrCancel,
    FillOrKill,
    PostOnly,
}

impl fmt::Display for OrderType {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            OrderType::Default => write!(fmt, "default"),
            OrderType::ImmediateOrCancel => write!(fmt, "ioc"),
            OrderType::FillOrKill => write!(fmt, "fok"),
            OrderType::PostOnly => write!(fmt, "post_only"),
        }
    }
}

impl OrderType {
    pub fn taker_only(&self) -> bool {
        matches!(self, OrderType::ImmediateOrCancel | OrderType::FillOrKill)
    }

    pub fn appendix_bit(&self) -> u128 {
        (match self {
            OrderType::Default => 0,
            OrderType::ImmediateOrCancel => 1,
            OrderType::FillOrKill => 2,
            OrderType::PostOnly => 3,
        }) << 9
    }
}

impl Order {
    pub fn new(
        sender: [u8; 32],
        priceX18: i128,
        amount: i128,
        expiration: u64,
        nonce: u64,
        appendix: u128,
    ) -> Self {
        Self {
            sender,
            priceX18,
            amount,
            expiration,
            nonce,
            appendix,
        }
    }

    pub fn to_offchain_exchange_binding(&self) -> offchain_exchange::Order {
        offchain_exchange::Order {
            sender: self.sender,
            price_x18: self.priceX18,
            amount: self.amount,
            expiration: self.expiration,
            nonce: self.nonce,
            appendix: self.appendix,
        }
    }

    pub fn to_binding(&self) -> endpoint::Order {
        endpoint::Order {
            sender: self.sender,
            price_x18: self.priceX18,
            amount: self.amount,
            expiration: self.expiration,
            nonce: self.nonce,
            appendix: self.appendix,
        }
    }

    pub fn to_signed_binding(&self, signature: &Bytes) -> endpoint::SignedOrder {
        endpoint::SignedOrder {
            order: self.to_binding(),
            signature: signature.clone(),
        }
    }

    pub fn to_offchain_exchange_signed_binding(
        &self,
        signature: &Bytes,
    ) -> offchain_exchange::SignedOrder {
        offchain_exchange::SignedOrder {
            order: self.to_offchain_exchange_binding(),
            signature: signature.clone(),
        }
    }

    pub fn from_binding(order: &endpoint::Order) -> Self {
        Self {
            sender: order.sender,
            priceX18: order.price_x18,
            amount: order.amount,
            expiration: order.expiration,
            nonce: order.nonce,
            appendix: order.appendix,
        }
    }

    pub fn reduce_only(&self) -> bool {
        (self.appendix >> 11 & 1) == 1
    }
    pub fn recv_time(&self) -> u64 {
        self.nonce >> 20
    }

    pub fn is_trigger_order(&self) -> bool {
        (self.appendix >> 12 & 3) > 0
    }

    pub fn is_price_trigger(&self) -> bool {
        (self.appendix >> 12 & 3) == 1
    }

    pub fn is_twap(&self) -> bool {
        (self.appendix >> 12 & 3) >= 2
    }

    pub fn is_twap_random(&self) -> bool {
        (self.appendix >> 12 & 3) == 3
    }

    pub fn twap_times(&self) -> Option<u32> {
        if self.is_twap() {
            Some((self.appendix >> 96) as u32)
        } else {
            None
        }
    }

    pub fn twap_slippage_x6(&self) -> Option<u32> {
        if self.is_twap() {
            Some((self.appendix >> 64 & ((1 << 32) - 1)) as u32)
        } else {
            None
        }
    }

    pub fn order_price(&self, oracle_price: i128) -> i128 {
        let mut price = self.priceX18;
        if self.is_twap() {
            let slippage = self.twap_slippage_x6().unwrap() as i128 * ONE_X18 / ONE_X6;

            let twap_price = if self.amount.is_positive() {
                mul_x18(oracle_price, ONE_X18 + slippage)
            } else {
                mul_x18(oracle_price, ONE_X18 - slippage)
            };

            if self.amount.is_positive() {
                price = min(price, twap_price);
            } else {
                price = max(price, twap_price);
            }
        }
        price
    }

    pub fn is_isolated(&self) -> bool {
        (self.appendix >> 8 & 1) == 1
    }

    pub fn isolated_margin(&self) -> Option<i128> {
        if self.is_isolated() {
            Some((self.appendix >> 64) as i128 * ONE_X18 / ONE_X6)
        } else {
            None
        }
    }

    pub fn builder_info(&self) -> (u32, i128) {
        let builder_id = ((self.appendix >> 48) & ((1 << 16) - 1)) as u32;
        let builder_fee_rate =
            ((self.appendix >> 38) & ((1 << 10) - 1)) as i128 * (ONE_X18 / 100_000); // 0.1bps
        (builder_id, builder_fee_rate)
    }
    pub fn reserved_bits(&self) -> u128 {
        (self.appendix >> 14) & ((1 << 24) - 1)
    }

    pub fn version(&self) -> u8 {
        (self.appendix & 255) as u8
    }
}

#[derive(
    Archive,
    RkyvDeserialize,
    RkyvSerialize,
    Serialize,
    Deserialize,
    Debug,
    Clone,
    Eip712,
    EthAbiType,
)]
#[eip712()]
#[archive(check_bytes)]
#[allow(non_snake_case)]
pub struct Cancellation {
    // #[ts(type = "string")]
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    pub productIds: Vec<u32>,
    #[serde(
        serialize_with = "serialize_vec_bytes32",
        deserialize_with = "deserialize_vec_bytes32"
    )]
    pub digests: Vec<[u8; 32]>,
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub nonce: u64,
}

impl Cancellation {
    pub fn to_binding(&self) -> endpoint::Cancellation {
        endpoint::Cancellation {
            sender: self.sender,
            product_ids: self.productIds.clone(),
            digests: self.digests.clone(),
            nonce: self.nonce,
        }
    }

    pub fn to_signed_binding(&self, signature: &Bytes) -> endpoint::SignedCancellation {
        endpoint::SignedCancellation {
            cancellation: self.to_binding(),
            signature: signature.clone(),
        }
    }

    pub fn recv_time(&self) -> u64 {
        self.nonce >> 20
    }
}

#[derive(
    Archive,
    RkyvDeserialize,
    RkyvSerialize,
    Serialize,
    Deserialize,
    Debug,
    Clone,
    Eip712,
    EthAbiType,
)]
#[eip712()]
#[archive(check_bytes)]
#[allow(non_snake_case)]
pub struct CancellationProducts {
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    pub productIds: Vec<u32>,
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub nonce: u64,
}

impl CancellationProducts {
    pub fn to_binding(&self) -> endpoint::CancellationProducts {
        endpoint::CancellationProducts {
            sender: self.sender,
            product_ids: self.productIds.clone(),
            nonce: self.nonce,
        }
    }

    pub fn to_signed_binding(&self, signature: &Bytes) -> endpoint::SignedCancellationProducts {
        endpoint::SignedCancellationProducts {
            cancellation_products: self.to_binding(),
            signature: signature.clone(),
        }
    }

    pub fn recv_time(&self) -> u64 {
        self.nonce >> 20
    }
}

#[derive(
    Archive,
    RkyvDeserialize,
    RkyvSerialize,
    Serialize,
    Deserialize,
    Debug,
    Clone,
    Eip712,
    EthAbiType,
)]
#[eip712()]
#[archive(check_bytes)]
#[allow(non_snake_case)]
pub struct LinkSigner {
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub signer: [u8; 32],
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub nonce: u64,
}

impl LinkSigner {
    pub fn to_binding(&self) -> endpoint::LinkSigner {
        endpoint::LinkSigner {
            sender: self.sender,
            signer: self.signer,
            nonce: self.nonce,
        }
    }

    pub fn to_signed_binding(&self, signature: &Bytes) -> endpoint::SignedLinkSigner {
        endpoint::SignedLinkSigner {
            tx: self.to_binding(),
            signature: signature.clone(),
        }
    }

    pub fn recv_time(&self) -> u64 {
        self.nonce >> 20
    }
}

#[derive(
    Archive,
    RkyvDeserialize,
    RkyvSerialize,
    Serialize,
    Deserialize,
    Debug,
    Clone,
    Eip712,
    EthAbiType,
)]
#[eip712()]
#[archive(check_bytes)]
#[allow(non_snake_case)]
pub struct LiquidateSubaccount {
    // #[ts(type = "string")]
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub liquidatee: [u8; 32],
    pub productId: u32,
    pub isEncodedSpread: bool,
    #[serde(
        serialize_with = "serialize_i128",
        deserialize_with = "deserialize_i128"
    )]
    // #[ts(type = "BigNumberish")]
    pub amount: i128,
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub nonce: u64,
}

impl LiquidateSubaccount {
    pub fn to_binding(&self) -> endpoint::LiquidateSubaccount {
        endpoint::LiquidateSubaccount {
            sender: self.sender,
            liquidatee: self.liquidatee,
            product_id: self.productId,
            is_encoded_spread: self.isEncodedSpread,
            amount: self.amount,
            nonce: self.nonce,
        }
    }
}

#[derive(
    Archive,
    RkyvDeserialize,
    RkyvSerialize,
    Serialize,
    Deserialize,
    Debug,
    Clone,
    Eip712,
    EthAbiType,
)]
#[eip712()]
#[archive(check_bytes)]
#[allow(non_snake_case)]
pub struct WithdrawCollateral {
    // #[ts(type = "string")]
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    pub productId: u32,
    #[serde(
        serialize_with = "serialize_u128",
        deserialize_with = "deserialize_u128"
    )]
    // #[ts(type = "BigNumberish")]
    pub amount: u128,
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub nonce: u64,
}

impl WithdrawCollateral {
    pub fn to_binding(&self) -> endpoint::WithdrawCollateral {
        endpoint::WithdrawCollateral {
            sender: self.sender,
            product_id: self.productId,
            amount: self.amount,
            nonce: self.nonce,
        }
    }
}

#[derive(
    Archive,
    RkyvDeserialize,
    RkyvSerialize,
    Serialize,
    Deserialize,
    Debug,
    Clone,
    Eip712,
    EthAbiType,
)]
#[eip712()]
#[archive(check_bytes)]
#[allow(non_snake_case)]
pub struct MintNlp {
    // #[ts(type = "typestring")]
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    #[serde(
        serialize_with = "serialize_u128",
        deserialize_with = "deserialize_u128"
    )]
    // #[ts(type = "BigNumberish")]
    pub quoteAmount: u128,
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub nonce: u64,
}

impl MintNlp {
    pub fn to_binding(&self) -> endpoint::MintNlp {
        endpoint::MintNlp {
            sender: self.sender,
            quote_amount: self.quoteAmount,
            nonce: self.nonce,
        }
    }
}

#[derive(
    Archive,
    RkyvDeserialize,
    RkyvSerialize,
    Serialize,
    Deserialize,
    Debug,
    Clone,
    Eip712,
    EthAbiType,
)]
#[eip712()]
#[archive(check_bytes)]
#[allow(non_snake_case)]
pub struct BurnNlp {
    // #[ts(type = "typestring")]
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    #[serde(
        serialize_with = "serialize_u128",
        deserialize_with = "deserialize_u128"
    )]
    // #[ts(type = "BigNumberish")]
    pub nlpAmount: u128,
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub nonce: u64,
}

impl BurnNlp {
    pub fn to_binding(&self) -> endpoint::BurnNlp {
        endpoint::BurnNlp {
            sender: self.sender,
            nlp_amount: self.nlpAmount,
            nonce: self.nonce,
        }
    }
}

#[derive(
    Archive,
    RkyvDeserialize,
    RkyvSerialize,
    Serialize,
    Deserialize,
    Debug,
    Clone,
    Eip712,
    EthAbiType,
)]
#[eip712()]
#[archive(check_bytes)]
#[allow(non_snake_case)]
pub struct TransferQuote {
    // #[ts(type = "string")]
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub recipient: [u8; 32],
    #[serde(
        serialize_with = "serialize_u128",
        deserialize_with = "deserialize_u128"
    )]
    // #[ts(type = "BigNumberish")]
    pub amount: u128,
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub nonce: u64,
}

impl TransferQuote {
    pub fn to_binding(&self) -> endpoint::TransferQuote {
        endpoint::TransferQuote {
            sender: self.sender,
            recipient: self.recipient,
            amount: self.amount,
            nonce: self.nonce,
        }
    }
}

#[derive(
    Serialize,
    Deserialize,
    Clone,
    Debug,
    Default,
    Eq,
    PartialEq,
    Eip712,
    ethers :: contract :: EthAbiType,
    ethers :: contract :: EthAbiCodec,
)]
#[eip712()]
#[allow(non_snake_case)]
pub struct ListTriggerOrders {
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub recvTime: u64,
}

#[derive(
    Serialize,
    Deserialize,
    Clone,
    Debug,
    Default,
    Eq,
    PartialEq,
    ethers :: contract :: EthAbiType,
    ethers :: contract :: EthAbiCodec,
)]
pub struct SignedListTriggerOrders {
    pub tx: ListTriggerOrders,
    pub signature: ethers::core::types::Bytes,
}

#[derive(
    Serialize,
    Deserialize,
    Clone,
    Debug,
    Default,
    Eq,
    PartialEq,
    Eip712,
    ethers :: contract :: EthAbiType,
    ethers :: contract :: EthAbiCodec,
)]
#[eip712()]
#[allow(non_snake_case)]
pub struct StreamAuthentication {
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub expiration: u64,
}

#[derive(
    Serialize,
    Deserialize,
    Clone,
    Debug,
    Default,
    Eq,
    PartialEq,
    Eip712,
    ethers :: contract :: EthAbiType,
    ethers :: contract :: EthAbiCodec,
)]
#[eip712()]
#[allow(non_snake_case)]
pub struct TaskAuthentication {
    #[serde(
        serialize_with = "serialize_bytes20",
        deserialize_with = "deserialize_bytes20"
    )]
    pub sender: [u8; 20],
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub expiration: u64,
}

#[derive(
    Serialize,
    Deserialize,
    Clone,
    Debug,
    Default,
    Eq,
    PartialEq,
    Eip712,
    ethers :: contract :: EthAbiType,
    ethers :: contract :: EthAbiCodec,
)]
#[eip712()]
#[allow(non_snake_case)]
pub struct LeaderboardAuthentication {
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub expiration: u64,
    pub contestIds: Vec<u32>,
}

#[derive(
    Serialize,
    Deserialize,
    Clone,
    Debug,
    Default,
    Eq,
    PartialEq,
    Eip712,
    ethers :: contract :: EthAbiType,
    ethers :: contract :: EthAbiCodec,
)]
#[eip712()]
#[allow(non_snake_case)]
pub struct SocialAuthentication {
    #[serde(
        serialize_with = "serialize_bytes32",
        deserialize_with = "deserialize_bytes32"
    )]
    pub sender: [u8; 32],
    #[serde(serialize_with = "serialize_u64", deserialize_with = "deserialize_u64")]
    pub expiration: u64,
    pub provider: String,
}

pub fn to_bytes12(s: &str) -> [u8; 12] {
    let b = s.as_bytes();
    let mut out = [0u8; 12];
    let n = std::cmp::min(b.len(), 12);
    out[..n].copy_from_slice(&b[..n]);
    out
}

pub fn to_bytes32(address: H160, name: &str) -> [u8; 32] {
    concat_to_bytes32(address.into(), to_bytes12(name))
}

pub fn concat_to_bytes32(address: [u8; 20], name: [u8; 12]) -> [u8; 32] {
    let mut ret = [0; 32];
    ret[..20].clone_from_slice(&address);
    ret[20..].clone_from_slice(&name);
    ret
}

pub fn from_bytes32(b: [u8; 32]) -> (H160, String) {
    (
        H160::from_slice(&b[..20]),
        from_bytes12(b[20..].try_into().unwrap()),
    )
}

pub fn from_bytes12(b: [u8; 12]) -> String {
    String::from_utf8(b.to_vec()).unwrap()
}

pub fn from_bytes32_safe(b: [u8; 32]) -> (H160, String) {
    let subaccount_name_bytes = b[20..].try_into().unwrap();
    let subaccount_name = if is_isolated_subaccount(b) {
        format!("0x{}", hex::encode(subaccount_name_bytes))
    } else {
        from_bytes12_safe(subaccount_name_bytes)
    };
    (H160::from_slice(&b[..20]), subaccount_name)
}

pub fn from_bytes12_safe(b: [u8; 12]) -> String {
    match String::from_utf8(b.iter().copied().filter(|&c| c != 0).collect()) {
        Ok(s) => s,
        Err(_) => {
            format!("0x{}", hex::encode(b))
        }
    }
}