Skip to content

Commit 2d286ca

Browse files
committed
internal methods for mint/burn
1 parent 9be2951 commit 2d286ca

File tree

2 files changed

+215
-202
lines changed

2 files changed

+215
-202
lines changed

contracts/WAMPL.sol

Lines changed: 85 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,57 @@ contract WAMPL is ERC20, ERC20Permit {
5656
//--------------------------------------------------------------------------
5757
// WAMPL write methods
5858

59+
/// @notice Transfers AMPLs from {msg.sender} and mints wAMPLs.
60+
///
61+
/// @param wamples The amount of wAMPLs to mint.
62+
/// @return The amount of AMPLs deposited.
63+
function mint(uint256 wamples) external returns (uint256) {
64+
uint256 amples = _wampleToAmple(wamples, _queryAMPLSupply());
65+
_deposit(_msgSender(), _msgSender(), amples, wamples);
66+
return amples;
67+
}
68+
69+
/// @notice Transfers AMPLs from {msg.sender} and mints wAMPLs,
70+
/// to the specified beneficiary.
71+
///
72+
/// @param to The beneficiary wallet.
73+
/// @param wamples The amount of wAMPLs to mint.
74+
/// @return The amount of AMPLs deposited.
75+
function mintFor(address to, uint256 wamples) external returns (uint256) {
76+
uint256 amples = _wampleToAmple(wamples, _queryAMPLSupply());
77+
_deposit(_msgSender(), to, amples, wamples);
78+
return amples;
79+
}
80+
81+
/// @notice Burns wAMPLs from {msg.sender} and transfers AMPLs back.
82+
///
83+
/// @param wamples The amount of wAMPLs to burn.
84+
/// @return The amount of AMPLs withdrawn.
85+
function burn(uint256 wamples) external returns (uint256) {
86+
uint256 amples = _wampleToAmple(wamples, _queryAMPLSupply());
87+
_withdraw(_msgSender(), _msgSender(), amples, wamples);
88+
return amples;
89+
}
90+
91+
/// @notice Burns wAMPLs from {msg.sender} and transfers AMPLs back,
92+
/// to the specified beneficiary.
93+
///
94+
/// @param to The beneficiary wallet.
95+
/// @param wamples The amount of wAMPLs to burn.
96+
/// @return The amount of AMPLs withdrawn.
97+
function burnTo(address to, uint256 wamples) external returns (uint256) {
98+
uint256 amples = _wampleToAmple(wamples, _queryAMPLSupply());
99+
_withdraw(_msgSender(), to, amples, wamples);
100+
return amples;
101+
}
102+
59103
/// @notice Transfers AMPLs from {msg.sender} and mints wAMPLs.
60104
///
61105
/// @param amples The amount of AMPLs to deposit.
62106
/// @return The amount of wAMPLs minted.
63107
function deposit(uint256 amples) external returns (uint256) {
64108
uint256 wamples = _ampleToWample(amples, _queryAMPLSupply());
65-
66-
IERC20(_ampl).safeTransferFrom(_msgSender(), address(this), amples);
67-
68-
_mint(_msgSender(), wamples);
69-
109+
_deposit(_msgSender(), _msgSender(), amples, wamples);
70110
return wamples;
71111
}
72112

@@ -78,11 +118,7 @@ contract WAMPL is ERC20, ERC20Permit {
78118
/// @return The amount of wAMPLs minted.
79119
function depositFor(address to, uint256 amples) external returns (uint256) {
80120
uint256 wamples = _ampleToWample(amples, _queryAMPLSupply());
81-
82-
IERC20(_ampl).safeTransferFrom(_msgSender(), address(this), amples);
83-
84-
_mint(to, wamples);
85-
121+
_deposit(_msgSender(), to, amples, wamples);
86122
return wamples;
87123
}
88124

@@ -92,11 +128,7 @@ contract WAMPL is ERC20, ERC20Permit {
92128
/// @return The amount of burnt wAMPLs.
93129
function withdraw(uint256 amples) external returns (uint256) {
94130
uint256 wamples = _ampleToWample(amples, _queryAMPLSupply());
95-
96-
_burn(_msgSender(), wamples);
97-
98-
IERC20(_ampl).safeTransfer(_msgSender(), amples);
99-
131+
_withdraw(_msgSender(), _msgSender(), amples, wamples);
100132
return wamples;
101133
}
102134

@@ -108,11 +140,7 @@ contract WAMPL is ERC20, ERC20Permit {
108140
/// @return The amount of burnt wAMPLs.
109141
function withdrawTo(address to, uint256 amples) external returns (uint256) {
110142
uint256 wamples = _ampleToWample(amples, _queryAMPLSupply());
111-
112-
_burn(_msgSender(), wamples);
113-
114-
IERC20(_ampl).safeTransfer(to, amples);
115-
143+
_withdraw(_msgSender(), to, amples, wamples);
116144
return wamples;
117145
}
118146

@@ -121,13 +149,8 @@ contract WAMPL is ERC20, ERC20Permit {
121149
/// @return The amount of burnt wAMPLs.
122150
function withdrawAll() external returns (uint256) {
123151
uint256 wamples = balanceOf(_msgSender());
124-
125152
uint256 amples = _wampleToAmple(wamples, _queryAMPLSupply());
126-
127-
_burn(_msgSender(), wamples);
128-
129-
IERC20(_ampl).safeTransfer(_msgSender(), amples);
130-
153+
_withdraw(_msgSender(), _msgSender(), amples, wamples);
131154
return wamples;
132155
}
133156

@@ -138,76 +161,11 @@ contract WAMPL is ERC20, ERC20Permit {
138161
/// @return The amount of burnt wAMPLs.
139162
function withdrawAllTo(address to) external returns (uint256) {
140163
uint256 wamples = balanceOf(_msgSender());
141-
142164
uint256 amples = _wampleToAmple(wamples, _queryAMPLSupply());
143-
144-
_burn(_msgSender(), wamples);
145-
146-
IERC20(_ampl).safeTransfer(to, amples);
147-
165+
_withdraw(_msgSender(), to, amples, wamples);
148166
return wamples;
149167
}
150168

151-
/// @notice Transfers AMPLs from {msg.sender} and mints wAMPLs.
152-
///
153-
/// @param wamples The amount of wAMPLs to mint.
154-
/// @return The amount of AMPLs deposited.
155-
function mint(uint256 wamples) external returns (uint256) {
156-
uint256 amples = _wampleToAmple(wamples, _queryAMPLSupply());
157-
158-
IERC20(_ampl).safeTransferFrom(_msgSender(), address(this), amples);
159-
160-
_mint(_msgSender(), wamples);
161-
162-
return amples;
163-
}
164-
165-
/// @notice Transfers AMPLs from {msg.sender} and mints wAMPLs,
166-
/// to the specified beneficiary.
167-
///
168-
/// @param to The beneficiary wallet.
169-
/// @param wamples The amount of wAMPLs to mint.
170-
/// @return The amount of AMPLs deposited.
171-
function mintTo(address to, uint256 wamples) external returns (uint256) {
172-
uint256 amples = _wampleToAmple(wamples, _queryAMPLSupply());
173-
174-
IERC20(_ampl).safeTransferFrom(_msgSender(), address(this), amples);
175-
176-
_mint(to, wamples);
177-
178-
return amples;
179-
}
180-
181-
/// @notice Burns wAMPLs from {msg.sender} and transfers AMPLs back.
182-
///
183-
/// @param wamples The amount of wAMPLs to burn.
184-
/// @return The amount of AMPLs withdrawn.
185-
function burn(uint256 wamples) external returns (uint256) {
186-
uint256 amples = _wampleToAmple(wamples, _queryAMPLSupply());
187-
188-
_burn(_msgSender(), wamples);
189-
190-
IERC20(_ampl).safeTransfer(_msgSender(), amples);
191-
192-
return amples;
193-
}
194-
195-
/// @notice Burns wAMPLs from {msg.sender} and transfers AMPLs back,
196-
/// to the specified beneficiary.
197-
///
198-
/// @param to The beneficiary wallet.
199-
/// @param wamples The amount of wAMPLs to burn.
200-
/// @return The amount of AMPLs withdrawn.
201-
function burnTo(address to, uint256 wamples) external returns (uint256) {
202-
uint256 amples = _wampleToAmple(wamples, _queryAMPLSupply());
203-
204-
_burn(_msgSender(), wamples);
205-
206-
IERC20(_ampl).safeTransfer(to, amples);
207-
208-
return amples;
209-
}
210-
211169
//--------------------------------------------------------------------------
212170
// WAMPL view methods
213171

@@ -228,21 +186,54 @@ contract WAMPL is ERC20, ERC20Permit {
228186
}
229187

230188
/// @param amples The amount of AMPL tokens.
231-
/// @return The amount of wAMPL tokens mintable.
189+
/// @return The amount of wAMPL tokens exchangeable.
232190
function underlyingToWrapper(uint256 amples) external view returns (uint256) {
233191
return _ampleToWample(amples, _queryAMPLSupply());
234192
}
235193

236194
/// @param wamples The amount of wAMPL tokens.
237-
/// @return The amount of AMPL tokens redeemable.
195+
/// @return The amount of AMPL tokens exchangeable.
238196
function wrapperToUnderlying(uint256 wamples) external view returns (uint256) {
239197
return _wampleToAmple(wamples, _queryAMPLSupply());
240198
}
241199

242200
//--------------------------------------------------------------------------
243201
// Private methods
244202

203+
/// @dev Internal helper function to handle deposit state change.
204+
/// @param from The initiator wallet.
205+
/// @param to The beneficiary wallet.
206+
/// @param amples The amount of AMPLs to deposit.
207+
/// @param wamples The amount of wAMPLs to mint.
208+
function _deposit(
209+
address from,
210+
address to,
211+
uint256 amples,
212+
uint256 wamples
213+
) private {
214+
IERC20(_ampl).safeTransferFrom(from, address(this), amples);
215+
216+
_mint(to, wamples);
217+
}
218+
219+
/// @dev Internal helper function to handle withdraw state change.
220+
/// @param from The initiator wallet.
221+
/// @param to The beneficiary wallet.
222+
/// @param amples The amount of AMPLs to withdraw.
223+
/// @param wamples The amount of wAMPLs to burn.
224+
function _withdraw(
225+
address from,
226+
address to,
227+
uint256 amples,
228+
uint256 wamples
229+
) private {
230+
_burn(from, wamples);
231+
232+
IERC20(_ampl).safeTransfer(to, amples);
233+
}
234+
245235
/// @dev Queries the current total supply of AMPL.
236+
/// @return The current AMPL supply.
246237
function _queryAMPLSupply() private view returns (uint256) {
247238
return IERC20(_ampl).totalSupply();
248239
}

0 commit comments

Comments
 (0)