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,040 points
37 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 66,880 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
• 16,450 points
210 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 ariaholic
• 7,320 points
33 views
0 votes
1 answer
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,790 points
77 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
• 66,880 points
40 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,330 points
1,066 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
• 66,880 points
87 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.