-
Notifications
You must be signed in to change notification settings - Fork 53
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3273962
Added the payable.
ebadiere c4549ff
Added immutable example.
ebadiere 26f5df8
Added indexed event parameters.
ebadiere d8dd523
Added Anonymous Event coverage.
ebadiere 3595422
Added an example of the virtual modifier.
ebadiere 894e4c6
Added override coverage.
ebadiere c8f4d58
Fixed license.
ebadiere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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"; | ||
} | ||
} |
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,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"; | ||
} | ||
} |
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,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"; | ||
} | ||
} |
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,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"; | ||
} | ||
|
||
} |
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,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"); | ||
}); | ||
|
||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.