Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💡 [REQUEST] - Return value from Contract.reads instead of lists #1168

Closed
ifavo opened this issue Aug 14, 2024 · 4 comments · Fixed by #1280
Closed

💡 [REQUEST] - Return value from Contract.reads instead of lists #1168

ifavo opened this issue Aug 14, 2024 · 4 comments · Fixed by #1280

Comments

@ifavo
Copy link
Contributor

ifavo commented Aug 14, 2024

Summary

When reading from a contract using a Contract instance from sdk-network, the result is always returned as a list. (This appears to be due to the raw ThorNode results being passed through, which return lists because of multi-clause processing.)

From a developer experience standpoint, this consistent use of lists adds unnecessary complexity and friction during development.

To enhance the developer experience, I suggest modifying the function to return the value of [0] directly in such cases, rather than leaving it in a list.

Basic Example

const VTHO = new Contract("0x...", [...ABI_ERC20], thorClient)

// current situation
const [balance] = await VTHO.read.balanceOf(address)

// desired situation
const balance = await VTHO.read.balanceOf(address)
@fabiorigam
Copy link
Member

fabiorigam commented Sep 10, 2024

@ifavo It's possible to return two values, try this one for example:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

contract SampleContract {
    function a() public pure returns (uint, string memory) {
        // Return values are (1, "a")
        return (1, "a");
    }

    function b() pure public {
        uint A;
        string memory B;

        // A is 1 and B is "a"
        (A, B) = a();

        // A is 1
        (A,) = a();

        // B is "a"
        (, B) = a();
    }
}

Given this, I wouldn't change the code. What do you think?

@ifavo
Copy link
Contributor Author

ifavo commented Sep 10, 2024

@fabiorigam the list in the result matches the clauses and because a single clause request only has a single clause response, it would always be wrapped in the first element, or am I mistaken?

If you return a tuple/struct, would that not be wrapped the same way?

to use your example:

// current situation
const [ [val1, val2] ] = await contract.read.a()
// val1 = 1, val2 =a

// desired situation
const [val1, val2] = await contract.read.a()
// val1 = 1, val2 =a

@fabiorigam fabiorigam linked a pull request Sep 11, 2024 that will close this issue
@fabiorigam
Copy link
Member

It's already like the desired situation, see: #1280

@ifavo
Copy link
Contributor Author

ifavo commented Sep 11, 2024

It's already like the desired situation, see: #1280

I just used the latest version (beta 30) and it already works as desired, at least in beta 18 it was still the case.
This issue is indeed obsolete and no longer valid.

Thank you 🙏

@ifavo ifavo closed this as completed Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants