From 31bddb6394e6d3716a652b9a16ff90714d656b4d Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Tue, 8 Aug 2017 11:44:28 +0100 Subject: [PATCH] added address balance API endpoint --- config/routes.php | 1 + src/Controller/MainController.php | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/config/routes.php b/config/routes.php index 4b74090..024f47e 100644 --- a/config/routes.php +++ b/config/routes.php @@ -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/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/charts/blocksize/:period', ['controller' => 'Main', 'action' => 'apiblocksize'], ['period' => '[012346789dhy]+', 'pass' => ['period']]); diff --git a/src/Controller/MainController.php b/src/Controller/MainController.php index 77aced5..52af76f 100644 --- a/src/Controller/MainController.php +++ b/src/Controller/MainController.php @@ -723,7 +723,7 @@ class MainController extends AppController { public function apiaddrtag($base58address = null) { $this->autoRender = false; 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')) { return $this->_jsonError('Invalid HTTP request method.', 400); @@ -783,6 +783,25 @@ class MainController extends AppController { 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) { $this->autoRender = false; $this->loadModel('Addresses');