2022-01-12 14:26:26 +00:00
from datetime import time , timedelta
2022-01-06 16:54:37 +00:00
from django . utils import timezone
2022-01-10 18:47:16 +00:00
from . lightning . node import LNNode
2022-01-06 16:54:37 +00:00
2022-01-07 22:46:30 +00:00
from . models import Order , LNPayment , MarketTick , User
2022-01-06 16:54:37 +00:00
from decouple import config
2022-01-07 22:46:30 +00:00
from . utils import get_exchange_rate
2022-01-06 16:54:37 +00:00
2022-01-11 14:36:43 +00:00
import math
2022-01-06 16:54:37 +00:00
FEE = float ( config ( ' FEE ' ) )
BOND_SIZE = float ( config ( ' BOND_SIZE ' ) )
MARKET_PRICE_API = config ( ' MARKET_PRICE_API ' )
ESCROW_USERNAME = config ( ' ESCROW_USERNAME ' )
2022-01-10 12:10:32 +00:00
PENALTY_TIMEOUT = int ( config ( ' PENALTY_TIMEOUT ' ) )
2022-01-06 16:54:37 +00:00
2022-01-06 21:36:22 +00:00
MIN_TRADE = int ( config ( ' MIN_TRADE ' ) )
MAX_TRADE = int ( config ( ' MAX_TRADE ' ) )
2022-01-06 20:33:40 +00:00
EXP_MAKER_BOND_INVOICE = int ( config ( ' EXP_MAKER_BOND_INVOICE ' ) )
EXP_TAKER_BOND_INVOICE = int ( config ( ' EXP_TAKER_BOND_INVOICE ' ) )
BOND_EXPIRY = int ( config ( ' BOND_EXPIRY ' ) )
ESCROW_EXPIRY = int ( config ( ' ESCROW_EXPIRY ' ) )
2022-01-06 16:54:37 +00:00
2022-01-12 00:02:17 +00:00
PUBLIC_ORDER_DURATION = int ( config ( ' PUBLIC_ORDER_DURATION ' ) )
INVOICE_AND_ESCROW_DURATION = int ( config ( ' INVOICE_AND_ESCROW_DURATION ' ) )
FIAT_EXCHANGE_DURATION = int ( config ( ' FIAT_EXCHANGE_DURATION ' ) )
2022-01-10 12:10:32 +00:00
2022-01-06 16:54:37 +00:00
class Logics ( ) :
def validate_already_maker_or_taker ( user ) :
''' Checks if the user is already partipant of an order '''
queryset = Order . objects . filter ( maker = user )
if queryset . exists ( ) :
2022-01-07 11:31:33 +00:00
return False , { ' bad_request ' : ' You are already maker of an order ' }
2022-01-06 16:54:37 +00:00
queryset = Order . objects . filter ( taker = user )
if queryset . exists ( ) :
2022-01-07 11:31:33 +00:00
return False , { ' bad_request ' : ' You are already taker of an order ' }
2022-01-06 16:54:37 +00:00
return True , None
2022-01-06 21:36:22 +00:00
def validate_order_size ( order ) :
''' Checks if order is withing limits at t0 '''
if order . t0_satoshis > MAX_TRADE :
2022-01-10 01:12:58 +00:00
return False , { ' bad_request ' : ' Your order is too big. It is worth ' + ' {:,} ' . format ( order . t0_satoshis ) + ' Sats now. But limit is ' + ' {:,} ' . format ( MAX_TRADE ) + ' Sats ' }
2022-01-06 21:36:22 +00:00
if order . t0_satoshis < MIN_TRADE :
2022-01-10 01:12:58 +00:00
return False , { ' bad_request ' : ' Your order is too small. It is worth ' + ' {:,} ' . format ( order . t0_satoshis ) + ' Sats now. But limit is ' + ' {:,} ' . format ( MIN_TRADE ) + ' Sats ' }
2022-01-06 21:36:22 +00:00
return True , None
2022-01-10 12:10:32 +00:00
@classmethod
def take ( cls , order , user ) :
is_penalized , time_out = cls . is_penalized ( user )
if is_penalized :
return False , { ' bad_request ' , f ' You need to wait { time_out } seconds to take an order ' }
else :
order . taker = user
order . status = Order . Status . TAK
2022-01-12 00:02:17 +00:00
order . expires_at = timezone . now ( ) + timedelta ( minutes = EXP_TAKER_BOND_INVOICE )
2022-01-10 12:10:32 +00:00
order . save ( )
return True , None
2022-01-06 16:54:37 +00:00
def is_buyer ( order , user ) :
is_maker = order . maker == user
is_taker = order . taker == user
return ( is_maker and order . type == Order . Types . BUY ) or ( is_taker and order . type == Order . Types . SELL )
def is_seller ( order , user ) :
is_maker = order . maker == user
is_taker = order . taker == user
return ( is_maker and order . type == Order . Types . SELL ) or ( is_taker and order . type == Order . Types . BUY )
def satoshis_now ( order ) :
''' checks trade amount in sats '''
if order . is_explicit :
satoshis_now = order . satoshis
else :
2022-01-09 01:18:11 +00:00
exchange_rate = get_exchange_rate ( Order . currency_dict [ str ( order . currency ) ] )
2022-01-07 22:46:30 +00:00
premium_rate = exchange_rate * ( 1 + float ( order . premium ) / 100 )
satoshis_now = ( float ( order . amount ) / premium_rate ) * 100 * 1000 * 1000
2022-01-06 16:54:37 +00:00
2022-01-07 11:31:33 +00:00
return int ( satoshis_now )
2022-01-10 01:12:58 +00:00
def price_and_premium_now ( order ) :
''' computes order premium live '''
exchange_rate = get_exchange_rate ( Order . currency_dict [ str ( order . currency ) ] )
if not order . is_explicit :
premium = order . premium
2022-01-11 14:36:43 +00:00
price = exchange_rate * ( 1 + float ( premium ) / 100 )
2022-01-10 01:12:58 +00:00
else :
exchange_rate = get_exchange_rate ( Order . currency_dict [ str ( order . currency ) ] )
order_rate = float ( order . amount ) / ( float ( order . satoshis ) / 100000000 )
premium = order_rate / exchange_rate - 1
2022-01-10 19:11:45 +00:00
premium = int ( premium * 100 ) # 2 decimals left
2022-01-10 01:12:58 +00:00
price = order_rate
2022-01-11 14:36:43 +00:00
significant_digits = 6
price = round ( price , significant_digits - int ( math . floor ( math . log10 ( abs ( price ) ) ) ) - 1 )
2022-01-10 19:11:45 +00:00
2022-01-10 01:12:58 +00:00
return price , premium
2022-01-06 16:54:37 +00:00
def order_expires ( order ) :
2022-01-12 00:02:17 +00:00
''' General case when time runs out. Only
used when the maker does not lock a publishing bond '''
2022-01-06 16:54:37 +00:00
order . status = Order . Status . EXP
order . maker = None
order . taker = None
order . save ( )
2022-01-12 00:02:17 +00:00
def kick_taker ( order ) :
''' The taker did not lock the taker_bond. Now he has to go '''
# Add a time out to the taker
profile = order . taker . profile
profile . penalty_expiration = timezone . now ( ) + timedelta ( seconds = PENALTY_TIMEOUT )
profile . save ( )
# Delete the taker_bond payment request, and make order public again
if LNNode . cancel_return_hold_invoice ( order . taker_bond . payment_hash ) :
order . status = Order . Status . PUB
order . taker = None
order . taker_bond = None
order . expires_at = timezone . now ( ) + timedelta ( hours = PUBLIC_ORDER_DURATION ) ## TO FIX. Restore the remaining order durantion, not all of it!
order . save ( )
return True
2022-01-07 11:31:33 +00:00
@classmethod
def buyer_invoice_amount ( cls , order , user ) :
''' Computes buyer invoice amount. Uses order.last_satoshis,
that is the final trade amount set at Taker Bond time '''
if cls . is_buyer ( order , user ) :
invoice_amount = int ( order . last_satoshis * ( 1 - FEE ) ) # Trading FEE is charged here.
return True , { ' invoice_amount ' : invoice_amount }
2022-01-06 16:54:37 +00:00
@classmethod
def update_invoice ( cls , order , user , invoice ) :
2022-01-07 19:22:07 +00:00
# only the buyer can post a buyer invoice
if not cls . is_buyer ( order , user ) :
return False , { ' bad_request ' : ' Only the buyer of this order can provide a buyer invoice. ' }
if not order . taker_bond :
return False , { ' bad_request ' : ' Wait for your order to be taken. ' }
if not ( order . taker_bond . status == order . maker_bond . status == LNPayment . Status . LOCKED ) :
return False , { ' bad_request ' : ' You cannot a invoice while bonds are not posted. ' }
num_satoshis = cls . buyer_invoice_amount ( order , user ) [ 1 ] [ ' invoice_amount ' ]
2022-01-11 14:36:43 +00:00
buyer_invoice = LNNode . validate_ln_invoice ( invoice , num_satoshis )
2022-01-12 00:02:17 +00:00
2022-01-11 14:36:43 +00:00
if not buyer_invoice [ ' valid ' ] :
return False , buyer_invoice [ ' context ' ]
2022-01-06 22:39:59 +00:00
order . buyer_invoice , _ = LNPayment . objects . update_or_create (
concept = LNPayment . Concepts . PAYBUYER ,
type = LNPayment . Types . NORM ,
sender = User . objects . get ( username = ESCROW_USERNAME ) ,
receiver = user ,
# if there is a LNPayment matching these above, it updates that one with defaults below.
defaults = {
' invoice ' : invoice ,
' status ' : LNPayment . Status . VALIDI ,
' num_satoshis ' : num_satoshis ,
2022-01-11 14:36:43 +00:00
' description ' : buyer_invoice [ ' description ' ] ,
' payment_hash ' : buyer_invoice [ ' payment_hash ' ] ,
' created_at ' : buyer_invoice [ ' created_at ' ] ,
' expires_at ' : buyer_invoice [ ' expires_at ' ] }
2022-01-06 22:39:59 +00:00
)
2022-01-12 00:02:17 +00:00
# If the order status is 'Waiting for invoice'. Move forward to 'chat'
2022-01-12 12:57:03 +00:00
if order . status == Order . Status . WFI :
order . status = Order . Status . CHA
order . expires_at = timezone . now ( ) + timedelta ( hours = FIAT_EXCHANGE_DURATION )
2022-01-06 22:39:59 +00:00
2022-01-11 01:02:06 +00:00
# If the order status is 'Waiting for both'. Move forward to 'waiting for escrow'
2022-01-06 22:39:59 +00:00
if order . status == Order . Status . WF2 :
2022-01-12 12:57:03 +00:00
# If the escrow is lock move to Chat.
if order . trade_escrow . status == LNPayment . Status . LOCKED :
order . status = Order . Status . CHA
order . expires_at = timezone . now ( ) + timedelta ( hours = FIAT_EXCHANGE_DURATION )
2022-01-06 22:39:59 +00:00
else :
order . status = Order . Status . WFE
order . save ( )
return True , None
2022-01-06 20:33:40 +00:00
2022-01-11 20:49:53 +00:00
def add_profile_rating ( profile , rating ) :
''' adds a new rating to a user profile '''
profile . total_ratings = profile . total_ratings + 1
latest_ratings = profile . latest_ratings
if len ( latest_ratings ) < = 1 :
profile . latest_ratings = [ rating ]
profile . avg_rating = rating
else :
latest_ratings = list ( latest_ratings ) . append ( rating )
profile . latest_ratings = latest_ratings
profile . avg_rating = sum ( latest_ratings ) / len ( latest_ratings )
profile . save ( )
2022-01-10 12:10:32 +00:00
def is_penalized ( user ) :
''' Checks if a user that is not participant of orders
has a limit on taking or making a order '''
if user . profile . penalty_expiration :
if user . profile . penalty_expiration > timezone . now ( ) :
time_out = ( user . profile . penalty_expiration - timezone . now ( ) ) . seconds
return True , time_out
return False , None
2022-01-06 20:33:40 +00:00
@classmethod
2022-01-07 19:22:07 +00:00
def cancel_order ( cls , order , user , state = None ) :
2022-01-07 11:31:33 +00:00
# 1) When maker cancels before bond
2022-01-06 22:39:59 +00:00
''' The order never shows up on the book and order
status becomes " cancelled " . That ' s it. ' ' '
if order . status == Order . Status . WFB and order . maker == user :
order . maker = None
order . status = Order . Status . UCA
order . save ( )
2022-01-12 00:02:17 +00:00
return True , None
2022-01-06 22:39:59 +00:00
# 2) When maker cancels after bond
2022-01-12 00:02:17 +00:00
''' The order dissapears from book and goes to cancelled. Maker is charged the bond to prevent DDOS
on the LN node and order book . TODO Only charge a small part of the bond ( requires maker submitting an invoice ) '''
2022-01-11 14:36:43 +00:00
elif order . status == Order . Status . PUB and order . maker == user :
2022-01-12 00:02:17 +00:00
#Settle the maker bond (Maker loses the bond for cancelling public order)
2022-01-11 20:49:53 +00:00
if cls . settle_maker_bond ( order ) :
2022-01-11 14:36:43 +00:00
order . maker = None
order . status = Order . Status . UCA
order . save ( )
2022-01-12 00:02:17 +00:00
return True , None
2022-01-06 20:33:40 +00:00
2022-01-06 22:39:59 +00:00
# 3) When taker cancels before bond
''' The order goes back to the book as public.
LNPayment " order.taker_bond " is deleted ( ) '''
2022-01-10 12:10:32 +00:00
elif order . status == Order . Status . TAK and order . taker == user :
# adds a timeout penalty
user . profile . penalty_expiration = timezone . now ( ) + timedelta ( seconds = PENALTY_TIMEOUT )
2022-01-11 20:49:53 +00:00
user . profile . save ( )
2022-01-10 12:10:32 +00:00
order . taker = None
order . status = Order . Status . PUB
order . save ( )
2022-01-12 00:02:17 +00:00
return True , None
2022-01-06 20:33:40 +00:00
2022-01-07 11:31:33 +00:00
# 4) When taker or maker cancel after bond (before escrow)
2022-01-06 22:39:59 +00:00
''' The order goes into cancelled status if maker cancels.
The order goes into the public book if taker cancels .
In both cases there is a small fee . '''
2022-01-11 14:36:43 +00:00
# 4.a) When maker cancel after bond (before escrow)
''' The order into cancelled status if maker cancels. '''
elif order . status > Order . Status . PUB and order . status < Order . Status . CHA and order . maker == user :
#Settle the maker bond (Maker loses the bond for canceling an ongoing trade)
valid = cls . settle_maker_bond ( order )
if valid :
order . maker = None
order . status = Order . Status . UCA
order . save ( )
2022-01-12 00:02:17 +00:00
return True , None
2022-01-11 14:36:43 +00:00
# 4.b) When taker cancel after bond (before escrow)
''' The order into cancelled status if maker cancels. '''
elif order . status > Order . Status . TAK and order . status < Order . Status . CHA and order . taker == user :
2022-01-11 20:49:53 +00:00
# Settle the maker bond (Maker loses the bond for canceling an ongoing trade)
2022-01-11 14:36:43 +00:00
valid = cls . settle_taker_bond ( order )
if valid :
order . taker = None
order . status = Order . Status . PUB
# order.taker_bond = None # TODO fix this, it overrides the information about the settled taker bond. Might make admin tasks hard.
order . save ( )
2022-01-12 00:02:17 +00:00
return True , None
2022-01-11 14:36:43 +00:00
2022-01-07 11:31:33 +00:00
# 5) When trade collateral has been posted (after escrow)
2022-01-06 22:39:59 +00:00
''' Always goes to cancelled status. Collaboration is needed.
When a user asks for cancel , ' order.is_pending_cancel ' goes True .
When the second user asks for cancel . Order is totally cancelled .
Has a small cost for both parties to prevent node DDOS . '''
else :
return False , { ' bad_request ' : ' You cannot cancel this order ' }
2022-01-06 20:33:40 +00:00
2022-01-12 00:02:17 +00:00
@classmethod
def is_maker_bond_locked ( cls , order ) :
if LNNode . validate_hold_invoice_locked ( order . maker_bond . payment_hash ) :
order . maker_bond . status = LNPayment . Status . LOCKED
order . maker_bond . save ( )
order . status = Order . Status . PUB
# With the bond confirmation the order is extended 'public_order_duration' hours
order . expires_at = timezone . now ( ) + timedelta ( hours = PUBLIC_ORDER_DURATION )
order . save ( )
return True
return False
2022-01-06 16:54:37 +00:00
@classmethod
2022-01-09 20:05:19 +00:00
def gen_maker_hold_invoice ( cls , order , user ) :
2022-01-06 16:54:37 +00:00
2022-01-12 00:02:17 +00:00
# Do not gen and cancel if order is older than expiry time
2022-01-06 16:54:37 +00:00
if order . expires_at < timezone . now ( ) :
cls . order_expires ( order )
2022-01-07 11:31:33 +00:00
return False , { ' bad_request ' : ' Invoice expired. You did not confirm publishing the order in time. Make a new order. ' }
2022-01-06 16:54:37 +00:00
2022-01-07 11:31:33 +00:00
# Return the previous invoice if there was one and is still unpaid
2022-01-06 16:54:37 +00:00
if order . maker_bond :
2022-01-12 00:02:17 +00:00
if cls . is_maker_bond_locked ( order ) :
2022-01-07 11:31:33 +00:00
return False , None
2022-01-12 00:02:17 +00:00
elif order . maker_bond . status == LNPayment . Status . INVGEN :
return True , { ' bond_invoice ' : order . maker_bond . invoice , ' bond_satoshis ' : order . maker_bond . num_satoshis }
2022-01-06 16:54:37 +00:00
2022-01-12 00:02:17 +00:00
# If there was no maker_bond object yet, generates one
2022-01-07 11:31:33 +00:00
order . last_satoshis = cls . satoshis_now ( order )
bond_satoshis = int ( order . last_satoshis * BOND_SIZE )
2022-01-11 14:36:43 +00:00
2022-01-12 12:57:03 +00:00
description = f " RoboSats - Publishing ' { str ( order ) } ' - This is a maker bond, it will freeze in your wallet temporarily and automatically return. It will be charged if you cheat or cancel. "
2022-01-06 20:33:40 +00:00
2022-01-09 20:05:19 +00:00
# Gen hold Invoice
2022-01-11 14:36:43 +00:00
hold_payment = LNNode . gen_hold_invoice ( bond_satoshis , description , BOND_EXPIRY * 3600 )
2022-01-06 16:54:37 +00:00
order . maker_bond = LNPayment . objects . create (
concept = LNPayment . Concepts . MAKEBOND ,
2022-01-11 14:36:43 +00:00
type = LNPayment . Types . HOLD ,
2022-01-06 16:54:37 +00:00
sender = user ,
receiver = User . objects . get ( username = ESCROW_USERNAME ) ,
2022-01-11 14:36:43 +00:00
invoice = hold_payment [ ' invoice ' ] ,
preimage = hold_payment [ ' preimage ' ] ,
2022-01-06 16:54:37 +00:00
status = LNPayment . Status . INVGEN ,
num_satoshis = bond_satoshis ,
description = description ,
2022-01-11 14:36:43 +00:00
payment_hash = hold_payment [ ' payment_hash ' ] ,
created_at = hold_payment [ ' created_at ' ] ,
expires_at = hold_payment [ ' expires_at ' ] )
2022-01-06 16:54:37 +00:00
order . save ( )
2022-01-11 14:36:43 +00:00
return True , { ' bond_invoice ' : hold_payment [ ' invoice ' ] , ' bond_satoshis ' : bond_satoshis }
2022-01-06 16:54:37 +00:00
2022-01-11 20:49:53 +00:00
@classmethod
2022-01-12 00:02:17 +00:00
def is_taker_bond_locked ( cls , order ) :
if LNNode . validate_hold_invoice_locked ( order . taker_bond . payment_hash ) :
# THE TRADE AMOUNT IS FINAL WITH THE CONFIRMATION OF THE TAKER BOND!
# (This is the last update to "last_satoshis", it becomes the escrow amount next!)
order . last_satoshis = cls . satoshis_now ( order )
order . taker_bond . status = LNPayment . Status . LOCKED
order . taker_bond . save ( )
2022-01-12 14:26:26 +00:00
# Log a market tick
MarketTick . log_a_tick ( order )
2022-01-12 00:02:17 +00:00
# With the bond confirmation the order is extended 'public_order_duration' hours
order . expires_at = timezone . now ( ) + timedelta ( minutes = INVOICE_AND_ESCROW_DURATION )
order . status = Order . Status . WF2
2022-01-11 20:49:53 +00:00
order . save ( )
return True
return False
2022-01-06 16:54:37 +00:00
@classmethod
2022-01-09 20:05:19 +00:00
def gen_taker_hold_invoice ( cls , order , user ) :
2022-01-06 16:54:37 +00:00
2022-01-12 00:02:17 +00:00
# Do not gen and kick out the taker if order is older than expiry time
if order . expires_at < timezone . now ( ) :
cls . kick_taker ( order )
return False , { ' bad_request ' : ' Invoice expired. You did not confirm taking the order in time. ' }
# Do not gen if a taker invoice exist. Do not return if it is already locked. Return the old one if still waiting.
2022-01-06 20:33:40 +00:00
if order . taker_bond :
2022-01-07 11:31:33 +00:00
# Check if status is INVGEN and still not expired
2022-01-12 00:02:17 +00:00
if cls . is_taker_bond_locked ( order ) :
2022-01-07 11:31:33 +00:00
return False , None
2022-01-12 00:02:17 +00:00
elif order . taker_bond . status == LNPayment . Status . INVGEN :
return True , { ' bond_invoice ' : order . taker_bond . invoice , ' bond_satoshis ' : order . taker_bond . num_satoshis }
2022-01-06 16:54:37 +00:00
2022-01-12 00:02:17 +00:00
# If there was no taker_bond object yet, generates one
order . last_satoshis = cls . satoshis_now ( order )
2022-01-07 11:31:33 +00:00
bond_satoshis = int ( order . last_satoshis * BOND_SIZE )
2022-01-12 12:57:03 +00:00
description = f " RoboSats - Taking ' { str ( order ) } ' - This is a taker bond, it will freeze in your wallet temporarily and automatically return. It will be charged if you cheat or cancel. "
2022-01-06 20:33:40 +00:00
2022-01-09 20:05:19 +00:00
# Gen hold Invoice
2022-01-11 14:36:43 +00:00
hold_payment = LNNode . gen_hold_invoice ( bond_satoshis , description , BOND_EXPIRY * 3600 )
2022-01-06 16:54:37 +00:00
2022-01-06 20:33:40 +00:00
order . taker_bond = LNPayment . objects . create (
concept = LNPayment . Concepts . TAKEBOND ,
2022-01-11 14:36:43 +00:00
type = LNPayment . Types . HOLD ,
2022-01-06 16:54:37 +00:00
sender = user ,
receiver = User . objects . get ( username = ESCROW_USERNAME ) ,
2022-01-11 14:36:43 +00:00
invoice = hold_payment [ ' invoice ' ] ,
preimage = hold_payment [ ' preimage ' ] ,
2022-01-06 16:54:37 +00:00
status = LNPayment . Status . INVGEN ,
num_satoshis = bond_satoshis ,
description = description ,
2022-01-11 14:36:43 +00:00
payment_hash = hold_payment [ ' payment_hash ' ] ,
created_at = hold_payment [ ' created_at ' ] ,
expires_at = hold_payment [ ' expires_at ' ] )
2022-01-06 16:54:37 +00:00
order . save ( )
2022-01-11 14:36:43 +00:00
return True , { ' bond_invoice ' : hold_payment [ ' invoice ' ] , ' bond_satoshis ' : bond_satoshis }
2022-01-07 11:31:33 +00:00
2022-01-12 00:02:17 +00:00
@classmethod
def is_trade_escrow_locked ( cls , order ) :
if LNNode . validate_hold_invoice_locked ( order . trade_escrow . payment_hash ) :
order . trade_escrow . status = LNPayment . Status . LOCKED
order . trade_escrow . save ( )
# If status is 'Waiting for both' move to Waiting for invoice
if order . status == Order . Status . WF2 :
order . status = Order . Status . WFI
# If status is 'Waiting for invoice' move to Chat
2022-01-12 12:57:03 +00:00
elif order . status == Order . Status . WFE :
2022-01-12 00:02:17 +00:00
order . status = Order . Status . CHA
order . expires_at = timezone . now ( ) + timedelta ( hours = FIAT_EXCHANGE_DURATION )
order . save ( )
return True
return False
2022-01-07 11:31:33 +00:00
@classmethod
2022-01-09 20:05:19 +00:00
def gen_escrow_hold_invoice ( cls , order , user ) :
2022-01-12 00:02:17 +00:00
# Do not generate if escrow deposit time has expired
if order . expires_at < timezone . now ( ) :
cls . cancel_order ( order , user )
return False , { ' bad_request ' : ' Invoice expired. You did not send the escrow in time. ' }
# Do not gen if an escrow invoice exist. Do not return if it is already locked. Return the old one if still waiting.
2022-01-07 11:31:33 +00:00
if order . trade_escrow :
# Check if status is INVGEN and still not expired
2022-01-12 00:02:17 +00:00
if cls . is_trade_escrow_locked ( order ) :
return False , None
elif order . trade_escrow . status == LNPayment . Status . INVGEN :
return True , { ' escrow_invoice ' : order . trade_escrow . invoice , ' escrow_satoshis ' : order . trade_escrow . num_satoshis }
2022-01-07 11:31:33 +00:00
2022-01-12 00:02:17 +00:00
# If there was no taker_bond object yet, generates one
escrow_satoshis = order . last_satoshis # Amount was fixed when taker bond was locked
description = f " RoboSats - Escrow amount for ' { str ( order ) } ' - The escrow will be released to the buyer once you confirm you received the fiat. It will automatically return if buyer does not confirm the payment. "
2022-01-07 11:31:33 +00:00
2022-01-09 20:05:19 +00:00
# Gen hold Invoice
2022-01-11 14:36:43 +00:00
hold_payment = LNNode . gen_hold_invoice ( escrow_satoshis , description , ESCROW_EXPIRY * 3600 )
2022-01-07 11:31:33 +00:00
2022-01-08 17:19:30 +00:00
order . trade_escrow = LNPayment . objects . create (
2022-01-07 11:31:33 +00:00
concept = LNPayment . Concepts . TRESCROW ,
2022-01-11 14:36:43 +00:00
type = LNPayment . Types . HOLD ,
2022-01-07 11:31:33 +00:00
sender = user ,
receiver = User . objects . get ( username = ESCROW_USERNAME ) ,
2022-01-11 14:36:43 +00:00
invoice = hold_payment [ ' invoice ' ] ,
preimage = hold_payment [ ' preimage ' ] ,
2022-01-07 11:31:33 +00:00
status = LNPayment . Status . INVGEN ,
num_satoshis = escrow_satoshis ,
description = description ,
2022-01-11 14:36:43 +00:00
payment_hash = hold_payment [ ' payment_hash ' ] ,
created_at = hold_payment [ ' created_at ' ] ,
expires_at = hold_payment [ ' expires_at ' ] )
2022-01-07 11:31:33 +00:00
order . save ( )
2022-01-11 14:36:43 +00:00
return True , { ' escrow_invoice ' : hold_payment [ ' invoice ' ] , ' escrow_satoshis ' : escrow_satoshis }
2022-01-07 22:46:30 +00:00
2022-01-07 18:22:52 +00:00
def settle_escrow ( order ) :
2022-01-11 14:36:43 +00:00
''' Settles the trade escrow hold invoice '''
# TODO ERROR HANDLING
2022-01-12 00:02:17 +00:00
if LNNode . settle_hold_invoice ( order . trade_escrow . preimage ) :
2022-01-11 14:36:43 +00:00
order . trade_escrow . status = LNPayment . Status . SETLED
2022-01-12 00:02:17 +00:00
order . trade_escrow . save ( )
return True
2022-01-11 14:36:43 +00:00
def settle_maker_bond ( order ) :
''' Settles the maker bond hold invoice '''
# TODO ERROR HANDLING
2022-01-11 20:49:53 +00:00
if LNNode . settle_hold_invoice ( order . maker_bond . preimage ) :
2022-01-11 14:36:43 +00:00
order . maker_bond . status = LNPayment . Status . SETLED
2022-01-12 00:02:17 +00:00
order . maker_bond . save ( )
2022-01-11 20:49:53 +00:00
return True
2022-01-11 14:36:43 +00:00
def settle_taker_bond ( order ) :
''' Settles the taker bond hold invoice '''
2022-01-07 18:22:52 +00:00
# TODO ERROR HANDLING
2022-01-12 00:02:17 +00:00
if LNNode . settle_hold_invoice ( order . taker_bond . preimage ) :
2022-01-11 14:36:43 +00:00
order . taker_bond . status = LNPayment . Status . SETLED
2022-01-12 00:02:17 +00:00
order . taker_bond . save ( )
return True
2022-01-12 12:57:03 +00:00
def return_bond ( bond ) :
''' returns a bond '''
if LNNode . cancel_return_hold_invoice ( bond . payment_hash ) :
bond . status = LNPayment . Status . RETNED
return True
2022-01-07 18:22:52 +00:00
def pay_buyer_invoice ( order ) :
2022-01-11 14:36:43 +00:00
''' Pay buyer invoice '''
2022-01-07 18:22:52 +00:00
# TODO ERROR HANDLING
2022-01-12 12:57:03 +00:00
if LNNode . pay_invoice ( order . buyer_invoice . invoice , order . buyer_invoice . num_satoshis ) :
2022-01-12 00:02:17 +00:00
return True
2022-01-07 18:22:52 +00:00
@classmethod
def confirm_fiat ( cls , order , user ) :
''' If Order is in the CHAT states:
2022-01-07 19:22:07 +00:00
If user is buyer : mark FIAT SENT and settle escrow !
If User is the seller and FIAT is SENT : Pay buyer invoice ! '''
2022-01-07 18:22:52 +00:00
if order . status == Order . Status . CHA or order . status == Order . Status . FSE : # TODO Alternatively, if all collateral is locked? test out
# If buyer, settle escrow and mark fiat sent
if cls . is_buyer ( order , user ) :
2022-01-08 17:19:30 +00:00
if cls . settle_escrow ( order ) : ##### !!! KEY LINE - SETTLES THE TRADE ESCROW !!!
2022-01-07 18:22:52 +00:00
order . trade_escrow . status = LNPayment . Status . SETLED
order . status = Order . Status . FSE
order . is_fiat_sent = True
# If seller and fiat sent, pay buyer invoice
elif cls . is_seller ( order , user ) :
if not order . is_fiat_sent :
return False , { ' bad_request ' : ' You cannot confirm to have received the fiat before it is confirmed to be sent by the buyer. ' }
2022-01-08 17:19:30 +00:00
# Make sure the trade escrow is at least as big as the buyer invoice
2022-01-12 12:57:03 +00:00
if order . trade_escrow . num_satoshis < = order . buyer_invoice . num_satoshis :
2022-01-08 17:19:30 +00:00
return False , { ' bad_request ' : ' Woah, something broke badly. Report in the public channels, or open a Github Issue. ' }
2022-01-07 18:22:52 +00:00
# Double check the escrow is settled.
if LNNode . double_check_htlc_is_settled ( order . trade_escrow . payment_hash ) :
2022-01-12 14:26:26 +00:00
is_payed , context = cls . pay_buyer_invoice ( order ) ##### !!! KEY LINE - PAYS THE BUYER INVOICE !!!
if is_payed :
order . status = Order . Status . SUC
order . buyer_invoice . status = LNPayment . Status . SUCCED
order . expires_at = timezone . now ( ) + timedelta ( days = 1 ) # One day to rate / see this order.
order . save ( )
2022-01-12 00:02:17 +00:00
# RETURN THE BONDS
2022-01-12 12:57:03 +00:00
cls . return_bond ( order . taker_bond )
cls . return_bond ( order . maker_bond )
2022-01-12 14:26:26 +00:00
else :
# error handling here
pass
2022-01-07 18:22:52 +00:00
else :
return False , { ' bad_request ' : ' You cannot confirm the fiat payment at this stage ' }
order . save ( )
2022-01-12 00:02:17 +00:00
return True , None
@classmethod
def rate_counterparty ( cls , order , user , rating ) :
# If the trade is finished
if order . status > Order . Status . PAY :
# if maker, rates taker
if order . maker == user :
cls . add_profile_rating ( order . taker . profile , rating )
# if taker, rates maker
if order . taker == user :
cls . add_profile_rating ( order . maker . profile , rating )
else :
return False , { ' bad_request ' : ' You cannot rate your counterparty yet. ' }
2022-01-09 20:05:19 +00:00
return True , None