Extend ABI to eval types directly to encoded vec
@@ -73,11 +73,11 @@ pub struct GatewayABIConfig {
/// cryptography algorithm
pub crypto: CryptoAlgo,
/// address length in bytes
- pub address_length: u32,
+ pub address_length: u16,
/// value length in bytes
- pub value_type_size: u32,
+ pub value_type_size: u16,
/// value length in bytes
- pub decimals: u32,
+ pub decimals: u16,
/// value length in bytes. ToDo: move as part of metadata.
pub structs: Vec<StructDecl>,
}
@@ -236,7 +236,7 @@ impl Type {
pub fn eval(
&self,
encoded_val: Vec<u8>,
- _gen: &GatewayABIConfig,
+ // _gen: &GatewayABIConfig,
) -> Result<Box<dyn sp_std::any::Any>, &'static str> {
match self {
Type::Address(size) => match size {
@@ -316,6 +316,38 @@ impl Type {
}
}
+pub fn eval_to_encoded(t: Type, raw_val: Vec<u8>) -> Result<Vec<u8>, &'static str> {
+ match t {
+ Type::Address(size) => match size {
+ 20 => {
+ let res: [u8; 20] = decode_buf2val(raw_val)?;
+ Ok(res.to_vec())
+ }
+ 32 => {
+ let res: [u8; 32] = decode_buf2val(raw_val)?;
+ Ok(res.to_vec())
+ }
+ _ => Err("Unknown Address size"),
+ },
+ Type::Uint(size) => match size {
+ 32 => {
+ let res: u32 = decode_buf2val(raw_val)?;
+ Ok(res.encode())
+ }
+ 64 => {
+ let res: u64 = decode_buf2val(raw_val)?;
+ Ok(res.encode())
+ }
+ 128 => {
+ let res: u128 = decode_buf2val(raw_val)?;
+ Ok(res.encode())
+ }
+ _ => Err("Unknown Uint size"),
+ },
+ _ => {Ok(vec![])}
+ }
+}
+
pub fn eval_trait_dynamically(t: Type) -> Result<Box<dyn sp_std::any::Any>, &'static str> {
match t {
Type::Hasher(hasher_alg, hash_size) => match hasher_alg {
