Search code examples
ethereumsoliditysmartcontracts

Stuck with deciding the working of a Smart Contract Project


So I'm doing a project which basically carries out the medical insurance claim process with the help of the smart contract. How it works is:

  1. User signs up to the website run by the insurer.
  2. They file a claim by entering the concerned hospital, insured amount and a pdf file containing the bills which are to be verified.
  3. The hospital uses the website to approve/deny the claim based on the info provided.
  4. The insurer uses the website approve/deny the claim based on the info provided and pays the user.
  5. Any time someone files, approves/denies, pays the claim, an event is emitted.

This is my first ethereum/solidity project and I'm unable to figure out how to pull this together.

This is the structure of a Claim:

struct Record {
        uint id; // unique id for the record
        address patientAddr;
        address hospitalAddr;
        string billId; // points to the pdf stored somewhere
        uint amount; 
        mapping (address => RecordStatus) status; // status of the record
        bool isValid; // variable to check if record has already been created or not
    }

Some of my questions were:

  1. How do I link a Record to a specific user? As a single user can use multiple Metamask wallets.
  2. Is it possible to fetch all the events linked to a specific record with an id so I can display all the approvals/denials happened with the Record to the user?
  3. For the hospital, is there a better way to get associated Records other than to get all Records from the smart contract and then filter it on the front end?

Thanks a lot for your help.


Solution

  • To link a Record to specific user you will need to add a mapping (I am assuming that you have a user ID) that will look like this:

    mapping(uint => Record[]) recordsByUserID;
    

    Then you will be able to get an array of Records knowing the user id by:

    Records userRecords[] = recordsByUserID[user_id];
    

    About the event logging it's actually kind of easy because we have the indexed keyword, let me show you an example:

    event Approved(uint indexed userId, uint indexed recordId);
    

    With an event like this you are able to query all the events using the user id and the record id.

    About the third question I suggest you to use the graph https://thegraph.com/en/. It basically creates your own GraphQL backend by indexing all the events for you in a very easy way. Then you are able to run your graphql queries and make something efficient.