Skip to content

Commit

Permalink
FIx correct variant in scales (#46)
Browse files Browse the repository at this point in the history
* Rebase and fix lints

* remove newlines

* remove newlines

---------

Co-authored-by: Daniel Olano <[email protected]>
  • Loading branch information
S0c5 and olanod authored Apr 23, 2024
1 parent 401d322 commit 78b5c4d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
5 changes: 4 additions & 1 deletion scales/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ license = "Apache-2.0"

[dependencies]
bytes = { version = "1.1.0", default-features = false }
scale-info = { version = "2.1.1", default-features = false, features = ["serde"] }
scale-info = { version = "2.10.0", default-features = false, features = ["serde"] }
serde = { version = "1.0.137", default-features = false }
serde_json = { version = "1.0.80", default-features = false, optional = true }
codec = { version = "3.1.2", package = "parity-scale-codec", default-features = false, optional = true }
hex = { version = "0.4.3", default-features = false, features = ["alloc"], optional = true }
log = "0.4.17"



[features]
default = ["std", "codec", "json", "hex", "experimental-serializer"]
Expand Down
12 changes: 7 additions & 5 deletions scales/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,14 @@ impl SpecificType {
match self {
SpecificType::Variant(_, _, Some(_)) => Some(self),
SpecificType::Variant(_, ref mut variants, idx @ None) => {
let i = variants
let (vf, _) = variants
.iter()
.map(get_field)
.position(|f| f.as_ref() == selection.as_ref())? as u8;
variants.retain(|v| v.index == i);
*idx = Some(i);
.map(|v| (v.index, get_field(v)))
.find(|(_, f)| f.as_ref() == selection.as_ref())?;

variants.retain(|v| v.index == vf);
*idx = Some(vf);

Some(self)
}
_ => panic!("Only for enum variants"),
Expand Down
Binary file added scales/src/registry.bin
Binary file not shown.
30 changes: 29 additions & 1 deletion scales/src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::prelude::*;
use bytes::BufMut;
use codec::Encode;
use core::fmt::{self, Debug};

use scale_info::{PortableRegistry, TypeInfo};
use serde::{ser, Serialize};

Expand Down Expand Up @@ -842,7 +843,7 @@ fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str {
mod tests {
use super::*;
use alloc::collections::BTreeMap;
use codec::Encode;
use codec::{Decode, Encode};
use core::mem::size_of;
use scale_info::{meta_type, Registry, TypeInfo};
use serde_json::to_value;
Expand Down Expand Up @@ -1282,4 +1283,31 @@ mod tests {
assert_eq!(out, expected);
Ok(())
}

#[test]
fn test_extrincic_call() -> Result<()> {
let bytes = include_bytes!("registry.bin");
let registry = PortableRegistry::decode(&mut &bytes[..]).expect("hello");

let transfer_call = serde_json::json!({
"transfer_keep_alive": {
"dest": {
"Id": hex::decode("12840f0626ac847d41089c4e05cf0719c5698af1e3bb87b66542de70b2de4b2b").expect("expected valid address")
},
"value": 1_000_000_000_000u64
}
});

let call_data =
to_vec_with_info(&transfer_call, (&registry, 106u32).into()).expect("call data");

let encooded = hex::encode(call_data);

assert_eq!(
"0x04030012840f0626ac847d41089c4e05cf0719c5698af1e3bb87b66542de70b2de4b2b070010a5d4e8",
format!("0x04{}", encooded)
);

Ok(())
}
}

0 comments on commit 78b5c4d

Please sign in to comment.