0

Im writing a simple smart contract to store and retrieve data of series of patients' name and blood group with an id using mapping function in solidity. I don't know the commands to interact with the deployed contract via truffle console. Anyone please write me the truffle console commands to interact with this contract. I'm using solidity version of 0.5.0.

pragma solidity ^0.5.0;

contract PatientDB {
    
    mapping (uint => patient) public patient_db;
    
    struct patient {
        string name;
        string blood;
    }
    
    function addPatient (uint _id, string memory _name, string memory _blood) public {
        patient_db[_id] = patient (_name, _blood);
    }
}

1 Answer 1

0

Deep down, you don't interact with the mapping. The public modifier does several things, one of those things is generate a "free" view function. You would get the same effect by removing public and instead, explicitly writing a view function like this:

function patient_db(uint index) public view returns(string memory name, string memory blood) {
  patient storage p = patient_db[index];
  return(p.name, p.blood);
}

Therefore, the truffle commands are the same as you would use for any view function. Something like:

let instance = await MyContract.deployed();
let patient = await instance.patient_db("0"); // where "0" is the unit index to inspect
console.log(patient[0].toString(10));
console.log(patient[1].toString(10));

The function returns a tuple, so the arguments are in an array, and the BigNumbers need to be converted to decimal strings to be human-readable.

Hope it helps.

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.