Ethereum-RPC-Client

 view release on metacpan or  search on metacpan

lib/Ethereum/RPC/Contract.pm  view on Meta::CPAN

sub _build_gas_price {
    return shift->rpc_client->eth_gasPrice();
}

has max_fee_per_gas => (is => 'rw');

has max_priority_fee_per_gas => (is => 'rw');

has gas => (is => 'rw');

has contract_decoded => (
    is      => 'rw',
    default => sub { {} },
);

=head2 BUILD

Constructor: Here we get all functions and events from the given ABI and set
it to the contract class.

=over 4

lib/Ethereum/RPC/Contract.pm  view on Meta::CPAN

=item max_fee_per_gas => numeric (optional)

=item max_priority_fee_per_gas => numeric (optional)

=back

=cut

sub BUILD {
    my ($self) = @_;
    my @decoded_json = @{decode_json($self->contract_abi // "[]")};

    for my $json_input (@decoded_json) {
        if ($json_input->{type} =~ /^function|event|constructor$/) {
            push(@{$self->contract_decoded->{$json_input->{name} // $json_input->{type}}}, $json_input->{inputs});
        }
    }

    unless ($self->contract_decoded->{constructor}) {
        push(@{$self->contract_decoded->{constructor}}, []);
    }

    return;

}

=head2 invoke

Prepare a function to be called/sent to a contract.

lib/Ethereum/RPC/Contract.pm  view on Meta::CPAN


=back

Returns a string hash

=cut

sub get_function_id {
    my ($self, $function_name, $params_size) = @_;

    my @inputs = @{$self->contract_decoded->{$function_name}};

    my $selected_data = first { (not $_ and not $params_size) or ($params_size and scalar @{$_} == $params_size) } @inputs;

    $function_name .= sprintf("(%s)", join(",", map { $_->{type} } grep { $_->{type} } @$selected_data));

    my $sha3_hex_function = '0x' . keccak_256_hex($function_name);

    return $sha3_hex_function;
}

lib/Ethereum/RPC/Contract.pm  view on Meta::CPAN


=back

Returns an encoded data string

=cut

sub encode {
    my ($self, $function_name, $params) = @_;

    my $inputs = $self->contract_decoded->{$function_name}->[0];

    # no inputs
    return "" unless $inputs;

    my $offset = $self->get_function_offset($inputs);

    my (@static, @dynamic);
    my @inputs = $inputs->@*;
    for (my $input_index = 0; $input_index < scalar @inputs; $input_index++) {
        my ($static, $dynamic) = $self->get_hex_param($offset, $inputs[$input_index]->{type}, $params->[$input_index]);

lib/Ethereum/RPC/Contract/Helper/ImportHelper.pm  view on Meta::CPAN

Return:
    {abi, bytecode}

=cut

sub from_truffle_build {
    my $file = shift;

    my $document = path($file)->slurp_utf8;

    my $decoded_json = decode_json($document);

    return {
        abi      => encode_json($decoded_json->{abi}),
        bytecode => $decoded_json->{bytecode}};
}

1;



( run in 0.235 second using v1.01-cache-2.11-cpan-0d8aa00de5b )