Skip to content

Commit

Permalink
fixed getting unconfirmed transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekliptor authored and Ekliptor committed Sep 10, 2020
1 parent 6a89aa7 commit 142c47e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
49 changes: 47 additions & 2 deletions src/BlockchainApi/BchdProtoGatewayApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ protected function __construct(string $blockchainApiUrl = '') {

public function getConfirmationCount(string $transactionID): int {
$txDetails = $this->getTransactionDetails($transactionID);
if (!$txDetails || !isset($txDetails->transaction) || !isset($txDetails->transaction->confirmations))
if (!$txDetails || !isset($txDetails->transaction))
return -1; // not found
if (!isset($txDetails->transaction->confirmations))
return 0;
return (int)$txDetails->transaction->confirmations;
}

Expand Down Expand Up @@ -107,6 +109,7 @@ public function getAddressDetails(string $address): ?BchAddress {
$bchAddress->balanceSat += (int)$output->value;
}
$bchAddress->balance = CashP::fromSatoshis($bchAddress->balanceSat);
$bchAddress->transactions = $this->getAddressTransactions($address);
return $bchAddress;
}

Expand Down Expand Up @@ -140,9 +143,15 @@ public function getSlpAddressDetails(string $address, string $tokenID): ?SlpToke
return $slpAddress;

foreach ($jsonRes->outputs as $output) {
$slpAddress->balanceSat += (int)$output->value;
if (!isset($output->slp_token))
continue;
$curTokenID = bin2hex(base64_decode($output->slp_token->token_id));
if ($curTokenID !== $tokenID)
continue;
$slpAddress->balanceSat += (int)$output->slp_token->amount;
}
$slpAddress->balance = CashP::fromSatoshis($slpAddress->balanceSat);
$slpAddress->transactions = $this->getAddressTransactions($address);

return $slpAddress;
}
Expand All @@ -167,6 +176,42 @@ protected function getTransactionDetails(string $transactionID): ?\stdClass {
return $jsonRes;
}

protected function getAddressTransactions(string $address, $confirmedOnly = false): array {
$transactions = array();
$url = $this->blockchainApiUrl . 'GetAddressTransactions';
$data = array(
'address' => $address,
'nb_skip' => 0,
'nb_fetch' => 0,
//'hash' => '',
'height' => 0
);
$response = $this->httpAgent->post($url, $data);
if ($response === false)
return $transactions;
$jsonRes = json_decode($response);
if ($jsonRes === null)
return $transactions;
if (isset($jsonRes->confirmed_transactions)) {
foreach ($jsonRes->confirmed_transactions as $tx) {
if (!isset($tx->hash))
continue;
$txHex = CashP::reverseBytes(bin2hex(base64_decode($tx->hash)));
$transactions[] = $txHex;
}
}
if ($confirmedOnly === false && isset($jsonRes->unconfirmed_transactions)) {
foreach ($jsonRes->unconfirmed_transactions as $tx) {
if (!isset($tx->transaction) || !isset($tx->transaction->hash))
continue;
$txHex = CashP::reverseBytes(bin2hex(base64_decode($tx->transaction->hash)));
$transactions[] = $txHex;
}
}

return $transactions;
}

protected function addTokenMetadata(SlpToken $token, array $bchdTokenMetadata): void {
foreach ($bchdTokenMetadata as $meta) {
$id = bin2hex(base64_decode($meta->token_id));
Expand Down
4 changes: 3 additions & 1 deletion tests/BchdBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function testGetSlpAddressDetails(): void {
$tokenID = '0be40e351ea9249b536ec3d1acd4e082e860ca02ec262777259ffe870d3b5cc3';
$address = $cashp->getBlockchain()->getSlpAddressDetails('simpleledger:qz7j7805n9yjdccpz00gq7d70k3h3nef9y75245epu', $tokenID);
$this->assertEquals($tokenID, $address->id, "SLP token ID must be: $tokenID");
$this->assertNotEmpty($address->transactions, 'SLP address has no transactions');
}

public function testAddressCreation(): void {
Expand All @@ -71,7 +72,8 @@ public function testAddressCreation(): void {
protected function getCashpForTesting(): CashP {
$opts = new CashpOptions();
$opts->blockchainApiImplementation = 'BchdProtoGatewayApi';
$opts->httpAgent = new BasicHttpAgent();
//$opts->httpAgent = new BasicHttpAgent();
$opts->httpAgent = new CurlHttpAgent();
return new CashP($opts);
}
}
Expand Down

0 comments on commit 142c47e

Please sign in to comment.