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

Issue with calling function #238

Closed Razorholt closed 2 years ago

Razorholt commented 2 years ago

My solidity function:

function withdrawToken(address _tokenContract, uint256 _amount, address _recipient, address _sender) public payable {
    IERC20 tokenContract = IERC20(_tokenContract);
    require(_sender == _owner, "Only owner can withdraw");
    require(IERC20(_tokenContract).balanceOf(address(this)) >= _amount, "Insufficient Funds");
    tokenContract.transfer(_recipient, _amount);
}

PHP file:

$contract->at($senderAccount)->send('withdrawToken', [
    '_tokenContract' => $tokenAddress,
    '_amount' => $amountInWholeNumber,
    '_recipient' => $destAccount,
    '_sender' => $senderOwner
], function ($err, $transres) use(&$done) {
    if($err) { 
        echo 'error: ' . $err->getMessage() . "<br>"; 
    } else {
        $done = $transres;
    }
});

Result:

Fatal error: Uncaught InvalidArgumentException: Please make sure you have put all function params and callback. 
in /home/project3/vendor/sc0vu/web3.php/src/Contract.php:561 Stack trace: #0 
/home/project3/public_html/app/withdraw_function.php(58): Web3\Contract->send('withdrawToken', Array, 
Object(Closure)) #1 {main} thrown in /home/project3/vendor/sc0vu/web3.php/src/Contract.php on line 561

Not sure what I'm doing wrong here. Thanks for your help!

sc0Vu commented 2 years ago

Hey there,

You should call with your parameters flatten instead of array, unless the solidity function parameter is struct.

$contract->at($senderAccount)->send('withdrawToken', $tokenAddress,$amountInWholeNumber,$destAccount,$senderOwner, function ($err, $transres) use(&$done) {
    if($err) { 
        echo 'error: ' . $err->getMessage() . "<br>"; 
    } else {
        $done = $transres;
    }
});