Skip to content

Instantly share code, notes, and snippets.

@y12studio
Last active March 27, 2016 02:45
Show Gist options
  • Save y12studio/51f31f11f2dd20140019 to your computer and use it in GitHub Desktop.
Save y12studio/51f31f11f2dd20140019 to your computer and use it in GitHub Desktop.
Y12 Marriage Contract 1
// ---------
// 智能婚約 2016
// 在法律許可的範圍內,作者(們)已將此軟體的著作權、相關權利與鄰接權釋出到全球的公眾領域。此軟體之散布不含任何擔保責任。
// 您應該已經連同軟體取得一份CC0的公眾領域貢獻宣告複本,若沒有收到,則請見:http://creativecommons.org/publicdomain/zero/1.0/。
// ---------
// Ethereum以太坊智能合約環境說明 https://gist.github.com/y12studio/f142cd9789c7d396d8f7
// 有意願測試簽智能婚約可聯絡 https://github.com/y12studio
// 1. 發起方(下記mblue)建立合約,並存入一筆金額(下記A)。
// 2. 發起方求婚執行合約 willYouMarryMe 條款,並填入身份證明的數位檔案摘要(SHA256)。
// 3. 提案受方(下記mred)確認 proposalYes 進入婚姻狀態或是拒絕 proposalSorry。
// mred Yes 需投入A金額並填入身份證明/婚約證明以及接受紅包時限。
// mred Sorry 會將A金額退回mblue結束合約
// 4. 親友於雙方確認後可於時限內 sendGift 發送紅包。
// 5. mblue/mred 於紅包時限過可提取一半金額。
// 6. mblue/mred 離緣解約先退一半紅包給每個親友之後再依約定分配婚約餘額。
// 注意
// 1. 以太坊不斷鏈下,即使不往來數十年後也可收到退紅包。
// 2. 以太坊式微前需提前轉約到其他智約網平台,類似現有候選HyperLedger或RootStock等。
// 3. 第三方未設計。
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;
mapping (address => uint) giftBalances;
mapping (uint => address) giftIndex;
uint public thirdStart;
uint public thirdTime;
State public state;
enum State { Created, Proposal ,Locked, Inactive }
modifier onlyGod(){
if (msg.sender != god) throw;
_
}
modifier onlyThirdPartyDao(){
if (msg.sender != thirdPartyDao) throw;
_
}
// 發起方為藍方
function YMarriageContract() {
if(msg.value < 1 finney ) throw;
mblue = msg.sender;
mValue = msg.value;
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;
state = State.Locked;
}
// Sorry.
function proposalSorry(){
if (state != State.Proposal) throw;
if (msg.sender == mblue) throw;
mblue.send(this.balance);
state = State.Inactive;
}
// 紅包只收0.999ETH或1.314ETH或大於6ETHH
function sendGift(){
if(state != State.Locked) throw;
// 過期不收
if (now > giftStart + giftTime) throw;
if(msg.value < 1 finney) throw;
// if(msg.value < 6 ether || msg.value != 999 finney || msg.value != 1314 finney) throw;
giftValue += msg.value;
giftIndex[giftCount] = msg.sender;
giftBalances[msg.sender] = msg.value;
giftCount++;
}
// 紅包到期每人可領走1/4,總計領走1/2
// [Update]經網友更正有個提光紅包的可能性,這裡不修正留著討論下面幾個議題:
// 1. 想像如智約如信用卡合約數千行,假設發起人被告上法院,故意跟過失問題。
// 2. 合約是否內建過失免責條款,或是引入過失保險智約或仲裁智約。
// 3. 包紅包可能不是人,而是發行代幣集資的紅包智約(例如YRT2/YRT3),如何發起求償。
function withdrawGift() {
if(state != State.Locked) throw;
if (msg.sender != mblue && msg.sender != mred) throw;
if (now < giftStart + giftTime) throw;
uint rValue = giftValue / 4;
mblue.send(rValue);
mred.send(rValue);
}
// 時間以秒計算,一天86400,一百年3153600000
function setupThirdPartyDao(address _address, uint _time) onlyGod {
if(state != State.Locked) throw;
thirdStart = now;
thirdTime = _time;
thirdPartyDao = _address;
}
function iterateReturnGifts(){
for(uint i=0;i<giftCount;i++){
address giftAddr = giftIndex[i];
giftAddr.send(giftBalances[giftAddr]/2);
}
}
// 合意解約取回1/2。
function doUsPartByPeace() {
if(state != State.Locked) throw;
if (msg.sender != mblue && msg.sender != mred) throw;
// 先退紅包 1/2
iterateReturnGifts();
// 剩下再分
uint rValue = this.balance / 2;
mblue.send(rValue);
mred.send(this.balance);
state = State.Inactive;
}
// 單方解約取回1/5
function doUsPartByOne() {
if(state != State.Locked) throw;
if (msg.sender != mblue && msg.sender != mred) throw;
// 先退紅包 1/2
iterateReturnGifts();
// 剩下再分
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;
}
// 第三方介入只能取回1/3
function doUsPartByThird() onlyThirdPartyDao {
if (state != State.Locked) throw;
if (now > thirdStart + thirdTime) throw;
// 先退紅包 1/2
iterateReturnGifts();
// 剩下再分
uint rValue = this.balance / 3;
mblue.send(rValue);
mred.send(rValue);
thirdPartyDao.send(this.balance);
state = State.Inactive;
}
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