From beefe9dcd2743aaab3be5b85652b6c2f16162601 Mon Sep 17 00:00:00 2001 From: "Shiv Bhonde | shivbhonde.eth" Date: Mon, 25 Nov 2024 16:35:55 +0530 Subject: [PATCH] Prettier for solidity (#994) --- packages/hardhat/.prettierrc.json | 7 +- packages/hardhat/contracts/YourContract.sol | 115 +++++++++----------- packages/hardhat/package.json | 3 +- yarn.lock | 13 +++ 4 files changed, 71 insertions(+), 67 deletions(-) diff --git a/packages/hardhat/.prettierrc.json b/packages/hardhat/.prettierrc.json index 8b9b8b776..1b1061e7a 100644 --- a/packages/hardhat/.prettierrc.json +++ b/packages/hardhat/.prettierrc.json @@ -1,4 +1,5 @@ { + "plugins": ["prettier-plugin-solidity"], "arrowParens": "avoid", "printWidth": 120, "tabWidth": 2, @@ -7,12 +8,10 @@ { "files": "*.sol", "options": { - "printWidth": 80, + "printWidth": 120, "tabWidth": 4, - "useTabs": true, "singleQuote": false, - "bracketSpacing": true, - "explicitTypes": "always" + "bracketSpacing": true } } ] diff --git a/packages/hardhat/contracts/YourContract.sol b/packages/hardhat/contracts/YourContract.sol index 3d364a0ee..053581d9d 100644 --- a/packages/hardhat/contracts/YourContract.sol +++ b/packages/hardhat/contracts/YourContract.sol @@ -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 {} } diff --git a/packages/hardhat/package.json b/packages/hardhat/package.json index c60c8d1c6..2a17b3ebe 100644 --- a/packages/hardhat/package.json +++ b/packages/hardhat/package.json @@ -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" @@ -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", diff --git a/yarn.lock b/yarn.lock index 55fbddf51..0260af23f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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"