monero-integrations / monerophp

Monero PHP library + JsonRPC Client
MIT License
116 stars 76 forks source link

Cannot transfer to multiple outputs #126

Closed php-wizard closed 2 years ago

php-wizard commented 3 years ago

Hello, i have tried the transfer method passing an array as the first parameter, with the "destinations" array key, like this

            $params = ["destinations"=>[ "amount"=>sprintf("%.12f",$amount), "address"=>$address, "amount"=>sprintf("%.12f",$amount2), "address"=>$address2], "priority"=>1 ];
            echo "Calling transfer. Param:";
            var_dump($params);
            $transfer = self::arrayToJson($walletRPC->transfer($params));

So i followed the example call in your example.php file

And i get this dump and a runtime error from the monerophp method


Calling transfer. Param:array(2) {
  ["destinations"]=>
  array(2) {
    ["amount"]=>
    string(14) "0.267274635336"
    ["address"]=>
    string(95) "59GS313HKyuBRTGMG8uL1KTxpANnG1BXcYEUeDttSfgogB4N5vKrbZMJJagqRYkoyYUeJ4jckgHym7snxs5h9VQNJocKojE"
  }
  ["priority"]=>
  int(1)
}

   ErrorException  : Undefined index: 0.267274635336

  at /www/wwwroot/myproject/vendor/monero-integrations/monerophp/src/walletRPC.php:498
    494|           throw new Exception('Error: destinations must be an array');
    495|         }
    496|
    497|         foreach ($destinations as $destination) {
  > 498|           if (array_key_exists('amount', $destinations[$destination])) {
    499|             $destinations[$destination]['amount'] = $this->_transform($destinations[$destination]['amount']);
    500|           } else {
    501|             throw new Exception('Error: Amount required');
    502|           }

Not only i get the error, but $params in the dump has just one address and one amount, not two , maybe due to PHP not allowing 2 equal indexes occurring at the same time in an array. So how can i achieve what i want? (sending 2 outputs)

php-wizard commented 3 years ago

I have also tried to pass the param like this, as an array of arrays

["destinations"=> [[ "amount"=>sprintf("%.12f",$amount), "address"=>$address], ["amount"=>sprintf("%.12f",$amount2), "address"=>$address2]] ]

But now i get another fatal error

  ErrorException  : Illegal offset type

  at /www/wwwroot/myproject/vendor/monero-integrations/monerophp/src/walletRPC.php:498
    494|           throw new Exception('Error: destinations must be an array');
    495|         }
    496|
    497|         foreach ($destinations as $destination) {
  > 498|           if (array_key_exists('amount', $destinations[$destination])) {
    499|             $destinations[$destination]['amount'] = $this->_transform($destinations[$destination]['amount']);
    500|           } else {
    501|             throw new Exception('Error: Amount required');
    502|           }
cypherbits commented 3 years ago

I think the implementation is wrong. Will take a look when I have some free time.

Could you try my php8 branch on my fork?

php-wizard commented 3 years ago

I must use php 7.3