Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Enums/AbiFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum AbiFunction: string
case USERNAME_RESIGNATION = 'resignUsername';
case MULTIPAYMENT = 'pay';
case TRANSFER = 'transfer';
case APPROVE = 'approve';

public function transactionClass(): string
{
Expand All @@ -35,6 +36,7 @@ public function transactionClass(): string
self::USERNAME_RESIGNATION => UsernameResignation::class,
self::MULTIPAYMENT => Multipayment::class,
self::TRANSFER => EvmCall::class,
self::APPROVE => EvmCall::class,
};
}
}
38 changes: 38 additions & 0 deletions src/Transactions/Builder/TokenApproveBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace ArkEcosystem\Crypto\Transactions\Builder;

use ArkEcosystem\Crypto\Enums\AbiFunction;
use ArkEcosystem\Crypto\Enums\ContractAbiType;
use ArkEcosystem\Crypto\Helpers;
use ArkEcosystem\Crypto\Transactions\Types\AbstractTransaction;
use ArkEcosystem\Crypto\Transactions\Types\EvmCall;
use ArkEcosystem\Crypto\Utils\AbiEncoder;
use Brick\Math\BigDecimal;

class TokenApproveBuilder extends AbstractTransactionBuilder
{
public function contractAddress(string $address): self
{
return $this->to($address);
}

public function spender(string $address, BigDecimal $amount): self
{
$payload = (new AbiEncoder(ContractAbiType::TOKEN))->encodeFunctionCall(
AbiFunction::APPROVE->value,
[$address, $amount]
);

$this->transaction->data['data'] = Helpers::removeLeadingHexZero($payload);

return $this;
}

protected function getTransactionInstance(array $data): AbstractTransaction
{
return new EvmCall($data);
}
}
37 changes: 37 additions & 0 deletions tests/Unit/Transactions/Builder/TokenApproveBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

use ArkEcosystem\Crypto\Transactions\Builder\TokenApproveBuilder;
use ArkEcosystem\Crypto\Utils\UnitConverter;
use Brick\Math\BigDecimal;

it('should build a token approve payload', function () {
$contractAddress = '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22';
$spender = '0xA5cc0BfEB09742C5e4C610f2EBaaB82Eb142Ca10';
$amount = BigDecimal::of('1000000000000');
$expectedPayload = '095ea7b3000000000000000000000000a5cc0bfeb09742c5e4c610f2ebaab82eb142ca10000000000000000000000000000000000000000000000000000000e8d4a51000';

$builder = TokenApproveBuilder::new()
->contractAddress($contractAddress)
->spender($spender, $amount);

expect($builder->transaction->data['to'])->toBe($contractAddress);
expect($builder->transaction->data['value']->isZero())->toBeTrue();
expect($builder->transaction->data['data'])->toBe($expectedPayload);
});

it('should sign and verify a token approve', function () {
$builder = TokenApproveBuilder::new()
->contractAddress('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
->spender('0xA5cc0BfEB09742C5e4C610f2EBaaB82Eb142Ca10', BigDecimal::of('1000000000000'))
->gasPrice(UnitConverter::parseUnits('5000000000', 'wei'))
->gasLimit(UnitConverter::parseUnits('21000', 'wei'))
->nonce('1')
->sign($this->passphrase);

expect($builder->verify())->toBeTrue();
expect($builder->transaction->data['data'])->toStartWith('095ea7b3');
expect($builder->transaction->data['to'])->toBe('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22');
expect($builder->transaction->data['value']->isZero())->toBeTrue();
});
Loading