|
| 1 | +import pytest |
| 2 | + |
| 3 | +from crypto.enums.contract_addresses import ContractAddresses |
| 4 | +from crypto.transactions.builder.batch_transfer_builder import ( |
| 5 | + BatchTransferBuilder, |
| 6 | +) |
| 7 | + |
| 8 | +RECIPIENT_A = '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22' |
| 9 | +RECIPIENT_B = '0xc3bbe9b1cee1ff85ad72b87414b0e9b7f2366763' |
| 10 | +TOKEN_ADDRESS = '0xdAC17F958D2ee523a2206206994597C13D831ec7' |
| 11 | + |
| 12 | + |
| 13 | +def test_it_should_default_to_the_batch_transfer_contract(): |
| 14 | + builder = BatchTransferBuilder.new() |
| 15 | + |
| 16 | + assert builder.transaction.data['to'] == ContractAddresses.BATCH_TRANSFER.value |
| 17 | + |
| 18 | + |
| 19 | +def test_it_should_sign_it_with_a_passphrase(passphrase, load_transaction_fixture): |
| 20 | + fixture = load_transaction_fixture('transactions/batch-transfer') |
| 21 | + |
| 22 | + builder = ( |
| 23 | + BatchTransferBuilder |
| 24 | + .new() |
| 25 | + .gas_price(fixture['data']['gasPrice']) |
| 26 | + .gas_limit(fixture['data']['gasLimit']) |
| 27 | + .nonce(fixture['data']['nonce']) |
| 28 | + .token_address(TOKEN_ADDRESS) |
| 29 | + .add_recipient(RECIPIENT_A, 100000) |
| 30 | + .add_recipient(RECIPIENT_B, 200000) |
| 31 | + .sign(passphrase) |
| 32 | + ) |
| 33 | + |
| 34 | + assert builder.transaction.data['gasPrice'] == int(fixture['data']['gasPrice']) |
| 35 | + assert builder.transaction.data['gasLimit'] == int(fixture['data']['gasLimit']) |
| 36 | + assert builder.transaction.data['nonce'] == fixture['data']['nonce'] |
| 37 | + assert builder.transaction.data['value'] == 0 |
| 38 | + assert builder.transaction.data['to'] == fixture['data']['to'] |
| 39 | + assert builder.transaction.data['data'] == fixture['data']['data'] |
| 40 | + assert builder.transaction.data['v'] == fixture['data']['v'] |
| 41 | + assert builder.transaction.data['r'] == fixture['data']['r'] |
| 42 | + assert builder.transaction.data['s'] == fixture['data']['s'] |
| 43 | + assert builder.transaction.data['hash'] == fixture['data']['hash'] |
| 44 | + |
| 45 | + assert builder.transaction.serialize().hex() == fixture['serialized'] |
| 46 | + assert builder.verify() |
| 47 | + |
| 48 | + |
| 49 | +def test_it_should_handle_single_recipient(passphrase, load_transaction_fixture): |
| 50 | + fixture = load_transaction_fixture('transactions/batch-transfer') |
| 51 | + |
| 52 | + builder = ( |
| 53 | + BatchTransferBuilder |
| 54 | + .new() |
| 55 | + .gas_price(fixture['data']['gasPrice']) |
| 56 | + .gas_limit(fixture['data']['gasLimit']) |
| 57 | + .nonce(fixture['data']['nonce']) |
| 58 | + .token_address(TOKEN_ADDRESS) |
| 59 | + .add_recipient(RECIPIENT_A, 100000) |
| 60 | + .sign(passphrase) |
| 61 | + ) |
| 62 | + |
| 63 | + assert builder.transaction.data['data'].startswith('4885b254') |
| 64 | + assert builder.transaction.data['value'] == 0 |
| 65 | + assert builder.verify() |
| 66 | + |
| 67 | + |
| 68 | +def test_it_should_encode_large_amounts(passphrase, load_transaction_fixture): |
| 69 | + fixture = load_transaction_fixture('transactions/batch-transfer') |
| 70 | + |
| 71 | + builder = ( |
| 72 | + BatchTransferBuilder |
| 73 | + .new() |
| 74 | + .gas_price(fixture['data']['gasPrice']) |
| 75 | + .gas_limit(fixture['data']['gasLimit']) |
| 76 | + .nonce(fixture['data']['nonce']) |
| 77 | + .token_address(TOKEN_ADDRESS) |
| 78 | + .add_recipient(RECIPIENT_A, 1000000000000000000000) |
| 79 | + .sign(passphrase) |
| 80 | + ) |
| 81 | + |
| 82 | + assert builder.verify() |
| 83 | + |
| 84 | + |
| 85 | +def test_it_should_throw_when_signing_with_no_recipients(passphrase): |
| 86 | + builder = ( |
| 87 | + BatchTransferBuilder |
| 88 | + .new() |
| 89 | + .gas_price('5000000000') |
| 90 | + .gas_limit('21000') |
| 91 | + .nonce('1') |
| 92 | + .token_address(TOKEN_ADDRESS) |
| 93 | + ) |
| 94 | + |
| 95 | + with pytest.raises(Exception, match='Must add at least one recipient before encoding.'): |
| 96 | + builder.sign(passphrase) |
| 97 | + |
| 98 | + |
| 99 | +def test_it_should_throw_when_signing_without_a_token_address(passphrase): |
| 100 | + builder = ( |
| 101 | + BatchTransferBuilder |
| 102 | + .new() |
| 103 | + .gas_price('5000000000') |
| 104 | + .gas_limit('21000') |
| 105 | + .nonce('1') |
| 106 | + .add_recipient(RECIPIENT_A, 100000) |
| 107 | + ) |
| 108 | + |
| 109 | + with pytest.raises(Exception, match='Must set tokenAddress before encoding.'): |
| 110 | + builder.sign(passphrase) |
0 commit comments