web3p / web3.php

A php interface for interacting with the Ethereum blockchain and ecosystem. Native ABI parsing and smart contract interactions.
MIT License
1.16k stars 542 forks source link

Please provide an example for call sendRawTransaction #168

Open realtebo opened 4 years ago

realtebo commented 4 years ago

Infura doesn't support sendTransaction, so we can't use your excellent contract->send method.

Could you document how to use sendRawTransaction, having to call a contract function that modifies the state and that needs parameters ?

Your library already is able to automatically prepare data for sending, but I am not able to understand how to manually preparare params for use with sendTransactionRaw.

Thanks.

indigoram89old commented 4 years ago

+1

Nanolucas commented 4 years ago

+1 for documented examples of all function calls. I spent hours trying to figure out why my call wasn't working and it turned out I just had the parameters passed in the wrong format (as an array instead of individual arguments) since the documentation is so limited in its examples.

lawrence296 commented 4 years ago

Usually, to send raw transactions, the prepared data has to be signed. I use the module web3p/ethereum-tx which has the 'Transaction' class to that. For example, to execute the "transfer" function of an ERC20 contract:

$web3 = new Web3(new HttpProvider(new HttpRequestManager($infura,5)));
$eth = $web3->eth;
$contract = new Contract($web3->provider, $contract_abi);
$data = '0x' . $contract->at($contract_addr)->getData('transfer', $to_addr, '0x75BCD15'));
$txParams = [
    'from' => $from_addr,
    'to' => $contract_addr,
    'value' => '0x0',
    'nonce' => dec_to_hex($from_addr_nonce->toString()),
    'gas' => $contract_transfer_gas,
    'gasPrice' => dec_to_hex($gas_price->toString()),
    'chainId' => $chain_id,
    'data' => $data,
];
$transaction = new Transaction($txParams);
$signedTransaction = $transaction->sign($from_addr_private_key);

// Send the transaction
$eth->sendRawTransaction('0x'. $signedTransaction, function ($err, $tx) {
    if ($err !== null) {
        err($err->getMessage());
    }
    echo 'TX: '. $tx . PHP_EOL;
});
wangzhilai commented 4 years ago

where is class Transaction?I can't find it.

indigoram89old commented 4 years ago

I found an another solution. I just created a small service on JS (adonis + web3) with API to sign transactions, interaction with contracts etc. I tried different variants to work with ethereum in PHP but web3js still the best =) So I just install adonis (like laravel for JS), install web3 and create few routes for my tasks. So this service working on the same server with my php application. I like it solution.

supenghai commented 4 years ago

Usually, to send raw transactions, the prepared data has to be signed. I use the module web3p/ethereum-tx which has the 'Transaction' class to that. For example, to execute the "transfer" function of an ERC20 contract:

$web3 = new Web3(new HttpProvider(new HttpRequestManager($infura,5)));
$eth = $web3->eth;
$contract = new Contract($web3->provider, $contract_abi);
$data = '0x' . $contract->at($contract_addr)->getData('transfer', $to_addr, '0x75BCD15'));
$txParams = [
    'from' => $from_addr,
    'to' => $contract_addr,
    'value' => '0x0',
    'nonce' => dec_to_hex($from_addr_nonce->toString()),
    'gas' => $contract_transfer_gas,
    'gasPrice' => dec_to_hex($gas_price->toString()),
    'chainId' => $chain_id,
    'data' => $data,
];
$transaction = new Transaction($txParams);
$signedTransaction = $transaction->sign($from_addr_private_key);

// Send the transaction
$eth->sendRawTransaction('0x'. $signedTransaction, function ($err, $tx) {
    if ($err !== null) {
        err($err->getMessage());
    }
    echo 'TX: '. $tx . PHP_EOL;
});

This is the address of the plug-in. https://github.com/web3p/ethereum-tx

ElNelyo commented 3 years ago

Usually, to send raw transactions, the prepared data has to be signed. I use the module web3p/ethereum-tx which has the 'Transaction' class to that. For example, to execute the "transfer" function of an ERC20 contract:

$web3 = new Web3(new HttpProvider(new HttpRequestManager($infura,5)));
$eth = $web3->eth;
$contract = new Contract($web3->provider, $contract_abi);
$data = '0x' . $contract->at($contract_addr)->getData('transfer', $to_addr, '0x75BCD15'));
$txParams = [
    'from' => $from_addr,
    'to' => $contract_addr,
    'value' => '0x0',
    'nonce' => dec_to_hex($from_addr_nonce->toString()),
    'gas' => $contract_transfer_gas,
    'gasPrice' => dec_to_hex($gas_price->toString()),
    'chainId' => $chain_id,
    'data' => $data,
];
$transaction = new Transaction($txParams);
$signedTransaction = $transaction->sign($from_addr_private_key);

// Send the transaction
$eth->sendRawTransaction('0x'. $signedTransaction, function ($err, $tx) {
    if ($err !== null) {
        err($err->getMessage());
    }
    echo 'TX: '. $tx . PHP_EOL;
});

Hello, I use a code like this one. I got a TX hash in return, but nothing send to the blockchain..Did you have any idea ?

EDIT : After some research, it was NONCE parameters was not good.


     $eth->getTransactionCount(
            $adresseClient, function($err, $result) use ($contract, $eth,$contractAddress, $adresseClient){
 $txParams = [
                 'from' => $adresseClient,
                 'to' => $contractAddress,
                 'value' => '0x0',
                 'nonce' => "0x".dechex($result->toString()),
                 'gas' => '0x200b20',
                 'gasPrice' => '0x9184e72a000',
                 'chainId' =>  97,
                 'data' => $data,
             ];
alansshang commented 3 years ago

I use same code , but response rlp: expected input string or byte for *big.Int, decoding into (types.LegacyTx).VTX: Does anyone know the reason ?

benrobot commented 2 years ago

With a lot of trial and error I finally got a working example of how to transfer tokens by signing the transaction. I put the example at https://github.com/benrobot/web3.php_send_tokens_example

Schiocco commented 1 year ago

+1