WEB3.PHP Contract Call

1.1k views Asked by At

I am using this PHP library https://github.com/web3p/web3.php to make the requests to smart contracts on BSC.

$web3 = new Web3('https://bsc-dataseed.binance.org');
$contract = new Contract($web3->provider, $abi);

$contract->at('0x10ED43C718714eb63d5aA57B78B54704E256024E')->call('WETH', function($err, $result) {
    print_r($result);
});

Works perfectly but the problem is when I try to call a function that has parameters both uint256 and address[] . For example

enter image description here

And here's the code:

$contract->at('0x10ED43C718714eb63d5aA57B78B54704E256024E')->call('quote', [
    '25000000000000000000',
    '[0x8C851d1a123Ff703BD1f9dabe631b69902Df5f97, 0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56]',
], function($err, $result) {
    print_r($result);
});

I get the following error. Tried to send the parameters as dictionary with param name. Couldn't figure out how should be done.

InvalidArgumentException
Please make sure you have put all function params and callback.

1

There are 1 answers

2
Petr Hejda On BEST ANSWER
  1. Your snippet tries to encode the second param as an array but you're passing a string.
  2. The number of $params is dynamic in the PHP code, don't wrap them in an array.
$contract->at('0x10ED43C718714eb63d5aA57B78B54704E256024E')->call(
    // function name
    'getAmountsOut',

    // note the removed array wrapper

    // first param
    '25000000000000000000',

    // second param
    [
        '0x8C851d1a123Ff703BD1f9dabe631b69902Df5f97',
        '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56'
    ],

    // callback
    function($err, $result) {
        print_r($result);
    }
);