我無法編譯我的solidity合約,有人可以幫助更正程式碼。我已經嘗試了大約 24 小時
這是我的智能合約:
pragma solidity ^0.5.0; contract Agent { struct patient { string name; string dob; address[] doctorAccessList; uint[] diagnosis; string record; } struct doctor { string name; string dob; address[] patientAccessList; } uint creditPool; address[] public patientList; address[] public doctorList; mapping (address => patient) patientInfo; mapping (address => doctor) doctorInfo; // might not be necessary mapping (address => string) patientRecords; function add_agent(string memory _name, string memory _dob, uint _designation, string memory _hash) public { address addr = msg.sender; if(_designation == 0){ patientInfo[addr].name = _name; patientInfo[addr].dob = _dob; patientInfo[addr].record = _hash; patientList.push(addr)-1; } else if (_designation == 1){ doctorInfo[addr].name = _name; doctorInfo[addr].dob = _dob; doctorList.push(addr)-1; } else{ revert(); } } function get_patient(address addr) view public returns (string memory, string memory, uint, string memory){ // if(keccak256(patientInfo[addr].name) == keccak256(""))revert(); return (patientInfo[addr].name, patientInfo[addr].dob, patientInfo[addr].diagnosis, patientInfo[addr].record); } function get_doctor(address addr) view public returns (string memory, string memory){ // if(keccak256(doctorInfo[addr].name)==keccak256(""))revert(); return (doctorInfo[addr].name, doctorInfo[addr].dob); } function get_patient_doctor_name(address paddr, address daddr) view public returns (string memory, string memory){ return (patientInfo[paddr].name,doctorInfo[daddr].name); } function permit_access(address addr) payable public { require(msg.value == 2 ether); creditPool += 2; doctorInfo[addr].patientAccessList.push(msg.sender)-1; patientInfo[msg.sender].doctorAccessList.push(addr)-1; } //must be called by doctor function insurance_claim(address paddr, uint _diagnosis, string memory _hash) public { bool patientFound = false; for(uint i = 0;i<doctorInfo[msg.sender].patientAccessList.length;i++){ if(doctorInfo[msg.sender].patientAccessList[i]==paddr){ msg.sender.transfer(2 ether); creditPool -= 2; patientFound = true; } } if(patientFound==true){ set_hash(paddr, _hash); remove_patient(paddr, msg.sender); }else { revert(); } bool DiagnosisFound = false; for(uint j = 0; j < patientInfo[paddr].diagnosis.length;j++){ if(patientInfo[paddr].diagnosis[j] == _diagnosis)DiagnosisFound = true; } } function remove_element_in_array(address[] storage Array, address addr) internal returns(uint) { bool check = false; uint del_index = 0; for(uint i = 0; i<Array.length; i++){ if(Array[i] == addr){ check = true; del_index = i; } } if(!check) revert(); else{ if(Array.length == 1){ delete Array[del_index]; } else { Array[del_index] = Array[Array.length - 1]; delete Array[Array.length - 1]; } Array.length--; } } function remove_patient(address paddr, address daddr) public { remove_element_in_array(doctorInfo[daddr].patientAccessList, paddr); remove_element_in_array(patientInfo[paddr].doctorAccessList, daddr); } function get_accessed_doctorlist_for_patient(address addr) public view returns (address) { address[] storage doctoraddr = patientInfo[addr].doctorAccessList; return doctoraddr; } function get_accessed_patientlist_for_doctor(address addr) public view returns (address) { return doctorInfo[addr].patientAccessList; } function revoke_access(address daddr) public payable{ remove_patient(msg.sender,daddr); msg.sender.transfer(2 ether); creditPool -= 2; } function get_patient_list() public view returns(address){ return patientList; } function get_doctor_list() public view returns(address){ return doctorList; } function get_hash(address paddr) public view returns(string memory){ return patientInfo[paddr].record; } function set_hash(address paddr, string memory _hash) internal { patientInfo[paddr].record = _hash; } }
它最初是在 0.4.15 版本中編寫的。我正在使用solidity 0.5.0版
顯示的錯誤:
home/superdupertafara/Desktop/HIT_400/contracts/PatientEMRSet.sol:1:1:SyntaxError:源文件需要不同的編譯器版本(目前編譯器是 0.5.0+commit.1d4f565a.Emscripten.clang - 請注意,每晚建構被認為是嚴格小於已發布的版本 pragma solidity ^0.4.0; ^———————^ ,/home/superduertafara/Desktop/HIT_400/contracts/Patient_Doctor。 sol:53:16:TypeError:返回參數類型元組(字元串儲存參考,字元串儲存參考,uint256
$$ $$storage ref,string storage ref) 不能隱式轉換為預期的類型元組(string memory,string memory,uint256,string memory)。返回(患者資訊$$ addr $$.name,患者資訊$$ addr $$.dob,患者資訊$$ addr $$.診斷,患者資訊$$ addr $$。記錄); ^————————————————- ————————————————– -^ ,/home/superdupertafara/Desktop/HIT_400/contracts/Patient_Doctor.sol:130:16: TypeError: Return argument type address$$ $$儲存指針不能隱式轉換為預期類型(第一個返回變數的類型)地址。返回醫生地址;^——–^ ,/home/superduertafara/Desktop/HIT_400/contracts/Patient_Doctor.sol:134:16: TypeError: Return argument type address$$ $$storage ref 不能隱式轉換為預期類型(第一個返回變數的類型)地址。返回醫生資訊$$ addr $$.患者訪問列表;^——————————–^ ,/home/superdupertafara/Desktop/HIT_400/contracts/Patient_Doctor.sol: 145:16:TypeError:返回參數類型地址$$ $$storage ref 不能隱式轉換為預期類型(第一個返回變數的類型)地址。返回患者列表;^———^ ,/home/superduertafara/Desktop/HIT_400/contracts/Patient_Doctor.sol:149:16: TypeError: Return argument type address$$ $$storage ref 不能隱式轉換為預期類型(第一個返回變數的類型)地址。返回醫生列表;^——–^
該錯誤與編譯器版本無關;如果您在 return 語句中指定了一個數組,則需要返回一個數組(例如
uint[]
, ):address[]
// return uint[], not uint function get_patient(address addr) view public returns (string memory, string memory, uint[] memory, string memory){ // if(keccak256(patientInfo[addr].name) == keccak256(""))revert(); return (patientInfo[addr].name, patientInfo[addr].dob, patientInfo[addr].diagnosis, patientInfo[addr].record); }
所有這些錯誤都是相同的。請記住檢查您的錯誤消息 - 這裡有提示:
Return argument type tuple(string storage ref,string storage ref,uint256[] storage ref,string storage ref) is not implicitly convertible to expected type tuple(string memory,string memory,uint256,string memory)
. 您告訴函式將數組作為單個元素返回,這就是為什麼它試圖將其“隱式轉換”為另一種類型。