Skip to content

Commit beefe9d

Browse files
Prettier for solidity (#994)
1 parent 7f0abcb commit beefe9d

File tree

4 files changed

+71
-67
lines changed

4 files changed

+71
-67
lines changed

packages/hardhat/.prettierrc.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"plugins": ["prettier-plugin-solidity"],
23
"arrowParens": "avoid",
34
"printWidth": 120,
45
"tabWidth": 2,
@@ -7,12 +8,10 @@
78
{
89
"files": "*.sol",
910
"options": {
10-
"printWidth": 80,
11+
"printWidth": 120,
1112
"tabWidth": 4,
12-
"useTabs": true,
1313
"singleQuote": false,
14-
"bracketSpacing": true,
15-
"explicitTypes": "always"
14+
"bracketSpacing": true
1615
}
1716
}
1817
]

packages/hardhat/contracts/YourContract.sol

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,75 +13,66 @@ import "hardhat/console.sol";
1313
* @author BuidlGuidl
1414
*/
1515
contract YourContract {
16-
// State Variables
17-
address public immutable owner;
18-
string public greeting = "Building Unstoppable Apps!!!";
19-
bool public premium = false;
20-
uint256 public totalCounter = 0;
21-
mapping(address => uint) public userGreetingCounter;
16+
// State Variables
17+
address public immutable owner;
18+
string public greeting = "Building Unstoppable Apps!!!";
19+
bool public premium = false;
20+
uint256 public totalCounter = 0;
21+
mapping(address => uint) public userGreetingCounter;
2222

23-
// Events: a way to emit log statements from smart contract that can be listened to by external parties
24-
event GreetingChange(
25-
address indexed greetingSetter,
26-
string newGreeting,
27-
bool premium,
28-
uint256 value
29-
);
23+
// Events: a way to emit log statements from smart contract that can be listened to by external parties
24+
event GreetingChange(address indexed greetingSetter, string newGreeting, bool premium, uint256 value);
3025

31-
// Constructor: Called once on contract deployment
32-
// Check packages/hardhat/deploy/00_deploy_your_contract.ts
33-
constructor(address _owner) {
34-
owner = _owner;
35-
}
26+
// Constructor: Called once on contract deployment
27+
// Check packages/hardhat/deploy/00_deploy_your_contract.ts
28+
constructor(address _owner) {
29+
owner = _owner;
30+
}
3631

37-
// Modifier: used to define a set of rules that must be met before or after a function is executed
38-
// Check the withdraw() function
39-
modifier isOwner() {
40-
// msg.sender: predefined variable that represents address of the account that called the current function
41-
require(msg.sender == owner, "Not the Owner");
42-
_;
43-
}
32+
// Modifier: used to define a set of rules that must be met before or after a function is executed
33+
// Check the withdraw() function
34+
modifier isOwner() {
35+
// msg.sender: predefined variable that represents address of the account that called the current function
36+
require(msg.sender == owner, "Not the Owner");
37+
_;
38+
}
4439

45-
/**
46-
* Function that allows anyone to change the state variable "greeting" of the contract and increase the counters
47-
*
48-
* @param _newGreeting (string memory) - new greeting to save on the contract
49-
*/
50-
function setGreeting(string memory _newGreeting) public payable {
51-
// Print data to the hardhat chain console. Remove when deploying to a live network.
52-
console.log(
53-
"Setting new greeting '%s' from %s",
54-
_newGreeting,
55-
msg.sender
56-
);
40+
/**
41+
* Function that allows anyone to change the state variable "greeting" of the contract and increase the counters
42+
*
43+
* @param _newGreeting (string memory) - new greeting to save on the contract
44+
*/
45+
function setGreeting(string memory _newGreeting) public payable {
46+
// Print data to the hardhat chain console. Remove when deploying to a live network.
47+
console.log("Setting new greeting '%s' from %s", _newGreeting, msg.sender);
5748

58-
// Change state variables
59-
greeting = _newGreeting;
60-
totalCounter += 1;
61-
userGreetingCounter[msg.sender] += 1;
49+
// Change state variables
50+
greeting = _newGreeting;
51+
totalCounter += 1;
52+
userGreetingCounter[msg.sender] += 1;
6253

63-
// msg.value: built-in global variable that represents the amount of ether sent with the transaction
64-
if (msg.value > 0) {
65-
premium = true;
66-
} else {
67-
premium = false;
68-
}
54+
// msg.value: built-in global variable that represents the amount of ether sent with the transaction
55+
if (msg.value > 0) {
56+
premium = true;
57+
} else {
58+
premium = false;
59+
}
6960

70-
// emit: keyword used to trigger an event
71-
emit GreetingChange(msg.sender, _newGreeting, msg.value > 0, msg.value);
72-
}
61+
// emit: keyword used to trigger an event
62+
emit GreetingChange(msg.sender, _newGreeting, msg.value > 0, msg.value);
63+
}
7364

74-
/**
75-
* Function that allows the owner to withdraw all the Ether in the contract
76-
* The function can only be called by the owner of the contract as defined by the isOwner modifier
77-
*/
78-
function withdraw() public isOwner {
79-
(bool success, ) = owner.call{ value: address(this).balance }("");
80-
require(success, "Failed to send Ether");
81-
}
65+
/**
66+
* Function that allows the owner to withdraw all the Ether in the contract
67+
* The function can only be called by the owner of the contract as defined by the isOwner modifier
68+
*/
69+
function withdraw() public isOwner {
70+
(bool success, ) = owner.call{ value: address(this).balance }("");
71+
require(success, "Failed to send Ether");
72+
}
8273

83-
/**
84-
* Function that allows the contract to receive ETH
85-
*/
86-
receive() external payable {}
74+
/**
75+
* Function that allows the contract to receive ETH
76+
*/
77+
receive() external payable {}
8778
}

packages/hardhat/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"flatten": "hardhat flatten",
1313
"lint": "eslint --config ./.eslintrc.json --ignore-path ./.eslintignore ./*.ts ./deploy/**/*.ts ./scripts/**/*.ts ./test/**/*.ts",
1414
"lint-staged": "eslint --config ./.eslintrc.json --ignore-path ./.eslintignore",
15-
"format": "prettier --write ./*.ts ./deploy/**/*.ts ./scripts/**/*.ts ./test/**/*.ts",
15+
"format": "prettier --write './**/*.(ts|sol)'",
1616
"test": "REPORT_GAS=true hardhat test --network hardhat",
1717
"verify": "hardhat etherscan-verify",
1818
"hardhat-verify": "hardhat verify"
@@ -42,6 +42,7 @@
4242
"hardhat-deploy-ethers": "^0.4.2",
4343
"hardhat-gas-reporter": "^2.2.1",
4444
"prettier": "^3.3.3",
45+
"prettier-plugin-solidity": "^1.4.1",
4546
"solidity-coverage": "^0.8.13",
4647
"ts-node": "^10.9.1",
4748
"typechain": "^8.3.2",

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,6 +2290,7 @@ __metadata:
22902290
hardhat-deploy-ethers: ^0.4.2
22912291
hardhat-gas-reporter: ^2.2.1
22922292
prettier: ^3.3.3
2293+
prettier-plugin-solidity: ^1.4.1
22932294
qrcode: ^1.5.4
22942295
solidity-coverage: ^0.8.13
22952296
ts-node: ^10.9.1
@@ -11091,6 +11092,18 @@ __metadata:
1109111092
languageName: node
1109211093
linkType: hard
1109311094

11095+
"prettier-plugin-solidity@npm:^1.4.1":
11096+
version: 1.4.1
11097+
resolution: "prettier-plugin-solidity@npm:1.4.1"
11098+
dependencies:
11099+
"@solidity-parser/parser": ^0.18.0
11100+
semver: ^7.5.4
11101+
peerDependencies:
11102+
prettier: ">=2.3.0"
11103+
checksum: ac9f3cc525553a45e70f60898da5d4a7b733128cdd9893686364790ff688c56dd6eb0234620759dc6fabad4dc354a27097927b29ea7761c5814c64613c07222f
11104+
languageName: node
11105+
linkType: hard
11106+
1109411107
"prettier@npm:*, prettier@npm:^3.3.3":
1109511108
version: 3.3.3
1109611109
resolution: "prettier@npm:3.3.3"

0 commit comments

Comments
 (0)