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

Error "Undefined array key "coders"" #346

Open
bjutkoski opened this issue Feb 1, 2024 · 4 comments
Open

Error "Undefined array key "coders"" #346

bjutkoski opened this issue Feb 1, 2024 · 4 comments
Assignees
Labels

Comments

@bjutkoski
Copy link

I'm having this error Undefined array key "coders" when methods has tuple[] or tuple as input/output

/web3p/web3.php/src/Contracts/Types/Tuple.php:116

input as tuple[]

{
"inputs": [
{
"internalType": "string",
"name": "documentId",
"type": "string"
},
{
"components": [
{
"internalType": "string",
"name": "documentItemId",
"type": "string"
},
{
"internalType": "string",
"name": "id",
"type": "string"
},
{
"internalType": "string",
"name": "locationId",
"type": "string"
},
{
"internalType": "string",
"name": "wasteId",
"type": "string"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"internalType": "struct WasteTraceability.CollectionRequestItem[]",
"name": "collectionRequestItems",
"type": "tuple[]"
}
],
"name": "createCollectionRequest",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}

tuple as output

{
"inputs": [
{
"internalType": "string",
"name": "wasteId",
"type": "string"
}
],
"name": "getWaste",
"outputs": [
{
"components": [
{
"internalType": "string",
"name": "name",
"type": "string"
},
{
"internalType": "uint256",
"name": "decimals",
"type": "uint256"
}
],
"internalType": "struct WasteTraceability.Waste",
"name": "",
"type": "tuple"
}
],
"stateMutability": "view",
"type": "function"
}

@sc0Vu sc0Vu self-assigned this Feb 1, 2024
@sc0Vu sc0Vu added the bug label Feb 1, 2024
@Ow3nTan
Copy link

Ow3nTan commented Apr 3, 2024

i am encountering the same issue now

@ShravanVerma
Copy link

I have same issue while reading tuple data output

I have modified Ethabi class decodeParameters function as given below and its working fine:

public function decodeParameters($types, $param)
{
if (!is_string($param)) {
throw new InvalidArgumentException('The param must be string.');
}

    // change json to array
    $outputTypes = [];
    if ($types instanceof stdClass && isset($types->outputs)) {
        $types = Utils::jsonToArray($types, 2);
    }
    if (is_array($types) && isset($types['outputs'])) {
        $outputTypes = $types;
        $types = [];
        if(!empty($outputTypes['outputs'][0]['components']))
        {
            foreach ($outputTypes['outputs'][0]['components'] as $component) {
                $types[] = $component['type'];
            }
        }
        else{
            foreach ($outputTypes['outputs'] as $output) {
                if (isset($output['type'])) {
                    $types[] = $output['type'];
                }
            }
        }
    }
    $typesLength = count($types);
    $abiTypes = $this->parseAbiTypes($types);

    // decode with tuple type
    $results = [];
    $decodeResults = $this->types['tuple']->decode(Utils::stripZero($param), 0, [ 'coders' => $abiTypes ]);
    for ($i = 0; $i < $typesLength; $i++) {

        if(!empty($outputTypes['outputs'][0]['components']))
        {
            if (isset($outputTypes['outputs'][0]['components'][$i]['name']) && empty($outputTypes['outputs'][0]['components'][$i]['name']) === false) {
                $results[$outputTypes['outputs'][0]['components'][$i]['name']] = $decodeResults[$i];
            } else {
                $results[$i] = $decodeResults[$i];
            }
        }
        else{
            if (isset($outputTypes['outputs'][$i]['name']) && empty($outputTypes['outputs'][$i]['name']) === false) {
                $results[$outputTypes['outputs'][$i]['name']] = $decodeResults[$i];
            } else {
                $results[$i] = $decodeResults[$i];
            }
        }


    }
    return $results;
}

@ShravanVerma
Copy link

I have same issue while reading tuple data output

I have modified Ethabi class decodeParameters function as given below and its working fine:

public function decodeParameters($types, $param)
{
if (!is_string($param)) {
throw new InvalidArgumentException('The param must be string.');
}

// change json to array
$outputTypes = [];
if ($types instanceof stdClass && isset($types->outputs)) {
    $types = Utils::jsonToArray($types, 2);
}
if (is_array($types) && isset($types['outputs'])) {
    $outputTypes = $types;
    $types = [];
    if(!empty($outputTypes['outputs'][0]['components']))
    {
        foreach ($outputTypes['outputs'][0]['components'] as $component) {
            $types[] = $component['type'];
        }
    }
    else{
        foreach ($outputTypes['outputs'] as $output) {
            if (isset($output['type'])) {
                $types[] = $output['type'];
            }
        }
    }
}
$typesLength = count($types);
$abiTypes = $this->parseAbiTypes($types);

// decode with tuple type
$results = [];
$decodeResults = $this->types['tuple']->decode(Utils::stripZero($param), 0, [ 'coders' => $abiTypes ]);
for ($i = 0; $i < $typesLength; $i++) {

    if(!empty($outputTypes['outputs'][0]['components']))
    {
        if (isset($outputTypes['outputs'][0]['components'][$i]['name']) && empty($outputTypes['outputs'][0]['components'][$i]['name']) === false) {
            $results[$outputTypes['outputs'][0]['components'][$i]['name']] = $decodeResults[$i];
        } else {
            $results[$i] = $decodeResults[$i];
        }
    }
    else{
        if (isset($outputTypes['outputs'][$i]['name']) && empty($outputTypes['outputs'][$i]['name']) === false) {
            $results[$outputTypes['outputs'][$i]['name']] = $decodeResults[$i];
        } else {
            $results[$i] = $decodeResults[$i];
        }
    }


}
return $results;

}

@fengxin2017
Copy link

I have same issue while reading tuple data output

I have modified Ethabi class decodeParameters function as given below and its working fine:

public function decodeParameters($types, $param) { if (!is_string($param)) { throw new InvalidArgumentException('The param must be string.'); }

    // change json to array
    $outputTypes = [];
    if ($types instanceof stdClass && isset($types->outputs)) {
        $types = Utils::jsonToArray($types, 2);
    }
    if (is_array($types) && isset($types['outputs'])) {
        $outputTypes = $types;
        $types = [];
        if(!empty($outputTypes['outputs'][0]['components']))
        {
            foreach ($outputTypes['outputs'][0]['components'] as $component) {
                $types[] = $component['type'];
            }
        }
        else{
            foreach ($outputTypes['outputs'] as $output) {
                if (isset($output['type'])) {
                    $types[] = $output['type'];
                }
            }
        }
    }
    $typesLength = count($types);
    $abiTypes = $this->parseAbiTypes($types);

    // decode with tuple type
    $results = [];
    $decodeResults = $this->types['tuple']->decode(Utils::stripZero($param), 0, [ 'coders' => $abiTypes ]);
    for ($i = 0; $i < $typesLength; $i++) {

        if(!empty($outputTypes['outputs'][0]['components']))
        {
            if (isset($outputTypes['outputs'][0]['components'][$i]['name']) && empty($outputTypes['outputs'][0]['components'][$i]['name']) === false) {
                $results[$outputTypes['outputs'][0]['components'][$i]['name']] = $decodeResults[$i];
            } else {
                $results[$i] = $decodeResults[$i];
            }
        }
        else{
            if (isset($outputTypes['outputs'][$i]['name']) && empty($outputTypes['outputs'][$i]['name']) === false) {
                $results[$outputTypes['outputs'][$i]['name']] = $decodeResults[$i];
            } else {
                $results[$i] = $decodeResults[$i];
            }
        }


    }
    return $results;
}

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants