Skip to content

Instantly share code, notes, and snippets.

@y12studio
Last active April 4, 2016 16:49
Show Gist options
  • Save y12studio/ff5b3f96b72632bfdf72 to your computer and use it in GitHub Desktop.
Save y12studio/ff5b3f96b72632bfdf72 to your computer and use it in GitHub Desktop.
Y12 Safe Remote Purchase 1
// Solidity by Example — Solidity 0.2.0 documentation http://solidity.readthedocs.org/en/latest/solidity-by-example.html
// Ethereum以太坊智能合約環境說明 https://gist.github.com/y12studio/f142cd9789c7d396d8f7
//
// 1.賣家建立合約並放入價值商品2倍ETH當押金。
// 2.買家確認賣家合約押金無誤,confirmPurchase()轉入與賣家等值押金。
// 3.買家收到貨物後確認,confirmReceived()賣家收回押金加價金,買家收回押金減去價金。
//
// Testnet Address : 0x4378717cb207EB27BF2f58d8Bb2C2deA66B1BF43
// JSON Interface
// [ { "constant": true, "inputs": [], "name": "seller", "outputs": [ { "name": "", "type": "address", "value": "0x13a9684203635a4f03e9cb30befdc3e0f69e8163", "displayName": "" } ], "type": "function", "displayName": "seller" }, { "constant": false, "inputs": [], "name": "abort", "outputs": [], "type": "function", "displayName": "abort" }, { "constant": true, "inputs": [], "name": "value", "outputs": [ { "name": "", "type": "uint256", "value": "5000000000000000", "displayName": "" } ], "type": "function", "displayName": "value" }, { "constant": true, "inputs": [], "name": "buyer", "outputs": [ { "name": "", "type": "address", "value": "0xdb271e79b114348915b0d7fca557f5e42cc90da8", "displayName": "" } ], "type": "function", "displayName": "buyer" }, { "constant": false, "inputs": [], "name": "confirmReceived", "outputs": [], "type": "function", "displayName": "confirm Received" }, { "constant": true, "inputs": [], "name": "state", "outputs": [ { "name": "", "type": "uint8", "value": "2", "displayName": "" } ], "type": "function", "displayName": "state" }, { "constant": false, "inputs": [], "name": "confirmPurchase", "outputs": [], "type": "function", "displayName": "confirm Purchase" }, { "inputs": [], "type": "constructor" }, { "anonymous": false, "inputs": [], "name": "aborted", "type": "event" }, { "anonymous": false, "inputs": [], "name": "purchaseConfirmed", "type": "event" }, { "anonymous": false, "inputs": [], "name": "itemReceived", "type": "event" } ]
//
contract YsrPurchase
{
uint public value;
address public seller;
address public buyer;
enum State { Created, Locked, Inactive }
State public state;
function YsrPurchase()
{
if(msg.value == 0 ) throw;
seller = msg.sender;
value = msg.value / 2;
if (2 * value != msg.value) throw;
}
modifier onlyBuyer()
{
if (msg.sender != buyer) throw;
_
}
modifier onlySeller()
{
if (msg.sender != seller) throw;
_
}
event aborted();
event purchaseConfirmed();
event itemReceived();
/// Abort the purchase and reclaim the ether.
/// Can only be called by the seller before
/// the contract is locked.
function abort()
onlySeller
{
if(state != State.Created) throw;
aborted();
seller.send(this.balance);
state = State.Inactive;
}
/// Confirm the purchase as buyer.
/// Transaction has to include `2 * value` ether.
/// The ether will be locked until confirmReceived
/// is called.
function confirmPurchase()
{
if(state != State.Created) throw;
if(msg.value != 2 * value) throw;
purchaseConfirmed();
buyer = msg.sender;
state = State.Locked;
}
/// Confirm that you (the buyer) received the item.
/// This will release the locked ether.
function confirmReceived()
onlyBuyer
{
if (state != State.Locked) throw;
if (msg.value > 0) throw;
itemReceived();
buyer.send(value); // We ignore the return value on purpose
seller.send(this.balance);
state = State.Inactive;
}
function() { throw; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment