4
  1. I am storing and returning the struct value by using mapping.

    pragma solidity >=0.5.0 <0.7.0;
    pragma experimental ABIEncoderV2;
    
    contract Manage{
    
     struct Policy{
      string  PolicyID ;
     }
     mapping (address => mapping(string => Policy)) private policy;
    
     function storePolicy(address insuranceHolder,string memory _PolicyID) public {        
       policy[insuranceHolder][_PolicyID].PolicyID = _PolicyID;
     }
     function getPolicy(address insuranceHolder,string memory _PolicyID) public view returns(Policy memory) {
      return policy[insuranceHolder][_PolicyID];
     }
    }
    

I am getting a return value like this. 0: tuple(string):_PolicyID I don't want this 0: tuple(string): I want only _PolicyID, how to do that can anyone help me.

  1. I am getting a warning for using pragma experimental ABIEncoderV2; warning: Experimental features are turned on. do not use experimental features on live deployment.

    The compiler gives the warning. That means I should only use it for development. That makes no sense to me.

2 Answers 2

3

pragma solidity >=0.5.0 <0.7.0;

Bad idea. You should target a specific compiler - matching the intended target. This will reduce confusion.

struct Policy{ string PolicyID ; } mapping (address => mapping(string => Policy)) private policy;

I don't think this structure is appropriate.

user address => policyId => Policy ?? .., so users can have multiple policies?

I am storing and returning the struct value...I want only _PolicyID

Then you should return only the PolicyID

I note also you are passing in a Policy struct and this seems odd because the Policies are supposed to be in the mapping, in storage.

The compiler gives the warning. That means I should only use it for development.

The warning means what it says, and you probably don't need it.

You have a function to set a policy and get a policy. The struct says nothing about the Policy except the ID, which is redundant. I'll show you why.

If you "get" policy ID x, then you already know the policy ID. There is no need to store that in the struct.

Have a look at this explainer to learn how to deal with create, retrieve, update and delete for your policies. I would recommend that you set aside the concern about more than one per person, at first. Master the basic structure and then turn your attention to a more advanced structure. https://medium.com/robhitchens/solidity-crud-part-1-824ffa69509a

Your functions should look like this:

function set(policyID, arguments ...) ...

function get(policyID) public view returns(arguments) ...

Hope it helps.

1
  • But I came here trying to find out how to return structs...
    – whamsicore
    Commented May 19, 2021 at 23:18
0

In case if you want to use web3 for interaction with smart contract. There you will get expected out put of you question.

Create an instance:

   let app ; 
     Manage.deployed().then(function(instance) {
      app = instance ;
      app.getPolicy(arg1,arg2).then(value => {
      console.log('Get policy response ', value); // your expected output is here 
     })
   })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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