-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Stefan Stefanov <[email protected]>
- Loading branch information
1 parent
6000257
commit 2bc1040
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.20; | ||
|
||
contract Base { | ||
receive() external payable {} | ||
function classIdentifier() public pure virtual returns (string memory) { | ||
return "Base"; | ||
} | ||
|
||
function getBalance() public view returns (uint256) { | ||
return address(this).balance; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.20; | ||
import { Base } from "./Base.sol"; | ||
|
||
contract Main is Base { | ||
function classIdentifier() public pure override(Base) returns (string memory) { | ||
return "Main"; | ||
} | ||
|
||
function returnThis() public view returns (Main) { | ||
return this; | ||
} | ||
|
||
function returnSuper() public view virtual returns (string memory) { | ||
return super.classIdentifier(); | ||
} | ||
|
||
function destroyContract(address recipient) public returns (bool) { | ||
selfdestruct(payable(recipient)); | ||
|
||
return true; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const { expect } = require('chai') | ||
const { ethers } = require('hardhat') | ||
|
||
describe('Crypto Inheritance tests', function () { | ||
let signers, contractMain, contractMainAddr, contractBase, contractBaseAddr, wallet | ||
const TOP_UP_AMOUNT = ethers.utils.parseEther('0.000001'); | ||
|
||
before(async function () { | ||
signers = await ethers.getSigners() | ||
wallet = signers[0]; | ||
|
||
const factoryMain = await ethers.getContractFactory('Main') | ||
contractMain = await factoryMain.deploy() | ||
await contractMain.deployed() | ||
contractMainAddr = contractMain.address | ||
|
||
const factoryBase = await ethers.getContractFactory('Base') | ||
contractBase = await factoryBase.deploy() | ||
await contractBase.deployed() | ||
contractBaseAddr = contractBase.address | ||
|
||
//top up the test contract with some funds | ||
const tx = { | ||
to: contractMainAddr, | ||
value: TOP_UP_AMOUNT | ||
} | ||
const topUpRes = await wallet.sendTransaction(tx) | ||
await topUpRes.wait(); | ||
}) | ||
|
||
it("this (current contract's type)", async function () { | ||
const mainThis = await contractMain.returnThis() | ||
|
||
expect(mainThis).to.equal(contractMainAddr) | ||
}) | ||
|
||
it('super', async function () { | ||
const res = await contractMain.classIdentifier() | ||
|
||
expect(res).to.equal("Main") | ||
}) | ||
|
||
it('selfdestruct(address payable recipient)', async function () { | ||
const balanceBaseInitial = await contractBase.getBalance() | ||
expect(balanceBaseInitial).to.be.equal(0) | ||
|
||
const tx = await contractMain.destroyContract(contractBaseAddr) | ||
tx.wait() | ||
const balanceBaseFinal = await contractBase.getBalance() | ||
|
||
expect(balanceBaseFinal.gt(balanceBaseInitial)).to.be.true | ||
}) | ||
|
||
}) |