0

I want to save data in the blockchain table whose format is as below:

{
  "docsample": {
    "id": 1,
    "creationDate": "2012-05-16T18:25:43.511Z",
    "contentType": "abc",
    "type": "abc",
    "owner": {
      "name": "abc",
      "certificate": {
        "serialNumber": "abc",
        "issuer": "abc"
      }
    },
    "fingerprint": "abc"
  }
}

In my smart contract, I declared owner as one struct and cert as another. I want to pass this struct as a parameter to action. Smart contract struct are:

struct [[eosio::table]] ipdoctable {
                        uint64_t id;
                        time_point_sec createdAt;
                        std::string contentType;
                        std::string type;
                        struct owner;
                        name fingerprint;

                        uint64_t primary_key() const { return fingerprint.value;}
                        };

                        typedef eosio::multi_index<"ipdocs"_n, ipdoctable> ipdoc_index;

                        struct [[eosio::table]] owner{
                                std::string name;
                                struct certificate;
                        };

                        struct [[eosio::table]] certificate{
                                std::string serialNumber;
                                std::string issuer;
                        };

And Action is:

[[eosio::action]]
 void upsertipdoc(time_point_sec createdAt, std::string contentType, std::string type, struct ownerinfo *owner,name fingerprint) {
      ipdoc_index ipdocs ( get_self(), get_first_receiver().value );
      bool found = false;
      for(auto itr = ipdocs.begin(); itr != ipdocs.end() && found!=true; itr++) {
      if(itr->fingerprint == fingerprint) {
         found = true;
         ipdocs.modify(itr, _self, [&]( auto& row ) {
                row.contentType = contentType,
                row.type = type,
                row.owner = owner,
                row.fingerprint = fingerprint;
                });
          }
     }

Apologies if any illogical mistake in the code. Not much familiar with C++. Please guide how would I save data in the given manner?

1 Answer 1

1

I manage to find the solution. I use struct in the struct as follows.

struct certificateinfo{
   string serialNumber;
   string issuer;
};

struct ownerinfo{
   string name;
   certificateinfo certificate;
};

struct [[eosio::table]] ipdoctable {
       uint64_t id;
       string creationDate;
       string contentType;
       string type;
       ownerinfo owner;
       name fingerprint;

       uint64_t primary_key() const { return fingerprint.value;}
       };

       typedef eosio::multi_index<"ipdocs"_n, ipdoctable> ipdoc_index;

Accept the parameters separately as string instead of struct in the action and set the owner and certificate struct values as follow:

[[eosio::action]]
 void upsertipdoc(time_point_sec createdAt, std::string contentType, std::string type, string ownername,string serialNumber,string issuer, name fingerprint) {

      ownerinfo ownerinfo = {ownername, {serialNumber,issuer}};

      ipdoc_index ipdocs ( get_self(), get_first_receiver().value );
      bool found = false;
      for(auto itr = ipdocs.begin(); itr != ipdocs.end() && found!=true; itr++) {
      if(itr->fingerprint == fingerprint) {
         found = true;
         ipdocs.modify(itr, _self, [&]( auto& row ) {
                row.contentType = contentType,
                row.type = type,
                row.owner = ownerinfo,
                row.fingerprint = fingerprint;
                });
          }
     }

Thank you!

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.