Created
September 1, 2020 03:15
-
-
Save Kowsi/147cf728ec0da29bfbd9c7a5977b7d2c 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.5.17+commit.d19bba13.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
pragma solidity >=0.4.22 <0.7.0; | |
/** | |
* @title Storage | |
* @dev Store & retrieve value in a variable | |
*/ | |
contract Storage { | |
uint256 number; | |
/** | |
* @dev Store value in variable | |
* @param num value to store | |
*/ | |
function store(uint256 num) public { | |
number = num; | |
} | |
/** | |
* @dev Return value | |
* @return value of 'number' | |
*/ | |
function retrieve() public view returns (uint256){ | |
return number; | |
} | |
} |
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.22 <0.7.0; | |
/** | |
* @title Owner | |
* @dev Set & change owner | |
*/ | |
contract Owner { | |
address private owner; | |
// event for EVM logging | |
event OwnerSet(address indexed oldOwner, address indexed newOwner); | |
// modifier to check if caller is owner | |
modifier isOwner() { | |
// If the first argument of 'require' evaluates to 'false', execution terminates and all | |
// changes to the state and to Ether balances are reverted. | |
// This used to consume all gas in old EVM versions, but not anymore. | |
// It is often a good idea to use 'require' to check if functions are called correctly. | |
// As a second argument, you can also provide an explanation about what went wrong. | |
require(msg.sender == owner, "Caller is not owner"); | |
_; | |
} | |
/** | |
* @dev Set contract deployer as owner | |
*/ | |
constructor() public { | |
owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor | |
emit OwnerSet(address(0), owner); | |
} | |
/** | |
* @dev Change owner | |
* @param newOwner address of new owner | |
*/ | |
function changeOwner(address newOwner) public isOwner { | |
emit OwnerSet(owner, newOwner); | |
owner = newOwner; | |
} | |
/** | |
* @dev Return owner address | |
* @return address of owner | |
*/ | |
function getOwner() external view returns (address) { | |
return owner; | |
} | |
} |
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.22 <0.7.0; | |
/** | |
* @title Ballot | |
* @dev Implements voting process along with vote delegation | |
*/ | |
contract Ballot { | |
struct Voter { | |
uint weight; // weight is accumulated by delegation | |
bool voted; // if true, that person already voted | |
address delegate; // person delegated to | |
uint vote; // index of the voted proposal | |
} | |
struct Proposal { | |
// If you can limit the length to a certain number of bytes, | |
// always use one of bytes1 to bytes32 because they are much cheaper | |
bytes32 name; // short name (up to 32 bytes) | |
uint voteCount; // number of accumulated votes | |
} | |
address public chairperson; | |
mapping(address => Voter) public voters; | |
Proposal[] public proposals; | |
/** | |
* @dev Create a new ballot to choose one of 'proposalNames'. | |
* @param proposalNames names of proposals | |
*/ | |
constructor(bytes32[] memory proposalNames) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 1; | |
for (uint i = 0; i < proposalNames.length; i++) { | |
// 'Proposal({...})' creates a temporary | |
// Proposal object and 'proposals.push(...)' | |
// appends it to the end of 'proposals'. | |
proposals.push(Proposal({ | |
name: proposalNames[i], | |
voteCount: 0 | |
})); | |
} | |
} | |
/** | |
* @dev Give 'voter' the right to vote on this ballot. May only be called by 'chairperson'. | |
* @param voter address of voter | |
*/ | |
function giveRightToVote(address voter) public { | |
require( | |
msg.sender == chairperson, | |
"Only chairperson can give right to vote." | |
); | |
require( | |
!voters[voter].voted, | |
"The voter already voted." | |
); | |
require(voters[voter].weight == 0); | |
voters[voter].weight = 1; | |
} | |
/** | |
* @dev Delegate your vote to the voter 'to'. | |
* @param to address to which vote is delegated | |
*/ | |
function delegate(address to) public { | |
Voter storage sender = voters[msg.sender]; | |
require(!sender.voted, "You already voted."); | |
require(to != msg.sender, "Self-delegation is disallowed."); | |
while (voters[to].delegate != address(0)) { | |
to = voters[to].delegate; | |
// We found a loop in the delegation, not allowed. | |
require(to != msg.sender, "Found loop in delegation."); | |
} | |
sender.voted = true; | |
sender.delegate = to; | |
Voter storage delegate_ = voters[to]; | |
if (delegate_.voted) { | |
// If the delegate already voted, | |
// directly add to the number of votes | |
proposals[delegate_.vote].voteCount += sender.weight; | |
} else { | |
// If the delegate did not vote yet, | |
// add to her weight. | |
delegate_.weight += sender.weight; | |
} | |
} | |
/** | |
* @dev Give your vote (including votes delegated to you) to proposal 'proposals[proposal].name'. | |
* @param proposal index of proposal in the proposals array | |
*/ | |
function vote(uint proposal) public { | |
Voter storage sender = voters[msg.sender]; | |
require(sender.weight != 0, "Has no right to vote"); | |
require(!sender.voted, "Already voted."); | |
sender.voted = true; | |
sender.vote = proposal; | |
// If 'proposal' is out of the range of the array, | |
// this will throw automatically and revert all | |
// changes. | |
proposals[proposal].voteCount += sender.weight; | |
} | |
/** | |
* @dev Computes the winning proposal taking all previous votes into account. | |
* @return winningProposal_ index of winning proposal in the proposals array | |
*/ | |
function winningProposal() public view | |
returns (uint winningProposal_) | |
{ | |
uint winningVoteCount = 0; | |
for (uint p = 0; p < proposals.length; p++) { | |
if (proposals[p].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[p].voteCount; | |
winningProposal_ = p; | |
} | |
} | |
} | |
/** | |
* @dev Calls winningProposal() function to get the index of the winner contained in the proposals array and then | |
* @return winnerName_ the name of the winner | |
*/ | |
function winnerName() public view | |
returns (bytes32 winnerName_) | |
{ | |
winnerName_ = proposals[winningProposal()].name; | |
} | |
} |
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.22 <0.6.0; | |
contract DealerlessAuction { | |
address public deployer; | |
//track the beneficiary of the contract | |
address payable public beneficiary; | |
//keep track of the address of the current highest bidder | |
address public highestBidder; | |
uint public highestBid; | |
mapping (address => uint) pendingReturns; | |
bool public ended; | |
event HighestBidIncreased(address bidder, uint amount); | |
event AuctionEnded(address winner, uint amount); | |
// The following is a so-called natspec comment, | |
// recognizable by the three slashes. | |
// It will be shown when the user is asked to | |
// confirm a transaction. | |
/// Create a simple auction with `_biddingTime` | |
/// seconds bidding time on behalf of the | |
/// beneficiary address `_beneficiary`. | |
constructor( | |
address payable _beneficiary | |
) public { | |
deployer = msg.sender; // set as the DealerlessMarket | |
beneficiary = _beneficiary; | |
} | |
/// Bid on the auction with the value sent | |
/// together with this transaction. | |
/// The value will only be refunded if the | |
/// auction is not won. | |
function bid(address payable sender) public payable { | |
// If the bid is not higher, send the | |
// money back. | |
require( | |
msg.value > highestBid, | |
"There already is a higher bid." | |
); | |
require(!ended, "auctionEnd has already been called."); | |
if (highestBid != 0) { | |
// Sending back the money by simply using | |
// highestBidder.send(highestBid) is a security risk | |
// because it could execute an untrusted contract. | |
// It is always safer to let the recipients | |
// withdraw their money themselves. | |
pendingReturns[highestBidder] += highestBid; | |
} | |
highestBidder = sender; | |
highestBid = msg.value; | |
emit HighestBidIncreased(sender, msg.value); | |
} | |
/// Withdraw a bid that was overbid. | |
function withdraw() public returns (bool) { | |
uint amount = pendingReturns[msg.sender]; | |
if (amount > 0) { | |
// It is important to set this to zero because the recipient | |
// can call this function again as part of the receiving call | |
// before `send` returns. | |
pendingReturns[msg.sender] = 0; | |
if (!msg.sender.send(amount)) { | |
// No need to call throw here, just reset the amount owing | |
pendingReturns[msg.sender] = amount; | |
return false; | |
} | |
} | |
return true; | |
} | |
function pendingReturn(address sender) public view returns (uint) { | |
return pendingReturns[sender]; | |
} | |
/// End the auction and send the highest bid | |
/// to the beneficiary. | |
function auctionEnd() public { | |
// It is a good guideline to structure functions that interact | |
// with other contracts (i.e. they call functions or send Ether) | |
// into three phases: | |
// 1. checking conditions | |
// 2. performing actions (potentially changing conditions) | |
// 3. interacting with other contracts | |
// If these phases are mixed up, the other contract could call | |
// back into the current contract and modify the state or cause | |
// effects (ether payout) to be performed multiple times. | |
// If functions called internally include interaction with external | |
// contracts, they also have to be considered interaction with | |
// external contracts. | |
// 1. Conditions | |
require(!ended, "auctionEnd has already been called."); | |
require(msg.sender == deployer, "You are not the auction deployer!"); | |
// 2. Effects | |
ended = true; | |
emit AuctionEnded(highestBidder, highestBid); | |
// 3. Interaction | |
beneficiary.transfer(highestBid); | |
} | |
} |
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.5.0; | |
//import "../node-modules/kohshiba/ERC-X/contracts/ERCX/Contract/ERCXFull.sol"; | |
import "https://github.com/kohshiba/ERC-X/blob/master/contracts/ERCX/Contract/ERCXFull.sol"; | |
//import '../node_modules/openzeppelin-solidity/contracts/ownership/Ownable.sol'; | |
import './DealerlessAuction.sol'; | |
import './Property.sol'; | |
contract DealerlessMarket is ERCXFull{ | |
constructor() ERCXFull("DealerlessMarket", "DEAL") public {} | |
uint owner_layer = 2; | |
using Counters for Counters.Counter; | |
Counters.Counter token_ids; | |
address payable private foundation_address = msg.sender; | |
mapping(uint => DealerlessAuction) public auctions; | |
modifier landRegistered(uint item_id) { | |
require(_exists(item_id, owner_layer), "Land not registered!"); | |
_; | |
} | |
modifier onlyOwner() { | |
require (msg.sender == foundation_address, 'Only Admin have previlage !') ; | |
_; | |
} | |
function owner() public view returns(address){ | |
return foundation_address; | |
} | |
function createAuction(uint token_id) public onlyOwner { | |
auctions[token_id] = new DealerlessAuction(foundation_address); | |
} | |
function registerLand(string memory uri) public payable onlyOwner { | |
token_ids.increment(); | |
uint token_id = token_ids.current(); | |
_mint(foundation_address, token_id); | |
_setItemURI(token_id, uri); | |
createAuction(token_id); | |
} | |
function endAuction(uint token_id) public onlyOwner landRegistered(token_id) { | |
DealerlessAuction auction = auctions[token_id]; | |
auction.auctionEnd(); | |
safeTransferFrom(ownerOf(token_id), auction.highestBidder(), token_id); | |
} | |
function auctionEnded(uint token_id) public view landRegistered(token_id) returns(bool) { | |
DealerlessAuction auction = auctions[token_id]; | |
return auction.ended(); | |
} | |
function highestBid(uint token_id) public view landRegistered(token_id) returns(uint) { | |
DealerlessAuction auction = auctions[token_id]; | |
return auction.highestBid(); | |
} | |
function pendingReturn(uint token_id, address sender) public view landRegistered(token_id) returns(uint) { | |
DealerlessAuction auction = auctions[token_id]; | |
return auction.pendingReturn(sender); | |
} | |
function bid(uint token_id) public payable landRegistered(token_id) { | |
DealerlessAuction auction = auctions[token_id]; | |
auction.bid.value(msg.value)(msg.sender); | |
} | |
event delegate_event( | |
address _from, | |
address _tenant, | |
uint i | |
); | |
function delegatecallex(address tenant, uint i) public{ | |
emit delegate_event(msg.sender, tenant, i); | |
} | |
} | |
contract DealerlessRental{ | |
DealerlessMarket dealerlessMarketDeployer; | |
mapping(uint => Property) public rentalProperty; | |
modifier isRentalAvailable(uint item_id) { | |
require (rentalProperty[item_id].isAvailable(), 'Property is not available for Rental!'); | |
_; | |
} | |
modifier onlyOwner(uint token_id) { | |
require (msg.sender == dealerlessMarketDeployer.ownerOf(token_id), 'Only owner has previlage !') ; | |
_; | |
} | |
constructor (address dealerlessMarketAddress) public { | |
dealerlessMarketDeployer = DealerlessMarket(dealerlessMarketAddress); | |
} | |
function getRental(uint itemId) public view returns(Property){ | |
return rentalProperty[itemId]; | |
} | |
function readyToRent(uint itemId, uint rent, uint deposit) public onlyOwner(itemId){ | |
rentalProperty[itemId] = new Property(msg.sender, rent, deposit); | |
} | |
//Tenant request for Tenant right approval for the first time | |
function requestForTenantRight(uint itemId, uint fromTimeStamp, uint toTimeStamp) public isRentalAvailable(itemId) { | |
require(msg.sender!= dealerlessMarketDeployer.ownerOf(itemId), 'Owner cannot be a Tenant!'); | |
require(dealerlessMarketDeployer.getApprovedTenantRight(itemId) == address(0x0), 'Property is under approval for other Renter!'); | |
Property rental = rentalProperty[itemId]; | |
rental.createTenantRightAgreement(msg.sender, fromTimeStamp, toTimeStamp); | |
} | |
function approveTenantRightAgreement(address tenant, uint256 itemId) public onlyOwner(itemId) returns(bool, bool, bytes memory, bytes memory){ | |
(bool status, bytes memory data) = address(dealerlessMarketDeployer).delegatecall(abi.encodeWithSignature("delegatecallex(address,uint256)", tenant, itemId)); | |
// abi.encodePacked(bytes4(keccak256("delegatecallex(uint160, uint)")), tenant, itemId)); | |
(bool status1, bytes memory data1) = address(dealerlessMarketDeployer).delegatecall(abi.encodeWithSignature("approveTenantRight(address,uint256)", tenant, itemId)); | |
/* | |
//dealerlessMarketDeployer.approveTenantRight(tenant, itemId); | |
if (status) { | |
Property rental = rentalProperty[itemId]; | |
rental.setStatusApproved(); | |
}*/ | |
return (status, status1, data, data1); | |
} | |
function setTenantRightAgreement(uint itemId) public returns(bool){ | |
(bool status,) = address(dealerlessMarketDeployer).delegatecall(abi.encodeWithSignature("setTenantRight(uint256)", itemId)); | |
Property rental = rentalProperty[itemId]; | |
rental.confirmAgreement(); | |
return status; | |
} | |
} |
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.5.0; | |
//import "https://github.com/kohshiba/ERC-X/blob/master/contracts/ERCX/Contract/ERCXFull.sol"; | |
contract Property { | |
//constructor() ERCXFull("DealerlessRentals", "RENT") public {} | |
address public owner; | |
uint deposit; | |
uint rent; | |
address public tenant; | |
string public house; | |
struct LeasePeriod { | |
uint fromTimestamp; | |
uint toTimestamp; | |
} | |
enum State {Available, Created, Approved, Started, Terminated} | |
State public state; | |
LeasePeriod leasePeriod; | |
modifier onlyTenant() { | |
require (msg.sender != tenant, 'Not a Tenant!') ; | |
_; | |
} | |
modifier onlyOwner() { | |
require (msg.sender != owner, 'Not an owner!') ; | |
_; | |
} | |
modifier inState(State _state) { | |
require (state == _state, 'State is !'); | |
_; | |
} | |
constructor (address _owner, uint _rent, uint _deposit) public{ | |
owner = _owner; | |
rent = _rent; | |
deposit = _deposit; | |
state = State.Available; | |
} | |
function isAvailable() view public returns(bool) { | |
if (state == State.Available){ | |
return true; | |
} | |
return false; | |
} | |
function createTenantRightAgreement(address _tenant, uint fromTimestamp, uint toTimestamp) inState(State.Available) public { | |
tenant = _tenant; | |
leasePeriod.toTimestamp = toTimestamp; | |
leasePeriod.fromTimestamp = fromTimestamp; | |
state = State.Created; | |
} | |
function setStatusApproved() inState(State.Approved) public onlyOwner{ | |
require(owner != address(0x0), 'Property not available for rentals!'); | |
state = State.Approved; | |
} | |
function confirmAgreement() inState(State.Approved) onlyTenant public{ | |
state = State.Started; | |
} | |
function clearTenant() public{ | |
tenant = address(0x0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment