ethers.js
ethers.js copied to clipboard
Add "struct" type to the Human-Readable ABI
With ABIv2, there are more contracts using structs as input and output parameters, so it would be useful to allow the Human-Readable ABI to support structs as a alias to its respective tuple.
For example:
abi = [
"event registered(tuple(address user, string email) user)",
"function getId(tuple(address user, string email) user) view returns (uint id)",
"function getUser(uint id) view returns (tuple(address user, string email) user)",
"function addUser(tuple(address user, string email) user)"
]
Should also be able to be written as:
abi = [
"struct User { address user, string email }",
"event registered(User user)",
"function getId(User user) view returns (uint id)",
"function getUser(uint id) view returns (User user)",
"function addUser(User user)",
// Also support composite structs
"struct Relationship { User a, User b, string relationship }",
]
Hello @ricmoo
Is it currently possible to pass a struct (as a tuple) to a public function using ethers?
For example:
function get(Info memory info) public returns(bool)
Is it yet safe to use it in production?
It certainly is available, here is an example: https://blog.ricmoo.com/solidity-abiv2-a-foray-into-the-experimental-a6afd3d47185
I believe it is discouraged for production use though, as it is still experimental.
This ticket is more about adding aliases for structs in the human-readable ABI.
YEAAH ! Go struct type in Human Readable ABI !
What is the status of this?
Should also be able to be written as
While struct syntax is not supported, this works for me
const User = "(address user, string email)";
const abi = [
`event registered(${User} user)`,
`function getId(${User} user) view returns (uint id)`,
`function getUser(uint id) view returns (${User} user)`,
`function addUser(${User} user)`,
];
const Relationship = `(${User} a, ${User} b, string relationship)`;
Should also be able to be written as
While struct syntax is not supported, this works for me
const User = "(address user, string email)"; const abi = [ `event registered(${User} user)`, `function getId(${User} user) view returns (uint id)`, `function getUser(uint id) view returns (${User} user)`, `function addUser(${User} user)`, ]; const Relationship = `(${User} a, ${User} b, string relationship)`;
OMG, you are my life saver
@borgbob that works when the returns are a single element, but when you want to return an array of structs it doesn't do the trick.
We could use
const User = "(address user, string email)";
const abi = [
`function getUsers() view returns (tuple(tuple${User}))`,
];
but then you need to know beforehand the number of elements the function is going to return, and normally you don`t
I'm finding that being able to indicate the struct type on the ABI and then indicating the function return type, would be very good.
abi = [
"struct User { address user, string email }",
"function getUsers() view returns (User[] memory)",
]
Try this it works for me 100% --
const TaskStruct = "(uint256 id , string content , bool completed)"
const Simple_ABI = [
function getTodos() public view returns(${TaskStruct}[] tasks),
function getTodo(uint256 _id) public view returns(${TaskStruct} task)
]
@01JAMIL Thanks for pointing it out! I probably missed trying it that way, will check next time I face the issue! :pray:
Hi from time machine. Is this feature by any chance supported? @ricmoo
Should also be able to be written as
While struct syntax is not supported, this works for me
const User = "(address user, string email)"; const abi = [ `event registered(${User} user)`, `function getId(${User} user) view returns (uint id)`, `function getUser(uint id) view returns (${User} user)`, `function addUser(${User} user)`, ]; const Relationship = `(${User} a, ${User} b, string relationship)`;
A clever solution. It works, a million thanks