0

i'm glad to follow this forum, it is very helpful. I'm using truffle to develop contracts and i just found that dynamic arrays are not yet suported, for that reason i was using string arrays with a fixed lenght and i found that the element with index 0 is not showing anything and last one says undefined. do you know ay reason of that?, do i have to get elements in a different way??

This is the function in solidity:

pragma solidity ^0.4.21;
pragma experimental ABIEncoderV2;

contract Example {
    function getStringArray() public returns (string[]) {
        string[] memory resultStr = new string;
        resultStr[0] = "355555555";
        resultStr[1] = "2";
        resultStr[2] = "8888";
        return resultStr;
    }
}

I also did a test with truffle like this:

in javascript i test getting elements one by one and i got this:

var ExampleCont = artifacts.require("Example");

contract('ExampleCont', async(accounts) => {
    before(async () => {
    ExampleContract = await ExampleCont.new();
    });
    it('Upload File', async () => {
        let stringArray = await ExampleContract.getStringArray.call();
        console.log("list size=" + stringArray.lenght+"0="
              + stringArray[0] + "1="+stringArray[0]+", 2=" + stringArray[1] + "3=" + stringArray[2]);
    }
}

list size=3, value 0= , 1=355555555, 2=2, 3=undefined

any idea about what's happening in this case?, how do i have work well with string arrays to get all elements?

  • 1
    Please make minimum effort and publish code that compiles (including pragma solidity)! – goodvibration Feb 28 at 11:17
  • And even after tweaking it to a working version, this function will never return what you say it does when called from the off-chain (i.e., from a Truffle test in your case), because it is not constant (neither pure nor view). You should post accurate details if you want to get proper help here. – goodvibration Feb 28 at 11:20
  • i already upload the code as i have it on each part. – Paulo N. Carrillo Peña Feb 28 at 12:36
  • So you are certain that the function is not declared view or pure??? – goodvibration Feb 28 at 12:50
  • In addition, I get Error: Type function (uint256) returns (string memory) is not implicitly convertible to expected type string memory[] memory. on the line string[] memory resultStr = new string;. – goodvibration Feb 28 at 12:52
0

I believe this is something not available in Solidity yet (having array of string). Because string in solidity is array of bytes, and now if you see the treatment of string[] becomes 2 d array bytes[][] and that's a limitation with Solidity as of now.

  • It is when you declare pragma experimental ABIEncoderV2; (which he did). – goodvibration Feb 28 at 13:22
0

I used ABIEncoderv2 with solidity v0.5.0, and below code works perfectly.

function _getStringArray() public view returns (string[] memory) {
    string[] memory resultStr = new string[](3);
    resultStr[0] = "355555555";
    resultStr[1] = "2";
    resultStr[2] = "8888";
    return resultStr;
}

JavaScript starts here. I have used "web3 ^1.0.0-beta.36". I have tested in Kovan network, so use your kovan INFURA configuration to see it works. It's what can I do for now.

const ABI = [{"constant":true,"inputs":[],"name":"_getStringArray","outputs":[{"name":"","type":"string[]"}],"payable":false,"stateMutability":"view","type":"function"}];
const infuraUrl = "INFURAURL";
const infuraKey = "INFURAKEY";
const web3 = new Web3(infuraUrl+infuraKey)


ReactDOM.render(
  <div id ="test"></div>,
  document.getElementById('app')
);

module.hot.accept();

function integrateContract() {
  return new Promise(function (resolve, reject) {
      var contract = new web3.eth.Contract(ABI, "0x9fb7ba497a7572cc3234c5d0ae4dddc08d7bc1c0");
      resolve(contract);
  });
}

function getList(){
  integrateContract().then(instance => { 
    console.log({instance})
    instance.methods._getStringArray().call().then(res=> {
      document.getElementById("test").append(res)
    })

  });
}

getList()
  • the difference i see on this is memory in the signature and view. Let me try, i thought that memory in the signature as a part of the returns statement was prohibited – Paulo N. Carrillo Peña Mar 1 at 8:17
  • may i ask how did you test it? i just set the method in the same way you did and i print the array in javascript and i got this: [ '', '355555555', '2', '8888', '323' ] – Paulo N. Carrillo Peña Mar 1 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.