Skip to content

Commit

Permalink
Add Coinbase balance and outstanding order balances to orders page
Browse files Browse the repository at this point in the history
This change adds a simple table to the orders page the displays 1) your current BTC balance on Coinbase, 2) the total of sell orders you have outstanding, 3) the difference between 1 and 2, 4) the total of buy orders you have outstanding.

The Coinbase balance incurs an API request to Coinbase, but it is unlikely to change frequently, so it is a good candidate for caching.

Test Plan:
Login and visit /homepage/orders
You should see a table with the 4 pieces of information described above.
  • Loading branch information
MattFaus committed Feb 27, 2014
1 parent 1d08b82 commit 40c3475
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
20 changes: 19 additions & 1 deletion www/app/model/DB/OrdersModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,26 @@ public function findById($id) {
return $this->findAll()->get($id);
}

public function findExposure($user_id, $action) {
$sqlWhere = Array('status' => 'ACTIVE', 'user_id' => $user_id, 'action' => $action);
$exposure = $this->findAll()->where($sqlWhere)->sum("amount");

if(empty($exposure)) {
$exposure = 0;
}
return $exposure;
}

public function findSellExposure($user_id) {
return $this->findExposure($user_id, 'SELL');
}

public function findBuyExposure($user_id) {
return $this->findExposure($user_id, 'BUY');
}

public function insert($values) {
return $this->findAll()->insert($values);
}

}
}
12 changes: 6 additions & 6 deletions www/app/presenters/BasePresenter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<?php

use Nette\Utils\Html;
Expand Down Expand Up @@ -26,20 +25,21 @@ protected function startup() {
'buy' => $this->context->values->get('coinbase', 'buyPrice'),
'sell' => $this->context->values->get('coinbase', 'sellPrice'),
);


// Handle OAuth2 redirect from Coinbase
if ($this->getParam('code') != NULL && $this->user->isLoggedIn()) {
$this->context->coinbase->user($this->user->id)->getAndSaveTokens($this->getParam('code'));
$this->redirect($this->home);
}
}

public function handleUpdateCurrentPrice(){
$updatedSecondsAgo = time() - $this->context->values->get('coinbase', 'sellPrice')->updated->getTimestamp();
if($updatedSecondsAgo > 600){ //price not updated for more than 10 minutes

if($updatedSecondsAgo > 600 && Nette\Environment::isProduction()){ //price not updated for more than 10 minutes
throw new Exception('Price not updated for more than 10 minutes');
}

$this->payload->currentBuyPrice = 'Buy $'.number_format($this->context->values->get('coinbase', 'buyPrice')->value, 2);
$this->payload->currentSellPrice = 'Sell $'.number_format($this->context->values->get('coinbase', 'sellPrice')->value, 2);
$this->payload->lastPriceCheck = "Price updated $updatedSecondsAgo seconds ago";
Expand Down
15 changes: 11 additions & 4 deletions www/app/presenters/HomepagePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
class HomepagePresenter extends BasePresenter {

public function renderDefault($code = NULL) {

}

public function renderOrders() {
$this->template->btc_balance = number_format($this->context->coinbase->user($this->user->id)->getBalance(), 4);
$this->template->btc_sell_total = number_format($this->context->orders->findSellExposure($this->user->id), 4);
$this->template->btc_available = number_format($this->template->btc_balance - $this->template->btc_sell_total, 4);
$this->template->btc_buy_total = number_format($this->context->orders->findBuyExposure($this->user->id), 4);

}

public function actionOrders() {
Expand All @@ -30,14 +37,14 @@ public function actionNewOrder() {
}
}
}

public function renderAdmin(){
if(!$this->user->isInRole('ADMIN')){
$this->flashMessage('Not authorized', 'error');
$this->redirect($this->home);
}
}

public function actionDisconnectFromCoinbase(){
if($this->user->isLoggedIn()){
$this->context->authenticator->update($this->user->id, Array(
Expand All @@ -46,7 +53,7 @@ public function actionDisconnectFromCoinbase(){
'coinbase_expire_time' => NULL,
));
$this->flashMessage('Coinbase disconnected. The app will not be able to fulfill any orders until you reconnect it.', 'success');
$this->redirect($this->home);
$this->redirect($this->home);
}
}

Expand Down
28 changes: 26 additions & 2 deletions www/app/templates/Homepage/orders.latte
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
{block head}
<link rel="stylesheet" media="screen,projection,tv" href="{$basePath}/css/orders.css" />
{/block}

{block content}
<h1>Orders</h1>

<table class="balances table table-bordered">
<tr>
<td class="title">Coinbase Balance</td>
<td class="amount">{$btc_balance} BTC</td>
</tr>
<tr>
<td class="title">Sell orders</td>
<td class="amount">{$btc_sell_total} BTC</td>
</tr>
<tr>
<td class="title">Available Balance</td>
<td class="amount">{$btc_available} BTC</td>
</tr>
<tr>
<td class="title">Buy orders</td>
<td class="amount">{$btc_buy_total} BTC</td>
</tr>
</table>

<a n:href="newOrder" class="btn btn-success btn-large"><i class="icon-white icon-plus"></i> Place new order</a>

{widget OrdersGrid}

<p>Price on Coinbase is checked about every second against your orders. Last check on {$latestPrice['buy']->updated} PST</p>
<p>Price on Coinbase is checked about every second against your orders. Last check on {$latestPrice['buy']->updated} PST</p>


17 changes: 17 additions & 0 deletions www/www/css/orders.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

table.balances {
width: 450px;
}

td.title {
font-weight: bold;
}

td.amount {
text-align: right;
font-family: 'Courier New', monospace;
}

h1 {
padding-bottom: 5px;
}

0 comments on commit 40c3475

Please sign in to comment.