|
| 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