diff --git a/app/Manager/XBT.php b/app/Manager/XBT.php index f23539a74..ea35bc07f 100644 --- a/app/Manager/XBT.php +++ b/app/Manager/XBT.php @@ -15,7 +15,7 @@ class XBT extends \Gazelle\Base { * @param string $CC Currency Code * @return float current rate, or null if API endpoint cannot be reached or is in error. */ - public function fetchRate(string $CC) { + public function fetchRate(string $CC): ?float { $curl = new \Gazelle\Util\Curl; if ($curl->fetch(sprintf(self::FX_QUOTE_URL, $CC))) { // {"data":{"base":"BTC","currency":"USD","amount":"8165.93"}} @@ -29,16 +29,16 @@ public function fetchRate(string $CC) { * * @param string $CC Currency Code * @param float $rate The current rate (e.g. from fetchRate()) - * @return boolean Success + * @return int, >0 indicates success */ - public function saveRate(string $CC, float $rate) { + public function saveRate(string $CC, float $rate): int { self::$db->prepared_query(' INSERT INTO xbt_forex (cc, rate) VALUES (?, ?) ', $CC, $rate ); - return self::$db->affected_rows() == 1; + return self::$db->affected_rows(); } /* Get the latest Forex rate for this currency @@ -46,7 +46,7 @@ public function saveRate(string $CC, float $rate) { * @param string $CC Currency Code * @return float Current rate, or null on failure */ - public function latestRate(string $CC) { + public function latestRate(string $CC): ?float { $key = sprintf(self::CACHE_KEY, $CC); $rate = self::$cache->get_value($key); if ($rate === false) { @@ -78,8 +78,8 @@ public function latestRate(string $CC) { * @param string $CC Currency Code * @return float Current amount in XBT, or null on failure */ - public function fiat2xbt(float $amount, string $CC) { + public function fiat2xbt(float $amount, string $CC): ?float { $rate = $this->latestRate($CC); - return is_null($rate) ? null : $amount / $rate; + return !$rate ? null : $amount / $rate; } } diff --git a/bin/process-bitcoin-donation b/bin/process-bitcoin-donation new file mode 100755 index 000000000..c3f7bc899 --- /dev/null +++ b/bin/process-bitcoin-donation @@ -0,0 +1,53 @@ +#! /usr/bin/env php +, [
, ...] + * + * bitcoin-cli -rpcwallet=test listtransactions \* 20 | jq 'map([.address, .amount]|join(","))| join("|")' | tr -d \" | tr '|' '\0' | xargs -0 php process-bitcoin-donation + */ + +require_once(__DIR__ . '/../lib/bootstrap.php'); + +$bitcoin = new Gazelle\Donate\Bitcoin; +$userMan = new Gazelle\Manager\User; + +$xbtRate = (new Gazelle\Manager\XBT)->latestRate('EUR'); +if (!$xbtRate) { + echo "No exchange rate for BTC\n"; + exit(1); +} + +array_shift($argv); +foreach ($argv as $paymentData) { + [$address, $value] = explode(',', $paymentData, limit: 2); + $userId = $bitcoin->findUserIdbyAddress($address); + if (is_null($userId)) { + echo "No such address $address\n"; + continue; + } + $user = $userMan->find($userId); + if (is_null($user)) { + echo "No such user $userId\n"; + continue; + } + $value = (float)$value; + if ($value <= 0) { + echo "Bad donation value $value for $address\n"; + continue; + } + + $donor = new Gazelle\User\Donor($user); + $donor->donate( + amount: $value, + xbtRate: $xbtRate, + source: 'BTC donation', + reason: "", + currency: 'XBT' + ); + $bitcoin->invalidate($userId); + $fiatValue = $value * $xbtRate; + echo "added $fiatValue EUR ($value BTC) donation for " . $user->username() . "\n"; +} diff --git a/misc/phpstan-baseline.neon b/misc/phpstan-baseline.neon index e931610cb..cb8aac492 100644 --- a/misc/phpstan-baseline.neon +++ b/misc/phpstan-baseline.neon @@ -1010,26 +1010,6 @@ parameters: count: 1 path: ../app/Manager/UserLink.php - - - message: "#^Method Gazelle\\\\Manager\\\\XBT\\:\\:fetchRate\\(\\) has no return type specified\\.$#" - count: 1 - path: ../app/Manager/XBT.php - - - - message: "#^Method Gazelle\\\\Manager\\\\XBT\\:\\:fiat2xbt\\(\\) has no return type specified\\.$#" - count: 1 - path: ../app/Manager/XBT.php - - - - message: "#^Method Gazelle\\\\Manager\\\\XBT\\:\\:latestRate\\(\\) has no return type specified\\.$#" - count: 1 - path: ../app/Manager/XBT.php - - - - message: "#^Method Gazelle\\\\Manager\\\\XBT\\:\\:saveRate\\(\\) has no return type specified\\.$#" - count: 1 - path: ../app/Manager/XBT.php - - message: "#^Method Gazelle\\\\ReleaseType\\:\\:findExtendedNameById\\(\\) has no return type specified\\.$#" count: 1