use sp_core::{H160, U256};
use sp_runtime::traits::Convert;
use sp_std::vec::Vec;
use super::types::{Balance, Bytes32, EthAddress, ItemId, RegistryId, TokenId, TrancheWeight};
impl From<Bytes32> for EthAddress {
fn from(v: Bytes32) -> Self {
EthAddress(v[..32].try_into().expect("Address wraps a 32 byte array"))
}
}
impl From<EthAddress> for Bytes32 {
fn from(a: EthAddress) -> Self {
a.0
}
}
impl From<RegistryId> for EthAddress {
fn from(r: RegistryId) -> Self {
let padded = r.0.to_fixed_bytes().iter().copied()
.chain([0; 12].iter().copied()).collect::<Vec<u8>>()[..32]
.try_into().expect("RegistryId is 20 bytes. 12 are padded. Converting to a 32 byte array should never fail");
EthAddress(padded)
}
}
impl From<EthAddress> for RegistryId {
fn from(a: EthAddress) -> Self {
RegistryId(H160::from_slice(&a.0[..20]))
}
}
impl From<[u8; 20]> for RegistryId {
fn from(d: [u8; 20]) -> Self {
RegistryId(H160::from(d))
}
}
impl AsRef<[u8]> for RegistryId {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl From<U256> for TokenId {
fn from(v: U256) -> Self {
Self(v)
}
}
impl From<u16> for ItemId {
fn from(v: u16) -> Self {
Self(v as u128)
}
}
impl From<u32> for ItemId {
fn from(v: u32) -> Self {
Self(v as u128)
}
}
impl From<u128> for ItemId {
fn from(v: u128) -> Self {
Self(v)
}
}
impl Convert<TrancheWeight, Balance> for TrancheWeight {
fn convert(weight: TrancheWeight) -> Balance {
weight.0
}
}
impl From<u128> for TrancheWeight {
fn from(v: u128) -> Self {
Self(v)
}
}