Skip to content

Commit

Permalink
feat(sol-macro): add return value names to simple getters
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jun 4, 2024
1 parent 9d5c39f commit bf8785a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
31 changes: 30 additions & 1 deletion crates/sol-types/tests/macros/sol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::{b256, bytes, hex, keccak256, Address, I256, U256};
use alloy_primitives::{b256, bytes, hex, keccak256, Address, B256, I256, U256};
use alloy_sol_types::{sol, SolCall, SolError, SolEvent, SolStruct, SolType};
use serde::Serialize;
use serde_json::Value;
Expand Down Expand Up @@ -238,6 +238,35 @@ fn getters() {
let _ = nestedMapArrayReturn { _0: U256::ZERO };
}

#[test]
fn getter_names() {
sol! {
contract Getters {
string public value;
string[] public array;
mapping(bytes32 => string) public map;
mapping(bytes32 k => string v) public mapWithNames;

mapping(bytes32 k1 => mapping(uint256 k2 => string v2) v1) public nestedMapWithNames;
}
}

let _ = Getters::valueCall {};
let _ = Getters::valueReturn { value: String::new() };

let _ = Getters::arrayCall { _0: U256::ZERO };
let _ = Getters::arrayReturn { _0: String::new() };

let _ = Getters::mapCall { _0: B256::ZERO };
let _ = Getters::mapReturn { _0: String::new() };

let _ = Getters::mapWithNamesCall { k: B256::ZERO };
let _ = Getters::mapWithNamesReturn { v: String::new() };

let _ = Getters::nestedMapWithNamesCall { k1: B256::ZERO, k2: U256::ZERO };
let _ = Getters::nestedMapWithNamesReturn { v2: String::new() };
}

#[test]
fn abigen_sol_multicall() {
sol!("../syn-solidity/tests/contracts/Multicall.sol");
Expand Down
14 changes: 11 additions & 3 deletions crates/syn-solidity/src/item/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,19 @@ impl ItemFunction {
pub fn new_getter(name: SolIdent, ty: Type) -> Self {
let span = name.span();
let kind = FunctionKind::new_function(span);
let mut function = Self::new(kind, Some(name));
let mut function = Self::new(kind, Some(name.clone()));

// `public view`
function.attributes.0 = vec![
FunctionAttribute::Visibility(Visibility::new_public(span)),
FunctionAttribute::Mutability(Mutability::new_view(span)),
];

// Recurse into mappings and arrays to generate arguments and the return type
// Recurse into mappings and arrays to generate arguments and the return type.
// If the return type is simple, the return value name is set to the variable name.
let mut ty = ty;
let mut return_name = None;
let mut first = true;
loop {
match ty {
// mapping(k => v) -> arguments += k, ty = v
Expand All @@ -171,8 +173,14 @@ impl ItemFunction {
function.parameters.push(VariableDeclaration::new(uint256));
ty = *array.ty;
}
_ => break,
_ => {
if first {
return_name = Some(name);
}
break;
}
}
first = false;
}
let mut returns = ParameterList::new();
returns.push(VariableDeclaration::new_with(ty, None, return_name));
Expand Down

0 comments on commit bf8785a

Please sign in to comment.