added address balance API endpoint

This commit is contained in:
Akinwale Ariwodola 2017-08-08 11:44:28 +01:00
parent f3454d78a2
commit 31bddb6394
2 changed files with 21 additions and 1 deletions

View file

@ -56,6 +56,7 @@ Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/api/v1/address/:addr/tag', ['controller' => 'Main', 'action' => 'apiaddrtag'], ['addr' => '[A-Za-z0-9,]+', 'pass' => ['addr']]); $routes->connect('/api/v1/address/:addr/tag', ['controller' => 'Main', 'action' => 'apiaddrtag'], ['addr' => '[A-Za-z0-9,]+', 'pass' => ['addr']]);
$routes->connect('/api/v1/address/:addr/utxo', ['controller' => 'Main', 'action' => 'apiaddrutxo'], ['addr' => '[A-Za-z0-9,]+', 'pass' => ['addr']]); $routes->connect('/api/v1/address/:addr/utxo', ['controller' => 'Main', 'action' => 'apiaddrutxo'], ['addr' => '[A-Za-z0-9,]+', 'pass' => ['addr']]);
$routes->connect('/api/v1/address/:addr/balance', ['controller' => 'Main', 'action' => 'apiaddrbalance'], ['addr' => '[A-Za-z0-9]+', 'pass' => ['addr']]);
$routes->connect('/api/v1/address/:addr/transactions', ['controller' => 'Main', 'action' => 'apiaddrtx'], ['addr' => '[A-Za-z0-9,]+', 'pass' => ['addr']]); $routes->connect('/api/v1/address/:addr/transactions', ['controller' => 'Main', 'action' => 'apiaddrtx'], ['addr' => '[A-Za-z0-9,]+', 'pass' => ['addr']]);
$routes->connect('/api/v1/charts/blocksize/:period', ['controller' => 'Main', 'action' => 'apiblocksize'], ['period' => '[012346789dhy]+', 'pass' => ['period']]); $routes->connect('/api/v1/charts/blocksize/:period', ['controller' => 'Main', 'action' => 'apiblocksize'], ['period' => '[012346789dhy]+', 'pass' => ['period']]);

View file

@ -723,7 +723,7 @@ class MainController extends AppController {
public function apiaddrtag($base58address = null) { public function apiaddrtag($base58address = null) {
$this->autoRender = false; $this->autoRender = false;
if (!isset($base58address) || strlen(trim($base58address)) !== 34) { if (!isset($base58address) || strlen(trim($base58address)) !== 34) {
return $this->_jsonError('Invalid base58 address not specified.', 400); return $this->_jsonError('Invalid base58 address specified.', 400);
} }
if (!$this->request->is('post')) { if (!$this->request->is('post')) {
return $this->_jsonError('Invalid HTTP request method.', 400); return $this->_jsonError('Invalid HTTP request method.', 400);
@ -783,6 +783,25 @@ class MainController extends AppController {
return $this->_jsonResponse(['success' => true, 'tag' => $entity->Tag]); return $this->_jsonResponse(['success' => true, 'tag' => $entity->Tag]);
} }
public function apiaddrbalance($base58address = null) {
$this->autoRender = false;
$this->loadModel('Addresses');
if (!isset($base58address)) {
return $this->_jsonError('Base58 address not specified.', 400);
}
// TODO: Add unconfirmed_balance to response
$result = $this->Addresses->find()->select(['Balance'])->where(['Address' => $base58address])->first();
if (!$result) {
// Return 0 for address that does not exist?
$result = new \stdClass();
$result->Balance = 0;
}
return $this->_jsonResponse(['success' => true, ['balance' => ['confirmed' => $result->Balance, 'unconfirmed' => 0]]]);
}
public function apiaddrutxo($base58address = null) { public function apiaddrutxo($base58address = null) {
$this->autoRender = false; $this->autoRender = false;
$this->loadModel('Addresses'); $this->loadModel('Addresses');