2019-01-14 08:45:08 +00:00
|
|
|
.. index:: purchase, remote purchase, escrow
|
|
|
|
|
|
|
|
********************
|
|
|
|
Safe Remote Purchase
|
|
|
|
********************
|
|
|
|
|
2019-11-25 15:16:21 +00:00
|
|
|
Purchasing goods remotely currently requires multiple parties that need to trust each other.
|
|
|
|
The simplest configuration involves a seller and a buyer. The buyer would like to receive
|
2023-05-22 11:54:43 +00:00
|
|
|
an item from the seller and the seller would like to get some compensation, e.g. Ether,
|
2019-11-25 15:16:21 +00:00
|
|
|
in return. The problematic part is the shipment here: There is no way to determine for
|
|
|
|
sure that the item arrived at the buyer.
|
|
|
|
|
|
|
|
There are multiple ways to solve this problem, but all fall short in one or the other way.
|
|
|
|
In the following example, both parties have to put twice the value of the item into the
|
2023-05-22 11:54:43 +00:00
|
|
|
contract as escrow. As soon as this happened, the Ether will stay locked inside
|
2019-11-25 15:16:21 +00:00
|
|
|
the contract until the buyer confirms that they received the item. After that,
|
|
|
|
the buyer is returned the value (half of their deposit) and the seller gets three
|
|
|
|
times the value (their deposit plus the value). The idea behind
|
|
|
|
this is that both parties have an incentive to resolve the situation or otherwise
|
2023-05-22 11:54:43 +00:00
|
|
|
their Ether is locked forever.
|
2019-11-25 15:16:21 +00:00
|
|
|
|
|
|
|
This contract of course does not solve the problem, but gives an overview of how
|
|
|
|
you can use state machine-like constructs inside a contract.
|
|
|
|
|
|
|
|
|
2021-06-25 10:25:29 +00:00
|
|
|
.. code-block:: solidity
|
2019-01-14 08:45:08 +00:00
|
|
|
|
2020-05-13 15:45:58 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2021-02-08 17:13:28 +00:00
|
|
|
pragma solidity ^0.8.4;
|
2019-01-14 08:45:08 +00:00
|
|
|
contract Purchase {
|
|
|
|
uint public value;
|
|
|
|
address payable public seller;
|
|
|
|
address payable public buyer;
|
2019-12-13 15:19:49 +00:00
|
|
|
|
2019-11-25 15:16:21 +00:00
|
|
|
enum State { Created, Locked, Release, Inactive }
|
2019-06-21 12:56:27 +00:00
|
|
|
// The state variable has a default value of the first member, `State.created`
|
2019-01-14 08:45:08 +00:00
|
|
|
State public state;
|
|
|
|
|
2021-08-10 18:55:45 +00:00
|
|
|
modifier condition(bool condition_) {
|
|
|
|
require(condition_);
|
2019-01-14 08:45:08 +00:00
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2021-02-08 17:13:28 +00:00
|
|
|
/// Only the buyer can call this function.
|
|
|
|
error OnlyBuyer();
|
|
|
|
/// Only the seller can call this function.
|
|
|
|
error OnlySeller();
|
|
|
|
/// The function cannot be called at the current state.
|
|
|
|
error InvalidState();
|
|
|
|
/// The provided value has to be even.
|
|
|
|
error ValueNotEven();
|
|
|
|
|
2019-01-14 08:45:08 +00:00
|
|
|
modifier onlyBuyer() {
|
2021-02-08 17:13:28 +00:00
|
|
|
if (msg.sender != buyer)
|
|
|
|
revert OnlyBuyer();
|
2019-01-14 08:45:08 +00:00
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
|
|
|
modifier onlySeller() {
|
2021-02-08 17:13:28 +00:00
|
|
|
if (msg.sender != seller)
|
|
|
|
revert OnlySeller();
|
2019-01-14 08:45:08 +00:00
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2021-08-10 18:55:45 +00:00
|
|
|
modifier inState(State state_) {
|
|
|
|
if (state != state_)
|
2021-02-08 17:13:28 +00:00
|
|
|
revert InvalidState();
|
2019-01-14 08:45:08 +00:00
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
|
|
|
event Aborted();
|
|
|
|
event PurchaseConfirmed();
|
|
|
|
event ItemReceived();
|
2019-11-25 15:16:21 +00:00
|
|
|
event SellerRefunded();
|
2019-01-14 08:45:08 +00:00
|
|
|
|
2019-12-13 15:19:49 +00:00
|
|
|
// Ensure that `msg.value` is an even number.
|
|
|
|
// Division will truncate if it is an odd number.
|
|
|
|
// Check via multiplication that it wasn't an odd number.
|
2020-06-23 16:11:34 +00:00
|
|
|
constructor() payable {
|
2020-12-03 22:05:05 +00:00
|
|
|
seller = payable(msg.sender);
|
2019-12-13 15:19:49 +00:00
|
|
|
value = msg.value / 2;
|
2021-02-08 17:13:28 +00:00
|
|
|
if ((2 * value) != msg.value)
|
|
|
|
revert ValueNotEven();
|
2019-12-13 15:19:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 08:45:08 +00:00
|
|
|
/// Abort the purchase and reclaim the ether.
|
|
|
|
/// Can only be called by the seller before
|
|
|
|
/// the contract is locked.
|
|
|
|
function abort()
|
2021-08-10 10:39:41 +00:00
|
|
|
external
|
2019-01-14 08:45:08 +00:00
|
|
|
onlySeller
|
|
|
|
inState(State.Created)
|
|
|
|
{
|
|
|
|
emit Aborted();
|
|
|
|
state = State.Inactive;
|
2019-11-25 15:16:21 +00:00
|
|
|
// We use transfer here directly. It is
|
|
|
|
// reentrancy-safe, because it is the
|
|
|
|
// last call in this function and we
|
|
|
|
// already changed the state.
|
2019-01-14 08:45:08 +00:00
|
|
|
seller.transfer(address(this).balance);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Confirm the purchase as buyer.
|
|
|
|
/// Transaction has to include `2 * value` ether.
|
|
|
|
/// The ether will be locked until confirmReceived
|
|
|
|
/// is called.
|
|
|
|
function confirmPurchase()
|
2021-08-10 10:39:41 +00:00
|
|
|
external
|
2019-01-14 08:45:08 +00:00
|
|
|
inState(State.Created)
|
|
|
|
condition(msg.value == (2 * value))
|
|
|
|
payable
|
|
|
|
{
|
|
|
|
emit PurchaseConfirmed();
|
2020-12-03 22:05:05 +00:00
|
|
|
buyer = payable(msg.sender);
|
2019-01-14 08:45:08 +00:00
|
|
|
state = State.Locked;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Confirm that you (the buyer) received the item.
|
|
|
|
/// This will release the locked ether.
|
|
|
|
function confirmReceived()
|
2021-08-10 10:39:41 +00:00
|
|
|
external
|
2019-01-14 08:45:08 +00:00
|
|
|
onlyBuyer
|
|
|
|
inState(State.Locked)
|
|
|
|
{
|
|
|
|
emit ItemReceived();
|
|
|
|
// It is important to change the state first because
|
|
|
|
// otherwise, the contracts called using `send` below
|
|
|
|
// can call in again here.
|
2019-11-25 15:16:21 +00:00
|
|
|
state = State.Release;
|
2019-01-14 08:45:08 +00:00
|
|
|
|
|
|
|
buyer.transfer(value);
|
2019-11-25 15:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This function refunds the seller, i.e.
|
|
|
|
/// pays back the locked funds of the seller.
|
|
|
|
function refundSeller()
|
2021-08-10 10:39:41 +00:00
|
|
|
external
|
2019-11-25 15:16:21 +00:00
|
|
|
onlySeller
|
|
|
|
inState(State.Release)
|
|
|
|
{
|
|
|
|
emit SellerRefunded();
|
|
|
|
// It is important to change the state first because
|
|
|
|
// otherwise, the contracts called using `send` below
|
|
|
|
// can call in again here.
|
|
|
|
state = State.Inactive;
|
|
|
|
|
|
|
|
seller.transfer(3 * value);
|
2019-01-14 08:45:08 +00:00
|
|
|
}
|
2020-09-28 09:14:45 +00:00
|
|
|
}
|