Skip to content

Commit 308dbc5

Browse files
authored
Merge pull request #10 from kleros/refactor/wrapped-pinakion-formatting
Rfactor: improved comments and their formatting
2 parents b28846d + 0e3f7bc commit 308dbc5

File tree

2 files changed

+127
-95
lines changed

2 files changed

+127
-95
lines changed

.prettierrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"printWidth": 120
3+
}

contracts/tokens/WrappedPinakion.sol

Lines changed: 124 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,51 @@ pragma solidity ^0.4.24;
1010

1111
import "openzeppelin-eth/contracts/zos-lib/Initializable.sol";
1212
import "openzeppelin-eth/contracts/math/SafeMath.sol";
13-
import { TokenController } from "minimetoken/contracts/TokenController.sol";
14-
import { ITokenBridge } from "../interfaces/ITokenBridge.sol";
15-
import { IERC677 } from "../interfaces/IERC677.sol";
13+
import {TokenController} from "minimetoken/contracts/TokenController.sol";
14+
import {ITokenBridge} from "../interfaces/ITokenBridge.sol";
15+
import {IERC677} from "../interfaces/IERC677.sol";
1616

1717
contract WrappedPinakion is Initializable {
18-
1918
using SafeMath for uint256;
2019

2120
/* Events */
2221

2322
/**
24-
* @dev Emitted when `value` tokens are moved from one account (`from`) to another (`to`).
25-
*
26-
* Note that `value` may be zero.
27-
*/
23+
* @notice Emitted when `value` tokens are moved from one account (`from`) to another (`to`).
24+
* @dev Notice that `value` may be zero.
25+
*/
2826
event Transfer(address indexed from, address indexed to, uint256 value);
2927

3028
/**
31-
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
32-
* a call to {approve}. `value` is the new allowance.
33-
*/
29+
* @notice Emitted when the allowance of a `spender` for an `owner` is set by
30+
* a call to {approve}. `value` is the new allowance.
31+
*/
3432
event Approval(address indexed owner, address indexed spender, uint256 value);
3533

3634
/* Storage */
3735

38-
mapping (address => uint256) private balances;
39-
mapping (address => mapping (address => uint256)) public allowance;
36+
mapping(address => uint256) private balances;
37+
mapping(address => mapping(address => uint256)) public allowance;
4038

41-
/// @dev Total supply of the token. Equals the total xPinakion deposit into the contract.
39+
/// @notice Total supply of the token. Equals the total xPinakion deposit into the contract.
4240
uint256 public totalSupply;
4341

44-
/// @dev Name of the token.
42+
/// @notice Name of the token.
4543
string public name;
4644

47-
/// @dev Symbol of the token.
45+
/// @notice Symbol of the token.
4846
string public symbol;
4947

50-
/// @dev Number of decimals of the token.
48+
/// @notice Number of decimals of the token.
5149
uint8 public decimals;
5250

53-
/// @dev The token's controller.
51+
/// @notice The token's controller.
5452
address public controller;
5553

56-
/// @dev Pinakion on xDai to be wrapped. This token is upgradeable.
54+
/// @notice Pinakion on xDai to be wrapped. This token is upgradeable.
5755
IERC677 public xPinakion;
5856

59-
/// @dev xDai Token Bridge. The Token Bridge is upgradeable.
57+
/// @notice xDai Token Bridge. The Token Bridge is upgradeable.
6058
ITokenBridge public tokenBridge;
6159

6260
/* Modifiers */
@@ -69,16 +67,17 @@ contract WrappedPinakion is Initializable {
6967

7068
/* Initializer */
7169

72-
/** @dev Constructor.
73-
* @param _name for the wrapped Pinakion on the home chain.
74-
* @param _symbol for wrapped Pinakion ticker on the home chain.
75-
* @param _xPinakion the home pinakion contract which is already bridged to the foreign pinakion contract.
76-
* @param _tokenBridge the TokenBridge contract.
70+
/**
71+
* @dev Constructor.
72+
* @param _name for the wrapped Pinakion on the home chain.
73+
* @param _symbol for wrapped Pinakion ticker on the home chain.
74+
* @param _xPinakion the home pinakion contract which is already bridged to the foreign pinakion contract.
75+
* @param _tokenBridge the TokenBridge contract.
7776
*/
7877
function initialize(
79-
string memory _name,
80-
string memory _symbol,
81-
IERC677 _xPinakion,
78+
string memory _name,
79+
string memory _symbol,
80+
IERC677 _xPinakion,
8281
ITokenBridge _tokenBridge
8382
) public initializer {
8483
name = _name;
@@ -92,33 +91,40 @@ contract WrappedPinakion is Initializable {
9291

9392
/* External */
9493

95-
/** @dev Changes `controller` to `_controller`.
96-
* @param _controller The new controller of the contract
94+
/**
95+
* @notice Changes `controller` to `_controller`.
96+
* @param _controller The new controller of the contract
9797
*/
9898
function changeController(address _controller) external onlyController {
9999
controller = _controller;
100100
}
101101

102-
103-
/** @dev Converts bridged pinakions into pinakions which can be staked in KlerosLiquid.
104-
* @param _amount The amount of wrapped pinakions to mint.
102+
/**
103+
* @notice Converts bridged pinakions into pinakions which can be staked in KlerosLiquid.
104+
* @param _amount The amount of wrapped pinakions to mint.
105105
*/
106-
function deposit(uint _amount) external {
106+
function deposit(uint256 _amount) external {
107107
_mint(msg.sender, _amount);
108-
require(xPinakion.transferFrom(msg.sender, address(this), _amount), "Sender does not have enough approved funds.");
108+
require(
109+
xPinakion.transferFrom(msg.sender, address(this), _amount),
110+
"Sender does not have enough approved funds."
111+
);
109112
}
110113

111-
112-
/** @dev IERC20 Receiver functionality.
113-
* Converts bridged PNK (xPinakion) into wrapped PNK which can be staked in KlerosLiquid.
114-
* If the tokenBridge is calling this function, then this contract has already received
115-
* the xPinakion tokens.
116-
* @param _token The token address the _amount belongs to.
117-
* @param _amount The amount of wrapped pinakions to mint.
118-
* @param _data Calldata containing the address of the recipient.
119-
* Notice that the address has to be padded to 32 bytes.
114+
/**
115+
* @notice IERC20 Receiver functionality.
116+
* @dev Converts bridged PNK (xPinakion) into wrapped PNK which can be staked in KlerosLiquid.
117+
* If the tokenBridge is calling this function, then this contract has already received
118+
* the xPinakion tokens.
119+
* @param _token The token address the _amount belongs to.
120+
* @param _amount The amount of wrapped pinakions to mint.
121+
* @param _data Calldata containing the address of the recipient. Notice that the address has to be padded to 32 bytes.
120122
*/
121-
function onTokenBridged(address _token, uint _amount, bytes _data) external {
123+
function onTokenBridged(
124+
address _token,
125+
uint256 _amount,
126+
bytes _data
127+
) external {
122128
require(msg.sender == address(tokenBridge), "Sender not authorized.");
123129
require(_token == address(xPinakion), "Token bridged is not xPinakion.");
124130

@@ -129,30 +135,34 @@ contract WrappedPinakion is Initializable {
129135
_mint(recipient, _amount);
130136
}
131137

132-
/** @dev Withdraws bridged pinakions.
133-
* @param _amount The amount of bridged pinakions to withdraw.
138+
/**
139+
* @notice Withdraws bridged pinakions.
140+
* @param _amount The amount of bridged pinakions to withdraw.
134141
*/
135-
function withdraw(uint _amount) external {
142+
function withdraw(uint256 _amount) external {
136143
_burn(_amount);
137144
require(xPinakion.transfer(msg.sender, _amount), "The `transfer` function must not fail.");
138145
}
139146

140-
/** @dev This function is not strictly needed, but it provides a good UX to users who want to get their Mainnet's PNK back.
141-
* What normally takes 3 transactions, here is done in one go.
142-
* Notice that the PNK have to be claimed on mainnet's TokenBride by the receiver.
143-
* @param _amount The amount of bridged pinakions to withdraw.
144-
* @param _receiver The address which will receive the PNK back in the foreign chain.
145-
*/
146-
function withdrawAndConvertToPNK(uint _amount, address _receiver) external {
147+
/**
148+
* @notice Withdraws the WrappedPinakion and transfers it through the Token Bridge.
149+
* @dev This function is not strictly needed, but it provides a good UX to users who want to get their Mainnet's PNK back.
150+
* What normally takes 3 transactions, here is done in one go.
151+
* Notice that the PNK have to be claimed on mainnet's TokenBride by the receiver.
152+
* @param _amount The amount of bridged pinakions to withdraw.
153+
* @param _receiver The address which will receive the PNK back in the foreign chain.
154+
*/
155+
function withdrawAndConvertToPNK(uint256 _amount, address _receiver) external {
147156
_burn(_amount);
148157
// Using approve is safe here, because this contract approves the bridge to spend the tokens and triggers the relay immediately.
149158
xPinakion.approve(address(tokenBridge), _amount);
150159
tokenBridge.relayTokens(xPinakion, _receiver, _amount);
151160
}
152161

153-
/** @dev Moves `_amount` tokens from the caller's account to `_recipient`.
154-
* @param _recipient The entity receiving the funds.
155-
* @param _amount The amount to tranfer in base units.
162+
/**
163+
* @notice Moves `_amount` tokens from the caller's account to `_recipient`.
164+
* @param _recipient The entity receiving the funds.
165+
* @param _amount The amount to tranfer in base units.
156166
*/
157167
function transfer(address _recipient, uint256 _amount) public returns (bool) {
158168
if (isContract(controller)) {
@@ -164,13 +174,18 @@ contract WrappedPinakion is Initializable {
164174
return true;
165175
}
166176

167-
/** @dev Moves `_amount` tokens from `_sender` to `_recipient` using the
168-
* allowance mechanism. `_amount` is then deducted from the caller's allowance.
169-
* @param _sender The entity to take the funds from.
170-
* @param _recipient The entity receiving the funds.
171-
* @param _amount The amount to tranfer in base units.
177+
/**
178+
* @notice Moves `_amount` tokens from `_sender` to `_recipient` using the
179+
* allowance mechanism. `_amount` is then deducted from the caller's allowance.
180+
* @param _sender The entity to take the funds from.
181+
* @param _recipient The entity receiving the funds.
182+
* @param _amount The amount to tranfer in base units.
172183
*/
173-
function transferFrom(address _sender, address _recipient, uint256 _amount) public returns (bool) {
184+
function transferFrom(
185+
address _sender,
186+
address _recipient,
187+
uint256 _amount
188+
) public returns (bool) {
174189
if (isContract(controller)) {
175190
require(TokenController(controller).onTransfer(_sender, _recipient, _amount));
176191
}
@@ -183,53 +198,65 @@ contract WrappedPinakion is Initializable {
183198
if (msg.sender != controller) {
184199
allowance[_sender][msg.sender] = allowance[_sender][msg.sender].sub(_amount); // ERC20: transfer amount exceeds allowance.
185200
}
186-
201+
187202
balances[_sender] = balances[_sender].sub(_amount); // ERC20: transfer amount exceeds balance
188203
balances[_recipient] = balances[_recipient].add(_amount);
189204
emit Transfer(_sender, _recipient, _amount);
190205
return true;
191206
}
192207

193-
/** @dev Approves `_spender` to spend `_amount`.
194-
* @param _spender The entity allowed to spend funds.
195-
* @param _amount The amount of base units the entity will be allowed to spend.
208+
/**
209+
* @notice Approves `_spender` to spend `_amount`.
210+
* @param _spender The entity allowed to spend funds.
211+
* @param _amount The amount of base units the entity will be allowed to spend.
196212
*/
197213
function approve(address _spender, uint256 _amount) public returns (bool) {
198214
// Alerts the token controller of the approve function call
199215
if (isContract(controller)) {
200-
require(TokenController(controller).onApprove(msg.sender, _spender, _amount), "Token controller does not approve.");
216+
require(
217+
TokenController(controller).onApprove(msg.sender, _spender, _amount),
218+
"Token controller does not approve."
219+
);
201220
}
202221

203222
allowance[msg.sender][_spender] = _amount;
204223
emit Approval(msg.sender, _spender, _amount);
205224
return true;
206225
}
207226

208-
/** @dev Increases the `_spender` allowance by `_addedValue`.
209-
* @param _spender The entity allowed to spend funds.
210-
* @param _addedValue The amount of extra base units the entity will be allowed to spend.
227+
/**
228+
* @notice Increases the `_spender` allowance by `_addedValue`.
229+
* @param _spender The entity allowed to spend funds.
230+
* @param _addedValue The amount of extra base units the entity will be allowed to spend.
211231
*/
212232
function increaseAllowance(address _spender, uint256 _addedValue) public returns (bool) {
213233
uint256 newAllowance = allowance[msg.sender][_spender].add(_addedValue);
214234
// Alerts the token controller of the approve function call
215235
if (isContract(controller)) {
216-
require(TokenController(controller).onApprove(msg.sender, _spender, newAllowance), "Token controller does not approve.");
236+
require(
237+
TokenController(controller).onApprove(msg.sender, _spender, newAllowance),
238+
"Token controller does not approve."
239+
);
217240
}
218241

219242
allowance[msg.sender][_spender] = newAllowance;
220243
emit Approval(msg.sender, _spender, newAllowance);
221244
return true;
222245
}
223246

224-
/** @dev Decreases the `_spender` allowance by `_subtractedValue`.
225-
* @param _spender The entity whose spending allocation will be reduced.
226-
* @param _subtractedValue The reduction of spending allocation in base units.
247+
/**
248+
* @notice Decreases the `_spender` allowance by `_subtractedValue`.
249+
* @param _spender The entity whose spending allocation will be reduced.
250+
* @param _subtractedValue The reduction of spending allocation in base units.
227251
*/
228252
function decreaseAllowance(address _spender, uint256 _subtractedValue) public returns (bool) {
229253
uint256 newAllowance = allowance[msg.sender][_spender].sub(_subtractedValue); // ERC20: decreased allowance below zero
230254
// Alerts the token controller of the approve function call
231255
if (isContract(controller)) {
232-
require(TokenController(controller).onApprove(msg.sender, _spender, newAllowance), "Token controller does not approve.");
256+
require(
257+
TokenController(controller).onApprove(msg.sender, _spender, newAllowance),
258+
"Token controller does not approve."
259+
);
233260
}
234261

235262
allowance[msg.sender][_spender] = newAllowance;
@@ -240,20 +267,21 @@ contract WrappedPinakion is Initializable {
240267
/* Internal */
241268

242269
/**
243-
* @dev Internal function that mints an amount of the token and assigns it to
244-
* an account. This encapsulates the modification of balances such that the
245-
* proper events are emitted.
246-
* @param _recipient The address which will receive the minted tokens.
247-
* @param _amount The amount that will be created.
248-
*/
270+
* @dev Internal function that mints an amount of the token and assigns it to
271+
* an account. This encapsulates the modification of balances such that the
272+
* proper events are emitted.
273+
* @param _recipient The address which will receive the minted tokens.
274+
* @param _amount The amount that will be created.
275+
*/
249276
function _mint(address _recipient, uint256 _amount) internal {
250277
totalSupply = totalSupply.add(_amount);
251278
balances[_recipient] = balances[_recipient].add(_amount);
252279
emit Transfer(address(0x0), _recipient, _amount);
253280
}
254281

255-
/** @dev Destroys `_amount` tokens from the caller. Cannot burn locked tokens.
256-
* @param _amount The quantity of tokens to burn in base units.
282+
/**
283+
* @dev Destroys `_amount` tokens from the caller. Cannot burn locked tokens.
284+
* @param _amount The quantity of tokens to burn in base units.
257285
*/
258286
function _burn(uint256 _amount) internal {
259287
if (isContract(controller)) {
@@ -264,12 +292,13 @@ contract WrappedPinakion is Initializable {
264292
emit Transfer(msg.sender, address(0x0), _amount);
265293
}
266294

267-
/** @dev Internal function to determine if an address is a contract.
268-
* @param _addr The address being queried.
269-
* @return True if `_addr` is a contract.
295+
/**
296+
* @dev Internal function to determine if an address is a contract.
297+
* @param _addr The address being queried.
298+
* @return True if `_addr` is a contract.
270299
*/
271-
function isContract(address _addr) internal view returns(bool) {
272-
uint size;
300+
function isContract(address _addr) internal view returns (bool) {
301+
uint256 size;
273302
if (_addr == 0) return false;
274303
assembly {
275304
size := extcodesize(_addr)
@@ -280,11 +309,11 @@ contract WrappedPinakion is Initializable {
280309
/* Getters */
281310

282311
/**
283-
* @dev Gets the balance of the specified address.
284-
* @param _owner The address to query the balance of.
285-
* @return uint256 value representing the amount owned by the passed address.
286-
*/
312+
* @dev Gets the balance of the specified address.
313+
* @param _owner The address to query the balance of.
314+
* @return uint256 value representing the amount owned by the passed address.
315+
*/
287316
function balanceOf(address _owner) public view returns (uint256) {
288317
return balances[_owner];
289318
}
290-
}
319+
}

0 commit comments

Comments
 (0)