PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 25, 2022

[FIXED] How to read arrays of struct from another deployed contract?

 September 25, 2022     blockchain, ethereum, smartcontracts, solidity     No comments   

Issue

OK i have struct array in one already deployed smart contract on mainnet.Now i need to read that array in my contract.I have address of deployed smart contract.I am trying to use it with interface. Something like this:

   pragma solidity ^0.8.7;

interface IContract1{
    struct UserInfo{
   uint id ;
   string ime;
}
    function arr(uint index) external returns(UserInfo memory );
}
contract Contract2{

struct UserInfo{
   uint id ;
   string ime;
}
event UserEVENT (UserInfo UserInfo);
UserInfo [] public newarr;

function foo(address addr,uint i) external {
    IContract1(addr).arr(i);
    emit UserEVENT (IContract1(addr).arr(i));
}
}

This is my caller contract. Of course this is oversimplified example. When i try to put IContract1(addr).arr(i); in emit or try to return UserInfo in foo function compiler throwing me an error TypeError: Invalid type for argument in function call. Invalid implicit conversion from struct IContract1.UserInfo memory to struct Contract2.UserInfo memory requested.

and sc with arr

    pragma solidity ^0.8.7;

contract Contract1{
struct UserInfo{
   uint id ;
   string ime;
}
  
UserInfo [5] public arr;

constructor()  {
    arr[0] = UserInfo(1,"Milos");
   arr[1] = UserInfo(2,"Stefan");
   arr[2] = UserInfo(3,"Sloba");
   arr[3] = UserInfo(4,"Prci");
    arr[4] = UserInfo(5,"Dovla");
}
}

Solution

The easiest fix is defining the struct outside both the contract and interface, this way you write it only once and there's no collision.

This compiles for me:

pragma solidity ^0.8.7;

struct UserInfo{
   uint id ;
   string ime;
}

interface IContract1{    
    function arr(uint index) external returns(UserInfo memory );
}

contract Contract2{
    event UserEVENT (UserInfo UserInfo);
    UserInfo [] public newarr;

    function foo(address addr,uint i) external {
        IContract1(addr).arr(i);
        emit UserEVENT (IContract1(addr).arr(i));
    }
}


Answered By - 0xSanson
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing