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

Commit

Permalink
Fix more types to avoid [] syntax and add types for Chains
Browse files Browse the repository at this point in the history
  • Loading branch information
hayesgm committed Apr 7, 2021
1 parent b50cbd8 commit d0b6e87
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 55 deletions.
142 changes: 131 additions & 11 deletions pallets/cash/src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,14 @@ impl FromStr for ChainId {
pub trait Chain {
const ID: ChainId;

type Address: Debuggable + Clone + Eq + Into<Vec<u8>> = [u8; 20];
type Amount: Debuggable + Clone + Eq + Into<AssetAmount> = u128;
type CashIndex: Debuggable + Clone + Eq + Into<CashIndex> = u128;
type Rate: Debuggable + Clone + Eq + Into<APR> = u128;
type Timestamp: Debuggable + Clone + Eq + Into<Timestamp> = u64;
type Hash: Debuggable + Clone + Eq = [u8; 32];
type PublicKey: Debuggable + Clone + Eq = [u8; 64];
type Signature: Debuggable + Clone + Eq = [u8; 65]; // secp256k1 sign
type Address: Debuggable + Clone + Eq + Into<Vec<u8>>;
type Amount: Debuggable + Clone + Eq + Into<AssetAmount>;
type CashIndex: Debuggable + Clone + Eq + Into<CashIndex>;
type Rate: Debuggable + Clone + Eq + Into<APR>;
type Timestamp: Debuggable + Clone + Eq + Into<Timestamp>;
type Hash: Debuggable + Clone + Eq;
type PublicKey: Debuggable + Clone + Eq;
type Signature: Debuggable + Clone + Eq;
type EventId: Debuggable + Clone + Eq + Ord;
type Event: Debuggable + Clone + Eq;

Expand Down Expand Up @@ -331,7 +331,34 @@ pub struct Tezos {}
impl Chain for Gateway {
const ID: ChainId = ChainId::Gate;

#[type_alias("Gateway__Chain__")]
type Address = [u8; 20];

#[type_alias("Gateway__Chain__")]
type Amount = u128;

#[type_alias("Gateway__Chain__")]
type CashIndex = u128;

#[type_alias("Gateway__Chain__")]
type Rate = u128;

#[type_alias("Gateway__Chain__")]
type Timestamp = u64;

#[type_alias("Gateway__Chain__")]
type Hash = [u8; 32];

#[type_alias("Gateway__Chain__")]
type PublicKey = [u8; 64];

#[type_alias("Gateway__Chain__")]
type Signature = [u8; 65];

#[type_alias("Gateway__Chain__")]
type EventId = comp::EventId;

#[type_alias("Gateway__Chain__")]
type Event = comp::Event;

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

#[type_alias("Polkadot__Chain__")]
type Address = [u8; 20];

#[type_alias("Polkadot__Chain__")]
type Amount = u128;

#[type_alias("Polkadot__Chain__")]
type CashIndex = u128;

#[type_alias("Polkadot__Chain__")]
type Rate = u128;

#[type_alias("Polkadot__Chain__")]
type Timestamp = u64;

#[type_alias("Polkadot__Chain__")]
type Hash = [u8; 32];

#[type_alias("Polkadot__Chain__")]
type PublicKey = [u8; 64];

#[type_alias("Polkadot__Chain__")]
type Signature = [u8; 65];

#[type_alias("Polkadot__Chain__")]
type EventId = dot::EventId;

#[type_alias("Polkadot__Chain__")]
type Event = dot::Event;

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

#[type_alias("Solana__Chain__")]
type Address = [u8; 20];

#[type_alias("Solana__Chain__")]
type Amount = u128;

#[type_alias("Solana__Chain__")]
type CashIndex = u128;

#[type_alias("Solana__Chain__")]
type Rate = u128;

#[type_alias("Solana__Chain__")]
type Timestamp = u64;

#[type_alias("Solana__Chain__")]
type Hash = [u8; 32];

#[type_alias("Solana__Chain__")]
type PublicKey = [u8; 64];

#[type_alias("Solana__Chain__")]
type Signature = [u8; 65];

#[type_alias("Solana__Chain__")]
type EventId = sol::EventId;

#[type_alias("Solana__Chain__")]
type Event = sol::Event;

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

#[type_alias("Tezos__Chain__")]
type Address = [u8; 20];

#[type_alias("Tezos__Chain__")]
type Amount = u128;

#[type_alias("Tezos__Chain__")]
type CashIndex = u128;

#[type_alias("Tezos__Chain__")]
type Rate = u128;

#[type_alias("Tezos__Chain__")]
type Timestamp = u64;

#[type_alias("Tezos__Chain__")]
type Hash = [u8; 32];

#[type_alias("Tezos__Chain__")]
type PublicKey = [u8; 64];

#[type_alias("Tezos__Chain__")]
type Signature = [u8; 65];

#[type_alias("Tezos__Chain__")]
type EventId = tez::EventId;

#[type_alias("Tezos__Chain__")]
type Event = tez::Event;

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

use types_derive::type_alias;

#[type_alias("comp__")]
pub type EventId = (u64, u64); // XXX

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

#[type_alias("Eth__")]
#[type_alias("eth__")]
pub type BlockNumber = u64;

#[type_alias("Eth__")]
#[type_alias("eth__")]
pub type LogIndex = u64;

#[type_alias("Eth__")]
#[type_alias("eth__")]
pub type EventId = (BlockNumber, LogIndex);

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

use types_derive::type_alias;

#[type_alias("dot__")]
pub type EventId = (u64, u64);

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

use types_derive::type_alias;

#[type_alias("sol__")]
pub type EventId = (u64, u64);

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

use types_derive::type_alias;

#[type_alias("tez__")]
pub type EventId = (u128, u128);

#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
Expand Down
12 changes: 8 additions & 4 deletions types-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,22 @@ fn process_fields(
if fields.unnamed.len() == 1 {
type_to_json(&fields.unnamed.iter().next().unwrap().ty)
} else {
let ty = json!(fields
let ty_fields = fields
.unnamed
.iter()
.map(|field| type_to_json(&field.ty))
.collect::<Vec<_>>());
.map(|field| type_to_str(&field.ty))
.collect::<Vec<_>>()
.join(",");

let ty = json!(format!("({})", ty_fields));

match prefix_opt {
Some(prefix) => {
let new_type_name = format!("{}", prefix);
new_types.push((new_type_name.clone(), ty));
json!(new_type_name)
}
None => json!(ty),
None => ty,
}
}
}
Expand Down
Loading

0 comments on commit d0b6e87

Please sign in to comment.