Last active
March 28, 2016 06:30
-
-
Save y12studio/bcf9dd4ce1792a897953 to your computer and use it in GitHub Desktop.
Y12 Marriage Contract 2
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
// --------- | |
// 智能證券化婚約 2016 | |
// 在法律許可的範圍內,作者(們)已將此軟體的著作權、相關權利與鄰接權釋出到全球的公眾領域。此軟體之散布不含任何擔保責任。 | |
// 您應該已經連同軟體取得一份CC0的公眾領域貢獻宣告複本,若沒有收到,則請見:http://creativecommons.org/publicdomain/zero/1.0/。 | |
// --------- | |
// Ethereum以太坊智能合約環境說明 https://gist.github.com/y12studio/f142cd9789c7d396d8f7 | |
// 有意願測試簽智能婚約可聯絡 https://github.com/y12studio | |
// 1. 發起方(下記mblue)建立合約,並存入一筆金額(下記A),得到股份B。 | |
// 2. 發起方求婚執行合約 willYouMarryMe 條款,並填入身份證明的數位檔案摘要(SHA256)。 | |
// 3. 提案受方(下記mred)確認 proposalYes 進入婚姻狀態或是拒絕 proposalSorry。 | |
// mred Yes 需投入A金額並填入身份證明/婚約證明以及接受紅包時限。 | |
// mred Sorry 會將A金額退回mblue結束合約 | |
// 4. 親友於雙方確認後可於時限內 buyGift 發送紅包,一半給新人,得到半價解約可贖回股份。 | |
// 注意 | |
// 1. 以太坊不斷鏈下,即使不往來數十年後也可在不需第三方介入下到網路上執行解約條款後收到退紅。 | |
// 2. 發行股份可轉手賣掉。 | |
// 3. 以太坊代幣如有斷網鏈現象前需提前轉約到其他公私智約網平台,如候選HyperLedger或RootStock等。 | |
// 4. 解約計算股份/股份贖回/第三方並未設計。 | |
contract YMarriageContract { | |
// 甲方 | |
address public mblue; | |
bytes32 public mblueHash; | |
uint public mValue; | |
// 乙方 | |
address public mred; | |
bytes32 public mredHash; | |
bytes32 public marriageHash; | |
// 天命 | |
address public god; | |
// 第三方:法院,時間,命運,第三者聯盟 | |
address public thirdPartyDao; | |
uint public giftStart; | |
uint public giftTime; | |
uint public giftValue; | |
uint32 public giftCount; | |
// 婚約證券化名稱 | |
string public name; | |
string public symbol; | |
uint8 public decimals; | |
uint256 public sellPrice; | |
uint256 public buyPrice; | |
// This creates an array with all balances | |
mapping(address => uint256) public balanceOf; | |
uint public thirdStart; | |
uint public thirdTime; | |
State public state; | |
enum State { Created, Proposal ,Locked, Inactive } | |
modifier onlyCouple(){ | |
if (msg.sender != mblue && msg.sender != mred) throw; | |
_ | |
} | |
modifier onlyGod(){ | |
if (msg.sender != god) throw; | |
_ | |
} | |
modifier onlyThirdPartyDao(){ | |
if (msg.sender != thirdPartyDao) throw; | |
_ | |
} | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
// 發起方為藍方 | |
function YMarriageContract(string tokenName, uint8 decimalUnits, string tokenSymbol) { | |
if(msg.value < 1 finney ) throw; | |
name = tokenName; | |
symbol = tokenSymbol; | |
decimals = decimalUnits; | |
sellPrice = 1 finney; | |
buyPrice = 1 finney; | |
mblue = msg.sender; | |
mValue = msg.value; | |
// 新人代幣 | |
balanceOf[msg.sender] = mValue*100/sellPrice; | |
state = State.Created; | |
} | |
// 將身份證明拍照取得SHA256(照片) | |
function willYouMarryMe(bytes32 id_hash){ | |
if(state != State.Created) throw; | |
if (msg.sender != mblue) throw; | |
mblueHash = id_hash; | |
state = State.Proposal; | |
} | |
// 確認方為紅方,提供SHA256(身份證明)以及SHA256(證書/錄影/合照) | |
function proposalYes(bytes32 id_hash, bytes32 marriage_fact_hash, uint gift_time){ | |
if (state != State.Proposal) throw; | |
if (msg.value != mValue) throw; | |
if (msg.sender == mblue) throw; | |
mred = msg.sender; | |
mredHash = id_hash; | |
marriageHash = marriage_fact_hash; | |
giftStart = now; | |
giftTime = gift_time; | |
balanceOf[msg.sender] = balanceOf[mblue]; | |
balanceOf[this] = balanceOf[mblue]*2; | |
state = State.Locked; | |
} | |
/* Send coins */ | |
function transfer(address _to, uint256 _value) { | |
if (balanceOf[msg.sender] < _value) throw; | |
if (balanceOf[_to] + _value < balanceOf[_to]) throw; | |
balanceOf[msg.sender] -= _value; | |
balanceOf[_to] += _value; | |
Transfer(msg.sender, _to, _value); | |
} | |
function setGiftPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyCouple { | |
sellPrice = newSellPrice; | |
buyPrice = newBuyPrice; | |
} | |
function buyGift() { | |
if(state != State.Locked) throw; | |
// 過期不收 | |
if (now > giftStart + giftTime) throw; | |
// if(msg.value < 1 finney) throw; | |
// 一半給新人,另一半發代幣。 | |
uint amount = msg.value / buyPrice / 2; | |
if (balanceOf[this] < amount) throw; | |
balanceOf[msg.sender] += amount; | |
balanceOf[this] -= amount; | |
Transfer(this, msg.sender, amount); | |
} | |
function sellGift(uint256 amount) { | |
if(state != State.Locked) throw; | |
if (now < giftStart + giftTime) throw; | |
if (balanceOf[msg.sender] < amount) throw; | |
balanceOf[this] += amount; | |
balanceOf[msg.sender] -= amount; | |
msg.sender.send(amount * sellPrice); | |
Transfer(msg.sender, this, amount); | |
} | |
// Sorry. | |
function proposalSorry(){ | |
if (state != State.Proposal) throw; | |
if (msg.sender == mblue) throw; | |
mblue.send(this.balance); | |
state = State.Inactive; | |
} | |
// 時間以秒計算,一天86400,一百年3153600000 | |
function setupThirdPartyDao(address _address, uint _time) onlyGod { | |
if(state != State.Locked) throw; | |
thirdStart = now; | |
thirdTime = _time; | |
thirdPartyDao = _address; | |
} | |
function doUsPartByPeace() onlyCouple { | |
if(state != State.Locked) throw; | |
// 先清算紅包股份後退紅包 1/2 | |
// TODO | |
// 剩下再分 | |
uint rValue = this.balance / 2; | |
mblue.send(rValue); | |
mred.send(this.balance); | |
state = State.Inactive; | |
} | |
function doUsPartByOne() onlyCouple { | |
if(state != State.Locked) throw; | |
// 先清算股份後退紅包 1/2 | |
// TODO | |
// 剩下再分 | |
uint rValue = this.balance / 5; | |
msg.sender.send(rValue); | |
if(msg.sender == mblue) { | |
mred.send(this.balance); | |
}else{ | |
mblue.send(this.balance); | |
} | |
state = State.Inactive; | |
} | |
function doUsPartByThird() onlyThirdPartyDao { | |
if (state != State.Locked) throw; | |
// 先清算股份後退紅包 1/2 | |
// TODO | |
thirdPartyDao.send(this.balance); | |
state = State.Inactive; | |
} | |
// DeveloperOnlyMethod | |
function devOnlyMintToken(uint256 mintedAmount) onlyCouple { | |
balanceOf[this] += mintedAmount; | |
Transfer(0, this, mintedAmount); | |
} | |
function devOnlyClose() { | |
mblue.send(this.balance); | |
} | |
function () { | |
throw; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment