Skip to content

Instantly share code, notes, and snippets.

@dimitrihartt
Created July 8, 2020 21:25
Show Gist options
  • Save dimitrihartt/96536f8dc4702b5e7ca3ad57e7d8c573 to your computer and use it in GitHub Desktop.
Save dimitrihartt/96536f8dc4702b5e7ca3ad57e7d8c573 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.6.10+commit.00c0fcaf.js&optimize=false&gist=
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.6.0 < 0.7.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/contracts/math/SafeMath.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
library StringsAndBytes {
function stringToBytes32(string memory _source) public pure returns (bytes32 result) {
// String have to be max 32 chars
// https://ethereum.stackexchange.com/questions/9603/understanding-mload-assembly-function
// http://solidity.readthedocs.io/en/latest/assembly.html
assembly {
result := mload(add(_source, 32))
}
}
function bytes32ToBytes(bytes32 _bytes32) public pure returns (bytes memory){
// bytes32 (fixed-size array) to bytes (dynamically-sized array)
// string memory str = string(_bytes32);
// TypeError: Explicit type conversion not allowed from "bytes32" to "string storage pointer"
bytes memory bytesArray = new bytes(32);
for (uint256 i; i < 32; i++) {
bytesArray[i] = _bytes32[i];
}
return bytesArray;
}
function bytes32ToString(bytes32 _bytes32) public pure returns (string memory){
// https://ethereum.stackexchange.com/questions/2519/how-to-convert-a-bytes32-to-string
// https://ethereum.stackexchange.com/questions/1081/how-to-concatenate-a-bytes32-array-to-a-string
bytes memory bytesArray = bytes32ToBytes(_bytes32);
return string(bytesArray);
}
}
contract MDS is ERC20 {
using SafeMath for uint;
// This is a type that represents the MDS Company`s owners.
struct Chairman {
address addr;
uint amount;
}
enum Gender { male, female }
// This is a type that represents the MDS Company.
struct Company {
bytes32 companyName; // Company Name short name (up to 32 bytes)
bytes32 website; // Website short name (up to 32 bytes)
uint balanceChairmen; // How much the owners balance is
uint balanceShare; // How much the share balance is
uint balanceBase; // How much the base balance is
uint balanceFamilies; // How much all the families balance is
uint numChairmen; // Number of Chairmen (5)
mapping (uint => Chairman) chairmen; // These are the chairmen. (Up to 5 chairmen.)
bool paused; // If the Company is paused
uint256 timeCreated; // TimeStamp for the creation of the Company
}
// This is a type that represents a User
struct User {
address addr; // User address
bytes32 firstName; // First short name (up to 32 bytes)
bytes32 familyName; // Family short name (up to 32 bytes)
bytes32 email; // User email
uint age; // User age
Gender gender; // If this User is male (true) or female (false)
uint balance; // How much the User has invested
address delegate; // Person delegated to
bool activeMember; // If the User is active or not inside a family
uint256 timeCreated; // TimeStamp for the creation of the User
}
// This is a type that represents the Family
struct Family {
bytes32 familyName; // Family name
address familyCreator; // Family creator address
uint balance; // How much the family has stored up
uint numMembers; // Number of members of this family (default value: 0)
address[] membersArray; //Array of all members in this family
uint256 timeCreated; // TimeStamp for the creation of the Family
}
// This is a type that represents some God`s kingdom principles
struct Misc {
uint twelfth; // The twelfth
uint tenth; // The tenth
uint fifth; // The fifth
uint bridePrice; // The bride-price (150 ETH)
}
Misc misc = Misc(12, 10, 5, 150000000000000000000);
// This declares a state variable that
// stores a `User` struct for each possible address.
mapping(address => User) public users;
// This declares a state variable that
// stores a `Family` struct for each familyName.
mapping(bytes32 => Family) public families;
// This declares a state variable that
// stores the `MDS` Contract struct
mapping(address => Company) public companies;
// Modifiers
modifier onlyFamilyCreator {
require(
msg.sender == families[users[msg.sender].familyName].familyCreator,
"Only the family creator can call this function."
);
_;
}
modifier onlyMale {
require(users[msg.sender].gender == Gender.male);
_;
}
modifier onlyFemale {
require(users[msg.sender].gender == Gender.female);
_;
}
modifier onlyOwners {
require(msg.sender != address(0), "Access denied!");
Company storage mds = companies[address(this)];
require(
msg.sender == mds.owners[1].addr ||
msg.sender == mds.owners[2].addr ||
msg.sender == mds.owners[3].addr ||
msg.sender == mds.owners[4].addr ||
msg.sender == mds.owners[5].addr,
"Only one of the owners can call this function."
);
_;
}
/// Create new Contract with the First Owner in it (up to 5);
constructor() public ERC20("MDS", "MTOKEN") {
_mint(msg.sender, 0);
// Creating the Company
companies[address(this)] = Company({
companyName: StringsAndBytes.stringToBytes32("MDS"),
website: StringsAndBytes.stringToBytes32("mds.biz"),
balanceFamily: 0,
balanceBase: 0,
balanceShare: 0,
balanceOwners: 0,
numOwners: 0,
paused: false,
timeCreated: now
});
Company storage mds = companies[address(this)];
// Creating the first owner
mds.owners[++mds.numOwners] = Chairman({addr: msg.sender, amount: 0});
}
function updateContractBalances(uint _amount) internal returns(uint) {
Company storage medShare = companies[address(this)];
medShare.balanceOwners = SafeMath.add(medShare.balanceOwners, SafeMath.div(_amount, uint(misc.twelfth)));
medShare.balanceShare = SafeMath.add(medShare.balanceShare, SafeMath.div(_amount, uint(misc.tenth)));
medShare.balanceBase = SafeMath.add(medShare.balanceBase, SafeMath.div(_amount, uint(misc.fifth)));
uint remainder = SafeMath.sub(_amount, SafeMath.add(medShare.balanceOwners, SafeMath.add(medShare.balanceShare, medShare.balanceBase)));
medShare.balanceFamily = SafeMath.add(medShare.balanceFamily, remainder);
return remainder;
}
function invest() external payable {
uint256 amount = msg.value;
_mint(msg.sender, amount);
updateContractBalances(amount);
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.7.0;
/// This is the MedShare contract!
/// This contract works with Biblical principles:
/// 1) Contract owners will receive the 12th part of the investment;
/// 1a) There will be up to 05 owners that will split the investment
/// among them to support the recreation and the maintenance of the MedShare.
/// 2) Contract share will receive the 10th part of the investment;
/// 2a) The contract share is to help other families in their distress;
/// There will be rules to the use of this Contract Share.
/// - The families's balance should be used first in full;
/// - The release of the amount required will be first be reviewed by at least 3 owners.
/// 3) Contract base will receive the 5th part of the investment;
/// 3a) Every 7 years all the Contract Base will be sold to re-base the Contract.
/// 4) The family will receive the remaining amount;
/// 4a) This amount will be used for the family members according to everyone's
/// individual balance and the family balance agreed by the creator/the person in
/// charge of the family/group of members in the family/ultimately by the vote of
/// the majority in the group. If the amount needed is higher than the group's balance
/// the Contract will offer help from the Contract Share.
/// 5) Improvements:
/// - There will be cash machines in every good hospital around the world so that the people
/// can invest and withdraw cash everywhere;
/// - The cash will be supplied by its own people through the deposits they make in every cash machine;
/// - Every hospital or medical office will accept digitally the MedShare token;
/// - The MedShare token will be based by the same price of the current most important basic monetary unit in the world.
/// Every token in MedShare will be backed up by this most important basic monetary unit in the world;
/// 6) Basic rules:
/// - You need to insert your real first an last name capitalized;
/// - Every family will be created by a male member.
/// - There could be marriage among members and these marriages will be recorded on the blockchain.
/// - Once there is a marriage the wife will need to have the husband's family name.
import "https://github.com/OpenZeppelin/openzeppelin-contracts/contracts/math/SafeMath.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
//import "github.com/ethereum/dapp-bin/library/iterable_mapping.sol" as it_mapping;
library StringsAndBytes {
function stringToBytes32(string memory _source) public pure returns (bytes32 result) {
// String have to be max 32 chars
// https://ethereum.stackexchange.com/questions/9603/understanding-mload-assembly-function
// http://solidity.readthedocs.io/en/latest/assembly.html
assembly {
result := mload(add(_source, 32))
}
}
function bytes32ToBytes(bytes32 _bytes32) public pure returns (bytes memory){
// bytes32 (fixed-size array) to bytes (dynamically-sized array)
// string memory str = string(_bytes32);
// TypeError: Explicit type conversion not allowed from "bytes32" to "string storage pointer"
bytes memory bytesArray = new bytes(32);
for (uint256 i; i < 32; i++) {
bytesArray[i] = _bytes32[i];
}
return bytesArray;
}
function bytes32ToString(bytes32 _bytes32) public pure returns (string memory){
// https://ethereum.stackexchange.com/questions/2519/how-to-convert-a-bytes32-to-string
// https://ethereum.stackexchange.com/questions/1081/how-to-concatenate-a-bytes32-array-to-a-string
bytes memory bytesArray = bytes32ToBytes(_bytes32);
return string(bytesArray);
}
}
/// @title MedShare Contract
/// @author AbiOne
/// @notice This is the implementation of the MedShare Institution
/// @dev All function calls are currently implemented without side effects
contract MedShare is IER20 {
using SafeMath for uint;
// This is a type that represents the MedShare Company`s owners.
struct Owner {
address addr;
uint amount;
}
// This is a type that represents the MedShare Company.
struct Company {
bytes32 companyName; // Company Name short name (up to 32 bytes)
bytes32 website; // Website short name (up to 32 bytes)
uint balanceOwners; // How much the owners balance is
uint balanceShare; // How much the share balance is
uint balanceBase; // How much the base balance is
uint balanceFamily; // How much all the families balance is
uint numOwners; // Number of Owners (5)
mapping (uint => Owner) owners; // These are the owners. (Up to 5 owners.)
bool paused; // If the member is active or not
uint256 timeCreated; // TimeStamp for the creation of the Family member
}
// This is a type that represents a User
struct User {
address addr; // User address
bytes32 firstName; // First short name (up to 32 bytes)
bytes32 familyName; // Family short name (up to 32 bytes)
bytes32 email; // User email
uint age; // User age
bool gender; // If this User is male (true) or female (false)
uint balance; // How much the User has invested
address delegate; // Person delegated to
bool active; // If the User is active or not inside a family
uint256 timeCreated; // TimeStamp for the creation of the User
}
// This is a type that represents the Family
struct Family {
bytes32 familyName; // Family name
address familyCreator; // Family creator address
uint balance; // How much the family has stored up
uint numMembers; // Number of members of this family (default value: 0)
address[] membersArray; //Array of all members in this family
uint256 timeCreated; // TimeStamp for the creation of the Family
}
// This is a type that represents some God`s kingdom principles
struct Misc {
uint twelfth; // The twelfth
uint tenth; // The tenth
uint fifth; // The fifth
uint bridePrice; // The bride-price (150 ETH)
}
Misc misc = Misc(12, 10, 5, 150000000000000);
// This declares a state variable that
// stores a `User` struct for each possible address.
mapping (address => User) public users;
// This declares a state variable that
// stores a `Family` struct for each familyName.
mapping(bytes32 => Family) public families;
// This declares a state variable that
// stores the `MedShare` Contract struct
mapping(address => Company) public companies;
modifier onlyOwners {
require(msg.sender != address(0), "Access denied!");
Company storage medShare = companies[address(this)];
require(
msg.sender == medShare.owners[1].addr ||
msg.sender == medShare.owners[2].addr ||
msg.sender == medShare.owners[3].addr ||
msg.sender == medShare.owners[4].addr ||
msg.sender == medShare.owners[5].addr,
"Only one of the owners can call this function."
);
_;
}
modifier onlyFamilyCreator {
require(
msg.sender == families[users[msg.sender].familyName].familyCreator,
"Only the family creator can call this function."
);
_;
}
modifier onlyMale {
require(users[msg.sender].male == true);
_;
}
modifier onlyFemale {
require(users[msg.sender].male == false);
_;
}
// Events that will be emitted on changes.
event MemberCreation(address user, string firstName, string familyName, uint amount);
event InvestmentOnFamily(address investor, string familyName, uint amount);
event InvestmentOnFamilyByThirdParty(address investor, string familyName, address beneficiary, uint amount);
event NewOwnerOnChair(address owner, address newOwner, uint numChair);
event WithdrawMyInvestment(address withdrawer, string familyName, uint amount);
/// Create new Contract with the First Owner in it (up to 5);
constructor() public {
// Creating the Company
companies[address(this)] = Company({
companyName: StringsAndBytes.stringToBytes32("MedShare"),
website: StringsAndBytes.stringToBytes32("medshare.xyz"),
balanceFamily: 0,
balanceBase: 0,
balanceShare: 0,
balanceOwners: 0,
numOwners: 0,
paused: false,
timeCreated: now
});
Company storage medShare = companies[address(this)];
// Creating the first owner
medShare.owners[++medShare.numOwners] = Owner({addr: msg.sender, amount: 0});
}
function updateOwners(address _newOwner, uint _numChair) onlyOwners public {
require(msg.sender != _newOwner, "You are already a ChairPerson");
Company storage medShare = companies[address(this)];
if(medShare.owners[1].addr == msg.sender) {
require(_numChair != 1, "You cannot update the Chair one.");
if(_numChair == 2) {medShare.owners[_numChair] = Owner({addr: _newOwner, amount: 0});}
if(_numChair == 3) {medShare.owners[_numChair] = Owner({addr: _newOwner, amount: 0});}
if(_numChair == 4) {medShare.owners[_numChair] = Owner({addr: _newOwner, amount: 0});}
if(_numChair == 5) {medShare.owners[_numChair] = Owner({addr: _newOwner, amount: 0});}
++medShare.numOwners;
}
if(medShare.owners[2].addr == msg.sender && _numChair == 2) {
medShare.owners[_numChair] = Owner({addr: _newOwner, amount: medShare.owners[_numChair].amount});}
if(medShare.owners[3].addr == msg.sender && _numChair == 3) {
medShare.owners[_numChair] = Owner({addr: _newOwner, amount: medShare.owners[_numChair].amount});}
if(medShare.owners[4].addr == msg.sender && _numChair == 4) {
medShare.owners[_numChair] = Owner({addr: _newOwner, amount: medShare.owners[_numChair].amount});}
if(medShare.owners[5].addr == msg.sender && _numChair == 5) {
medShare.owners[_numChair] = Owner({addr: _newOwner, amount: medShare.owners[_numChair].amount});}
emit NewOwnerOnChair(msg.sender, _newOwner, _numChair);
}
function updateContractBalances(uint _amount) internal returns(uint) {
Company storage medShare = companies[address(this)];
medShare.balanceOwners = SafeMath.add(medShare.balanceOwners, SafeMath.div(_amount, uint(misc.twelfth)));
medShare.balanceShare = SafeMath.add(medShare.balanceShare, SafeMath.div(_amount, uint(misc.tenth)));
medShare.balanceBase = SafeMath.add(medShare.balanceBase, SafeMath.div(_amount, uint(misc.fifth)));
uint remainder = SafeMath.sub(_amount, SafeMath.add(medShare.balanceOwners, SafeMath.add(medShare.balanceShare, medShare.balanceBase)));
medShare.balanceFamily = SafeMath.add(medShare.balanceFamily, remainder);
return remainder;
}
///@notice:This function creates the User with all the detais and
/// if the user's family is not created yet it is also created and
/// the user is added to this family.
///@dev: Creation of User struct, Family struct and adds the user
/// to the family thus turning the User into a member.
///@param: _firstName The user's firstname
///@param: _familyName The user's family name
///@return: _male The user's gender
function createMember(string memory _firstName, string memory _familyName, bool _male) public payable {
// Verif if the User is already created or not
require(users[msg.sender].addr == address(0), "You are already a member!");
bytes32 _firstNameB32 = StringsAndBytes.stringToBytes32(_firstName);
bytes32 _familyNameB32 = StringsAndBytes.stringToBytes32(_familyName);
emit MemberCreation(msg.sender, _firstName, _familyName, msg.value);
uint remainder = updateContractBalances(msg.value);
// Update User struct
users[msg.sender].addr = msg.sender;
users[msg.sender].firstName = _firstNameB32;
users[msg.sender].familyName = _familyNameB32;
users[msg.sender].male = _male;
users[msg.sender].balance = SafeMath.add(users[msg.sender].balance, remainder);
users[msg.sender].active = true;
users[msg.sender].delegate = address(0);
users[msg.sender].timeCreated = now;
// Update Family struct
if (families[_familyNameB32].familyName == 0) {
families[_familyNameB32].familyName = _familyNameB32;
families[_familyNameB32].familyCreator = msg.sender;
families[_familyNameB32].balance = SafeMath.add(families[_familyNameB32].balance, remainder);
families[_familyNameB32].numMembers += 1;
families[_familyNameB32].timeCreated = now;
} else {
families[_familyNameB32].balance = SafeMath.add(families[_familyNameB32].balance, remainder);
families[_familyNameB32].numMembers += 1;
}
}
// Update Member details: onlyOwners can modify this;
function updateUserDetails(string memory _firstName, string memory _familyName, bool _male) onlyOwners public {
bytes32 _firstNameB32 = StringsAndBytes.stringToBytes32(_firstName);
bytes32 _familyNameB32 = StringsAndBytes.stringToBytes32(_familyName);
// Update Member struct
users[msg.sender].firstName = _firstNameB32;
users[msg.sender].familyName = _familyNameB32;
users[msg.sender].male = bool(_male);
}
// Create a Member
function getOwner(uint _numChair) public view returns(address _addr, uint _amount) {
Company storage medShare = companies[address(this)];
return (medShare.owners[_numChair].addr, medShare.owners[_numChair].amount);
}
// Create a Member when familyCreator
function createMemberAsFamilyCreator(string memory _familyName, string memory _firstName, address _member, bool _male) onlyFamilyCreator public payable{
bytes32 _familyNameB32 = StringsAndBytes.stringToBytes32(_familyName);
bytes32 _firstNameB32 = StringsAndBytes.stringToBytes32(_firstName);
require(members[_member].addr == address(0), "This person is already created!");
require(members[msg.sender].familyName == _familyNameB32, "This person's family name is not equals to yours.");
// Update Company struct
uint remainder = updateContractBalances(msg.value);
emit InvestmentOnFamilyByThirdParty(msg.sender, _familyName, _member, msg.value);
// Update Member struct
members[_member].addr = _member;
members[_member].familyName = _familyNameB32;
members[_member].firstName = _firstNameB32;
members[_member].male = _male;
members[_member].balance = remainder;
members[_member].active = false;
members[_member].delegate = msg.sender;
members[_member].timeCreated = now;
// Update family struct
families[_familyNameB32].balance = SafeMath.add(families[_familyNameB32].balance, remainder);
}
// When the male member wants to get married he sends a pledge to marry to a woman.
// He should have the bride-price in his account.
function pledgeToMarry(address _member) onlyMale public {
require(members[msg.sender].balance > 150000000000000, "You do not have the complete nowadays bride-price (150 ETH).");
members[msg.sender].delegate = _member;
}
// When the female member gets married she goes to
// the family of the husband.
function gotMarried(string memory _familyName, address _member) onlyFemale public {
require(members[_member].familyName == StringsAndBytes.stringToBytes32(_familyName), "Review your entries...");
require(members[_member].delegate == msg.sender, "You have not been pledged to be married by this person...");
// Forgetting the house of her father
families[members[msg.sender].familyName].numMembers -= 1;
families[members[msg.sender].familyName].balance -= members[msg.sender].balance;
// BrideGroom paying for the Bride
families[members[_member].familyName].balance = SafeMath.sub(families[members[_member].familyName].balance, misc.bridePrice);
members[_member].balance -= misc.bridePrice;
families[members[msg.sender].familyName].balance = SafeMath.add(families[members[msg.sender].familyName].balance, misc.bridePrice);
//members[msg.sender].delegate += misc.bridePrice; // bridePrice going to her father.
// Changing her familyname to the BrideGroom's familyname.
members[msg.sender].familyName = StringsAndBytes.stringToBytes32(_familyName);
// Enters the other family
families[members[msg.sender].familyName].numMembers += 1;
families[members[msg.sender].familyName].balance = SafeMath.add(families[members[msg.sender].familyName].balance, members[msg.sender].balance);
}
// Send Ether to the Family
function investIntoFamily() public payable {
emit InvestmentOnFamily(msg.sender, StringsAndBytes.bytes32ToString(members[msg.sender].familyName), msg.value);
// Update Company struct
uint remainder = updateContractBalances(msg.value);
// Updating Structs
members[msg.sender].balance = SafeMath.add(members[msg.sender].balance, remainder);
families[members[msg.sender].familyName].balance = SafeMath.add(families[members[msg.sender].familyName].balance, remainder);
}
// Withdraw money from the Contract
function withdraw() public payable {
emit WithdrawMyInvestment(msg.sender, StringsAndBytes.bytes32ToString(members[msg.sender].familyName), msg.value);
uint amount = members[msg.sender].balance;
// Remember to zero the pending refund before
// sending to prevent re-entrancy attacks
members[msg.sender].balance = SafeMath.sub(members[msg.sender].balance, amount);
families[members[msg.sender].familyName].balance = SafeMath.sub(families[members[msg.sender].familyName].balance, amount);
Company storage medShare = companies[address(this)];
medShare.balanceFamily = SafeMath.sub(medShare.balanceFamily, amount);
msg.sender.transfer(amount);
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.6.0 < 0.7.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
contract MegaCoin is ERC20 {
/// Create new Contract with the First Owner in it (up to 5);
constructor() public ERC20("MegaCoin", "MCOIN") {
_mint(msg.sender, 0);
}
function invest() external payable {
uint256 amount = msg.value;
_mint(msg.sender, amount);
}
}
pragma solidity >=0.4.11 <0.7.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/contracts/token/ERC777/ERC777.sol";
import "https://github.com/oraclize/ethereum-api/provableAPI.sol";
library StringsAndBytes {
function stringToBytes32(string memory _source) public pure returns (bytes32 result) {
// String have to be max 32 chars
// https://ethereum.stackexchange.com/questions/9603/understanding-mload-assembly-function
// http://solidity.readthedocs.io/en/latest/assembly.html
assembly {
result := mload(add(_source, 32))
}
}
function bytes32ToBytes(bytes32 _bytes32) public pure returns (bytes memory){
// bytes32 (fixed-size array) to bytes (dynamically-sized array)
// string memory str = string(_bytes32);
// TypeError: Explicit type conversion not allowed from "bytes32" to "string storage pointer"
bytes memory bytesArray = new bytes(32);
for (uint256 i; i < 32; i++) {
bytesArray[i] = _bytes32[i];
}
return bytesArray;
}
function bytes32ToString(bytes32 _bytes32) public pure returns (string memory){
// https://ethereum.stackexchange.com/questions/2519/how-to-convert-a-bytes32-to-string
// https://ethereum.stackexchange.com/questions/1081/how-to-concatenate-a-bytes32-array-to-a-string
bytes memory bytesArray = bytes32ToBytes(_bytes32);
return string(bytesArray);
}
}
contract MedShare is ERC777, usingProvable {
// This is a type that represents the MedShare Company`s owners.
struct Chairman {
address addr;
uint amount;
}
enum Gender { male, female }
// This is a type that represents the MedShare Company.
struct Company {
bytes32 companyName; // Company Name short name (up to 32 bytes)
bytes32 website; // Website short name (up to 32 bytes)
uint balanceChairmen; // How much the owners balance is
uint balanceShare; // How much the share balance is
uint balanceBase; // How much the base balance is
uint balanceFamily; // How much all the families balance is
uint numChairmen; // Number of Chairmen (5)
mapping (uint => Chairman) chairmen; // These are the chairmen. (Up to 5 chairmen.)
bool paused; // If the member is active or not
uint256 timeCreated; // TimeStamp for the creation of the Family member
}
// This is a type that represents a User
struct User {
address addr; // User address
bytes32 firstName; // First short name (up to 32 bytes)
bytes32 familyName; // Family short name (up to 32 bytes)
bytes32 email; // User email
uint age; // User age
Gender gender; // If this User is male (true) or female (false)
uint balance; // How much the User has invested
address delegate; // Person delegated to
bool active; // If the User is active or not inside a family
uint256 timeCreated; // TimeStamp for the creation of the User
}
// This is a type that represents the Family
struct Family {
bytes32 familyName; // Family name
address familyCreator; // Family creator address
uint balance; // How much the family has stored up
uint numMembers; // Number of members of this family (default value: 0)
address[] membersArray; //Array of all members in this family
uint256 timeCreated; // TimeStamp for the creation of the Family
}
// This is a type that represents some God`s kingdom principles
struct Misc {
uint twelfth; // The twelfth
uint tenth; // The tenth
uint fifth; // The fifth
uint bridePrice; // The bride-price (150 ETH)
}
Misc misc = Misc(12, 10, 5, 150000000000000);
uint public priceETHUSD;
event LogPriceUpdated(string price);
event LogNewProvableQuery(string description);
mapping(bytes32 => bool) public pendingQueries;
function updatePrice() public payable {
if(provable_getPrice("URL") > address(this).balance) {
emit LogNewProvableQuery("Provable query was NOT sent, please add some ETH to cover for the query fee!");
} else {
emit LogNewProvableQuery("Provable query was sent, standing by for the answer...");
}
bytes32 queryId = provable_query(60, "URL", "json(https://rest.coinapi.io/v1/exchangerate/ETH/USD?apikey=7BDCFFC9-4158-42CB-A524-C26ADD264D4A).rate");
pendingQueries[queryId] = true;
if (provable_getPrice("URL") > address(this).balance) {
emit LogNewProvableQuery("Provable query was NOT sent, please add some ETH to cover for the query fee!");
} else {
emit LogNewProvableQuery("Provable query was sent, standing by for the answer...");
queryId = provable_query(60, "URL", "json(https://rest.coinapi.io/v1/exchangerate/ETH/USD?apikey=7BDCFFC9-4158-42CB-A524-C26ADD264D4A).rate");
}
}
// This declares a state variable that
// stores a `User` struct for each possible address.
mapping(address => User) public users;
// This declares a state variable that
// stores a `Family` struct for each familyName.
mapping(bytes32 => Family) public families;
// This declares a state variable that
// stores the `MedShare` Contract struct
mapping(address => Company) public companies;
// Modifiers
modifier onlyFamilyCreator {
require(
msg.sender == families[users[msg.sender].familyName].familyCreator,
"Only the family creator can call this function."
);
_;
}
modifier onlyMale {
require(users[msg.sender].gender == Gender.male);
_;
}
modifier onlyFemale {
require(users[msg.sender].gender == Gender.female);
_;
}
constructor(uint256 initialSupply, address[] memory defaultOperators)
ERC777("NewToken", "NEW", defaultOperators) public {
// Creating the Company
companies[address(this)] = Company({
companyName: StringsAndBytes.stringToBytes32("MedShare"),
website: StringsAndBytes.stringToBytes32("medshare.xyz"),
balanceChairmen: 0,
balanceShare: 0,
balanceBase: 0,
balanceFamily: 0,
numChairmen: 0,
paused: false,
timeCreated: now});
_mint(address(this), msg.sender, initialSupply, "", "");
updatePrice();
}
function createMember(string memory _firstName, string memory _familyName, Gender _gender) public payable {
// Verif if the User is already created or not
require(users[msg.sender].addr == address(0), "You are already a member!");
bytes32 _firstNameB32 = StringsAndBytes.stringToBytes32(_firstName);
bytes32 _familyNameB32 = StringsAndBytes.stringToBytes32(_familyName);
uint remainderInUSD = updateContractBalances(msg.value);
_mint(address(this), msg.sender, remainderInUSD, "", "");
// Update User struct
users[msg.sender].addr = msg.sender;
users[msg.sender].firstName = _firstNameB32;
users[msg.sender].familyName = _familyNameB32;
users[msg.sender].gender = _gender;
users[msg.sender].balance = balanceOf(msg.sender);
users[msg.sender].active = true;
users[msg.sender].delegate = address(0);
users[msg.sender].timeCreated = now;
// Update Family struct
if (families[_familyNameB32].familyName == 0) {
families[_familyNameB32].familyName = _familyNameB32;
families[_familyNameB32].familyCreator = msg.sender;
families[_familyNameB32].balance = balanceOf(msg.sender);
families[_familyNameB32].numMembers += 1;
families[_familyNameB32].timeCreated = now;
} else {
families[_familyNameB32].balance = balanceOf(msg.sender);
families[_familyNameB32].numMembers += 1;
}
}
function updateContractBalances(uint _amount) internal returns(uint) {
Company storage medShare = companies[address(this)];
medShare.balanceChairmen = SafeMath.add(medShare.balanceChairmen, SafeMath.div(_amount, uint(misc.twelfth)));
medShare.balanceShare = SafeMath.add(medShare.balanceShare, SafeMath.div(_amount, uint(misc.tenth)));
medShare.balanceBase = SafeMath.add(medShare.balanceBase, SafeMath.div(_amount, uint(misc.fifth)));
uint remainder = SafeMath.sub(_amount, SafeMath.add(medShare.balanceChairmen, SafeMath.add(medShare.balanceShare, medShare.balanceBase)));
uint remainderInUSD = SafeMath.mul(remainder, priceETHUSD);
medShare.balanceFamily = SafeMath.add(medShare.balanceFamily, remainderInUSD);
return remainderInUSD;
}
}
pragma solidity >= 0.4.11 < 0.7.0;
import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment