Blockchain-Contract-Solidity-ABI

 view release on metacpan or  search on metacpan

lib/Blockchain/Contract/Solidity/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, C<< <refeco at cpan.org> >>

=head1 BUGS

Please report any bugs or feature requests to L<https://github.com/refeco/perl-ABI>

=head1 SUPPORT

t/decode.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More;
use Blockchain::Contract::Solidity::ABI::Decoder;

my $decoder = Blockchain::Contract::Solidity::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),



( run in 0.577 second using v1.01-cache-2.11-cpan-26ccb49234f )