Marlowe Pioneer
 
w0801_escrow asnap
20220831

The escrow contract

This page is a report on the scripted implementation of the marlowe escrow contract. The first section is for reading, the rest (the sources of all scripts) are for reference.

Every transition from one state to the next corresponds to the execution of one or more scripts. Eg. when the seller disputes the problem, this corresponds to executing script: 404_seller_disputes.sh

Each and every flow is implemented except for flow 100 -> 270, where the buyer doesn’t deposit, and the mediator tries to get his funds back after the deadline passed: I’ll look later if I can get it to work.

Scripts

  • Here is a zip with the scripts and other data: scripts_801.zip.
  • Look at the files in the ‘core’ directory in the zip-file. These scripts are also documented in the 2nd half of this page.

Notes

  • The price of the item bought/sold is 100 ADA.
  • Short deadlines: to be able to test the ‘deadline passed’ flows, the deadlines are set at 15 .. 18 minutes in the future. The ‘red’ squares in above diagram all have a deadline in their flow. (these take long to execute, because of waiting 20min for the deadlines to pass).
  • A release request is the transaction that moves the funds from the contract to the payout address after deadline expiry
  • In case you want to execute the scripts: at the starting point the buyer, mediator, and seller addresses are expected to have been created, the tokens (BUY, MED,and SEL) minted and distributed. Those scripts are not included.
  • A python script (which uses pandas) is used for computing the balance (install on linux with: apt-get install python3-pandas), but this is not needed for core execution.
  • The tool used for querying the blockchain is asnap, this is its gitlab repo: gitlab.com/wmoco/asnap (also see: usage and advanced_usage ). If you want to execute the scripts, you’ll need at least version v0.1.2 of asnap. Install it like this: go install gitlab.com/wmoco/asnap@v0.1.2.

Report

  • Following is a generated report, which details the scripts that are executed in each flow, and what the before/after balance difference (ADA) is for buyer, seller, mediator after execution.
  • The most expensive flow (highest cost) is flow550.sh which costs 6.26 ADA.
  • The flows are numbered according to the end-state, eg. Flow 550 is from “100-start” to “550-end”.

flow330.sh

102_create_escrow_contract.sh
104_mediator_funds_contract.sh
202_buyer_deposits.sh
302_buyer_approves.sh
322_seller_withdraws.sh
324_mediator_withdraws.sh

Difference before/after execution

    ALIAS        DIFF
    buyer -102.532313
 mediator   -0.657771
   seller   99.579051
    TOTAL   -3.611033

flow430.sh

102_create_escrow_contract.sh
104_mediator_funds_contract.sh
202_buyer_deposits.sh
304_buyer_objects.sh
402_seller_confirms.sh
422_buyer_withdraws.sh
424_mediator_withdraws.sh

Difference before/after execution

    ALIAS      DIFF
    buyer -3.083517
 mediator -0.653429
   seller -1.195388
    TOTAL -4.932334

flow530.sh

102_create_escrow_contract.sh
104_mediator_funds_contract.sh
202_buyer_deposits.sh
304_buyer_objects.sh
404_seller_disputes.sh
502_mediator_confirms.sh
522_buyer_withdraws.sh
524_mediator_withdraws.sh

Difference before/after execution

    ALIAS      DIFF
    buyer -3.079175
 mediator -1.808434
   seller -1.301125
    TOTAL -6.188734

flow550.sh

102_create_escrow_contract.sh
104_mediator_funds_contract.sh
202_buyer_deposits.sh
304_buyer_objects.sh
404_seller_disputes.sh
504_mediator_dismisses.sh
542_seller_withdraws.sh
544_mediator_withdraws.sh

Difference before/after execution

    ALIAS        DIFF
    buyer -102.709091
 mediator   -1.892715
   seller   98.341982
    TOTAL   -6.259824

flow370_deadline.sh

102_create_escrow_contract.sh
104_mediator_funds_contract.sh
202_buyer_deposits.sh
# BUYER DEPOSITS, BUT DOESN'T EVALUATE. SLEEP UNTIL PAYMENT DEADLINE PASSED  -> 20min 
echo "Sleeping for 20 minutes, until deadlines have passed"
sleep 1200
echo "Awake again ... at `date "+%a %Hh%M"` " 
306_seller_requests_release.sh  
362_seller_withdraws.sh  
364_mediator_withdraws.sh

Difference before/after execution

    ALIAS        DIFF
    buyer -101.349774
 mediator   -0.653429
   seller   98.478028
    TOTAL   -3.525175

flow470_deadline.sh

102_create_escrow_contract.sh
104_mediator_funds_contract.sh
202_buyer_deposits.sh
304_buyer_objects.sh
# SELLER DOESN'T CONFIRM NOR DISPUTE , SLEEP UNTIL PAYMENT DEADLINE PASSED  -> 20min 
echo "Sleeping for 20 minutes, until deadlines have passed"
sleep 1200
echo "Awake again ... at `date "+%a %Hh%M"` " 
406_buyer_requests_release.sh 
462_buyer_withdraws.sh  
464_mediator_withdraws.sh 

Difference before/after execution

    ALIAS      DIFF
    buyer -4.143685
 mediator -0.657771
   seller  0.000000
    TOTAL -4.801456

flow570_deadline.sh

102_create_escrow_contract.sh
104_mediator_funds_contract.sh
202_buyer_deposits.sh
304_buyer_objects.sh
404_seller_disputes.sh
# MEDIATOR DOESN'T DISMISS NOR CONFIRMS, SLEEP UNTIL PAYMENT DEADLINE PASSED  -> 20min 
echo "Sleeping for 20 minutes, until deadlines have passed"
sleep 1200
echo "Awake again ... at `date "+%a %Hh%M"` " 
506_buyer_requests_release.sh  
562_buyer_withdraws.sh  
564_mediator_withdraws.sh

# Note: I tried first to run seller withdraw, but that didn't work, it should be the buyer withdrawing
# The 562_seller_withdraws.sh  gave this error: 
# TxBodyErrorMinUTxONotMet (TxOutInAnyEra AlonzoEra (TxOut (AddressInEra (ShelleyAddressInEra ShelleyBasedEraAlonzo) 
# (ShelleyAddress Testnet (KeyHashObj (KeyHash "d70c3139e3c5f4c90c52f78829c57b0044dd3566f753b62e0509f75a")) StakeRefNull)) 
# (TxOutValue MultiAssetInAlonzoEra (valueFromList [])) TxOutDatumNone)) (Lovelace 999978)

Difference before/after execution

    ALIAS      DIFF
    buyer -4.190786
 mediator -0.647625
   seller -1.301125
    TOTAL -6.139536

All the scripts

000_setenv.sh

#!/bin/bash 

#----------------------------------------------------------------------
# --- general ---
export MINIMUM_ADA=2000000              # the minimum lovelace to be included with native token ouptut.
export INITIAL_LOVELACE=3000000         # the creation transaction will deposit 3₳.
export PRICE=100000000                  # the price of the item is 100₳.

# time to sleep AFTER the tx, before querying the blockchain 
export SLEEPTIME=15

#----------------------------------------------------------------------
# --- the mediator, buyer and seller ---
if [ ! -e mediator.address ]; then
    echo "mediator.address doesn't exist"
    exit 1
fi
export MEDIATOR_ADDRESS=$(cat mediator.address)

if [ ! -e buyer.address ]; then
    echo "buyer.address doesn't exist"
    exit 1
fi
export BUYER_ADDRESS=$(cat buyer.address)

if [ ! -e seller.address ]; then
    echo "seller.address doesn't exist"
    exit 1
fi
export SELLER_ADDRESS=$(cat seller.address)

export MEDIATOR_ROLE='MED'
export BUYER_ROLE='BUY'
export SELLER_ROLE='SEL'

# Token in HEX:
# MED = 4D 45 44
# BUY = 42 55 59
# SEL= 53 45 4C

#----------------------------------------------------------------------
# --- policy aka currency ---
export POLICY_ID=""
export MEDIATOR_TOKEN=""
export BUYER_TOKEN=""
export SELLER_TOKEN=""

if [ -f policy_id.txt ] 
then 
    export POLICY_ID=$(cat policy_id.txt)
    export MEDIATOR_TOKEN="${POLICY_ID}.${MEDIATOR_ROLE}"
    export BUYER_TOKEN="${POLICY_ID}.${BUYER_ROLE}"
    export SELLER_TOKEN="${POLICY_ID}.${SELLER_ROLE}"
fi

#----------------------------------------------------------------------
# --- contract and payout addressess and tx'es ---
export CONTRACT_ADDRESS=""
if [ -f contract.address ] ; then 
    export CONTRACT_ADDRESS=$(cat contract.address)
fi

export PAYOUT_ADDRESS=""
if [ -f payout.address ];  then 
    export PAYOUT_ADDRESS=$(cat payout.address)
fi

# watchout: tx without the ix!!
export CONTRACT_TX=""
if [ -f contract_tx.txt ] ; then 
    export CONTRACT_TX=$(cat contract_tx.txt)
fi

# watchout: tx without the ix!!
export PAYOUT_TX=""
if [ -f payout_tx.txt ] ; then 
    export PAYOUT_TX=$(cat payout_tx.txt)
fi

010_clean_for_collateral.sh

102_create_escrow_contract.sh

#!/bin/bash 
echo 
echo "--- `date "+%a %Hh%M"`: $0 ----------------------------------------------------------------------"
. ./000_setenv.sh

# first instantiate the deadlines, then load the envir variables via the ./00_setenv.sh
export NOW=$(($(date -u +%s)*1000))         # The current time in POSIX milliseconds.
export HOUR=$((60*60*1000))                 # One hour, in POSIX milliseconds.
export QUARTER=$((15*60*1000))              # One hour, in POSIX milliseconds.
export MINUTE=$((60*1000))              # One hour, in POSIX milliseconds.

# short deadline!
export PAYMENT_DEADLINE=$((NOW+QUARTER)) 
export COMPLAINT_DEADLINE=$((NOW+QUARTER+MINUTE)) 
export DISPUTE_DEADLINE=$((NOW+QUARTER+2*MINUTE)) 
export MEDIATION_DEADLINE=$((NOW+QUARTER+3*MINUTE))

marlowe-cli template escrow --minimum-ada "$INITIAL_LOVELACE"          \
                            --price "$PRICE"                           \
                            --seller "Role=$SELLER_ROLE"               \
                            --buyer "Role=$BUYER_ROLE"                 \
                            --mediator "Role=$MEDIATOR_ROLE"           \
                            --payment-deadline "$PAYMENT_DEADLINE"     \
                            --complaint-deadline "$COMPLAINT_DEADLINE" \
                            --dispute-deadline "$DISPUTE_DEADLINE"     \
                            --mediation-deadline "$MEDIATION_DEADLINE" \
                            --out-contract-file 103_contract.json   \
                            --out-state-file    103_state.json 


# from the contract and state file, produce a marlowe file
marlowe-cli run initialize --roles-currency "$POLICY_ID"     \
                           --contract-file 103_contract.json     \
                           --state-file    103_state.json     \
                           --out-file      103_marlowe.json   \
                           --print-stats

jq -r '.marloweValidator.address' 103_marlowe.json > contract.address
jq -r '.rolesValidator.address' 103_marlowe.json > payout.address


asnap -t dev mediator.address buyer.address seller.address contract.address payout.address > 103_snap.txt  

104_mediator_funds_contract.sh

202_buyer_deposits.sh

206_mediator_requests_release.sh

262_mediator_withdraws.sh

302_buyer_approves.sh

304_buyer_objects.sh

306_seller_requests_release.sh

322_seller_withdraws.sh

324_mediator_withdraws.sh

362_seller_withdraws.sh

364_mediator_withdraws.sh

402_seller_confirms.sh

404_seller_disputes.sh

406_buyer_requests_release.sh

422_buyer_withdraws.sh

424_mediator_withdraws.sh

462_buyer_withdraws.sh

464_mediator_withdraws.sh

502_mediator_confirms.sh

504_mediator_dismisses.sh

506_buyer_requests_release.sh

522_buyer_withdraws.sh

524_mediator_withdraws.sh

542_seller_withdraws.sh

544_mediator_withdraws.sh

562_buyer_withdraws.sh

564_mediator_withdraws.sh

 
Notes by wmoco. Generated on momo:/home/willem/sync/20220806_wmoco_pubcoms/20220806_marlowe_pioneer at 2023-06-14 18:32