diff --git a/contracts/solidity/inhetitance/Base.sol b/contracts/solidity/inhetitance/Base.sol new file mode 100644 index 000000000..4b83c625a --- /dev/null +++ b/contracts/solidity/inhetitance/Base.sol @@ -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; + } +} diff --git a/contracts/solidity/inhetitance/Main.sol b/contracts/solidity/inhetitance/Main.sol new file mode 100644 index 000000000..0426cb77f --- /dev/null +++ b/contracts/solidity/inhetitance/Main.sol @@ -0,0 +1,21 @@ +// 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 { + selfdestruct(payable(recipient)); + } +} diff --git a/test/constants.js b/test/constants.js index df8cb60ae..11537868c 100644 --- a/test/constants.js +++ b/test/constants.js @@ -107,6 +107,7 @@ const Contract = { New: 'New', AddressContract: 'AddressContract', Recipient: 'Recipient', + Inheritance: 'Inheritance', } const CALL_EXCEPTION = 'CALL_EXCEPTION' diff --git a/test/solidity/inheritance/inheritance.js b/test/solidity/inheritance/inheritance.js new file mode 100644 index 000000000..32a158423 --- /dev/null +++ b/test/solidity/inheritance/inheritance.js @@ -0,0 +1,52 @@ +const { expect } = require('chai') +const { ethers } = require('hardhat') + +describe('Crypto Inheritance tests', function () { + let signers, contractMain, contractBase, 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() + + const factoryBase = await ethers.getContractFactory('Base') + contractBase = await factoryBase.deploy() + await contractBase.deployed() + + //top up the test contract with some funds + const tx = { + to: contractMain.address, + value: TOP_UP_AMOUNT + } + const topUpRes = await wallet.sendTransaction(tx) + await topUpRes.wait(); + }) + + it('should confirm solidity functionality: this (current contract\'s type)', async function () { + const mainThis = await contractMain.returnThis() + + expect(mainThis).to.equal(contractMain.address) + }) + + it('should confirm solidity functionality: super', async function () { + const res = await contractMain.classIdentifier() + + expect(res).to.equal("Main") + }) + + it('should confirm solidity functionality: selfdestruct(address payable recipient)', async function () { + const balanceBaseInitial = await contractBase.getBalance() + expect(balanceBaseInitial).to.be.equal(0) + + const tx = await contractMain.destroyContract(contractBase.address) + await tx.wait() + const balanceBaseFinal = await contractBase.getBalance() + + expect(balanceBaseFinal.gt(balanceBaseInitial)).to.be.true + }) + +})