How to save complex object in the contract in solidity

0 votes

I have this use case where I need to persist the following sample data in my contract.

{
    Linkage : {"4" : "1", "77" : "59", "5" : "64", "4" : "464", "455" : "364", "25" : "364", "25" : "164", "55" : "8684", "85" : "864"},
    UserId : "Some Id",
}

The dictionary is obviously expandable (root And Linkage). I want to send the data and retreive it as an object (c# and Java style). So when I communicate from WEB3 I can pass json. Is that possible?

This is where I got stuck...

pragma solidity ^0.4.13;

contract Test{         
  struct UserProfile{       
    string UserId;
  }   

  UserProfile public Profile;

  function setProfile(UserProfile newProfile) public {
    Profile  = newProfile;
  }        
}
Oct 15, 2018 in Blockchain by slayer
• 29,270 points
232 views

1 answer to this question.

0 votes

You can't pass objects when initiating a transaction (yet). You can only pass in/return a structthrough internal function calls (see Solidity FAQ).

You'll have to pass in your data using primitive types and add them to your internal struct:

pragma solidity ^0.4.13;

contract Test { 
  struct UserProfile {
    string userId;
    mapping(uint256 => uint256) linkage;
  }   

  UserProfile public profile;

  function addLinkage(uint256 id, uint256 value) public {
    profile.linkage[id] = value;
  }
}

Note, if you want to pass in your linkages in bulk, you can use addLinkage(uint256[] ids, uint256[] values).

answered Oct 15, 2018 by Omkar
• 68,940 points

Related Questions In Blockchain

0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,090 points
593 views
0 votes
1 answer

How to prevent the smart contract from being modified and deployed in the blockchain network?

To expand on Matthew's answer, each state ...READ MORE

answered Jul 5, 2018 in Blockchain by aryya
• 7,350 points
77 views
0 votes
1 answer

How to manually create the instance of the contract in truffle?

You can try to do it like ...READ MORE

answered Sep 24, 2018 in Blockchain by slayer
• 29,270 points
105 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,810 points
226 views
0 votes
1 answer
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 68,940 points
144 views
+1 vote
4 answers

How to estimate the cost for deploying smart contract on mainnet?

Since you have already deployed the contract ...READ MORE

answered Apr 9, 2018 in Blockchain by Shashank
• 10,360 points
1,872 views
0 votes
1 answer

How to set the hex-encoded data field in a Web3j Ethereum transaction?

You can use the "data" field of ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 68,940 points
261 views