Blockchain-Ethereum-ABI
view release on metacpan or search on metacpan
lib/Blockchain/Ethereum/ABI/Decoder.pm view on Meta::CPAN
Decodes appended signatures
Usage:
decode() -> []
=over 4
=back
Returns an array reference containing all decoded values
=head1 AUTHOR
Reginaldo Costa <refeco@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2022 by REFECO.
This is free software, licensed under:
];
$encoder->function('foo')->append('address[]' => $addresses);
my $encoded = $encoder->encode;
my $encoded_expected =
'0x13cb49d4000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
is $encoded, $encoded_expected;
my $decoder = Blockchain::Ethereum::ABI::Decoder->new;
my $decoded = $decoder->append('address[]')->decode(substr $encoded_expected, 10);
is_deeply $decoded->[0], $addresses;
};
done_testing;
use strict;
use warnings;
use Test::More;
use Blockchain::Ethereum::ABI::Decoder;
my $decoder = Blockchain::Ethereum::ABI::Decoder->new();
subtest "Int" => sub {
my $data = "0x0000000000000000000000000000000000000000000000000858898f93629000";
my $decoded = $decoder->append('uint256')->decode($data)->[0];
is $decoded->bstr + 0, 601381800000000000;
};
subtest "String" => sub {
my $data =
"0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000063496620796f75206472696e6b206d7563682066726f6d206120626f74746c65206d61726b65642027706f69736f6e2720697420697320636...
my $decoded = $decoder->append('string')->decode($data)->[0];
is $decoded, "If you drink much from a bottle marked 'poison' it is certain to disagree with you sooner or later.";
};
subtest "Bytes" => sub {
my $data =
"0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000063496620796f75206472696e6b206d7563682066726f6d206120626f74746c65206d61726b65642027706f69736f6e2720697420697320636...
my $decoded = $decoder->append('bytes')->decode($data)->[0];
is $decoded,
"0x496620796f75206472696e6b206d7563682066726f6d206120626f74746c65206d61726b65642027706f69736f6e27206974206973206365727461696e20746f206469736167726565207769746820796f7520736f6f6e6572206f72206c617465722e";
};
subtest "Address" => sub {
my $data = "0x000000000000000000000000541662633a0097e3b429685505cd4d233dfe9167";
my $decoded = $decoder->append('address')->decode($data)->[0];
is $decoded, "0x541662633a0097e3b429685505cd4d233dfe9167";
};
subtest "Static Arrays" => sub {
my $data = "0x61626300000000000000000000000000000000000000000000000000000000006465660000000000000000000000000000000000000000000000000000000000";
my $decoded = $decoder->append('bytes3[2]')->decode($data);
is_deeply $decoded->@*, ['0x' . unpack("H*", 'abc'), '0x' . unpack("H*", 'def')];
};
subtest "Dynamic Arrays" => sub {
my $data =
"0000000000000000000000000000000000000000000000000000000063538907000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000...
my $decoded = $decoder->append('uint256')->append('bytes[]')->decode($data);
is_deeply $decoded,
[
1666418951,
[
'0x04e45aaf000000000000000000000000def1ca1fb7fbcdc777520aa7f396b4e015f497ab000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000...
'0x49404b7c00000000000000000000000000000000000000000000000000088face1a633580000000000000000000000009b4ac5feb3fa9c087906cce730dc1bed34f08514'
]];
};
subtest "Tuples" => sub {
my $data =
'0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
my $decoded = $decoder->append(
'(address,uint256,uint256,address,address,address,uint256,uint256,uint8,uint256,uint256,bytes32,uint256,bytes32,bytes32,uint256,(uint256,address)[],bytes)'
)->decode($data)->[0];
is_deeply $decoded,
[
'0x0000000000000000000000000000000000000000',
Math::BigInt->new(0),
Math::BigInt->new(2850000000000000),
'0x5e9988b0c47b47a5b1d7d2e65358789044c2ef9a',
'0x004c00500000ad104d7dbd00e3ae0a5c00560c00',
'0x2cc8342d7c8bff5a213eb2cde39de9a59b3461a7',
Math::BigInt->new(45981),
Math::BigInt->new(1),
Math::BigInt->new(2),
subtest "Negative" => sub {
my $encoder = Blockchain::Ethereum::ABI::Encoder->new;
$encoder->function('foo')->append('int256' => -100);
my $encoded = $encoder->encode;
is $encoded, '0x4c970b2fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff9c';
my $decoder = Blockchain::Ethereum::ABI::Decoder->new;
my $decoded = $decoder->append('uint256')->decode('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff9c');
is $decoded->[0], -100;
};
done_testing;
( run in 0.337 second using v1.01-cache-2.11-cpan-26ccb49234f )