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 543 forks source link

trouble trying to send data #333

Open hafizah65 opened 1 year ago

hafizah65 commented 1 year ago

i'm trying to send the data but i got the following error. i managed to get an existing data from the blockchain, but sending gives out either "Please make sure you have put all function params and callback"

solidity function

     function addInfo(
 uint256 uniqueKey,
     string memory _userInfo
     ) public {
     uniqueKeyIndexes[uniqueKey].push(uniqueKey);
    userInfo[uniqueKey] = _userInfo;
emit infoAdded(uniqueKey);
    }

php

    $web3 = new Web3(getenv('NODE_PATH') . ':8545'); 
    $abiAddress = 'contract.json'; 
    $contractAbi = json_decode(file_get_contents($abiAddress));

    $contract = new Contract($web3->getProvider(), $contractAbi->abi);

    $senderAddress = 'sender_add';
    $contractAddress = 'contact_add';

    $functionName = 'addInfo';
    $data = [123, $data_bc];

    $privateKey = '123priv';

    $contract->at($contractAddress)->send($functionName, $data, function ($err, $transactionHash) use ($web3, $privateKey) {
        if ($err !== null) {
            echo 'Error: ' . $err->getMessage() . PHP_EOL;
        } else {
            // Sign the transaction
            $signedTransaction = $web3->eth->accounts->signTransaction([
                'data' => $transactionHash,
            ], $privateKey);

            // Send the signed transaction to the network
            $web3->eth->sendRawTransaction($signedTransaction, function ($err, $transactionHash) {
                if ($err !== null) {
                "echo 'Error sending transacton: ' . $err->getMessage() . PHP_EOL;
                } else {
                    echo 'Transaction Hash: ' . Utils::toString($transactionHash) . PHP_EOL;
                }
            });
        }
    }); //FIXME: error here

i'm not exactly sure what i've done wrong. i've tried following #238 and i got "Wrong type of eth_sendTransaction method argument 0"

    $data_new = json_decode($data_bc, true);
    $data_new['from'] = $senderAddress;
    $data_new['privateKey'] = $privateKey;

    $data_new = json_encode($data_new);

    $contract->at($contractAddress)->send($functionName, 5678, $data_new, function ($err, $transres) use (&$done) {
        if ($err) {
            echo 'error: ' . $err->getMessage() . "<br>";
        } else {
            $done = $transres;
        }
    }); //FIXME: Wrong type of eth_sendTransaction method argument 0`
hafizah65 commented 1 year ago

update: i change the code and i'm getting "Call to a member function signTransaction() on bool"

   $eth = new Eth($web3->getProvider());
    // Create the transaction object
    $transaction = [
        'from' => $senderAddress,
        'to' => $contractAddress,
        'data' => $contract->getData($functionName, 5678, $data_bc),
        //'data' => '0x'. $contract->getData($functionName, 5678, $data_bc),

    // Sign the transaction
    $signedTransaction = $eth->accounts->signTransaction($transaction, $privateKey); //FIXME
    ];

    // Send the transaction to the network
    $transactionHash = $eth->sendRawTransaction($signedTransaction);

    echo 'Transaction Hash: ' . Utils::toString($transactionHash) . PHP_EOL;

    // Wait for the transaction to be mined
    $receipt = $eth->getTransactionReceipt($transactionHash);
    if ($receipt !== null) {
        echo 'Transaction Mined' . PHP_EOL;
    }