Created
July 8, 2020 21:25
-
-
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=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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