Skip to content

Commit

Permalink
inherited-edge-case
Browse files Browse the repository at this point in the history
  • Loading branch information
rin-st committed Oct 26, 2023
1 parent f60aea5 commit 1a1c324
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 91 deletions.
8 changes: 8 additions & 0 deletions packages/hardhat/contracts/A.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

contract A {
function foo() public pure virtual returns (string memory) {
return "A";
}
}
12 changes: 12 additions & 0 deletions packages/hardhat/contracts/B.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

import "./A.sol";

// Contracts inherit other contracts by using the keyword 'is'.
contract B is A {
// Override A.foo()
function foo() public pure virtual override returns (string memory) {
return "B";
}
}
11 changes: 11 additions & 0 deletions packages/hardhat/contracts/C.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

import "./A.sol";

contract C is A {
// Override A.foo()
function foo() public pure virtual override returns (string memory) {
return "C";
}
}
18 changes: 18 additions & 0 deletions packages/hardhat/contracts/D.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

import "./B.sol";
import "./C.sol";

// Contracts can inherit from multiple parent contracts.
// When a function is called that is defined multiple times in
// different contracts, parent contracts are searched from
// right to left, and in depth-first manner.

contract D is B, C {
// D.foo() returns "C"
// since C is the right most parent contract with function foo()
function foo() public pure override(B, C) returns (string memory) {
return super.foo();
}
}
13 changes: 13 additions & 0 deletions packages/hardhat/contracts/E.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

import "./B.sol";
import "./C.sol";

contract E is C, B {
// E.foo() returns "B"
// since B is the right most parent contract with function foo()
function foo() public pure override(C, B) returns (string memory) {
return super.foo();
}
}
13 changes: 13 additions & 0 deletions packages/hardhat/contracts/F.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

import "./A.sol";
import "./B.sol";

// Inheritance must be ordered from “most base-like” to “most derived”.
// Swapping the order of A and B will throw a compilation error.
contract F is A, B {
function foo() public pure override(A, B) returns (string memory) {
return super.foo();
}
}
87 changes: 0 additions & 87 deletions packages/hardhat/contracts/YourContract.sol

This file was deleted.

53 changes: 51 additions & 2 deletions packages/hardhat/deploy/00_deploy_your_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,65 @@ const deployYourContract: DeployFunction = async function (hre: HardhatRuntimeEn
const { deployer } = await hre.getNamedAccounts();
const { deploy } = hre.deployments;

await deploy("YourContract", {
await deploy("A", {
from: deployer,
// Contract constructor arguments
args: [deployer],
args: [],
log: true,
// autoMine: can be passed to the deploy function to make the deployment process faster on local networks by
// automatically mining the contract deployment transaction. There is no effect on live networks.
autoMine: true,
});

await deploy("B", {
from: deployer,
// Contract constructor arguments
args: [],
log: true,
// autoMine: can be passed to the deploy function to make the deployment process faster on local networks by
// automatically mining the contract deployment transaction. There is no effect on live networks.
autoMine: true,
});

await deploy("C", {
from: deployer,
// Contract constructor arguments
args: [],
log: true,
// autoMine: can be passed to the deploy function to make the deployment process faster on local networks by
// automatically mining the contract deployment transaction. There is no effect on live networks.
autoMine: true,
});

await deploy("D", {
from: deployer,
// Contract constructor arguments
args: [],
log: true,
// autoMine: can be passed to the deploy function to make the deployment process faster on local networks by
// automatically mining the contract deployment transaction. There is no effect on live networks.
autoMine: true,
});

await deploy("E", {
from: deployer,
// Contract constructor arguments
args: [],
log: true,
// autoMine: can be passed to the deploy function to make the deployment process faster on local networks by
// automatically mining the contract deployment transaction. There is no effect on live networks.
autoMine: true,
});

await deploy("F", {
from: deployer,
// Contract constructor arguments
args: [],
log: true,
// autoMine: can be passed to the deploy function to make the deployment process faster on local networks by
// automatically mining the contract deployment transaction. There is no effect on live networks.
autoMine: true,
});
// Get the deployed contract
// const yourContract = await hre.ethers.getContract("YourContract", deployer);
};
Expand Down
144 changes: 142 additions & 2 deletions packages/nextjs/generated/deployedContracts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,143 @@
const deployedContracts = null;
const contracts = {
31337: [
{
chainId: "31337",
name: "localhost",
contracts: {
A: {
address: "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512",
abi: [
{
inputs: [],
name: "foo",
outputs: [
{
internalType: "string",
name: "",
type: "string",
},
],
stateMutability: "pure",
type: "function",
},
],
inheritedFunctions: {},
},
B: {
address: "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0",
abi: [
{
inputs: [],
name: "foo",
outputs: [
{
internalType: "string",
name: "",
type: "string",
},
],
stateMutability: "pure",
type: "function",
},
],
inheritedFunctions: {
foo: "contracts/A.sol",
},
},
C: {
address: "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9",
abi: [
{
inputs: [],
name: "foo",
outputs: [
{
internalType: "string",
name: "",
type: "string",
},
],
stateMutability: "pure",
type: "function",
},
],
inheritedFunctions: {
foo: "contracts/A.sol",
},
},
D: {
address: "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9",
abi: [
{
inputs: [],
name: "foo",
outputs: [
{
internalType: "string",
name: "",
type: "string",
},
],
stateMutability: "pure",
type: "function",
},
],
inheritedFunctions: {
foo: "contracts/C.sol",
},
},
E: {
address: "0x5FC8d32690cc91D4c39d9d3abcBD16989F875707",
abi: [
{
inputs: [],
name: "foo",
outputs: [
{
internalType: "string",
name: "",
type: "string",
},
],
stateMutability: "pure",
type: "function",
},
],
inheritedFunctions: {
foo: "contracts/C.sol",
},
},
F: {
address: "0x0165878A594ca255338adfa4d48449f69242Eb8F",
abi: [
{
inputs: [],
name: "foo",
outputs: [
{
internalType: "string",
name: "",
type: "string",
},
],
stateMutability: "pure",
type: "function",
},
],
inheritedFunctions: {
foo: "contracts/B.sol",
},
},
},
},
],
11155111: [
{
chainId: "11155111",
name: "sepolia",
contracts: {},
},
],
} as const;

export default deployedContracts;
export default contracts;

0 comments on commit 1a1c324

Please sign in to comment.