AnswerBun.com

Solidity & truffle: get elements from strings array

Ethereum Asked by Paulo N. Carrillo Peña on December 5, 2020

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?

2 Answers

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()

Answered by Mehmet Doğan on December 5, 2020

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.

Answered by Anil Singh on December 5, 2020

Add your own answers!

Related Questions

Nethereum C# FilterInput.GetAllChanges always empty

1  Asked on February 12, 2021 by exaltedoil

       

Testing Gas Token on testnet failing transaction

0  Asked on February 9, 2021 by marr

   

I am unable to install web3 on my mac

1  Asked on February 5, 2021 by saiteja-kuruva

 

Out of stack DUP2 1/1024 when adding string to parameter

0  Asked on February 1, 2021 by koukotsu

   

How to find contract’s address?

6  Asked on January 31, 2021 by user3280964

   

Import error in Truffle Pet Shop tutorial

1  Asked on January 29, 2021 by xpain

   

Timeout w/ Mocha on deploying a contract

2  Asked on January 26, 2021 by tulun

   

How to send ERC20 token to smart contract in constructor

1  Asked on January 24, 2021 by user938363

 

Deploy bytecode

1  Asked on January 19, 2021 by william-entriken

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir