Skip to content

Commit

Permalink
add support for array of Struct and array of NestedStruct (#111)
Browse files Browse the repository at this point in the history
- add function for computing Struct type that considers arrays
- render snapshots to include updates from previous PR #109 & this PR
- typo fixes

---------

Co-authored-by: Sheng Lundquist <sheng@delv.tech>
  • Loading branch information
dpaiton and Sheng Lundquist authored Jun 10, 2024
1 parent 46e0682 commit 024ffde
Show file tree
Hide file tree
Showing 50 changed files with 973 additions and 249 deletions.
25 changes: 25 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"plugins": [
"prettier-plugin-solidity",
"prettier-plugin-organize-imports"
],
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 80,
"tabWidth": 4,
"useTabs": false,
"singleQuote": false,
"bracketSpacing": true
}
},
{
"files": "*.ts",
"options": {
"printWidth": 80,
"tabWidth": 4
}
}
]
}
12 changes: 6 additions & 6 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Test fixture for deploying local anvil chain."""
"""Pypechain test fixtures."""

from __future__ import annotations

Expand Down Expand Up @@ -82,7 +82,7 @@ def local_chain() -> Iterator[str]:

@pytest.fixture(scope="session")
def w3_init(local_chain: str) -> Web3:
"""gets a Web3 instance connected to the local chain.
"""Get a Web3 instance connected to the local chain.
Parameters
----------
Expand All @@ -102,7 +102,7 @@ def w3_init(local_chain: str) -> Web3:

@pytest.fixture(scope="function")
def w3(w3_init: Web3) -> Iterator[Web3]:
"""resets the anvil instance at the function level so each test gets a fresh chain.
"""Reset the anvil instance at the function level so each test gets a fresh chain.
Parameters
----------
Expand Down Expand Up @@ -155,9 +155,9 @@ def initialize_web3_with_http_provider(

@pytest.fixture(scope="class")
def process_contracts(request):
"""Generates abis for all contracts and pypechain types from those abis."""
"""Generate ABIs for all contracts and pypechain types from those abis."""

# don't regenerate files in CI, it can cause things to break in weird ways.
# Don't regenerate files in CI, it can cause things to break in weird ways.
if os.environ.get("IN_CI"):
return

Expand Down Expand Up @@ -189,5 +189,5 @@ def process_contracts(request):
with open(output_file, "w", encoding="utf-8") as file:
json.dump(data, file, ensure_ascii=False, indent=2)

# # Run the pypechain module after processing all contracts
# Run the pypechain module after processing all contracts
pypechain(f"{test_dir}/abis", f"{test_dir}/types")
47 changes: 45 additions & 2 deletions example/abis/Example.json

Large diffs are not rendered by default.

96 changes: 67 additions & 29 deletions example/contracts/Example.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
pragma solidity ^0.8.0;

contract Example {

event Flip(uint flip);
event Flop(uint flop);

string contractName;

enum Letters { A, B, C }
enum Letters {
A,
B,
C
}
error WrongChoice(Letters answer, string errorMessage);

struct SimpleStruct {
Expand All @@ -30,28 +33,41 @@ contract Example {
contractName = name;
}


function guessALetter(Letters guess) public pure {
Letters answer = Letters((uint8(guess) + uint8(1)) % 3);

revert WrongChoice(answer, "Thank you for playing, but you chose the wrong letter");
revert WrongChoice(
answer,
"Thank you for playing, but you chose the wrong letter"
);
}

function flipFlop(uint flip, uint flop) public returns (uint _flop, uint _flip) {
function flipFlop(
uint flip,
uint flop
) public returns (uint _flop, uint _flip) {
emit Flip(flip);
emit Flop(flop);
return (flop,flip);
return (flop, flip);
}

function singleSimpleStruct(SimpleStruct calldata simpleStruct) public pure returns (SimpleStruct memory) {
function singleSimpleStruct(
SimpleStruct calldata simpleStruct
) public pure returns (SimpleStruct memory) {
return simpleStruct;
}

function singleNestedStruct(NestedStruct calldata nestedStruct) public pure returns (NestedStruct memory) {
function singleNestedStruct(
NestedStruct calldata nestedStruct
) public pure returns (NestedStruct memory) {
return nestedStruct;
}

function twoSimpleStructs() public pure returns (SimpleStruct memory, SimpleStruct memory) {
function twoSimpleStructs()
public
pure
returns (SimpleStruct memory, SimpleStruct memory)
{
SimpleStruct memory simpleStruct1 = SimpleStruct({
intVal: 1,
strVal: "You are number 1"
Expand All @@ -65,50 +81,72 @@ contract Example {
return (simpleStruct1, simpleStruct2);
}

function twoMixedStructs() public pure returns (SimpleStruct memory, NestedStruct memory) {
function twoMixedStructs()
public
pure
returns (SimpleStruct memory, NestedStruct memory)
{
SimpleStruct memory simpleStruct = SimpleStruct({
intVal: 1,
strVal: "You are number 1"
});
NestedStruct memory nestedtStruct = NestedStruct({
intVal: 2,
strVal: "You are number 2",
innerStruct: InnerStruct({boolVal: true})
innerStruct: InnerStruct({ boolVal: true })
});

return (simpleStruct, nestedtStruct);
}

function namedSingleStruct() public pure returns (SimpleStruct memory struct1) {
return SimpleStruct({
intVal: 1,
strVal: "You are number 1"
});
function namedSingleStruct()
public
pure
returns (SimpleStruct memory struct1)
{
return SimpleStruct({ intVal: 1, strVal: "You are number 1" });
}

function namedTwoMixedStructs() public pure returns (SimpleStruct memory simpleStruct, NestedStruct memory nestedStruct) {
simpleStruct = SimpleStruct({
intVal: 1,
strVal: "You are number 1"
});
function namedTwoMixedStructs()
public
pure
returns (
SimpleStruct memory simpleStruct,
NestedStruct memory nestedStruct
)
{
simpleStruct = SimpleStruct({ intVal: 1, strVal: "You are number 1" });
nestedStruct = NestedStruct({
intVal: 2,
strVal: "You are number 2",
innerStruct: InnerStruct({boolVal: true})
innerStruct: InnerStruct({ boolVal: true })
});
}

function mixStructsAndPrimitives() public pure returns (SimpleStruct memory simpleStruct, NestedStruct memory, uint, string memory _name, bool YesOrNo) {
simpleStruct = SimpleStruct({
intVal: 1,
strVal: "You are number 1"
});
function mixStructsAndPrimitives()
public
pure
returns (
SimpleStruct memory simpleStruct,
NestedStruct memory,
uint,
string memory _name,
bool YesOrNo
)
{
simpleStruct = SimpleStruct({ intVal: 1, strVal: "You are number 1" });
NestedStruct memory nestedStruct = NestedStruct({
intVal: 2,
strVal: "You are number 2",
innerStruct: InnerStruct({boolVal: true})
innerStruct: InnerStruct({ boolVal: true })
});

return (simpleStruct, nestedStruct, 1, "ReturnTypesContract", false);
}
}

function vecOfStruct(
SimpleStruct[] memory inVecSimpleStruct
) public pure returns (SimpleStruct[] memory vecSimpleStruct) {
return inVecSimpleStruct;
}
}
Loading

0 comments on commit 024ffde

Please sign in to comment.