forked from dooglus/intersango
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder_utils.php
147 lines (124 loc) · 4.67 KB
/
order_utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
require_once 'util.php';
function place_order($have_amount_disp, $have_currency,
$want_amount_disp, $want_currency)
{
global $is_logged_in;
$have_currency = strtoupper($have_currency);
$want_currency = strtoupper($want_currency);
curr_supported_check($have_currency);
curr_supported_check($want_currency);
// convert for inclusion into database
$have_amount = numstr_to_internal($have_amount_disp);
$want_amount = numstr_to_internal($want_amount_disp);
if ($have_currency == 'BTC') {
order_worthwhile_check($have_amount, $have_amount_disp, $have_currency, MINIMUM_BTC_AMOUNT);
order_worthwhile_check($want_amount, $want_amount_disp, $want_currency, MINIMUM_FIAT_AMOUNT);
} else {
order_worthwhile_check($have_amount, $have_amount_disp, $have_currency, MINIMUM_FIAT_AMOUNT);
order_worthwhile_check($want_amount, $want_amount_disp, $want_currency, MINIMUM_BTC_AMOUNT);
}
enough_money_check($have_amount, $have_currency);
do_query("START TRANSACTION");
// deduct money from their account
deduct_funds($have_amount, $have_currency);
// add the money to the order book
$query = "
INSERT INTO orderbook (
uid,
initial_amount,
amount,
type,
initial_want_amount,
want_amount,
want_type)
VALUES (
'$is_logged_in',
'$have_amount',
'$have_amount',
'$have_currency',
'$want_amount',
'$want_amount',
'$want_currency');
";
$result = do_query($query);
$orderid = mysql_insert_id();
do_query("COMMIT");
return $orderid;
}
function cancel_order($orderid, $uid)
{
// cancel an order
$query = "
UPDATE orderbook
SET status='CANCEL'
WHERE
orderid='$orderid'
AND uid='$uid'
AND status='OPEN'
";
do_query($query);
if (mysql_affected_rows() != 1) {
if (mysql_affected_rows() > 1)
throw new Error('Serious...', 'More rows updated than should be. Contact the sysadmin ASAP.');
else if (mysql_affected_rows() == 0)
throw new Problem(_('Cannot...'), _('Your order got bought up before you were able to cancel.'));
else
throw new Error('Serious...', 'Internal error. Contact sysadmin ASAP.');
}
// Refetch order in case something has happened.
$info = fetch_order_info($orderid);
if ($uid != $info->uid)
throw new Error('Permission...', '... Denied! Now GTFO.');
add_funds($info->uid, $info->amount, $info->type);
// these records indicate returned funds.
create_record($orderid, $info->amount, 0,
0, -1, 0);
addlog(LOG_RESULT, " cancelled order $orderid");
}
function get_orders()
{
global $is_logged_in;
$result = do_query("
SELECT
orderid, initial_amount, amount, type, initial_want_amount, want_amount, want_type
FROM
orderbook
WHERE
status = 'OPEN'
AND
uid = $is_logged_in
");
$orders = array();
while ($row = mysql_fetch_array($result)) {
$orderid = $row['orderid'];
$have_amount = $row['amount'];
$have_currency = $row['type'];
$want_amount = $row['want_amount'];
$want_currency = $row['want_type'];
if ($have_currency == 'BTC')
$text = sprintf("%s %s %s %s %s %s",
_("Sell"),
internal_to_numstr($have_amount, BTC_PRECISION),
$have_currency,
_("for"),
internal_to_numstr($want_amount, FIAT_PRECISION),
$want_currency);
else
$text = sprintf("%s %s %s %s %s %s",
_("Buy"),
internal_to_numstr($want_amount, BTC_PRECISION),
$want_currency,
_("for"),
internal_to_numstr($have_amount, FIAT_PRECISION),
$have_currency);
array_push($orders, array('orderid' => $orderid,
'text' => $text,
'have_amount' => internal_to_numstr($have_amount),
'have_currency' => $row['type'],
'want_amount' => internal_to_numstr($want_amount),
'want_currency' => $want_currency));
}
return $orders;
}
?>