Solidity

如何將儲存在區塊鏈中的數據顯示給客戶端應用程序

  • April 25, 2022

我為將數據儲存到區塊鏈的“線上出生和死亡登記系統”編寫了這份契約,現在我想知道如何檢索我儲存的數據,以便將其顯示在客戶端的另一個頁面上。

pragma solidity >=0.4.22 <0.9.0;

contract Obdrs {

   uint public regCount;
   uint256 dateOfBirth; //save date as unix timestamp or use a string
   string gender;
   string fullName;
   string placeOfBirth;
   string motherFullname;
   string motherNrc;
   string fatherFullname;
   string fatherNrc;
   string bo; //birthOccurence
   string permanentAddress; //address
   string contactNumber;
   //... with all of your properties

   struct form1 {

   uint256 dateOfBirth; //save date as unix timestamp or use a string
   string gender;
   string fullName;
   string placeOfBirth;
   string motherFullname;
   string motherNrc;
}    
   struct form2 {
   string fatherFullname;
   string fatherNrc;
   string bo; //birthOccurence
   string permanentAddress; //address
   string contactNumber;
   //... with all of your properties
}

         mapping(uint => form1) public forms1;
         mapping(uint => form2) public forms2;

       event formsReg1(
   uint id,
   uint256 dateOfBirth, //save date as unix timestamp or use a string
   string gender,
   string fullName,
   string placeOfBirth,
   string motherFullname,
   string motherNrc
 );     

     event formsReg2(
   uint id,
   string fatherFullname,
   string fatherNrc,
   string bo, //birthOccurence
   string permanentAddress, //address
   string contactNumber
   //... with all of your properties

 );


   function storeBirthCertificate1(uint256 _dateOfBirth, string memory _gender, 
   string memory _fullName, string memory _placeOfBirth, string memory _motherFullname, 
   string memory _motherNrc) public {

           regCount ++;
   forms1[regCount] = form1(_dateOfBirth, _gender, _fullName,
                            _placeOfBirth, _motherFullname, _motherNrc);
   emit formsReg1(regCount, _dateOfBirth, _gender, _fullName,
                            _placeOfBirth, _motherFullname, _motherNrc);


   }

    function storeBirthCertificate2(string memory _fatherFullname,
   string memory _fatherNrc, string memory _bo, string memory _permanentAddress, 
   string memory _contactNumber) public {

           regCount ++;
   forms2[regCount] = form2(_fatherFullname, _fatherNrc, _bo,
                            _permanentAddress, _contactNumber);
   emit formsReg2(regCount, _fatherFullname, _fatherNrc, _bo,
                            _permanentAddress, _contactNumber);


   }

您可以使用兩個選項:

  • 由於您的兩個映射具有公共訪問修飾符,因此您可以查詢將特定索引傳遞給它們,並且您將檢索您傳遞的特定索引的元組form1/form2結構;
  • getter第二種方法是為您的結構和查詢編寫一個方法,將使用storeBirthCertificate1or 函式儲存的元組的索引傳遞給 getterstoreBirthCertificate2函式。關於最後一種情況的一個例子是:
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract Obdrs {
uint public regCount;
uint256 dateOfBirth; //save date as unix timestamp or use a string
string gender;
string fullName;
string placeOfBirth;
string motherFullname;
string motherNrc;
string fatherFullname;
string fatherNrc;
string bo; //birthOccurence
string permanentAddress; //address
string contactNumber;
//... with all of your properties

struct form1 {
    uint256 dateOfBirth; //save date as unix timestamp or use a string
    string gender;
    string fullName;
    string placeOfBirth;
    string motherFullname;
    string motherNrc;
}

struct form2 {
    string fatherFullname;
    string fatherNrc;
    string bo; //birthOccurence
    string permanentAddress; //address
    string contactNumber;
    //... with all of your properties
}

mapping(uint => form1) private forms1;
mapping(uint => form2) private forms2;

event formsReg1(uint id, uint256 dateOfBirth, string gender, string fullName, string placeOfBirth, string motherFullname,string motherNrc);     
event formsReg2(uint id, string fatherFullname, string fatherNrc, string bo, string permanentAddress, string contactNumber);


function storeBirthCertificate1(uint256 _dateOfBirth, string memory _gender, string memory _fullName, string memory _placeOfBirth, string memory _motherFullname, string memory _motherNrc) public {
    regCount ++;
    forms1[regCount] = form1(_dateOfBirth, _gender, _fullName, _placeOfBirth, _motherFullname, _motherNrc);
    emit formsReg1(regCount, _dateOfBirth, _gender, _fullName, _placeOfBirth, _motherFullname, _motherNrc);
}

 function storeBirthCertificate2(string memory _fatherFullname, string memory _fatherNrc, string memory _bo, string memory _permanentAddress, string memory _contactNumber) public {
    regCount ++;
    forms2[regCount] = form2(_fatherFullname, _fatherNrc, _bo, _permanentAddress, _contactNumber);
    emit formsReg2(regCount, _fatherFullname, _fatherNrc, _bo, _permanentAddress, _contactNumber);
}

function getBirthCertificateForm1(uint _index) external view returns(form1 memory) {
    return forms1[_index];
}

function getBirthCertificateForm2(uint _index) external view returns(form2 memory) {
    return forms2[_index];
}
}

您需要使用 web3js 或 ethersjs 從客戶端與區塊鏈進行通信。它的文件實施起來相當容易理解

引用自:https://ethereum.stackexchange.com/questions/126758