From aaff7b79ae5950c8752d50df0b7c259680c09093 Mon Sep 17 00:00:00 2001 From: boston Date: Tue, 2 Jul 2019 02:35:22 +0300 Subject: [PATCH] add fetch-balances, get-last-txs --- README.md | 8 +++++--- examples/cli.php | 22 +++++++++++++++++++++- src/MetaHash.php | 28 ++++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 587a2ff..02ab712 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,11 @@ git clone git@github.com:xboston/php-metahash.git cd php-metahash composer install --no-dev php examples/cli.php method=generate -php examples/cli.php method=fetch-balance net=main address=0x00fa2a5279f8f0fd2f0f9d3280ad70403f01f9d62f52373833 -php examples/cli.php method=fetch-history net=main address=0x00fa2a5279f8f0fd2f0f9d3280ad70403f01f9d62f52373833 -php examples/cli.php method=get-tx net=main hash=bc4a521c1d0d958e2c00e9cdf90a66b15df918cd22e3c408b0f793d913fc7626 +php examples/cli.php method=fetch-balance address=0x00fa2a5279f8f0fd2f0f9d3280ad70403f01f9d62f52373833 +php examples/cli.php method=fetch-balances address=0x00fa2a5279f8f0fd2f0f9d3280ad70403f01f9d62f52373833,0x0039f42ad734606d250ea0b0151d4aeab6b4edc6587c4b27ef +php examples/cli.php method=fetch-history address=0x00fa2a5279f8f0fd2f0f9d3280ad70403f01f9d62f52373833 +php examples/cli.php method=get-tx hash=bc4a521c1d0d958e2c00e9cdf90a66b15df918cd22e3c408b0f793d913fc7626 +php examples/cli.php method=get-last-txs ``` ### Server mode examples diff --git a/examples/cli.php b/examples/cli.php index 9e03a5b..a6ea56e 100644 --- a/examples/cli.php +++ b/examples/cli.php @@ -10,7 +10,7 @@ \parse_str(\strtolower(\implode('&', \array_slice($argv, 1))), $args); $args['method'] = isset($args['method']) && ! empty($args['method']) ? $args['method'] : null; - $args['net'] = isset($args['net']) && ! empty($args['net']) ? $args['net'] : 'dev'; + $args['net'] = isset($args['net']) && ! empty($args['net']) ? $args['net'] : 'main'; $args['address'] = isset($args['address']) && ! empty($args['address']) ? $args['address'] : null; $args['hash'] = isset($args['hash']) && ! empty($args['hash']) ? $args['hash'] : null; @@ -40,6 +40,22 @@ echo \json_encode($metaHash->fetchBalance((string)$args['address']), JSON_PRETTY_PRINT); break; + case 'fetch-balances': + if (empty($args['address'])) { + throw new \RuntimeException('address is empty', 1); + } + + $addresess = \explode(',', $args['address']); + + \array_walk($addresess, static function ($address) use ($metaHash) { + if ($metaHash->checkAddress((string)$address) === false) { + throw new \RuntimeException('invalid address value '.$address, 1); + } + }); + + echo \json_encode($metaHash->fetchBalances($addresess), JSON_PRETTY_PRINT); + break; + case 'fetch-history': if (empty($args['address'])) { throw new \RuntimeException('address is empty', 1); @@ -60,6 +76,10 @@ echo \json_encode($metaHash->getTx((string)$args['hash']), JSON_PRETTY_PRINT); break; + case 'get-last-txs': + echo \json_encode($metaHash->getLastTxs(), JSON_PRETTY_PRINT); + break; + default: throw new \RuntimeException('unknown method'); break; diff --git a/src/MetaHash.php b/src/MetaHash.php index e55f587..d826fa2 100644 --- a/src/MetaHash.php +++ b/src/MetaHash.php @@ -177,11 +177,11 @@ public function getConnectionAddress(string $nodeName) switch ($nodeName) { case self::NODE_PROXY: - $nodeUrl = \sprintf($this->proxy['url'], $this->network); + $nodeUrl = \sprintf($this->proxy['url'], $this->getNetwork()); $nodePort = $this->proxy['port']; break; case self::NODE_TORRENT: - $nodeUrl = \sprintf($this->torrent['url'], $this->network); + $nodeUrl = \sprintf($this->torrent['url'], $this->getNetwork()); $nodePort = $this->torrent['port']; break; default: @@ -307,6 +307,30 @@ public function fetchBalance(string $address): array return $this->queryTorrent('fetch-balance', ['address' => $address]); } + /** + * @see https://github.com/xboston/metahash-api#fetch-balances + * + * @param array $addresses + * + * @return array + * @throws GuzzleException + */ + public function fetchBalances(array $addresses): array + { + return $this->queryTorrent('fetch-balances', ['addresses' => $addresses]); + } + + /** + * @see https://github.com/xboston/metahash-api#get-last-txs + * + * @return array + * @throws GuzzleException + */ + public function getLastTxs(): array + { + return $this->queryTorrent('get-last-txs'); + } + /** * Signature data *