Skip to content

Commit

Permalink
Store Chain Wallet Tests + Getters (#119)
Browse files Browse the repository at this point in the history
* Store Chain Wallet Tests + Getters
  • Loading branch information
utxo-one authored Aug 1, 2023
1 parent c6876af commit 81e1b17
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/Client/StoreOnChainWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use BTCPayServer\Result\StoreOnChainWalletFeeRate;
use BTCPayServer\Result\StoreOnChainWalletTransaction;
use BTCPayServer\Result\StoreOnChainWalletTransactionList;
use BTCPayServer\Result\StoreOnChainWalletUTXOList;
use BTCPayServer\Result\StoreOnChainWalletUtxoList;

class StoreOnChainWallet extends AbstractClient
{
Expand Down Expand Up @@ -241,10 +241,10 @@ public function updateStoreOnChainWalletTransaction(
}
}

public function getStoreOnChainWalletUTXOs(
public function getStoreOnChainWalletUtxos(
string $storeId,
string $cryptoCode
): StoreOnChainWalletUTXOList {
): StoreOnChainWalletUtxoList {
$url = $this->getApiUrl() . 'stores/' .
urlencode($storeId) . '/payment-methods' . '/OnChain' . '/' .
urlencode($cryptoCode) . '/wallet' . '/utxos';
Expand All @@ -255,7 +255,7 @@ public function getStoreOnChainWalletUTXOs(
$response = $this->getHttpClient()->request($method, $url, $headers);

if ($response->getStatus() === 200) {
return new StoreOnChainWalletUTXOList(
return new StoreOnChainWalletUtxoList(
json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR)
);
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/Result/StoreOnChainWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ public function getConfirmedBalance(): PreciseNumber
$data = $this->getData();
return PreciseNumber::parseString($data['confirmedBalance']);
}

public function getLabel(): string
{
$data = $this->getData();
return $data['label'];
}
}
6 changes: 4 additions & 2 deletions src/Result/StoreOnChainWalletFeeRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace BTCPayServer\Result;

use BTCPayServer\Util\PreciseNumber;

class StoreOnChainWalletFeeRate extends AbstractResult
{
public function getFeeRate(): float
public function getFeeRate(): PreciseNumber
{
$data = $this->getData();
return $data['feeRate'];
return PreciseNumber::parseFloat($data['feeRate']);
}
}
45 changes: 28 additions & 17 deletions src/Result/StoreOnChainWalletTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,61 @@

namespace BTCPayServer\Result;

use BTCPayServer\Util\PreciseNumber;

class StoreOnChainWalletTransaction extends AbstractResult
{
public function getDestinations(): StoreOnChainWalletTransactionDestinationList
public function getTransactionHash(): string
{
$data = $this->getData();
return $data['transactionHash'];
}

public function getComment(): string
{
$data = $this->getData();
return $data['comment'];
}

public function getAmount(): PreciseNumber
{
$data = $this->getData();
return new StoreOnChainWalletTransactionDestinationList($data['destinations']);
return new PreciseNumber($data['amount']);
}

public function getFeeRate(): StoreOnChainWalletFeeRate
public function getLabels(): array
{
$data = $this->getData();
return new StoreOnChainWalletFeeRate($data['feeRate']);
return $data['labels'];
}

public function proceedWithPayjoin(): bool
public function getBlockHash(): ?string
{
$data = $this->getData();
return $data['proceedWithPayjoin'];
return $data['blockHash'];
}

public function proceedWithBroadcast(): bool
public function getBlockHeight(): ?int
{
$data = $this->getData();
return $data['proceedWithBroadcast'];
return $data['blockHeight'];
}

public function noChange(): bool
public function getConfirmations(): int
{
$data = $this->getData();
return $data['noChange'];
return $data['confirmations'];
}

public function rbf(): bool
public function getTimestamp(): int
{
$data = $this->getData();
return $data['rbf'];
return $data['timestamp'];
}

/**
* @return array strings
*/
public function selectedInputs(): array
public function getStatus(): string
{
$data = $this->getData();
return $data['selectedInputs'];
return $data['status'];
}
}
20 changes: 0 additions & 20 deletions src/Result/StoreOnChainWalletUTXOList.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use BTCPayServer\Util\PreciseNumber;

class StoreOnChainWalletUTXO extends AbstractResult
class StoreOnChainWalletUtxo extends AbstractResult
{
public function getComment(): string
{
Expand Down
20 changes: 20 additions & 0 deletions src/Result/StoreOnChainWalletUtxoList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace BTCPayServer\Result;

class StoreOnChainWalletUtxoList extends AbstractListResult
{
/**
* @return \BTCPayServer\Result\StoreOnChainWalletUtxo[]
*/
public function all(): array
{
$storeWalletUtxos = [];
foreach ($this->getData() as $storeWalletUtxo) {
$storeWalletUtxos[] = new StoreOnChainWalletUtxo($storeWalletUtxo);
}
return $storeWalletUtxos;
}
}
198 changes: 198 additions & 0 deletions tests/StoreOnChainWalletTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

declare(strict_types=1);

namespace BTCPayServer\Tests;

use BTCPayServer\Client\Store;
use BTCPayServer\Client\StoreOnChainWallet;
use BTCPayServer\Result\StoreOnChainWallet as ResultStoreOnChainWallet;
use BTCPayServer\Result\StoreOnChainWalletAddress;
use BTCPayServer\Result\StoreOnChainWalletFeeRate;
use BTCPayServer\Result\StoreOnChainWalletTransaction;
use BTCPayServer\Result\StoreOnChainWalletTransactionList;
use BTCPayServer\Result\StoreOnChainWalletUtxo;
use BTCPayServer\Result\StoreOnChainWalletUtxoList;
use BTCPayServer\Util\PreciseNumber;

final class StoreOnChainWalletTest extends BaseTest
{
public Store $storeClient;
public StoreOnChainWallet $storeOnChainWalletClient;

public function setUp(): void
{
parent::setUp();

$this->storeClient = new Store($this->host, $this->apiKey);
$this->storeOnChainWalletClient = new StoreOnChainWallet($this->host, $this->apiKey);
}

/** @group getStoreOnChainWalletOverview */
public function testItCanGetStoreOnChainWalletOverview(): void
{
//$this->markTestIncomplete('BTC doesnt have any derivation scheme set');
$overview = $this->storeOnChainWalletClient->getStoreOnChainWalletOverview(
$this->storeId,
'BTC'
);

$this->assertInstanceOf(ResultStoreOnChainWallet::class, $overview);
$this->assertInstanceOf(PreciseNumber::class, $overview->getBalance());
$this->assertInstanceOf(PreciseNumber::class, $overview->getUnconfirmedBalance());
$this->assertInstanceOf(PreciseNumber::class, $overview->getConfirmedBalance());
$this->assertIsString($overview->getLabel());
}

/** @group getStoreOnChainWalletFeeRate */
public function testItCanGetStoreOnChainWalletFeeRate(): void
{
$feeRate = $this->storeOnChainWalletClient->getStoreOnChainWalletFeeRate(
$this->storeId,
'BTC'
);

$this->assertInstanceOf(StoreOnChainWalletFeeRate::class, $feeRate);
$this->assertInstanceOf(PreciseNumber::class, $feeRate->getFeeRate());
}

/** @group getStoreOnChainWalletAddress */
public function testItCanGetStoreOnChainWalletAddress(): void
{
$address = $this->storeOnChainWalletClient->getStoreOnChainWalletAddress(
$this->storeId,
'BTC'
);

$this->assertInstanceOf(StoreOnChainWalletAddress::class, $address);
$this->assertIsString($address->getAddress());
$this->assertIsString($address->getKeyPath());
$this->assertIsString($address->getPaymentLink());
}

/** @group unReserveLastStoreOnChainWalletAddress */
public function testItCanunReserveLastStoreOnChainWalletAddress(): void
{
$address = $this->storeOnChainWalletClient->unReserveLastStoreOnChainWalletAddress(
$this->storeId,
'BTC'
);

$this->assertIsBool($address);
}

/** @group getStoreOnChainWalletTransactions */
public function testItCanGetStoreOnChainWalletTransactions(): void
{
$transactions = $this->storeOnChainWalletClient->getStoreOnChainWalletTransactions(
$this->storeId,
'BTC'
);

$this->assertInstanceOf(StoreOnChainWalletTransactionList::class, $transactions);
$this->assertIsArray($transactions->all());

foreach ($transactions->all() as $transaction) {
$this->assertInstanceOf(StoreOnChainWalletTransaction::class, $transaction);
$this->assertIsString($transaction->getTransactionHash());
$this->assertIsString($transaction->getComment());
$this->assertIsArray($transaction->getLabels());
$this->assertIsInt($transaction->getConfirmations());
$this->assertIsInt($transaction->getTimestamp());
$this->assertIsString($transaction->getStatus());
}
}

/** @group createStoreOnChainWalletTransaction */
public function testItCanCreateGetUpdateStoreOnChainWalletTransaction(): void
{
$destination =
[
'destination' => 'tb1q2yy5gxpdlsr40xjvy7v6x4gjxr5y8t428nqppa',
'amount' => "0.00001",
'subtractFromAmount' => true,
];

$transaction = $this->storeOnChainWalletClient->createStoreOnChainWalletTransaction(
$this->storeId,
'BTC',
[$destination],
2.0,
false,
true,
false,
);

$this->assertInstanceOf(StoreOnChainWalletTransaction::class, $transaction);
$this->assertIsString($transaction->getTransactionHash());
$this->assertIsString($transaction->getComment());
$this->assertIsArray($transaction->getLabels());
$this->assertIsInt($transaction->getConfirmations());
$this->assertIsInt($transaction->getTimestamp());
$this->assertIsString($transaction->getStatus());

if ($transaction->getBlockHash() !== null) {
$this->assertIsString($transaction->getBlockHash());
}

if ($transaction->getBlockHeight() !== null) {
$this->assertIsInt($transaction->getBlockHeight());
}

$getTransaction = $this->storeOnChainWalletClient->getStoreOnChainWalletTransaction(
$this->storeId,
'BTC',
$transaction->getTransactionHash(),
);

$this->assertInstanceOf(StoreOnChainWalletTransaction::class, $getTransaction);
$this->assertIsString($getTransaction->getTransactionHash());
$this->assertIsString($getTransaction->getComment());
$this->assertIsArray($getTransaction->getLabels());
$this->assertIsInt($getTransaction->getConfirmations());
$this->assertIsInt($getTransaction->getTimestamp());
$this->assertIsString($getTransaction->getStatus());

$updatedTransaction = $this->storeOnChainWalletClient->updateStoreOnChainWalletTransaction(
$this->storeId,
'BTC',
$transaction->getTransactionHash(),
'test comment',
['test label'],
);

$this->assertInstanceOf(StoreOnChainWalletTransaction::class, $updatedTransaction);
$this->assertIsString($updatedTransaction->getTransactionHash());
$this->assertIsString($updatedTransaction->getComment());
$this->assertIsArray($updatedTransaction->getLabels());
$this->assertIsInt($updatedTransaction->getConfirmations());
$this->assertIsInt($updatedTransaction->getTimestamp());
$this->assertIsString($updatedTransaction->getStatus());

$this->assertEquals('test comment', $updatedTransaction->getComment());
}

/** @group getStoreOnChainWalletUtxos */
public function testItCanGetStoreOnChainWalletUtxos(): void
{
$utxos = $this->storeOnChainWalletClient->getStoreOnChainWalletUtxos(
$this->storeId,
'BTC'
);

$this->assertInstanceOf(StoreOnChainWalletUtxoList::class, $utxos);

foreach($utxos as $utxo) {
$this->assertInstanceOf(StoreOnChainWalletUtxo::class, $utxo);
$this->assertIsString($utxo->getComment());
$this->assertIsString($utxo->getAmount());
$this->assertIsString($utxo->getOutpoint());
$this->assertIsString($utxo->getLink());
$this->assertIsArray($utxo->getLabels());
$this->assertIsInt($utxo->getTimestamp());
$this->assertIsString($utxo->getKeyPath());
$this->assertIsString($utxo->getAddress());
$this->assertIsInt($utxo->getConfirmations());
}
}
}

0 comments on commit 81e1b17

Please sign in to comment.