Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: advanced search using amount range returns no results for multipayment #502

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions app/Models/Composers/MultiPaymentAmountValueRangeComposer.php
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Models\Composers;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;

final class MultiPaymentAmountValueRangeComposer
{
public static function compose(Builder $query, array $parameters): Builder
{
$from = Arr::get($parameters, 'amountFrom');
$to = Arr::get($parameters, 'amountTo');

if (is_null($from) && is_null($to)) {
return $query;
}

$query->where('amount', '=', 0);

$query->whereExists(function (\Illuminate\Database\Query\Builder $query) use ($to, $from): void {
$query->selectRaw('i.id')
->fromRaw("( SELECT id, (jsonb_array_elements(asset -> 'payments') ->> 'amount')::bigint am FROM transactions t WHERE t.id = id ) i")
->whereRaw('i.id = transactions.id')
->groupBy('i.id');

if (! is_null($from) && $from > 0) {
$query->havingRaw('sum(am) >= ?', [$from * 1e8]);
}

if (! is_null($to) && $to > 0) {
$query->havingRaw('sum(am) <= ?', [$to * 1e8]);
}
});

return $query;
}
}
8 changes: 7 additions & 1 deletion app/Services/Search/TransactionSearch.php
Expand Up @@ -6,6 +6,7 @@

use App\Contracts\Search;
use App\Facades\Wallets;
use App\Models\Composers\MultiPaymentAmountValueRangeComposer;
use App\Models\Composers\TimestampRangeComposer;
use App\Models\Composers\ValueRangeComposer;
use App\Models\Scopes\BusinessEntityRegistrationScope;
Expand Down Expand Up @@ -147,7 +148,12 @@ private function applyScopes(Builder $query, array $parameters): void
}
}

ValueRangeComposer::compose($query, $parameters, 'amount');
$query->where(function ($query) use ($parameters): void {
ValueRangeComposer::compose($query, $parameters, 'amount');
$query->orWhere(function ($query) use ($parameters): void {
MultiPaymentAmountValueRangeComposer::compose($query, $parameters);
});
});

ValueRangeComposer::compose($query, $parameters, 'fee');

Expand Down
20 changes: 10 additions & 10 deletions config/explorer.php
Expand Up @@ -12,29 +12,29 @@
'alias' => env('EXPLORER_NETWORK_ALIAS', 'mainnet'),
'currency' => env('EXPLORER_NETWORK_CURRENCY', 'ARK'),
'currencySymbol' => env('EXPLORER_NETWORK_CURRENCY_SYMBOL', 'Ѧ'),
'confirmations' => env('EXPLORER_NETWORK_CONFIRMATIONS', 51),
'confirmations' => intval(env('EXPLORER_NETWORK_CONFIRMATIONS', 51)),
'knownWallets' => env('EXPLORER_NETWORK_KNOWN_WALLETS', 'https://raw.githubusercontent.com/ArkEcosystem/common/master/mainnet/known-wallets-extended.json'),
'canBeExchanged' => env('EXPLORER_NETWORK_CAN_BE_EXCHANGED', true),
'usesMarketsquare' => env('EXPLORER_NETWORK_USES_MARKETSQUARE', false),
'epoch' => env('EXPLORER_NETWORK_EPOCH', '2017-03-21T13:00:00.000Z'),
'delegateCount' => env('EXPLORER_NETWORK_DELEGATE_COUNT', 51),
'blockTime' => env('EXPLORER_NETWORK_BLOCK_TIME', 8),
'blockReward' => env('EXPLORER_NETWORK_BLOCK_REWARD', 2),
'base58Prefix' => env('EXPLORER_NETWORK_BASE58_PREFIX', 23),
'delegateCount' => intval(env('EXPLORER_NETWORK_DELEGATE_COUNT', 51)),
'blockTime' => intval(env('EXPLORER_NETWORK_BLOCK_TIME', 8)),
'blockReward' => intval(env('EXPLORER_NETWORK_BLOCK_REWARD', 2)),
'base58Prefix' => intval(env('EXPLORER_NETWORK_BASE58_PREFIX', 23)),
],
'development' => [
'name' => env('EXPLORER_NETWORK_NAME', 'ARK Development Network'),
'alias' => env('EXPLORER_NETWORK_ALIAS', 'devnet'),
'currency' => env('EXPLORER_NETWORK_CURRENCY', 'DARK'),
'currencySymbol' => env('EXPLORER_NETWORK_CURRENCY_SYMBOL', 'DѦ'),
'confirmations' => env('EXPLORER_NETWORK_CONFIRMATIONS', 51),
'confirmations' => intval(env('EXPLORER_NETWORK_CONFIRMATIONS', 51)),
'canBeExchanged' => env('EXPLORER_NETWORK_CAN_BE_EXCHANGED', false),
'usesMarketsquare' => env('EXPLORER_NETWORK_USES_MARKETSQUARE', false),
'epoch' => env('EXPLORER_NETWORK_EPOCH', '2017-03-21T13:00:00.000Z'),
'delegateCount' => env('EXPLORER_NETWORK_DELEGATE_COUNT', 51),
'blockTime' => env('EXPLORER_NETWORK_BLOCK_TIME', 8),
'blockReward' => env('EXPLORER_NETWORK_BLOCK_REWARD', 2),
'base58Prefix' => env('EXPLORER_NETWORK_BASE58_PREFIX', 30),
'delegateCount' => intval(env('EXPLORER_NETWORK_DELEGATE_COUNT', 51)),
'blockTime' => intval(env('EXPLORER_NETWORK_BLOCK_TIME', 8)),
'blockReward' => intval(env('EXPLORER_NETWORK_BLOCK_REWARD', 2)),
'base58Prefix' => intval(env('EXPLORER_NETWORK_BASE58_PREFIX', 30)),
],
],

Expand Down
48 changes: 48 additions & 0 deletions tests/Unit/Services/Search/TransactionSearchTest.php
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use App\Enums\CoreTransactionTypeEnum;
use App\Enums\TransactionTypeGroupEnum;
use App\Models\Transaction;

use App\Models\Wallet;
Expand Down Expand Up @@ -119,6 +121,52 @@
expect($result->get())->toHaveCount(10);
});

it('should search for multipayment transactions by amount range', function () {
marianogoldman marked this conversation as resolved.
Show resolved Hide resolved
Transaction::factory()->create([
'type_group' => TransactionTypeGroupEnum::CORE,
'type' => CoreTransactionTypeEnum::MULTI_PAYMENT,
'amount' => 0,
'asset' => [
'payments' => [
['amount' => 750 * 1e8, 'recipientId' => 'D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib'],
['amount' => 251 * 1e8, 'recipientId' => 'DFJ5Z51F1euNNdRUQJKQVdG4h495LZkc6T'],
],
],
]);
Transaction::factory()->create(['amount' => 2000 * 1e8]);

$result = (new TransactionSearch())->search([
'transactionType' => 'multiPayment',
'amountFrom' => 900,
'amountTo' => 1100,
]);

expect($result->get())->toHaveCount(1);
});

it('should search for multipayment transactions by amount range with decimals', function () {
Transaction::factory()->create([
'type_group' => TransactionTypeGroupEnum::CORE,
'type' => CoreTransactionTypeEnum::MULTI_PAYMENT,
'amount' => 0,
'asset' => [
'payments' => [
['amount' => 0.45 * 1e8, 'recipientId' => 'D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib'],
['amount' => 0.50 * 1e8, 'recipientId' => 'DFJ5Z51F1euNNdRUQJKQVdG4h495LZkc6T'],
],
],
]);
Transaction::factory()->create(['amount' => 2000 * 1e8]);

$result = (new TransactionSearch())->search([
'transactionType' => 'multiPayment',
'amountFrom' => 0.900,
'amountTo' => 1.100,
]);

expect($result->get())->toHaveCount(1);
});

it('should search for transactions by fee minimum', function () {
Transaction::factory(10)->create(['fee' => 1000 * 1e8]);
Transaction::factory(10)->create(['fee' => 2000 * 1e8]);
Expand Down
Expand Up @@ -22,7 +22,7 @@ public function up()
$table->unsignedBigInteger('amount');
$table->unsignedBigInteger('fee');
$table->binary('vendor_field')->nullable();
$table->json('asset')->nullable();
$table->jsonb('asset')->nullable();
$table->timestamps();
});
}
Expand Down