/*
* GajuMarket Sales Offer
*
* Author: Craig Everett <ceverett@tsuriai.jp>
* Copyright: Tsuriai Corporation (2022), QPQ IaaS AG (2026)
* License: GPLv3
* Version: 1
*
* Sale offers at GajuMarket (gaju.market) are clones of this contract.
* When a sale offer is created on the site, the seller signs a contract call to
*
*
* Calls to clones of this contract can come from five sources:
* 1. The base contract (that cloned it in the first place)
* - init(qpq : address, // Service fee destination
* base : BaseContract, // Get born
* agent : address, // Deployment agent address
* portion : int, // Deploying agent's fee portion
* seller : address, // Address of the seller
* id : int,
* price : int)
* - do_a_backflip() // Get the opposite of born
* 2. Sellers who posted clones of this contract
* - adjust(price : int) // update price
* - accept() // Sale is DONE
* - refuse() // Cancel a negotation
* - revoke() // Invalidate and disable this offer
* 3. Buyers who are interested in buying through this contract
* - bid(price : int) // Update the bid amout in negotiation
* - hold() // Set HOLD for price negotiation
* - cancel() // Back out of a purchase
* 4. GajuMarket (the base contract)
* - reassign(qpq : address) // Reset QPQ's payable address
* 5. The market's network service (or the public)
* - price() // Check current contract price
* - status() // Check contract status
* - seller() // Retrieve the seller's public key
* - buyer() // Retrieve the buyer's public key
*/
@compiler == 9.0.0
include "Option.aes"
contract SalesOffer =
record state =
{id : int,
base : address,
agent : address,
portion : int,
price : int,
seller : address,
buyer : option(address),
status : status,
qpq : address}
datatype status = OPEN | NEGO | HOLD | DONE
// Seller Interface
stateful entrypoint init(qpq : address,
base : address,
agent : address,
portion : int,
seller : address,
id : int,
price : int) : state =
{id = id,
base = base,
agent = Call.origin,
portion = portion,
price = price,
seller = seller,
buyer = None,
status = OPEN,
qpq = qpq}
public stateful entrypoint adjust(price : int) : int =
require(state.status != DONE, "Contract closed.")
require(Call.caller == state.seller, "Action not authorized.")
require(price > 0, "Prices must be positive.")
let refund =
switch(state.buyer)
Some(buyer) =>
if(Contract.balance > price)
let refunded = Contract.balance - price
Chain.spend(buyer, refunded)
refunded
else
0
None =>
dead_claim()
put(state{price = price})
refund
public stateful entrypoint accept() : int =
require(state.status == NEGO, "Sale is not under negotiation.")
require(Call.caller == state.seller, "Action not authorized.")
require(Contract.balance >= state.price, "Insufficient funds.")
let buyer = Option.force(state.buyer)
if(Contract.balance > state.price)
Chain.spend(buyer, Contract.balance - state.price)
Chain.spend(state.qpq, calc_fee())
if(state.portion > 0)
Chain.spend(state.agent, (Contract.balance / state.portion))
let payout = Contract.balance
Chain.spend(state.seller, Contract.balance)
put(state{status = DONE})
payout
public stateful entrypoint refuse() : int =
require(state.status == NEGO || state.status == HOLD, "Sale is not under negotiation.")
require(Call.caller == state.seller, "Action not authorized.")
let refund = refund()
put(state{buyer = None})
put(state{status = OPEN})
refund
public stateful entrypoint revoke() : int =
require(state.status != DONE, "Contract closed.")
require(Call.caller == state.seller, "Action not authorized.")
let relocated =
switch(state.status)
OPEN => dead_claim()
NEGO => refund()
HOLD => refund()
put(state{status = DONE})
relocated
// Buyer Interface
public stateful payable entrypoint bid(amount: int) : int =
require(state.status != DONE, "Contract closed.")
require(amount >= state.price, "Stop being poor.")
switch(state.status)
OPEN =>
require(Call.value >= state.price, "Stop being poor.")
put(state{status = HOLD})
put(state{buyer = Some(Call.caller)})
0
NEGO =>
switch(state.buyer)
Some(buyer) =>
require(Call.caller == buyer, "Action not authorized.")
require(Contract.balance >= state.price, "Stop being poor.")
if(Contract.balance > amount)
let refund = Contract.balance - amount
Chain.spend(buyer, refund)
refund
else
0
HOLD =>
let return =
switch(state.buyer)
Some(buyer) =>
require(Call.caller == buyer, "Action not authorized.")
require(Contract.balance >= state.price, "Stop being poor.")
if(Contract.balance > amount)
let refund = Contract.balance - amount
Chain.spend(buyer, refund)
refund
else
0
put(state{status = NEGO})
return
public stateful entrypoint hold() : unit =
require(state.status == NEGO, "Sale is not in negotiation")
switch(state.buyer)
Some(buyer) =>
require(Call.caller == buyer, "Action not authorized.")
put(state{status = HOLD})
None =>
abort("Nacho bid!")
public stateful entrypoint cancel() : int =
require(state.status == NEGO || state.status == HOLD, "Contract is not in NEGO or HOLD status.")
switch(state.buyer)
Some(buyer) =>
require(Call.caller == buyer, "Action not authorized.")
Chain.spend(state.qpq, calc_fee())
put(state{buyer = None})
put(state{status = OPEN})
let refund = Contract.balance
Chain.spend(buyer, Contract.balance)
refund
None =>
abort("Action not authorized.")
// QuidProQuo Interface
public stateful entrypoint reassign(qpq : address) : unit =
require(Call.caller == state.base || Call.origin == state.qpq, "Illegal call origin.")
put(state{qpq = qpq})
public stateful entrypoint sweep_the_table() : int =
require(state.status == DONE, "Contract is not DONE.")
require(Call.caller == state.qpq, "Illegal call origin.")
refund()
public stateful entrypoint do_a_backflip() : int =
require(Call.caller == state.base, "Illegal call origin.")
put(state{status = DONE})
refund()
// Service/Public Network Interface
public entrypoint price() : int =
state.price
public entrypoint status() : string =
switch(state.status)
OPEN => "open"
NEGO => "nego"
HOLD => "hold"
DONE => "done"
public entrypoint agent() : address =
state.agent
public entrypoint seller() : address =
state.seller
public entrypoint buyer() : address =
require(state.status != OPEN, "No buyer.")
Option.force(state.buyer)
// Utilities
function calc_fee() : int =
Contract.balance / 50
private stateful function refund() : int =
if(Contract.balance > 0)
switch(state.buyer)
Some(buyer) =>
let refund = Contract.balance
Chain.spend(buyer, Contract.balance)
refund
None =>
0
else
0
private stateful function dead_claim() : int =
if(Contract.balance > 0)
let refund = Contract.balance
Chain.spend(state.qpq, Contract.balance)
refund
else
0