Skip to content

Commit

Permalink
Merge branch 'release/v0.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
leeovery committed Jan 16, 2022
2 parents ee6c3a2 + 41d6b81 commit 1f4e03d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ final class ConversionTransactionDataMapper implements TransactionDataMapper
{
public function execute(Transaction $transaction): Transaction
{
// TODO: Need data...
return $transaction;
$txJson = json_encode($transaction->getRawData());
abort(404,
"You have discovered a transaction type that we have no data for.\n\nPlease raise a new issue here (https://github.com/leeovery/cryptocredible/issues) and paste in the following message.\n\nPlease add the `conversion` transaction type for the CoinbasePro exchange.\n\nTransaction dump: {$txJson}"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ final class RebateTransactionDataMapper implements TransactionDataMapper
{
public function execute(Transaction $transaction): Transaction
{
// TODO: Need data...
return $transaction;
$txJson = json_encode($transaction->getRawData());
abort(404,
"You have discovered a transaction type that we have no data for.\n\nPlease raise a new issue here (https://github.com/leeovery/cryptocredible/issues) and paste in the following message.\n\nPlease add the `rebate` transaction type for the CoinbasePro exchange.\n\nTransaction dump: {$txJson}"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ public function execute(Transaction $transaction): Transaction
$transaction->getRaw('amount'),
$transaction->getRaw('currency')
))
->setNotes('Funds moved to Coinbase from Coinbase Pro');
->when(is_fiat($transaction->getRaw('currency')), function (Transaction $transaction) {
return $transaction->setNotes('To Coinbase');
});
} else {
$transaction
->setType(TransactionType::Deposit())
->setBuyAmount(new Amount(
$transaction->getRaw('amount'),
$transaction->getRaw('currency')
))
->setNotes('Funds moved from Coinbase to Coinbase Pro');
->when(is_fiat($transaction->getRaw('currency')), function (Transaction $transaction) {
return $transaction->setNotes('From Coinbase');
});
}

return $transaction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@

use App\Contracts\TransactionDataMapper;
use App\Contracts\TransactionProcessor;
use App\Exchanges\CoinbasePro\DataMappers\ConversionTransactionDataMapper;
use App\Exchanges\CoinbasePro\DataMappers\FeeTransactionDataMapper;
use App\Exchanges\CoinbasePro\DataMappers\MatchTransactionDataMapper;
use App\Exchanges\CoinbasePro\DataMappers\RebateTransactionDataMapper;
use App\Exchanges\CoinbasePro\DataMappers\TransferTransactionDataMapper;
use App\ValueObjects\Transaction;
use Illuminate\Support\Collection;

class MapRawDataToTransactionProcessor implements TransactionProcessor
{
private array $dataMappers = [
'transfer' => TransferTransactionDataMapper::class,
'match' => MatchTransactionDataMapper::class,
'fee' => FeeTransactionDataMapper::class,
// 'rebate' => RebateTransactionDataMapper::class,
// 'conversion' => ConversionTransactionDataMapper::class,
'transfer' => TransferTransactionDataMapper::class,
'match' => MatchTransactionDataMapper::class,
'fee' => FeeTransactionDataMapper::class,
'rebate' => RebateTransactionDataMapper::class,
'conversion' => ConversionTransactionDataMapper::class,
];

public function handle(Collection $transactions, callable $next): Collection
{
return $next(
$transactions->map(
fn (array $transaction) => $this
fn(array $transaction) => $this
->getMapperByType(data_get($transaction, 'type'))
->execute(new Transaction($transaction))
)
Expand Down
15 changes: 8 additions & 7 deletions app/ValueObjects/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use App\Enums\TransactionType;
use Carbon\CarbonImmutable;
use Illuminate\Support\Traits\Conditionable;

class Transaction
{
use Conditionable;

public TransactionType $type;

public ?Amount $buyAmount = null;
Expand Down Expand Up @@ -61,6 +64,11 @@ public function getRaw($key = null, $default = null): mixed
return data_get($this->rawData, $key, $default);
}

public function getRawData(): array
{
return $this->rawData;
}

public function setType(TransactionType $type): static
{
$this->type = $type;
Expand Down Expand Up @@ -109,11 +117,4 @@ public function setNotes(string $notes): static

return $this;
}

public function setRawData(array $array): static
{
$this->notes = $notes;

return $this;
}
}
15 changes: 11 additions & 4 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function str(string $value): Illuminate\Support\Stringable
function is_negative(int|float|string $number): bool
{
return filter_var(
$number,
FILTER_VALIDATE_INT | FILTER_VALIDATE_FLOAT,
['options' => ['min_range' => 0]]
) === false;
$number,
FILTER_VALIDATE_INT | FILTER_VALIDATE_FLOAT,
['options' => ['min_range' => 0]]
) === false;
}
}

Expand All @@ -26,3 +26,10 @@ function is_positive(int|float|string $number): bool
return ! is_negative($number);
}
}

if (! function_exists('is_fiat')) {
function is_fiat(string $currency): bool
{
return in_array((string) \str($currency)->upper()->trim(), config('app.fiat_currencies'));
}
}
11 changes: 11 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@

'name' => 'Cryptocredible',

'fiat_currencies' => [
'ALL', 'AFN', 'ARS', 'AWG', 'AUD', 'AZN', 'BSD', 'BBD', 'BDT', 'BYR', 'BZD', 'BMD', 'BOB', 'BAM', 'BWP', 'BGN',
'BRL', 'BND', 'KHR', 'CAD', 'KYD', 'CLP', 'CNY', 'COP', 'CRC', 'HRK', 'CUP', 'CZK', 'DKK', 'DOP', 'XCD', 'EGP',
'SVC', 'EEK', 'EUR', 'FKP', 'FJD', 'GHC', 'GIP', 'GTQ', 'GGP', 'GYD', 'HNL', 'HKD', 'HUF', 'ISK', 'INR', 'IDR',
'IRR', 'IMP', 'ILS', 'JMD', 'JPY', 'JEP', 'KZT', 'KPW', 'KRW', 'KGS', 'LAK', 'LVL', 'LBP', 'LRD', 'LTL', 'MKD',
'MYR', 'MUR', 'MXN', 'MNT', 'MZN', 'NAD', 'NPR', 'ANG', 'NZD', 'NIO', 'NGN', 'NOK', 'OMR', 'PKR', 'PAB', 'PYG',
'PEN', 'PHP', 'PLN', 'QAR', 'RON', 'RUB', 'SHP', 'SAR', 'RSD', 'SCR', 'SGD', 'SBD', 'SOS', 'ZAR', 'LKR', 'SEK',
'CHF', 'SRD', 'SYP', 'TWD', 'THB', 'TTD', 'TRY', 'TRL', 'TVD', 'UAH', 'GBP', 'USD', 'UYU', 'UZS', 'VEF', 'VND',
'YER', 'ZWD',
],

/*
|--------------------------------------------------------------------------
| Application Version
Expand Down

0 comments on commit 1f4e03d

Please sign in to comment.