Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

487 Solidity Support Modifiers #508

Merged
merged 7 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions contracts/solidity/modifiers/A.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

contract A{

function show() public pure virtual returns(string memory) {
return "This is contract A";
}
}
16 changes: 16 additions & 0 deletions contracts/solidity/modifiers/B.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import "./Modifiers.sol";
import "./A.sol";

contract B is Modifiers, A {

constructor(uint256 _baseValue) Modifiers(_baseValue){

}

function show() public override(Modifiers, A) pure returns(string memory) {
return "This is the overriding contract B";
}
}
15 changes: 15 additions & 0 deletions contracts/solidity/modifiers/DerivedContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import "./Modifiers.sol";

contract DerivedContract is Modifiers {

constructor(uint256 _baseValue) Modifiers(_baseValue) {

}

function show() public override pure returns(string memory) {
return "This is the derived contract";
}
}
48 changes: 48 additions & 0 deletions contracts/solidity/modifiers/Modifiers.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

contract Modifiers {
uint256 public data;
address public owner;

uint256 public constant MAX_SUPPLY = 1000000;
uint256 public immutable deploymentTimestamp;

event RegularEvent(address indexed from, address indexed to, uint256 value, string message);
event AnonymousEvent(address indexed sender, uint256 value) anonymous;

constructor(uint256 _initialData) {
data = _initialData;
owner = msg.sender;
deploymentTimestamp = block.timestamp;
}

function addPure(uint256 a, uint256 b) public pure returns (uint256) {
return a + b;
}

function makePayment() public payable {
require(msg.value > 0, "Payment amount should be greater than 0");
}

function getBalance() public view returns (uint256) {
return address(this).balance;
}

function getData() public view returns (uint256) {
return data;
}

function triggerRegularEvent(address _to, uint256 _value, string memory _message) public {
emit RegularEvent(msg.sender, _to, _value, _message);
}

function triggerAnonymousEvent(uint256 _value) public {
emit AnonymousEvent(msg.sender, _value);
}

function show() public virtual returns(string memory) {
return "This is the base Modifiers contract";
}

}
139 changes: 139 additions & 0 deletions test/solidity/modifiers/Modifiers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*-
*
* Hedera Smart Contracts
*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const { expect } = require("chai");
const { ethers } = require("hardhat");
const Utils = require('../../hts-precompile/utils');


describe("@solidityevmequiv1 Modifiers", function() {
let accounts, contractA, contractB, derivedContract, modifiersContract, owner;

const tinybarToWeibar = (amount) => amount.mul(Utils.tinybarToWeibarCoef)
const weibarTotinybar = (amount) => amount.div(Utils.tinybarToWeibarCoef)

beforeEach(async function() {
const Modifiers = await ethers.getContractFactory("Modifiers");
modifiersContract = await Modifiers.deploy(42);
await modifiersContract.deployed();

const Derived = await ethers.getContractFactory("DerivedContract");
derivedContract = await Derived.deploy(55);
await derivedContract.deployed();

const ContractA = await ethers.getContractFactory("A");
contractA = await ContractA.deploy();
await contractA.deployed();

const ContractB = await ethers.getContractFactory("B");
contractB = await ContractB.deploy(79);
await contractB.deployed();

[owner] = await ethers.getSigners();
accounts = await ethers.getSigners();
});

it("Should not modify the contract's state after calling a pure function", async function() {
const initialState = await ethers.provider.getCode(modifiersContract.address);

const result = await modifiersContract.addPure(7, 5);
expect(result).to.equal(12);

const finalState = await ethers.provider.getCode(modifiersContract.address);
expect(initialState).to.equal(finalState);
});

it("Should not modify the contract's state when calling a view function", async function() {
const initialState = await ethers.provider.getStorageAt(modifiersContract.address, 0);

const result = await modifiersContract.getData();
expect(result).to.equal(42);

const finalState = await ethers.provider.getStorageAt(modifiersContract.address, 0);
expect(initialState).to.equal(finalState);
});

it("Should accept payments and increase the contract's balance", async function() {
const initialBalance = await modifiersContract.getBalance();

const paymentAmount = weibarTotinybar(ethers.utils.parseEther("100"));
await owner.sendTransaction({
to: modifiersContract.address,
value: paymentAmount,
data: modifiersContract.interface.encodeFunctionData("makePayment")
});

const finalBalance = await modifiersContract.getBalance();
expect(tinybarToWeibar(finalBalance.add(initialBalance))).to.equal(paymentAmount);
});

it("Should have the correct MAX_SUPPLY value", async function() {
const maxSupply = await modifiersContract.MAX_SUPPLY();
expect(maxSupply).to.equal(1000000);
});

it("Should set deploymentTimestamp to the block timestamp of deployment", async function() {
const txReceipt = await modifiersContract.deployTransaction.wait();
const block = await ethers.provider.getBlock(txReceipt.blockHash);

const deploymentTimestamp = await modifiersContract.deploymentTimestamp();
expect(deploymentTimestamp).to.equal(block.timestamp);
});

it("Should emit indexed from and to values in the RegularEvent", async function() {
const toAddress = accounts[1].address;
const tx = await modifiersContract.triggerRegularEvent(toAddress, 100, "test transfer");
const receipt = await tx.wait();

expect(receipt.events?.length).to.equal(1);
const event = receipt.events[0];

// Check the event's topics. The first topic is the event's signature.
// The next topics are the indexed parameters in the order they appear in the event.
expect(event.topics[1].toLowerCase()).to.equal(ethers.utils.hexZeroPad(accounts[0].address, 32).toLowerCase()); // from address
expect(event.topics[2].toLowerCase()).to.equal(ethers.utils.hexZeroPad(toAddress, 32).toLowerCase()); // to address
});

it("Should emit the AnonymousEvent with correct values", async function() {
const tx = await modifiersContract.triggerAnonymousEvent(257);
const receipt = await tx.wait();

expect(receipt.events?.length).to.equal(1);

const anonymousEvent = receipt.events[0];
expect(anonymousEvent.event).to.undefined;

// Since it's anonymous, we access the topics directly to get the indexed values.
const senderAddress = "0x" + anonymousEvent.topics[0].slice(-40);
expect(senderAddress.toLowerCase()).to.equal(accounts[0].address.toLowerCase());
const value = ethers.utils.defaultAbiCoder.decode(["uint256"], anonymousEvent.data);
expect(value[0]).to.equal(257);
});

it("Should return the message in the from the derived contract that overrides the virtual function", async function() {
expect(await derivedContract.getData()).to.equal(55);
expect(await derivedContract.show()).to.equal("This is the derived contract");
});

it("Should return the message in the from ContractB that overrides the show function", async function() {
expect(await contractB.getData()).to.equal(79);
expect(await contractB.show()).to.equal("This is the overriding contract B");
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: a few of these have an extra line before the final closing bracket

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

});
Loading