Skip to content

Commit

Permalink
Add Bitcoin only wallet, extend Daikon money VO, add validateFee serv…
Browse files Browse the repository at this point in the history
…ice endpoint
  • Loading branch information
MrHash committed Aug 23, 2020
1 parent 94c8ca9 commit b89ddc1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 148 deletions.
68 changes: 34 additions & 34 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/Service/BitcoinServiceInterface.php
Expand Up @@ -21,6 +21,8 @@ public function request(BitcoinTransaction $transaction): BitcoinTransaction;

public function send(BitcoinTransaction $transaction): BitcoinTransaction;

public function validateAddress(Address $address): bool;

public function estimateFee(BitcoinTransaction $transaction): Bitcoin;

public function getBlock(Hash $hash): BitcoinBlock;
Expand Down
116 changes: 2 additions & 114 deletions src/ValueObject/Bitcoin.php
Expand Up @@ -11,97 +11,11 @@
use Daikon\Interop\Assertion;
use Daikon\Interop\InvalidArgumentException;
use Daikon\Interop\MakeEmptyInterface;
use Daikon\Money\ValueObject\MoneyInterface;
use Money\Currency;
use Money\Money;
use NGUtech\Bitcoin\Service\BitcoinCurrencies;
use Daikon\Money\ValueObject\Money;
use NGUtech\Bitcoin\Service\SatoshiCurrencies;

final class Bitcoin implements MakeEmptyInterface, MoneyInterface
final class Bitcoin extends Money implements MakeEmptyInterface
{
private Money $money;

/** @param self $comparator */
public function equals($comparator): bool
{
Assertion::isInstanceOf($comparator, self::class);
return $this->toNative() === $comparator->toNative();
}

public function getAmount(): string
{
return $this->money->getAmount();
}

public function getCurrency(): string
{
return $this->money->getCurrency()->getCode();
}

public function multiply($multiplier, int $roundingMode = self::ROUND_HALF_UP): self
{
Assertion::numeric($multiplier, 'Multipler must be numeric.');
$multiplied = $this->money->multiply($multiplier, $roundingMode);
return new self($multiplied);
}

public function divide($divisor, int $roundingMode = self::ROUND_HALF_UP): self
{
Assertion::numeric($divisor, 'Divider must be numeric.');
$divided = $this->money->divide($divisor, $roundingMode);
return new self($divided);
}

public function percentage($percentage, int $roundingMode = self::ROUND_HALF_UP): self
{
return $this->multiply($percentage)->divide(100, $roundingMode);
}

public function add(MoneyInterface $money): self
{
$added = $this->money->add(
self::asBaseMoney($money->getAmount(), $money->getCurrency())
);
return new self($added);
}

public function subtract(MoneyInterface $money): self
{
$subtracted = $this->money->subtract(
self::asBaseMoney($money->getAmount(), $money->getCurrency())
);
return new self($subtracted);
}

public function isZero(): bool
{
return $this->money->isZero();
}

public function isPositive(): bool
{
return $this->money->isPositive();
}

public function isNegative(): bool
{
return $this->money->isNegative();
}

public function isLessThanOrEqual(MoneyInterface $money): bool
{
return $this->money->lessThanOrEqual(
self::asBaseMoney($money->getAmount(), $money->getCurrency())
);
}

public function isGreaterThanOrEqual(MoneyInterface $money): bool
{
return $this->money->greaterThanOrEqual(
self::asBaseMoney($money->getAmount(), $money->getCurrency())
);
}

/** @param string $value */
public static function fromNative($value): self
{
Expand All @@ -128,30 +42,4 @@ public function isEmpty(): bool
{
return $this->isZero();
}

public function toNative(): string
{
return $this->getAmount().$this->getCurrency();
}

public function __toString(): string
{
return $this->toNative();
}

private static function asBaseMoney(string $amount, string $currency): Money
{
return new Money($amount, new Currency($currency));
}

private function __construct(Money $money)
{
Assertion::choice((string)$money->getCurrency(), [
SatoshiCurrencies::MSAT,
SatoshiCurrencies::SAT,
BitcoinCurrencies::BTC,
BitcoinCurrencies::XBT
], 'Invalid currency');
$this->money = $money;
}
}
18 changes: 18 additions & 0 deletions src/ValueObject/BitcoinWallet.php
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);
/**
* This file is part of the ngutech/bitcoin-interop project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NGUtech\Bitcoin\ValueObject;

use Daikon\Money\ValueObject\Wallet;

/**
* @type(NGUtech\Bitcoin\ValueObject\Bitcoin)
*/
final class BitcoinWallet extends Wallet
{
}

0 comments on commit b89ddc1

Please sign in to comment.