Skip to content

Commit 5435efb

Browse files
authored
feat: add token approve builder (#192)
1 parent d88de2a commit 5435efb

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

src/Enums/AbiFunction.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum AbiFunction: string
2323
case USERNAME_RESIGNATION = 'resignUsername';
2424
case MULTIPAYMENT = 'pay';
2525
case TRANSFER = 'transfer';
26+
case APPROVE = 'approve';
2627

2728
public function transactionClass(): string
2829
{
@@ -35,6 +36,7 @@ public function transactionClass(): string
3536
self::USERNAME_RESIGNATION => UsernameResignation::class,
3637
self::MULTIPAYMENT => Multipayment::class,
3738
self::TRANSFER => EvmCall::class,
39+
self::APPROVE => EvmCall::class,
3840
};
3941
}
4042
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArkEcosystem\Crypto\Transactions\Builder;
6+
7+
use ArkEcosystem\Crypto\Enums\AbiFunction;
8+
use ArkEcosystem\Crypto\Enums\ContractAbiType;
9+
use ArkEcosystem\Crypto\Helpers;
10+
use ArkEcosystem\Crypto\Transactions\Types\AbstractTransaction;
11+
use ArkEcosystem\Crypto\Transactions\Types\EvmCall;
12+
use ArkEcosystem\Crypto\Utils\AbiEncoder;
13+
use Brick\Math\BigDecimal;
14+
15+
class TokenApproveBuilder extends AbstractTransactionBuilder
16+
{
17+
public function contractAddress(string $address): self
18+
{
19+
return $this->to($address);
20+
}
21+
22+
public function spender(string $address, BigDecimal $amount): self
23+
{
24+
$payload = (new AbiEncoder(ContractAbiType::TOKEN))->encodeFunctionCall(
25+
AbiFunction::APPROVE->value,
26+
[$address, $amount]
27+
);
28+
29+
$this->transaction->data['data'] = Helpers::removeLeadingHexZero($payload);
30+
31+
return $this;
32+
}
33+
34+
protected function getTransactionInstance(array $data): AbstractTransaction
35+
{
36+
return new EvmCall($data);
37+
}
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use ArkEcosystem\Crypto\Transactions\Builder\TokenApproveBuilder;
6+
use ArkEcosystem\Crypto\Utils\UnitConverter;
7+
use Brick\Math\BigDecimal;
8+
9+
it('should build a token approve payload', function () {
10+
$contractAddress = '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22';
11+
$spender = '0xA5cc0BfEB09742C5e4C610f2EBaaB82Eb142Ca10';
12+
$amount = BigDecimal::of('1000000000000');
13+
$expectedPayload = '095ea7b3000000000000000000000000a5cc0bfeb09742c5e4c610f2ebaab82eb142ca10000000000000000000000000000000000000000000000000000000e8d4a51000';
14+
15+
$builder = TokenApproveBuilder::new()
16+
->contractAddress($contractAddress)
17+
->spender($spender, $amount);
18+
19+
expect($builder->transaction->data['to'])->toBe($contractAddress);
20+
expect($builder->transaction->data['value']->isZero())->toBeTrue();
21+
expect($builder->transaction->data['data'])->toBe($expectedPayload);
22+
});
23+
24+
it('should sign and verify a token approve', function () {
25+
$builder = TokenApproveBuilder::new()
26+
->contractAddress('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
27+
->spender('0xA5cc0BfEB09742C5e4C610f2EBaaB82Eb142Ca10', BigDecimal::of('1000000000000'))
28+
->gasPrice(UnitConverter::parseUnits('5000000000', 'wei'))
29+
->gasLimit(UnitConverter::parseUnits('21000', 'wei'))
30+
->nonce('1')
31+
->sign($this->passphrase);
32+
33+
expect($builder->verify())->toBeTrue();
34+
expect($builder->transaction->data['data'])->toStartWith('095ea7b3');
35+
expect($builder->transaction->data['to'])->toBe('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22');
36+
expect($builder->transaction->data['value']->isZero())->toBeTrue();
37+
});

0 commit comments

Comments
 (0)