Skip to content

Commit

Permalink
Fix ordering issue with named tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
danhper committed Jul 9, 2024
1 parent e2a802c commit bdab784
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ async-trait = "0.1.80"
futures-util = "0.3.30"
semver = "1.0.23"
shellexpand = { version = "3.1.0", features = ["path"] }
indexmap = "2.2.6"
4 changes: 2 additions & 2 deletions src/interpreter/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
use std::str::FromStr;

use alloy::hex::FromHex;
use alloy::primitives::{Address, B256, I256, U256};
use anyhow::{anyhow, bail, Result};
use futures::future::{BoxFuture, FutureExt};
use indexmap::IndexMap;
use solang_parser::pt::{ContractPart, Expression, Statement};

use crate::loaders::types::Project;
Expand Down Expand Up @@ -519,7 +519,7 @@ pub fn evaluate_expression(env: &mut Env, expr: Box<Expression>) -> BoxFuture<'_
} else {
bail!("expected variable, found {:?}", name_expr);
};
let mut fields = BTreeMap::new();
let mut fields = IndexMap::new();
for arg in args.iter() {
let value = evaluate_expression(env, Box::new(arg.expr.clone())).await?;
fields.insert(arg.name.name.clone(), value);
Expand Down
7 changes: 4 additions & 3 deletions src/interpreter/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, fmt::Display};
use std::fmt::Display;

use alloy::{
dyn_abi::DynSolType,
Expand All @@ -7,6 +7,7 @@ use alloy::{
rpc::types::{Log, TransactionReceipt},
};
use anyhow::{bail, Result};
use indexmap::IndexMap;
use itertools::Itertools;

use super::{
Expand Down Expand Up @@ -110,7 +111,7 @@ pub enum Type {
String,
Array(Box<Type>),
FixedArray(Box<Type>, usize),
NamedTuple(String, BTreeMap<String, Type>),
NamedTuple(String, IndexMap<String, Type>),
Tuple(Vec<Type>),
Contract(ContractInfo),
Transaction,
Expand Down Expand Up @@ -311,7 +312,7 @@ impl Type {
Ok(Value::FixBytes(B256::from_slice(&new_vector), *size))
}
(Type::NamedTuple(name, types_), Value::Tuple(values)) => {
let mut new_values = BTreeMap::new();
let mut new_values = IndexMap::new();
for (key, value) in types_.iter().zip(values.iter()) {
new_values.insert(key.0.clone(), key.1.cast(value)?);
}
Expand Down
10 changes: 5 additions & 5 deletions src/interpreter/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use alloy::{
primitives::{Address, B256, I256, U256},
};
use anyhow::{bail, Result};
use indexmap::IndexMap;
use itertools::Itertools;
use std::{
collections::BTreeMap,
fmt::{self, Display, Formatter},
ops::{Add, Div, Mul, Rem, Sub},
};
Expand All @@ -28,7 +28,7 @@ pub enum Value {
Addr(Address),
Contract(ContractInfo, Address),
Tuple(Vec<Value>),
NamedTuple(String, BTreeMap<String, Value>),
NamedTuple(String, IndexMap<String, Value>),
Array(Vec<Value>),
TypeObject(Type),
Transaction(B256),
Expand All @@ -40,7 +40,7 @@ fn _values_to_string(values: &[Value]) -> String {
values.iter().map(|v| format!("{}", v)).join(", ")
}

fn _format_struct_fields(fields: &BTreeMap<String, Value>) -> String {
fn _format_struct_fields(fields: &IndexMap<String, Value>) -> String {
fields
.iter()
.map(|(k, v)| format!("{}: {}", k, v))
Expand Down Expand Up @@ -125,7 +125,7 @@ impl TryFrom<&Value> for alloy::dyn_abi::DynSolValue {

impl From<alloy::rpc::types::Log> for Value {
fn from(log: alloy::rpc::types::Log) -> Self {
let mut fields = BTreeMap::new();
let mut fields = IndexMap::new();
fields.insert("address".to_string(), Value::Addr(log.address()));
fields.insert(
"topics".to_string(),
Expand Down Expand Up @@ -166,7 +166,7 @@ impl TryFrom<alloy::dyn_abi::DynSolValue> for Value {
.zip(tuple)
.map(|(k, dv)| Value::try_from(dv).map(|v| (k.clone(), v)))
.collect::<Result<Vec<_>>>()?;
Ok(Value::NamedTuple(name, BTreeMap::from_iter(vs)))
Ok(Value::NamedTuple(name, IndexMap::from_iter(vs)))
}
v => Err(anyhow::anyhow!("{:?} not supported", v)),
}
Expand Down

0 comments on commit bdab784

Please sign in to comment.