-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlend.php
106 lines (83 loc) · 2.84 KB
/
lend.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
<?php
$currency = @$_GET['currency'] ? htmlspecialchars($_GET['currency']) : 'usd';
include_once("./config-$currency.php");
include_once('./functions.php');
include_once('./bitfinex.php');
$bfx = new Bitfinex($config['api_key'], $config['api_secret']);
$current_offers = $bfx->get_offers();
// Something is wrong most likely API key
if (array_key_exists('message', $current_offers)) {
die($current_offers['message']);
}
// Remove offers that weren't executed for too long
foreach ($current_offers as $item) {
$id = $item['id'];
$timestamp = (int) $item['timestamp'];
$current_timestamp = time();
$diff_minutes = round(($current_timestamp - $timestamp) / 60);
if ($config['remove_after'] <= $diff_minutes) {
message("Removing offer # $id");
$bfx->cancel_offer($id);
}
}
$balances = $bfx->get_balances();
$available_balance = 0;
if ($balances) {
foreach ($balances as $item) {
if ($item['type'] == 'deposit' && $item['currency'] == strtolower($config['currency'])) {
$available_balance = floatval($item['available']);
break;
}
}
}
if ($currency !== 'usd') {
$ticker = $bfx->get_ticker("{$currency}usd");
$minimum_balance = ceil(($config['minimum_balance'] / $ticker['last_price']) * 100000) / 100000; // Better precision
} else {
$minimum_balance = $config['minimum_balance'];
}
// Is there enough balance to lend?
if ($available_balance >= $minimum_balance) {
message("Lending availabe balance of $available_balance");
$lendbook = $bfx->get_lendbook($config['currency']);
$offers = $lendbook['asks'];
$bids = $lendbook['bids'];
$total_amount = 0;
$next_rate = 0;
$next_amount = 0;
$check_next = FALSE;
$lowest_rate = count($bids) ? $bids[0]['rate'] : 0;
// Find the right rate
foreach ($offers as $item) {
// Save next closest item
if ($check_next) {
$next_rate = $item['rate'];
$next_amount = $item['amount'];
$check_next = FALSE;
}
$total_amount += floatval($item['amount']);
// Possible closest rate to what we want to lend (never go lower than the highest bid rate)
if ($next_rate < $lowest_rate || $total_amount <= $config['max_total_swaps']) {
$rate = $item['rate'];
$check_next = TRUE;
}
}
// Current rate is too low, move closer to the next rate
if($next_amount <= $config['max_total_swaps']) {
$rate = $next_rate - 0.01;
}
$amount = amount($available_balance);
$result = $bfx->new_offer($config['currency'], $amount, $rate, $config['period'], 'lend');
// Successfully lent
if (array_key_exists('id', $result)) {
$daily_rate = daily_rate($rate);
message("$amount {$config['currency']} lent for {$config['period']} days at daily rate of $daily_rate%. Offer id {$result['id']}.");
} else {
// Something went wrong
message($result);
}
}
else {
message("Balance of $amount {$config['currency']} is not enough to lend. At least $minimum_balance {$config['currency']} is needed.");
}
?>