Created
September 10, 2019 19:15
-
-
Save andrewgordstewart/6fa284c4ef02e4b7063fbbba7a5f8594 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.11; | |
pragma experimental ABIEncoderV2; | |
contract ItemDefiner { | |
struct Item { | |
bool isGood; | |
} | |
} | |
contract ValidItemChecker { | |
function checkIfIsGood(bytes memory itemBytes) public pure returns (bool) { | |
// This function shows valid solidity (which compiles) that is also successfully | |
// parsed. | |
ItemDefiner.Item memory item = abi.decode(itemBytes, (ItemDefiner.Item)); | |
return item.isGood; | |
} | |
function checkIfArePlentiful(bytes memory itemsBytes) public pure returns (bool) { | |
// The following line is correct code. | |
// This contract will compile, but the parser fails to parse it. | |
ItemDefiner.Item[] memory items = abi.decode(itemsBytes, (ItemDefiner.Item[])); | |
return items.length > 5; | |
} | |
} | |
contract InvalidItemChecker { | |
function checkIfIsGood(bytes memory itemBytes) public pure returns (bool) { | |
ItemDefiner.Item memory item = abi.decode(itemBytes, (ItemDefiner.Item)); | |
return item.isGood; | |
} | |
function checkIfArePlentiful(bytes memory itemsBytes) public pure returns (bool) { | |
// The following line is incorrect code: abi.decode will try to assign | |
// a value of type ItemDefiner.Item to a variable of type ItemDefiner.Item[] | |
// This contract will not compile, but the parser parses it as valid code. | |
ItemDefiner.Item[] memory items = abi.decode(itemsBytes, (ItemDefiner.Item)); | |
return items.length > 5; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment