Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit d0b6e87

Browse files
committed
Fix more types to avoid [] syntax and add types for Chains
1 parent b50cbd8 commit d0b6e87

File tree

3 files changed

+196
-55
lines changed

3 files changed

+196
-55
lines changed

pallets/cash/src/chains.rs

+131-11
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,14 @@ impl FromStr for ChainId {
289289
pub trait Chain {
290290
const ID: ChainId;
291291

292-
type Address: Debuggable + Clone + Eq + Into<Vec<u8>> = [u8; 20];
293-
type Amount: Debuggable + Clone + Eq + Into<AssetAmount> = u128;
294-
type CashIndex: Debuggable + Clone + Eq + Into<CashIndex> = u128;
295-
type Rate: Debuggable + Clone + Eq + Into<APR> = u128;
296-
type Timestamp: Debuggable + Clone + Eq + Into<Timestamp> = u64;
297-
type Hash: Debuggable + Clone + Eq = [u8; 32];
298-
type PublicKey: Debuggable + Clone + Eq = [u8; 64];
299-
type Signature: Debuggable + Clone + Eq = [u8; 65]; // secp256k1 sign
292+
type Address: Debuggable + Clone + Eq + Into<Vec<u8>>;
293+
type Amount: Debuggable + Clone + Eq + Into<AssetAmount>;
294+
type CashIndex: Debuggable + Clone + Eq + Into<CashIndex>;
295+
type Rate: Debuggable + Clone + Eq + Into<APR>;
296+
type Timestamp: Debuggable + Clone + Eq + Into<Timestamp>;
297+
type Hash: Debuggable + Clone + Eq;
298+
type PublicKey: Debuggable + Clone + Eq;
299+
type Signature: Debuggable + Clone + Eq;
300300
type EventId: Debuggable + Clone + Eq + Ord;
301301
type Event: Debuggable + Clone + Eq;
302302

@@ -331,7 +331,34 @@ pub struct Tezos {}
331331
impl Chain for Gateway {
332332
const ID: ChainId = ChainId::Gate;
333333

334+
#[type_alias("Gateway__Chain__")]
335+
type Address = [u8; 20];
336+
337+
#[type_alias("Gateway__Chain__")]
338+
type Amount = u128;
339+
340+
#[type_alias("Gateway__Chain__")]
341+
type CashIndex = u128;
342+
343+
#[type_alias("Gateway__Chain__")]
344+
type Rate = u128;
345+
346+
#[type_alias("Gateway__Chain__")]
347+
type Timestamp = u64;
348+
349+
#[type_alias("Gateway__Chain__")]
350+
type Hash = [u8; 32];
351+
352+
#[type_alias("Gateway__Chain__")]
353+
type PublicKey = [u8; 64];
354+
355+
#[type_alias("Gateway__Chain__")]
356+
type Signature = [u8; 65];
357+
358+
#[type_alias("Gateway__Chain__")]
334359
type EventId = comp::EventId;
360+
361+
#[type_alias("Gateway__Chain__")]
335362
type Event = comp::Event;
336363

337364
fn zero_hash() -> Self::Hash {
@@ -466,7 +493,34 @@ impl Chain for Ethereum {
466493
impl Chain for Polkadot {
467494
const ID: ChainId = ChainId::Dot;
468495

496+
#[type_alias("Polkadot__Chain__")]
497+
type Address = [u8; 20];
498+
499+
#[type_alias("Polkadot__Chain__")]
500+
type Amount = u128;
501+
502+
#[type_alias("Polkadot__Chain__")]
503+
type CashIndex = u128;
504+
505+
#[type_alias("Polkadot__Chain__")]
506+
type Rate = u128;
507+
508+
#[type_alias("Polkadot__Chain__")]
509+
type Timestamp = u64;
510+
511+
#[type_alias("Polkadot__Chain__")]
512+
type Hash = [u8; 32];
513+
514+
#[type_alias("Polkadot__Chain__")]
515+
type PublicKey = [u8; 64];
516+
517+
#[type_alias("Polkadot__Chain__")]
518+
type Signature = [u8; 65];
519+
520+
#[type_alias("Polkadot__Chain__")]
469521
type EventId = dot::EventId;
522+
523+
#[type_alias("Polkadot__Chain__")]
470524
type Event = dot::Event;
471525

472526
fn zero_hash() -> Self::Hash {
@@ -508,7 +562,34 @@ impl Chain for Polkadot {
508562
impl Chain for Solana {
509563
const ID: ChainId = ChainId::Sol;
510564

565+
#[type_alias("Solana__Chain__")]
566+
type Address = [u8; 20];
567+
568+
#[type_alias("Solana__Chain__")]
569+
type Amount = u128;
570+
571+
#[type_alias("Solana__Chain__")]
572+
type CashIndex = u128;
573+
574+
#[type_alias("Solana__Chain__")]
575+
type Rate = u128;
576+
577+
#[type_alias("Solana__Chain__")]
578+
type Timestamp = u64;
579+
580+
#[type_alias("Solana__Chain__")]
581+
type Hash = [u8; 32];
582+
583+
#[type_alias("Solana__Chain__")]
584+
type PublicKey = [u8; 64];
585+
586+
#[type_alias("Solana__Chain__")]
587+
type Signature = [u8; 65];
588+
589+
#[type_alias("Solana__Chain__")]
511590
type EventId = sol::EventId;
591+
592+
#[type_alias("Solana__Chain__")]
512593
type Event = sol::Event;
513594

514595
fn zero_hash() -> Self::Hash {
@@ -550,7 +631,34 @@ impl Chain for Solana {
550631
impl Chain for Tezos {
551632
const ID: ChainId = ChainId::Tez;
552633

634+
#[type_alias("Tezos__Chain__")]
635+
type Address = [u8; 20];
636+
637+
#[type_alias("Tezos__Chain__")]
638+
type Amount = u128;
639+
640+
#[type_alias("Tezos__Chain__")]
641+
type CashIndex = u128;
642+
643+
#[type_alias("Tezos__Chain__")]
644+
type Rate = u128;
645+
646+
#[type_alias("Tezos__Chain__")]
647+
type Timestamp = u64;
648+
649+
#[type_alias("Tezos__Chain__")]
650+
type Hash = [u8; 32];
651+
652+
#[type_alias("Tezos__Chain__")]
653+
type PublicKey = [u8; 64];
654+
655+
#[type_alias("Tezos__Chain__")]
656+
type Signature = [u8; 65];
657+
658+
#[type_alias("Tezos__Chain__")]
553659
type EventId = tez::EventId;
660+
661+
#[type_alias("Tezos__Chain__")]
554662
type Event = tez::Event;
555663

556664
fn zero_hash() -> Self::Hash {
@@ -597,6 +705,9 @@ pub mod comp {
597705
use codec::{Decode, Encode};
598706
use our_std::RuntimeDebug;
599707

708+
use types_derive::type_alias;
709+
710+
#[type_alias("comp__")]
600711
pub type EventId = (u64, u64); // XXX
601712

602713
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
@@ -618,13 +729,13 @@ pub mod eth {
618729
SignatureRecoveryError,
619730
}
620731

621-
#[type_alias("Eth__")]
732+
#[type_alias("eth__")]
622733
pub type BlockNumber = u64;
623734

624-
#[type_alias("Eth__")]
735+
#[type_alias("eth__")]
625736
pub type LogIndex = u64;
626737

627-
#[type_alias("Eth__")]
738+
#[type_alias("eth__")]
628739
pub type EventId = (BlockNumber, LogIndex);
629740

630741
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
@@ -658,6 +769,9 @@ pub mod dot {
658769
use codec::{Decode, Encode};
659770
use our_std::RuntimeDebug;
660771

772+
use types_derive::type_alias;
773+
774+
#[type_alias("dot__")]
661775
pub type EventId = (u64, u64);
662776

663777
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
@@ -668,6 +782,9 @@ pub mod sol {
668782
use codec::{Decode, Encode};
669783
use our_std::RuntimeDebug;
670784

785+
use types_derive::type_alias;
786+
787+
#[type_alias("sol__")]
671788
pub type EventId = (u64, u64);
672789

673790
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
@@ -678,6 +795,9 @@ pub mod tez {
678795
use codec::{Decode, Encode};
679796
use our_std::RuntimeDebug;
680797

798+
use types_derive::type_alias;
799+
800+
#[type_alias("tez__")]
681801
pub type EventId = (u128, u128);
682802

683803
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]

types-derive/src/lib.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,22 @@ fn process_fields(
161161
if fields.unnamed.len() == 1 {
162162
type_to_json(&fields.unnamed.iter().next().unwrap().ty)
163163
} else {
164-
let ty = json!(fields
164+
let ty_fields = fields
165165
.unnamed
166166
.iter()
167-
.map(|field| type_to_json(&field.ty))
168-
.collect::<Vec<_>>());
167+
.map(|field| type_to_str(&field.ty))
168+
.collect::<Vec<_>>()
169+
.join(",");
170+
171+
let ty = json!(format!("({})", ty_fields));
172+
169173
match prefix_opt {
170174
Some(prefix) => {
171175
let new_type_name = format!("{}", prefix);
172176
new_types.push((new_type_name.clone(), ty));
173177
json!(new_type_name)
174178
}
175-
None => json!(ty),
179+
None => ty,
176180
}
177181
}
178182
}

0 commit comments

Comments
 (0)