feat(abi): add helper functions to access solidity types (#2081)
This commit is contained in:
parent
8ecd566930
commit
d3ad5478ae
|
@ -331,20 +331,35 @@ impl InternalStructs {
|
||||||
/// Returns the name of the rust type that will be generated if the given input is a struct
|
/// Returns the name of the rust type that will be generated if the given input is a struct
|
||||||
/// NOTE: this does not account for arrays or fixed arrays
|
/// NOTE: this does not account for arrays or fixed arrays
|
||||||
pub fn get_function_input_struct_type(&self, function: &str, input: &str) -> Option<&str> {
|
pub fn get_function_input_struct_type(&self, function: &str, input: &str) -> Option<&str> {
|
||||||
let key = (function.to_string(), input.to_string());
|
self.get_function_input_struct_solidity_id(function, input)
|
||||||
self.function_params
|
|
||||||
.get(&key)
|
|
||||||
.and_then(|id| self.rust_type_names.get(id))
|
.and_then(|id| self.rust_type_names.get(id))
|
||||||
.map(String::as_str)
|
.map(String::as_str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns solidity type identifier as it's used in the ABI.
|
||||||
|
pub fn get_function_input_struct_solidity_id(
|
||||||
|
&self,
|
||||||
|
function: &str,
|
||||||
|
input: &str,
|
||||||
|
) -> Option<&str> {
|
||||||
|
let key = (function.to_string(), input.to_string());
|
||||||
|
self.function_params.get(&key).map(String::as_str)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the name of the rust type that will be generated if the given input is a struct
|
/// Returns the name of the rust type that will be generated if the given input is a struct
|
||||||
/// This takes the index of event's parameter instead of the parameter's name like
|
/// This takes the index of event's parameter instead of the parameter's name like
|
||||||
/// [`Self::get_function_input_struct_type`] does because we can't rely on the name since events
|
/// [`Self::get_function_input_struct_type`] does because we can't rely on the name since events
|
||||||
/// support nameless parameters NOTE: this does not account for arrays or fixed arrays
|
/// support nameless parameters NOTE: this does not account for arrays or fixed arrays
|
||||||
pub fn get_event_input_struct_type(&self, event: &str, idx: usize) -> Option<&str> {
|
pub fn get_event_input_struct_type(&self, event: &str, idx: usize) -> Option<&str> {
|
||||||
|
self.get_event_input_struct_solidity_id(event, idx)
|
||||||
|
.and_then(|id| self.rust_type_names.get(id))
|
||||||
|
.map(String::as_str)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the type identifier as it's used in the solidity ABI
|
||||||
|
pub fn get_event_input_struct_solidity_id(&self, event: &str, idx: usize) -> Option<&str> {
|
||||||
let key = (event.to_string(), idx);
|
let key = (event.to_string(), idx);
|
||||||
self.event_params.get(&key).and_then(|id| self.rust_type_names.get(id)).map(String::as_str)
|
self.event_params.get(&key).map(String::as_str)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the name of the rust type that will be generated if the given output is a struct
|
/// Returns the name of the rust type that will be generated if the given output is a struct
|
||||||
|
@ -353,13 +368,24 @@ impl InternalStructs {
|
||||||
&self,
|
&self,
|
||||||
function: &str,
|
function: &str,
|
||||||
internal_type: &str,
|
internal_type: &str,
|
||||||
|
) -> Option<&str> {
|
||||||
|
self.get_function_output_struct_solidity_id(function, internal_type)
|
||||||
|
.and_then(|id| self.rust_type_names.get(id))
|
||||||
|
.map(String::as_str)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the name of the rust type that will be generated if the given output is a struct
|
||||||
|
/// NOTE: this does not account for arrays or fixed arrays
|
||||||
|
pub fn get_function_output_struct_solidity_id(
|
||||||
|
&self,
|
||||||
|
function: &str,
|
||||||
|
internal_type: &str,
|
||||||
) -> Option<&str> {
|
) -> Option<&str> {
|
||||||
self.outputs
|
self.outputs
|
||||||
.get(function)
|
.get(function)
|
||||||
.and_then(|outputs| {
|
.and_then(|outputs| {
|
||||||
outputs.iter().find(|s| s.as_str() == struct_type_identifier(internal_type))
|
outputs.iter().find(|s| s.as_str() == struct_type_identifier(internal_type))
|
||||||
})
|
})
|
||||||
.and_then(|id| self.rust_type_names.get(id))
|
|
||||||
.map(String::as_str)
|
.map(String::as_str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,6 +400,8 @@ impl InternalStructs {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns all the solidity struct types
|
/// Returns all the solidity struct types
|
||||||
|
///
|
||||||
|
/// These are grouped by their case-sensitive type identifiers extracted from the ABI.
|
||||||
pub fn structs_types(&self) -> &HashMap<String, SolStruct> {
|
pub fn structs_types(&self) -> &HashMap<String, SolStruct> {
|
||||||
&self.structs
|
&self.structs
|
||||||
}
|
}
|
||||||
|
@ -562,7 +590,7 @@ fn struct_type_name(name: &str) -> &str {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `Pairing.G2Point` -> `Pairing.G2Point`
|
/// `Pairing.G2Point` -> `Pairing.G2Point`
|
||||||
fn struct_type_identifier(name: &str) -> &str {
|
pub fn struct_type_identifier(name: &str) -> &str {
|
||||||
name.trim_start_matches("struct ").split('[').next().unwrap()
|
name.trim_start_matches("struct ").split('[').next().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue