Skip to content

Commit

Permalink
Prettier for solidity (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 authored Nov 25, 2024
1 parent 7f0abcb commit beefe9d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 67 deletions.
7 changes: 3 additions & 4 deletions packages/hardhat/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"plugins": ["prettier-plugin-solidity"],
"arrowParens": "avoid",
"printWidth": 120,
"tabWidth": 2,
Expand All @@ -7,12 +8,10 @@
{
"files": "*.sol",
"options": {
"printWidth": 80,
"printWidth": 120,
"tabWidth": 4,
"useTabs": true,
"singleQuote": false,
"bracketSpacing": true,
"explicitTypes": "always"
"bracketSpacing": true
}
}
]
Expand Down
115 changes: 53 additions & 62 deletions packages/hardhat/contracts/YourContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,75 +13,66 @@ import "hardhat/console.sol";
* @author BuidlGuidl
*/
contract YourContract {
// State Variables
address public immutable owner;
string public greeting = "Building Unstoppable Apps!!!";
bool public premium = false;
uint256 public totalCounter = 0;
mapping(address => uint) public userGreetingCounter;
// State Variables
address public immutable owner;
string public greeting = "Building Unstoppable Apps!!!";
bool public premium = false;
uint256 public totalCounter = 0;
mapping(address => uint) public userGreetingCounter;

// Events: a way to emit log statements from smart contract that can be listened to by external parties
event GreetingChange(
address indexed greetingSetter,
string newGreeting,
bool premium,
uint256 value
);
// Events: a way to emit log statements from smart contract that can be listened to by external parties
event GreetingChange(address indexed greetingSetter, string newGreeting, bool premium, uint256 value);

// Constructor: Called once on contract deployment
// Check packages/hardhat/deploy/00_deploy_your_contract.ts
constructor(address _owner) {
owner = _owner;
}
// Constructor: Called once on contract deployment
// Check packages/hardhat/deploy/00_deploy_your_contract.ts
constructor(address _owner) {
owner = _owner;
}

// Modifier: used to define a set of rules that must be met before or after a function is executed
// Check the withdraw() function
modifier isOwner() {
// msg.sender: predefined variable that represents address of the account that called the current function
require(msg.sender == owner, "Not the Owner");
_;
}
// Modifier: used to define a set of rules that must be met before or after a function is executed
// Check the withdraw() function
modifier isOwner() {
// msg.sender: predefined variable that represents address of the account that called the current function
require(msg.sender == owner, "Not the Owner");
_;
}

/**
* Function that allows anyone to change the state variable "greeting" of the contract and increase the counters
*
* @param _newGreeting (string memory) - new greeting to save on the contract
*/
function setGreeting(string memory _newGreeting) public payable {
// Print data to the hardhat chain console. Remove when deploying to a live network.
console.log(
"Setting new greeting '%s' from %s",
_newGreeting,
msg.sender
);
/**
* Function that allows anyone to change the state variable "greeting" of the contract and increase the counters
*
* @param _newGreeting (string memory) - new greeting to save on the contract
*/
function setGreeting(string memory _newGreeting) public payable {
// Print data to the hardhat chain console. Remove when deploying to a live network.
console.log("Setting new greeting '%s' from %s", _newGreeting, msg.sender);

// Change state variables
greeting = _newGreeting;
totalCounter += 1;
userGreetingCounter[msg.sender] += 1;
// Change state variables
greeting = _newGreeting;
totalCounter += 1;
userGreetingCounter[msg.sender] += 1;

// msg.value: built-in global variable that represents the amount of ether sent with the transaction
if (msg.value > 0) {
premium = true;
} else {
premium = false;
}
// msg.value: built-in global variable that represents the amount of ether sent with the transaction
if (msg.value > 0) {
premium = true;
} else {
premium = false;
}

// emit: keyword used to trigger an event
emit GreetingChange(msg.sender, _newGreeting, msg.value > 0, msg.value);
}
// emit: keyword used to trigger an event
emit GreetingChange(msg.sender, _newGreeting, msg.value > 0, msg.value);
}

/**
* Function that allows the owner to withdraw all the Ether in the contract
* The function can only be called by the owner of the contract as defined by the isOwner modifier
*/
function withdraw() public isOwner {
(bool success, ) = owner.call{ value: address(this).balance }("");
require(success, "Failed to send Ether");
}
/**
* Function that allows the owner to withdraw all the Ether in the contract
* The function can only be called by the owner of the contract as defined by the isOwner modifier
*/
function withdraw() public isOwner {
(bool success, ) = owner.call{ value: address(this).balance }("");
require(success, "Failed to send Ether");
}

/**
* Function that allows the contract to receive ETH
*/
receive() external payable {}
/**
* Function that allows the contract to receive ETH
*/
receive() external payable {}
}
3 changes: 2 additions & 1 deletion packages/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"flatten": "hardhat flatten",
"lint": "eslint --config ./.eslintrc.json --ignore-path ./.eslintignore ./*.ts ./deploy/**/*.ts ./scripts/**/*.ts ./test/**/*.ts",
"lint-staged": "eslint --config ./.eslintrc.json --ignore-path ./.eslintignore",
"format": "prettier --write ./*.ts ./deploy/**/*.ts ./scripts/**/*.ts ./test/**/*.ts",
"format": "prettier --write './**/*.(ts|sol)'",
"test": "REPORT_GAS=true hardhat test --network hardhat",
"verify": "hardhat etherscan-verify",
"hardhat-verify": "hardhat verify"
Expand Down Expand Up @@ -42,6 +42,7 @@
"hardhat-deploy-ethers": "^0.4.2",
"hardhat-gas-reporter": "^2.2.1",
"prettier": "^3.3.3",
"prettier-plugin-solidity": "^1.4.1",
"solidity-coverage": "^0.8.13",
"ts-node": "^10.9.1",
"typechain": "^8.3.2",
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2290,6 +2290,7 @@ __metadata:
hardhat-deploy-ethers: ^0.4.2
hardhat-gas-reporter: ^2.2.1
prettier: ^3.3.3
prettier-plugin-solidity: ^1.4.1
qrcode: ^1.5.4
solidity-coverage: ^0.8.13
ts-node: ^10.9.1
Expand Down Expand Up @@ -11091,6 +11092,18 @@ __metadata:
languageName: node
linkType: hard

"prettier-plugin-solidity@npm:^1.4.1":
version: 1.4.1
resolution: "prettier-plugin-solidity@npm:1.4.1"
dependencies:
"@solidity-parser/parser": ^0.18.0
semver: ^7.5.4
peerDependencies:
prettier: ">=2.3.0"
checksum: ac9f3cc525553a45e70f60898da5d4a7b733128cdd9893686364790ff688c56dd6eb0234620759dc6fabad4dc354a27097927b29ea7761c5814c64613c07222f
languageName: node
linkType: hard

"prettier@npm:*, prettier@npm:^3.3.3":
version: 3.3.3
resolution: "prettier@npm:3.3.3"
Expand Down

0 comments on commit beefe9d

Please sign in to comment.