@ -37,4 +37,144 @@ defmodule Explorer.Chain.TransactionTest do
assert % Changeset { valid? : true } = Transaction . changeset ( % Transaction { } , changeset_params )
end
end
describe " transactions_with_token_transfers/2 " do
test " returns the transaction when there is token transfer from the given address " do
address = insert ( :address )
token = insert ( :token )
transaction =
:transaction
|> insert ( )
|> with_block ( )
insert (
:token_transfer ,
from_address : address ,
transaction : transaction ,
token_contract_address : token . contract_address
)
result =
address . hash
|> Transaction . transactions_with_token_transfers ( token . contract_address_hash )
|> Repo . all ( )
|> Enum . map ( & &1 . hash )
assert result == [ transaction . hash ]
end
test " returns the transaction when there is token transfer to the given address " do
address = insert ( :address )
token = insert ( :token )
transaction =
:transaction
|> insert ( )
|> with_block ( )
insert (
:token_transfer ,
to_address : address ,
transaction : transaction ,
token_contract_address : token . contract_address
)
result =
address . hash
|> Transaction . transactions_with_token_transfers ( token . contract_address_hash )
|> Repo . all ( )
|> Enum . map ( & &1 . hash )
assert result == [ transaction . hash ]
end
test " returns only transactions that have token transfers from the given token hash " do
address = insert ( :address )
token = insert ( :token )
transaction =
:transaction
|> insert ( )
|> with_block ( )
:transaction
|> insert ( )
|> with_block ( )
insert (
:token_transfer ,
to_address : address ,
transaction : transaction ,
token_contract_address : token . contract_address
)
insert (
:token_transfer ,
to_address : address ,
transaction : transaction ,
token_contract_address : insert ( :token ) . contract_address
)
result =
address . hash
|> Transaction . transactions_with_token_transfers ( token . contract_address_hash )
|> Repo . all ( )
|> Enum . map ( & &1 . hash )
assert result == [ transaction . hash ]
end
test " order the results DESC by block_number " do
address = insert ( :address )
token = insert ( :token )
transaction_a =
:transaction
|> insert ( )
|> with_block ( insert ( :block , number : 1000 ) )
transaction_b =
:transaction
|> insert ( )
|> with_block ( insert ( :block , number : 1002 ) )
transaction_c =
:transaction
|> insert ( )
|> with_block ( insert ( :block , number : 1003 ) )
insert (
:token_transfer ,
amount : 2 ,
to_address : address ,
token_contract_address : token . contract_address ,
transaction : transaction_a
)
insert (
:token_transfer ,
amount : 1 ,
to_address : address ,
token_contract_address : token . contract_address ,
transaction : transaction_b
)
insert (
:token_transfer ,
amount : 1 ,
to_address : address ,
token_contract_address : token . contract_address ,
transaction : transaction_c
)
result =
address . hash
|> Transaction . transactions_with_token_transfers ( token . contract_address_hash )
|> Repo . all ( )
|> Enum . map ( & &1 . block_number )
assert result == [ transaction_c . block_number , transaction_b . block_number , transaction_a . block_number ]
end
end
end