I want to create a contract between a Patient and Doctor where the first Patient fills his details like name, age, and problems he is facing. As soon as the above details are filled function createDoctor() is called and name, age and problems are sent as parameters to the Doctor. Considering the details filled by the patient the doctor sets the Physician and updates the problems faced by the patient if any. But I am not able to figure out the way to implement the above statement. Below is the code where I am asking the details of Patient and send it to the Doctor. Now I am not able to implement the Doctor part. Below is the code:
Patient.sol
pragma solidity ^0.4.6;
import './Doctor.sol';
contract Patient {
bytes private name = "";
bytes private dateOfBirth="NA";
bytes private problemFaced = "";
address public myDoctor;
// Event that is fired when patient is changed
event patientChanged(string whatChanged);
function createDoctor() public {
myDoctor = new Doctor(getName(), getDateOfBirth(), getGender(), problemFaced);
}
function ProblemFaced(string problems) public {
problemFaced = bytes(problems);
}
function getName() internal view returns (bytes) {
return name; // First_Name Last_Name
}
function getDateOfBirth() internal view returns (bytes) {
return dateOfBirth; // YYYYMMDD
}
function setName(string _name) public {
name = bytes(_name); // First_Name Last_Name
emit patientChanged("name changed"); // fire the event
}
function setDateOfBirth(string _dateOfBirth) public {
dateOfBirth = bytes(_dateOfBirth); // YYYYMMDD
emit patientChanged("dateOfBirth changed"); // fire the event
}
}
Doctor.sol
pragma solidity ^0.4.6;
contract Doctor {
// the address of the owner (the patient)
address public owner;
uint balance;
// address of physician that can add allergies
string public physician;
// name of the patient LAST^FIRST
string public patient_name;
string public patient_dob;
string public patient_gender;
string public patient_problem = '';
modifier isOwnerPatient {
require(msg.sender == owner);
_;
}
function Doctor(bytes _name, bytes _dob, bytes _gender, bytes _problems) {
owner = msg.sender;
patient_name = string(_name);
patient_dob = string(_dob);
patient_gender = string(_gender);
patient_problem = string(_problems);
}
function updateProblem(bytes _condition) {
patient_problem = strConcat(patient_problem, string(_condition));
}
// allows owner to set the physician that can add allergies
function SetPhysician(string _physician) isOwnerPatient {
physician = _physician;
}
function strConcat(string _a, string _b) internal pure returns (string){
bytes memory _ba = bytes(_a);
bytes memory _bb = bytes(_b);
string memory abcde = new string(_ba.length + 2 + _bb.length);
delete abcde;
bytes memory babcde = bytes(abcde);
uint k = 0;
for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i];
babcde[k++] = ",";
babcde[k++] = " ";
for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i];
return string(babcde);
}
}
Is there any possibility of using owner and msg.sender while executing above problem statement.
Related
Working on solidity 8.12, I have two structs, namely singer and event, two arrays one for singers and another for events. I want to write down a function that creates an event, specifying the singer id, and automatically append the event id into the array of events attended by the singer:
struct Singer {
uint id;
uint[] events_ids; // THIS IS WHAT I WANT TO UPDATE WITH THE NEW EVENT ID
}
struct Event {
uint id;
uint singer_id;
}
singer[] public SingerList;
event[] public EventList;
uint public n_singers;
uint public n_events;
function CreateEvent(uint singer_id) external {
if (singer_id > n_singers) { // singer id greater than the number of current singers.
revert SingerIdError(singer_id);
}
else if (singer_id == n_singers){ // the singer does not exist yet! Create it!
n_singers ++;
--------> SingerList.push(Singer(singer_id))); // HOW TO APPEND THE EVENT ID TO THE LIST OF EVENTS IN SINGER STRUCT?
EventList.push(Event(n_events, singer_id));
}
else { // the pilot already exists
Singer storage _singer = SingerList[singer_id];
_singer.event_ids.push(n_events);
EventList.push(Event(n_events, n_events));
}
n_events ++;
}```
Using 'mapping' instead of array-of-struct, gives you better control over your state variables with no extra hassles to control the array indexes. It also costs lower gas fee.
mapping(uint => uint[]) Singer;
mapping(uint => uint[]) Event;
uint public n_singers;
uint public n_events;
function CreateEvent(uint singer_id) external {
if(singer_id > n_singers) {
revert SingerIdError(singer_id);
}
else if (singer_id == n_singers) {
n_singers ++;
Singer[singer_id].push(n_events); // HOW TO APPEND THE EVENT ID TO THE LIS....
Event[n_events].push(singer_id);
} else {
Singer[singer_id].push(n_events);
}
n_events++;
}`
When I am compiling with truffle it is giving me this error
I got this error after adding payable
prior to that it was
winners[j].transfer(betwa*(10000+(LoserBet*10000/WinnerBet))/10000);
I had to add it because I was getting another error
Which was
TypeError: "send" and "transfer" are only available for objects of type "address payable", not "address".
My Complete Contract :
pragma solidity ^0.5.16;
contract Betting {
address public owner;
uint256 public minimumBet;
uint256 public totalBetsOne;
uint256 public totalBetsTwo;
address[] public players;
struct Player {
uint256 amountBet;
uint16 teamSelected;
}
// The address of the player and => the user info
mapping(address => Player) public playerInfo;
function() external payable {}
constructor() public {
owner = msg.sender;
minimumBet = 100000000000000;
}
function kill() public {
if(msg.sender == owner) selfdestruct(msg.sender);
}
function checkPlayerExists(address player) public view returns(bool){
for(uint256 i = 0; i < players.length; i++){
if(players[i] == player) return true;
}
return false;
}
function bet(uint8 _teamSelected) public payable {
//The first require is used to check if the player already exist
require(!checkPlayerExists(msg.sender));
//The second one is used to see if the value sended by the player is
//Higher than the minimum value
require(msg.value >= minimumBet);
//We set the player informations : amount of the bet and selected team
playerInfo[msg.sender].amountBet = msg.value;
playerInfo[msg.sender].teamSelected = _teamSelected;
//then we add the address of the player to the players array
players.push(msg.sender);
//at the end, we increment the stakes of the team selected with the player bet
if ( _teamSelected == 1){
totalBetsOne += msg.value;
}
else{
totalBetsTwo += msg.value;
}
}
// Generates a number between 1 and 10 that will be the winner
function distributePrizes(uint16 teamWinner) public {
address[1000] memory winners;
//We have to create a temporary in memory array with fixed size
//Let's choose 1000
uint256 count = 0; // This is the count for the array of winners
uint256 LoserBet = 0; //This will take the value of all losers bet
uint256 WinnerBet = 0; //This will take the value of all winners bet
address playerAddress;
//We loop through the player array to check who selected the winner team
for(uint256 i = 0; i < players.length; i++){
playerAddress = players[i];
//If the player selected the winner team
//We add his address to the winners array
if(playerInfo[playerAddress].teamSelected == teamWinner){
winners[count] = playerAddress;
count++;
}
}
//We define which bet sum is the Loser one and which one is the winner
if ( teamWinner == 1){
LoserBet = totalBetsTwo;
WinnerBet = totalBetsOne;
}
else{
LoserBet = totalBetsOne;
WinnerBet = totalBetsTwo;
}
//We loop through the array of winners, to give ethers to the winners
for(uint256 j = 0; j < count; j++){
// Check that the address in this fixed array is not empty
if(winners[j] != address(0)){
address add = winners[j];
uint256 betwa = playerInfo[add].amountBet;
//Transfer the money to the user
payable(winners[j]).transfer( (betwa*(10000+(LoserBet*10000/WinnerBet)))/10000 );
}
}
delete playerInfo[playerAddress]; // Delete all the players
players.length = 0; // Delete all the players array
LoserBet = 0; //reinitialize the bets
WinnerBet = 0;
totalBetsOne = 0;
totalBetsTwo = 0;
}
function AmountOne() public view returns(uint256){
return totalBetsOne;
}
function AmountTwo() public view returns(uint256){
return totalBetsTwo;
}
}
What I have tried is making the address payable in this function but It is not working I have tried to replace memory with payable but still it isn't working
My versions
Truffle v5.4.18 (core: 5.4.18)
Solidity v0.5.16 (solc-js)
Node v14.15.1
Web3.js v1.5.3
The payable() conversion from address to address payable was introduced in Solidity 0.6.
Source: https://docs.soliditylang.org/en/latest/060-breaking-changes.html#new-features
Since your contract is using (deprecated) version 0.5.16, it doesn't allow this conversion. So you need to define the winners array already as address payable:
// added `payable`
address payable[1000] memory winners;
Then you'll be able to use the .transfer() method of the address payable type:
// `winners[j]` is payable
winners[j].transfer(...);
I am new to Solidity and building a Todo List project.
I still got below error messages in the deductTask function even after using keccak256(abi.encodePacked())
to pack my string variables
error 1 . Type bytes32 is not implicitly convertible to expected type string memory.
error 2 . Operator == not compatible with types string memory and string memory
pragma solidity >=0.4.22 <0.9.0;
contract myTodoList {
// Model a Task
struct Task {
uint id;
string content;
bool completed;
}
// Read/write Tasks
mapping(uint => Task) public Tasks;
// Store Tasks Count
uint public TasksCount = 0;
function addTask (string memory _content) public {
TasksCount ++;
Tasks[TasksCount] = Task(TasksCount, _content, false);
}
Task TaskTemp;
function deductTask (string memory _contentClicked) public {
string memory comparedContent;
uint i ;
for(i = 1; i <= TasksCount; i++) {
TaskTemp = Tasks[i];
comparedContent = TaskTemp.content;
comparedContent = keccak256(abi.encodePacked(comparedContent));
//comparedContent = keccak256(abi.encodePacked(comparedContent));
_contentClicked = keccak256(abi.encodePacked(_contentClicked));
//_contentClicked = keccak256(abi.encodePacked(_contentClicked));
if(_contentClicked == comparedContent ){
delete Tasks[i];
}
}
TasksCount --;
}
constructor () public {
addTask("my first task");
addTask("my second task");
}
}
This is Auction smart contract.
I want to make Auction like this. If someone registers his product for biding, anyone who want to buy can join on this bidding.
But When I test on Remix, it can't be complied even.
I think my coding is problem.
Please help me.
pragma solidity ^0.5.0;
contract Auction{
//product for auction
struct Product{
string name;
string description;
uint time;
}
//top bid
uint topMoney;
//presen owner of product
mapping (uint => address) productToOwner;
//owner of top bidder
mapping (uint => address) topMoneyOwner;
event Listed(uint id, string name, uint itme);
Product[] public products;
//register product
function listUp(string memory _name, string memory _description) public {
//time limit for bidding, 1 minutes;
uint time = now + 1 minutes;
//index of product
uint id = products.push(Product(_name, _description, time)) - 1;
//initial bid = 0
topMoney=0;
//initial owner of product
productToOwner[id] = msg.sender;
emit Listed(id, _name, time);
}
//bidding
function bidOn() payable public {
if ( topMoney < msg.value){
topMoney = msg.value;
topMoneyOwner[topMoney] = msg.sender;
} else {
msg.sender.transfer(msg.value);
}
}
//bidding end? return (bool)
function _end(uint _id) private view returns (bool) {
require(now >= products[_id].time);
return true;
}
//who is winner? Then, transfer money to owner of product.
function winner(uint _id) public {
require( true == _end(_id));
address(uint160(productToOwner[_id])).transfer(topMoney);
productToOwner[_id] = topMoneyOwner[topMoney];
}
}
// #param physicalAddress - the actual address of the home a host wants to list (not the ethereum address)
// #return _id - list of ids for homes
function listHomesByAddress(string _physicalAddress) public returns(uint [] _id ) {
uint [] results;
for(uint i = 0 ; i<homes.length; i++) {
if(keccak256(homes[i].physicalAddress) == keccak256(_physicalAddress) && homes[i].available == true) {
results.push(homes[i].id);
}
}
return results;
}
The result is supposed to be a list of ids which match the physical address entered however it does not filter through but returns all the available homes.
When I change to using String utils nothing changes.
Here is the whole code:
pragma solidity ^0.4.0;
import "browser/StringUtils.sol";
// #title HomeListing
contract HomeListing {
struct Home {
uint id;
string physicalAddress;
bool available;
}
Home[] public homes;
mapping (address => Home) hostToHome;
event HomeEvent(uint _id);
event Test(uint length);
constructor() {
}
// #param physicalAddress - the actual address of the home a host wants to list (not the ethereum address)
function addHome(string _physicalAddress) public {
uint _id = uint(keccak256(_physicalAddress, msg.sender));
homes.push(Home(_id, _physicalAddress, true));
}
// #param physicalAddress - the actual address of the home a host wants to list (not the ethereum address)
// #return _id - list of ids for homes
function listHomesByAddress(string _physicalAddress) public returns(uint [] _id ) {
uint [] results;
for(uint i = 0 ; i<homes.length; i++) {
string location = homes[i].physicalAddress;
if(StringUtils.equal(location,_physicalAddress )) {
results.push(homes[i].id);
}
}
return results;
}
}
The part giving you trouble is the line uint[] results;. Arrays declared as local variables reference storage memory by default. From the "What is the memory keyword" section of the Solidity docs:
There are defaults for the storage location depending on which type of variable it concerns:
state variables are always in storage
function arguments are in memory by default
local variables of struct, array or mapping type reference storage by default
local variables of value type (i.e. neither array, nor struct nor mapping) are stored in the stack
The result is you're referencing the first storage slot of your contract, which happens to be Home[] public homes. That's why you're getting the entire array back.
To fix the problem, you need to use a memory array. However, you have an additional problem in that you can't use dynamic memory arrays in Solidity. A workaround is to decide on a result size limit and declare your array statically.
Example (limited to 10 results):
function listHomesByAddress(string _physicalAddress) public view returns(uint[10]) {
uint [10] memory results;
uint j = 0;
for(uint i = 0 ; i<homes.length && j < 10; i++) {
if(keccak256(homes[i].physicalAddress) == keccak256(_physicalAddress) && homes[i].available == true) {
results[j++] = homes[i].id;
}
}
return results;
}