web3p / web3.php

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

Call to a member function toHex() on array #250

Open vnxz opened 2 years ago

vnxz commented 2 years ago

I try to use contract method by web3.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Web3\Web3;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Contract;
use Exception;

class Web3Controller extends Controller
{
       public function web3() {

$abi = json_decode('[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balances","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]');

$Bytecode = '0x608060405269021e19e0c9bab24000006002556040518060400160405280600981526020017f54657374546f6b656e0000000000000000000000000000000000000000000000815250600390805190602001906200005f9291906200010c565b506040518060400160405280600381526020017f54544b000000000000000000000000000000000000000000000000000000000081525060049080519060200190620000ad9291906200010c565b506012600555348015620000c057600080fd5b506002546000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555062000221565b8280546200011a90620001bc565b90600052602060002090601f0160209004810192826200013e57600085556200018a565b82601f106200015957805160ff19168380011785556200018a565b828001600101855582156200018a579182015b82811115620001895782518255916020019190600101906200016c565b5b5090506200019991906200019d565b5090565b5b80821115620001b85760008160009055506001016200019e565b5090565b60006002820490506001821680620001d557607f821691505b60208210811415620001ec57620001eb620001f2565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b610d4280620002316000396000f3fe608060405234801561001057600080fd5b506004361061009e5760003560e01c8063313ce56711610066578063313ce5671461016f57806370a082311461018d57806395d89b41146101bd578063a9059cbb146101db578063dd62ed3e1461020b5761009e565b806306fdde03146100a3578063095ea7b3146100c157806318160ddd146100f157806323b872dd1461010f57806327e235e31461013f575b600080fd5b6100ab61023b565b6040516100b89190610a48565b60405180910390f35b6100db60048036038101906100d69190610950565b6102c9565b6040516100e89190610a2d565b60405180910390f35b6100f96103bb565b6040516101069190610aaa565b60405180910390f35b610129600480360381019061012491906108fd565b6103c1565b6040516101369190610a2d565b60405180910390f35b61015960048036038101906101549190610890565b6105e7565b6040516101669190610aaa565b60405180910390f35b6101776105ff565b6040516101849190610aaa565b60405180910390f35b6101a760048036038101906101a29190610890565b610605565b6040516101b49190610aaa565b60405180910390f35b6101c561064d565b6040516101d29190610a48565b60405180910390f35b6101f560048036038101906101f09190610950565b6106db565b6040516102029190610a2d565b60405180910390f35b610225600480360381019061022091906108bd565b610841565b6040516102329190610aaa565b60405180910390f35b6003805461024890610be6565b80601f016020809104026020016040519081016040528092919081815260200182805461027490610be6565b80156102c15780601f10610296576101008083540402835291602001916102c1565b820191906000526020600020905b8154815290600101906020018083116102a457829003601f168201915b505050505081565b600081600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040516103a99190610aaa565b60405180910390a36001905092915050565b60025481565b6000816103cd85610605565b101561040e576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161040590610a6a565b60405180910390fd5b81600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156104cd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104c490610a8a565b60405180910390fd5b816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825461051b9190610ae1565b92505081905550816000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546105709190610b37565b925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040516105d49190610aaa565b60405180910390a3600190509392505050565b60006020528060005260406000206000915090505481565b60055481565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b6004805461065a90610be6565b80601f016020809104026020016040519081016040528092919081815260200182805461068690610be6565b80156106d35780601f106106a8576101008083540402835291602001916106d3565b820191906000526020600020905b8154815290600101906020018083116106b657829003601f168201915b505050505081565b6000816106e733610605565b1015610728576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161071f90610a6a565b60405180910390fd5b816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546107769190610ae1565b92505081905550816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546107cb9190610b37565b925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef8460405161082f9190610aaa565b60405180910390a36001905092915050565b6001602052816000526040600020602052806000526040600020600091509150505481565b60008135905061087581610cde565b92915050565b60008135905061088a81610cf5565b92915050565b6000602082840312156108a6576108a5610c76565b5b60006108b484828501610866565b91505092915050565b600080604083850312156108d4576108d3610c76565b5b60006108e285828601610866565b92505060206108f385828601610866565b9150509250929050565b60008060006060848603121561091657610915610c76565b5b600061092486828701610866565b935050602061093586828701610866565b92505060406109468682870161087b565b9150509250925092565b6000806040838503121561096757610966610c76565b5b600061097585828601610866565b92505060206109868582860161087b565b9150509250929050565b61099981610b7d565b82525050565b60006109aa82610ac5565b6109b48185610ad0565b93506109c4818560208601610bb3565b6109cd81610c7b565b840191505092915050565b60006109e5600f83610ad0565b91506109f082610c8c565b602082019050919050565b6000610a08601183610ad0565b9150610a1382610cb5565b602082019050919050565b610a2781610ba9565b82525050565b6000602082019050610a426000830184610990565b92915050565b60006020820190508181036000830152610a62818461099f565b905092915050565b60006020820190508181036000830152610a83816109d8565b9050919050565b60006020820190508181036000830152610aa3816109fb565b9050919050565b6000602082019050610abf6000830184610a1e565b92915050565b600081519050919050565b600082825260208201905092915050565b6000610aec82610ba9565b9150610af783610ba9565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03821115610b2c57610b2b610c18565b5b828201905092915050565b6000610b4282610ba9565b9150610b4d83610ba9565b925082821015610b6057610b5f610c18565b5b828203905092915050565b6000610b7682610b89565b9050919050565b60008115159050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60005b83811015610bd1578082015181840152602081019050610bb6565b83811115610be0576000848401525b50505050565b60006002820490506001821680610bfe57607f821691505b60208210811415610c1257610c11610c47565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600080fd5b6000601f19601f8301169050919050565b7f62616c616e636520746f6f206c6f770000000000000000000000000000000000600082015250565b7f616c6c6f77616e636520746f6f206c6f77000000000000000000000000000000600082015250565b610ce781610b6b565b8114610cf257600080fd5b50565b610cfe81610ba9565b8114610d0957600080fd5b5056fea2646970667358221220a47a6433e16b8607bfad8ee058bcea2b3f93a6888264c0037e631160b5b4b35a64736f6c63430008070033';

$contract = new Contract('https://ropsten.infura.io/v3/34bc013ca1124ee0af5004bfd1cfc454', $abi);

$contractAddress = '0x1838d501c4F37059CA6C8059c882bB3e8BEaA90d';

$accountaddress = '0xeE01EDe04D42e1a51C99f2737B9E14Fc930a1fd4';
$toaddress ='0x5a86f0cafd4ef3ba4f0344c138afcc84bd1ed222';
$amount = 1000000000000000000000;
    // call contract function
// $contract->at($contractAddress)

//              ->call('balanceOf', $accountaddress , function ($err, $data) {
//                if ($err !== null) {
//                  throw new Exception($err->getMessage());
//                }
//                dd($data);
//              });

$contract->at($contractAddress)
          ->call('transfer', $toaddress, $amount , function ($err, $data) {
                if ($err !== null) {
                  throw new Exception($err->getMessage());
                }
                dd($data);
              });

   }

}

when call balanceof() ,it is ok. when call transfer function , not ok ,

Call to a member function toHex() on array 

what is that error, I try to transfer all balance of contract to another address, here is contract code


// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.2;

contract Token {
    mapping(address => uint) public balances;
    mapping(address => mapping(address => uint)) public allowance;
    uint public totalSupply = 10000 * 10 ** 18;
    string public name = "TestToken";
    string public symbol = "TTK";
    uint public decimals = 18;

    event Transfer(address indexed from, address indexed to, uint value);
    event Approval(address indexed owner, address indexed spender, uint value);

    constructor() {
        balances[msg.sender] = totalSupply;
    }

    function balanceOf(address owner) public view returns(uint) {
        return balances[owner];
    }

    function transfer(address to, uint value) public returns(bool) {
        require(balanceOf(msg.sender) >= value, 'balance too low');
        balances[to] += value;
        balances[msg.sender] -= value;
       emit Transfer(msg.sender, to, value);
        return true;
    }

    function transferFrom(address from, address to, uint value) public returns(bool) {
        require(balanceOf(from) >= value, 'balance too low');
        require(allowance[from][msg.sender] >= value, 'allowance too low');
        balances[to] += value;
        balances[from] -= value;
        emit Transfer(from, to, value);
        return true;   
    }

    function approve(address spender, uint value) public returns (bool) {
        allowance[msg.sender][spender] = value;
        emit Approval(msg.sender, spender, value);
        return true;   
    }
}
deathemporer commented 2 years ago

Hey, did you find any solution. Facing the same issue.

mdeprezzo commented 2 years ago

hi all, i notice that sometimes, when tohex in called to a double number, toBn return an array, so this is the error on "Call to a member function toHex() on array". Any suggestions?

GotFly commented 2 years ago

@vnxz you can use ->call('transfer', $toaddress, new BigInteger($amount)) to fix this error

hhun commented 1 year ago

@vnxz you can use ->call('transfer', $toaddress, new BigInteger($amount)) to fix this error

$amount 若是浮点数,BigInteger返回内容是空的。