Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Support returning structs from functions #33

Closed
skywinder opened this issue Apr 3, 2018 · 11 comments
Closed

Support returning structs from functions #33

skywinder opened this issue Apr 3, 2018 · 11 comments

Comments

@skywinder
Copy link
Contributor

skywinder commented Apr 3, 2018

Version 0.4.22 still gets issues with that;

ethereum/solidity#40
FYI:
https://ethereum.stackexchange.com/questions/7317/how-can-i-return-struct-when-function-is-called
ethereum/solidity#1601
ethereum/solidity#2948
ethereum/solidity#2863

Depends on #32

@skywinder skywinder added this to the Solidity 0.4.22 milestone Apr 3, 2018
@skywinder skywinder changed the title Return structs from functions Support returning structs from functions Apr 3, 2018
@kyriediculous
Copy link

Is there an ETA for this , next protocol release / Solidity 0.5.0?

@shamatar
Copy link
Contributor

shamatar commented Apr 6, 2018

EncoderV2 is in 0.4.0 already, you can specify to use it with the following code

let contract = web3.contract(jsonString, at: nil, abiVersion: 2)

Although I didn't have a chance to test it well in read world yet, requires some beta Solidity compiler voodoo. So far it's backward compatible with V1 anyway

@kyriediculous
Copy link

I've tried it with the 0.4.22 compiler.
It seems to not play nice with remix and web3 still? Last thing I read is that it only worked with Ether.js so far. Not sure if there have been any updates on this since 29th of jan.

@shamatar
Copy link
Contributor

shamatar commented Apr 7, 2018

Hello @kyriediculous

I've made the following contract

pragma solidity ^0.4.21;

pragma experimental ABIEncoderV2;

contract Tester {
    struct TestStruct {
        uint256 number;
        string someText;
        uint256[2] staticArray;
        uint256[] dynamicArray;
        string[2] anotherDynamicArray;
    }
    
    
    constructor() {
        
    }
    
    function testSingle() public pure returns (TestStruct t) {
        uint256[] memory dynArray = new uint256[](3);
        dynArray[0] = 0;
        dynArray[1] = 1;
        dynArray[2] = 2;
        uint256[2] memory stArray = [uint256(0), uint256(1)];
        string[2] memory anotherDynArray = ["Hello", "World"];
        t = TestStruct({
            number: 1,
            someText: "Hello world",
            staticArray: stArray,
            dynamicArray: dynArray,
            anotherDynamicArray: anotherDynArray
        });
        return t;
    }
    
    function testDynArray() public pure returns (TestStruct[] ts) {
        ts = new TestStruct[](2);
        ts[0] = testSingle();
        ts[1] = testSingle();
    }
    
    function testStaticArray() public pure returns (TestStruct[2] ts) {
        ts[0] = testSingle();
        ts[1] = testSingle();
    }
}

So far the latest version from develop branch branch successfully works with "testSingle" method.

Sincerely, Alex

@shamatar
Copy link
Contributor

shamatar commented Apr 7, 2018

Full support should be in master now

@shamatar
Copy link
Contributor

shamatar commented Apr 7, 2018

Full encoding support is also in master now

@shamatar shamatar closed this as completed Apr 7, 2018
@kyriediculous
Copy link

It's likely that this is compatible with web3 1.0.0 right now but not with anything else; Not with remix or web3 implementations that are compatible with Metamask (since they are web3 1.0.0 incompatible) as of writing.

Of course this isn't at your end.

Remix:

[call] from:0xca35b7d915458ef540ade6068dfe2f44e8fa733c, to:Tester.testSingle(), data:f376e...6e013, return: 
{
	"error": "Failed to decode output: Error: Unsupported or invalid type: tuple"
}

@shamatar
Copy link
Contributor

shamatar commented Apr 8, 2018 via email

@roschler
Copy link

roschler commented May 4, 2018

If I deploy a contract using the ABI experimental function(s) described here for returning a struct from a Soldity function, will it work with Web3.js on Rinkeby and the main net? I'm getting this error during a compile in Remix:

browser/video-dummy-data.sol:2:1: Warning: Experimental features are turned on. Do not use experimental features on live deployments.
pragma experimental ABIEncoderV2;
^-------------------------------^

@shamatar
Copy link
Contributor

shamatar commented May 4, 2018

Hello @roschler

First of all, ABIv2 encoder/decoder in web3swift is backward compatible with a normal one. It will understand all current encoding types, but will also decode experimental features, such as returning structs. You can already use these features if you want by putting this #pragma is your code, and it will be included in 0.5 version of solidity. Interestingly, solidity docs describing ABI encoding imply features of ABIv2, looks like those features just were not implemented yet in main solidity branch.

So, if you do not need this features - just remove a pragma and everything will work. If you do need them, you will have to supply a pragma and use V2 decoder from web3swift.

Sincerely, Alex

@roschler
Copy link

roschler commented May 4, 2018

Thanks Alex!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants