Last active
March 19, 2018 01:58
-
-
Save ghiliweld/021f684c0f2bd286bbe92b2fcc15daad to your computer and use it in GitHub Desktop.
Property Factory in Vyper
This file contains hidden or 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
# @author Ghilia Weldesselasie | |
# @title PropertyFactory | |
# Contract that creates a new building with condos in them that can be purchased | |
# This contract would have to be redeployed for every buolding you own | |
# You could have a central contract that controls several buildings but that would be complicated | |
# Condos can be purchased both in cash (ETH really), or by rent-to-own | |
# Using Vyper for prototyping cause I like it better | |
# Events of the token. | |
Transfer: __log__({_from: indexed(address), _to: indexed(address), _condoNum: num}) | |
Approval: __log__({_owner: indexed(address), _approved: indexed(address), _condoNum: num}) | |
Purchase: __log__({_condoNum: num, _purchaser: indexed(address)}) | |
NewCondoForSale: __log__({_condoNum: num, _price: num}) | |
# Variables of the contract. | |
name = "Ghili's Building" | |
symbol = "G-BUILD" | |
totalCondos: public(num) = 9001 # The number of condos inside this building | |
buildingOwner: public(address) = 0xAbcD # A single address initially owns all condos in a building | |
# Each condo is a struct which will later be a ERC-721 token as well | |
Condo: { | |
ownerName: bytes, | |
condoNumber: num, | |
price: num, # The price of the condo | |
balance: num, # How much money has been payed into the condo. amountOwed = price - balance | |
locked: bool, # Whether a property is owned or rented, locked will evaluate to true | |
} | |
condos: Condo[num] # An mapping of Condos tied to their condo number | |
condoToOwner: address[num] # Ties a condo number (suite #) to an owner (an address) | |
priceOfCondo: public(num[num]) # Each condo has a price, they key is the condo number and the value is the price | |
condoOwnershipCount: num[address] # Number of condos an address owns | |
# THE FUNCTIONS | |
# What is the condo balance of a particular account? | |
@public | |
@constant | |
def balanceOf(_owner: address) -> (num256): | |
return as_num256(condoOwnershipCount[_owner]) | |
# What condo is this address the owner of? | |
@public | |
@constant | |
def ownerOf(_condoNum: num256) -> (address): | |
owner = condoToOwner[_condoNum] | |
assert owner != address(0) | |
return owner | |
@private | |
@constant | |
def _owns(_claimant: address, _condoNum: num256) -> (bool): | |
return (condoToOwner[_condoNum] == _claimant) | |
# Send `_value` tokens to `_to` from your account | |
@private | |
def _transfer( _from: address, _to: address, _condoNum: num256): | |
condoOwnershipCount[_to] += 1 | |
condoToOwner[_condoNum] = _to | |
if _from != address(0): | |
condoOwnershipCount[_from] -= 1 | |
log.Transfer(_from, _to, _condoNum) | |
@public | |
def transfer(_to: address, _condoNum: num256): | |
assert _to != address(0) | |
assert _to != msg.sender | |
assert _owns(msg.sender, _condoNum)) | |
_transfer(msg.sender, _to, _condoNum) | |
# Transfer allowed tokens from a specific account to another. | |
@public | |
def transferFrom(_to: address, _condoNum: num256): | |
assert _to != address(0) | |
assert _to != msg.sender | |
assert _approvedFor(msg.sender, _condoNum) | |
assert _owns(msg.sender, _condoNum)) | |
_transfer(msg.sender, _to, _condoNum) | |
@public | |
def putForSale(_condoNum: num, _price: num): | |
assert msg.sender == buildingOwner # Only the building owner can put up a condo for sale | |
condo: Condo | |
condo.ownerName = "Ghili" | |
condo.condoNumber = _condoNum | |
condo.price = _price | |
condo.balance = 0 | |
condo.locked = False | |
condos[_condoNum] = condo | |
condoToOwner[_condoNum] = buildingOwner | |
priceOfCondo[_condoNum] = _price | |
condoOwnershipCount[buildingOwner]++ | |
log.NewCondoForSale(_condoNum, _price) | |
_transfer(0, buildingOwner, condoNum) | |
@public | |
@constant | |
def condosOfOwner(_owner: address) -> (num256[]): | |
balance: num256 = balanceOf(_owner) | |
if balance == 0 | |
return num256[0] | |
else: | |
result: num256[balance] | |
uint256 maxCondoNum = totalSupply() | |
idx: num256 = 0 | |
condoNum: num256 | |
for condoNum in range(0, maxCondoNum) | |
if condoToOwner[condoNum] == _owner: | |
result[idx] = condoNum | |
idx += 1 | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment