2

I'm not the author of Ethereum part but have to connect to using php. Choosen Ethereum-PHP package (https://github.com/digitaldonkey/ethereum-php). Have connection to ethereum and smart contract but cannot execute smart contract function with parameter successfuly. I have Schema contract and function test ():

 function test(uint value) public returns(uint) {
return value*2;
 }

and ABI:

...
{"constant":false,"inputs":[{"name":"value","type":"uint256"}],
"name":"test","outputs":
[{"name":"","type":"uint256"}],
"payable":false,"stateMutability":"nonpayable","type":"function"}
...

Now in php I have:

  $eth = new Ethereum('https://kovan.infura.io/<unique address>');
  $schemaContract = new SmartContract($abi, '<contract address>', $eth);

  // Call a function with result : 2
  $result = $schemaContract->test(1)->Val();

I cannot recieve the result. Don't know exactly how to achieve this. Tried many different options like :

$result = $schemaContract->test(1);
$result = $schemaContract->test('1');
$result = $schemaContract->test(1);
$result = $schemaContract->__call('test',1);

But always recieved some error messages.

What am I doing wrong?

Best regards, TB

  • I see a interesting approach in your code: the contract "knows" your datatype, so it would be able to convert it it for you. Maybe some day I''ll add a Contract which is capable to do what you like, but for now the answer below is the solution. – digitaldonkey Aug 10 '18 at 11:27
2

Checking the unit tests I found

public function testSimpleContract()
{
    $number = 2;
    $result = $this->contract->multiplyWithSeven(
        new EthQ($number, ['abi'=> 'uint256'])
    );
    $this->assertEquals($number*7, $result->val());
}

Probably it would work if you do this

$result = $schemaContract->test(
    new EthQ(1, ['abi' => 'uint256'])
);
echo $result->val();
  • Thank you very much. It's working now. Didn't aware that need to specify data type. Can you give me also advice how to get few return values: `function product(uint32 _prdId) public view returns (uint32 prdId, string prdName, address prd, uint closeTime, uint promoTime,uint8 bonus, uint256 price, uint8 numberOfOptions, uint256 totalSupply, uint8 status, uint32 next)' – Tom B Apr 26 '18 at 5:55
  • I don't have experience with this library. Ask another question, probably somebody else can help you, or I'll have some time to try it. It will be easier to understand if it's properly formatted – cleanunicorn Apr 26 '18 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.