Skip to content

Commit

Permalink
refactor to use the model factory
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsobries committed Jan 16, 2025
1 parent 80c60e3 commit 1fa5acf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
18 changes: 14 additions & 4 deletions database/factories/TransactionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use App\Models\Transaction;
use App\Models\Wallet;
use App\Services\BigNumber;
use ArkEcosystem\Crypto\Enums\AbiFunction;
use ArkEcosystem\Crypto\Enums\ContractAbiType;
use ArkEcosystem\Crypto\Utils\AbiEncoder;
use Illuminate\Database\Eloquent\Factories\Factory;
use function Tests\faker;

Expand Down Expand Up @@ -77,12 +80,19 @@ public function tokenTransfer(string $address, int $amount): Factory
*/
public function multiPayment(array $recipients, array $amounts): Factory
{
$method = ContractMethod::multiPayment();
$pay = [];

$method .= implode('', array_map(fn (string $recipient) => str_pad(preg_replace('/^0x/', '', $recipient), 64, '0', STR_PAD_LEFT), $recipients));
$method .= implode('', array_map(fn (BigNumber $amount) => str_pad($amount->toHex(), 64, '0', STR_PAD_LEFT), $amounts));
foreach ($recipients as $index => $recipient) {
$pay[0][] = $recipient;
$pay[1][] = $amounts[$index]->__toString();
}

return $this->withPayload($method)
$payload = (new AbiEncoder(ContractAbiType::MULTIPAYMENT))
->encodeFunctionCall(AbiFunction::MULTIPAYMENT->value, $pay);

$payload = preg_replace('/^0x/', '', $payload);

return $this->withPayload($payload)
->state(fn () => [
'recipient_address' => Network::knownContract('multipayment'),
]);
Expand Down
16 changes: 14 additions & 2 deletions tests/Unit/ViewModels/TransactionViewModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\Receipt;
use App\Models\Transaction;
use App\Models\Wallet;
use App\Services\BigNumber;
use App\Services\Cache\CryptoDataCache;
use App\Services\Cache\NetworkCache;
use App\ViewModels\TransactionViewModel;
Expand Down Expand Up @@ -315,8 +316,15 @@

it('should get formatted multi payment receipts', function () {
$transaction = new TransactionViewModel(Transaction::factory()
->withPayload('084ce708000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000b693449adda7efc015d87944eae8b7c37eb1690a000000000000000000000000b693449adda7efc015d87944eae8b7c37eb1690a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000000000000000000000000000000000000bebc200')
->create());
->multiPayment([
'0xb693449AdDa7EFc015D87944EAE8b7C37EB1690A',
'0xb693449AdDa7EFc015D87944EAE8b7C37EB1690A',
'0xEd0C906b8fcCDe71A19322DFfe929c6e04460cFF',
], [
BigNumber::new(100000000),
BigNumber::new(200000000),
BigNumber::new(1234567),
])->create());

expect($transaction->multiPaymentRecipients())->toEqual([
'0' => [
Expand All @@ -327,6 +335,10 @@
'address' => '0xb693449AdDa7EFc015D87944EAE8b7C37EB1690A',
'amount' => '200000000',
],
'2' => [
'address' => '0xEd0C906b8fcCDe71A19322DFfe929c6e04460cFF',
'amount' => '1234567',
],
]);
});

Expand Down

0 comments on commit 1fa5acf

Please sign in to comment.