Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue#188:https://github.com/web3p/web3.php/issues/188, support t… #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Contract.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Web3\Validators\TagValidator;
use Web3\Validators\QuantityValidator;
use Web3\Formatters\AddressFormatter;
use Web3\Contracts\Types\Tuple;

class Contract
{
Expand Down Expand Up @@ -162,6 +163,7 @@ public function __construct($provider, $abi, $defaultBlock = 'latest')
'int' => new Integer,
'string' => new Str,
'uint' => new Uinteger,
'tuple' => new Tuple,
]);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Contracts/Ethabi.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ public function decodeParameters($types, $param)
if (isset($outputTypes['outputs'][$i]['name']) && empty($outputTypes['outputs'][$i]['name']) === false) {
$result[$outputTypes['outputs'][$i]['name']] = $solidityTypes[$i]->decode($param, $offsets[$i], $types[$i]);
} else {
$result[$i] = $solidityTypes[$i]->decode($param, $offsets[$i], $types[$i]);
if($types[$i] == "tuple"){
$result[$i] = $solidityTypes[$i]->decodeTuple($outputTypes['outputs'][$i],$param);
}else{
$result[$i] = $solidityTypes[$i]->decode($param, $offsets[$i], $types[$i]);
}
}
}

Expand Down
110 changes: 110 additions & 0 deletions src/Contracts/SolidityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,49 @@

use Web3\Utils;
use Web3\Formatters\IntegerFormatter;
use InvalidArgumentException;
use Web3\Contracts\Types\Address;
use Web3\Contracts\Types\Boolean;
use Web3\Contracts\Types\Bytes;
use Web3\Contracts\Types\DynamicBytes;
use Web3\Contracts\Types\Integer;
use Web3\Contracts\Types\Str;
use Web3\Contracts\Types\Uinteger;
use Web3\Contracts\Types\Tuple;


class SolidityType
{
private array $types = [];

/**
* construct
*
* @return void
*/
// public function __construct() {}

/**
* set types
*/
private function setTypes()
{
if(!empty($this->types)){
return $this->types;
}
$this->types = [
'address' => new Address,
'bool' => new Boolean,
'bytes' => new Bytes,
'dynamicBytes' => new DynamicBytes,
'int' => new Integer,
'string' => new Str,
'uint' => new Uinteger,
'tuple' => new Tuple,
];
return $this->types;
}

/**
* get
*
Expand Down Expand Up @@ -270,4 +303,81 @@ public function decode($value, $offset, $name)

return $this->outputFormat($param, $name);
}

/**
* decode struct data
*
* @author abaowu <[email protected]>
* @param array $type tuple struct
* @param mix $value tuple value
* @return array
*/
public function decodeTuple($type, $value)
{
$outputTypes = $type["components"];
$typesLength = count($outputTypes);
$solidityTypes = $this->getSolidityTypes($outputTypes);
$offsets = array_fill(0, $typesLength, 0);

for ($i=0; $i<$typesLength; $i++) {
$offsets[$i] = $solidityTypes[$i]->staticPartLength($outputTypes[$i]['type']);
}
for ($i=1; $i<$typesLength; $i++) {
$offsets[$i] += $offsets[$i - 1];
}
for ($i=0; $i<$typesLength; $i++) {
$offsets[$i] -= $solidityTypes[$i]->staticPartLength($outputTypes[$i]['type']);
}
$result = [];
$param = mb_strtolower(Utils::stripZero($value));

for ($i=0; $i<$typesLength; $i++) {
if (isset($outputTypes[$i]['name']) && empty($outputTypes[$i]['name']) === false) {
$result[$outputTypes[$i]['name']] = $solidityTypes[$i]->decode($param, $offsets[$i], $outputTypes[$i]['type']);
} else {
$result[$i] = $solidityTypes[$i]->decode($param, $offsets[$i], $outputTypes[$i]['type']);
}
}

return $result;

}


/**
* getSolidityTypes
*
* @param array $types
* @return array
*/
protected function getSolidityTypes($types)
{
if (!is_array($types)) {
throw new InvalidArgumentException('Types must be array');
}
$solidityTypes = array_fill(0, count($types), 0);
$this->setTypes();

foreach ($types as $key => $type) {
$match = [];
$type = $type['type'];
if (preg_match('/^([a-zA-Z]+)/', $type, $match) === 1) {
if (isset($this->types[$match[0]])) {
$className = $this->types[$match[0]];

if (call_user_func([$this->types[$match[0]], 'isType'], $type) === false) {
// check dynamic bytes
if ($match[0] === 'bytes') {
$className = $this->types['dynamicBytes'];
} else {
throw new InvalidArgumentException('Unsupport solidity parameter type: ' . $type);
}
}
$solidityTypes[$key] = $className;
}
}
}
return $solidityTypes;
}

}
86 changes: 86 additions & 0 deletions src/Contracts/Types/Tuple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* This file is part of web3.php package.
*
* (c) abaowu <[email protected]>
*
* @author abaowu <[email protected]>
* @license MIT
*/

namespace Web3\Contracts\Types;

use Web3\Contracts\SolidityType;
use Web3\Contracts\Types\IType;

class Tuple extends SolidityType implements IType
{
/**
* construct
*
* @return void
*/
public function __construct()
{
//
}

/**
* isType
*
* @param string $name
* @return bool
*/
public function isType($name)
{
return (preg_match('/^tuple(\[([0-9]*)\])*$/', $name) === 1);
}

/**
* isDynamicType
*
* @return bool
*/
public function isDynamicType()
{
return true;
}

/**
* inputFormat
* to do: iban
*
* @param mixed $value
* @param string $name
* @return string
*/
public function inputFormat($value, $name)
{
// $value = (string) $value;

// if (Utils::isAddress($value)) {
// $value = mb_strtolower($value);

// if (Utils::isZeroPrefixed($value)) {
// $value = Utils::stripZero($value);
// }
// }
// $value = IntegerFormatter::format($value);

return $value;
}

/**
* outputFormat
*
* @param mixed $value
* @param string $name
* @return string
*/
public function outputFormat($value, $name)
{
// return '0x' . mb_substr($value, 24, 40);
return $value;
}
}