Skip to content

Commit ab2eb62

Browse files
committed
added burn all and burn all to
1 parent 2d286ca commit ab2eb62

File tree

2 files changed

+125
-7
lines changed

2 files changed

+125
-7
lines changed

contracts/WAMPL.sol

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ contract WAMPL is ERC20, ERC20Permit {
100100
return amples;
101101
}
102102

103+
/// @notice Burns all wAMPLs from {msg.sender} and transfers AMPLs back.
104+
///
105+
/// @return The amount of AMPLs withdrawn.
106+
function burnAll() external returns (uint256) {
107+
uint256 wamples = balanceOf(_msgSender());
108+
uint256 amples = _wampleToAmple(wamples, _queryAMPLSupply());
109+
_withdraw(_msgSender(), _msgSender(), amples, wamples);
110+
return amples;
111+
}
112+
113+
/// @notice Burns all wAMPLs from {msg.sender} and transfers AMPLs back,
114+
/// to the specified beneficiary.
115+
///
116+
/// @param to The beneficiary wallet.
117+
/// @return The amount of AMPLs withdrawn.
118+
function burnAllTo(address to) external returns (uint256) {
119+
uint256 wamples = balanceOf(_msgSender());
120+
uint256 amples = _wampleToAmple(wamples, _queryAMPLSupply());
121+
_withdraw(_msgSender(), to, amples, wamples);
122+
return amples;
123+
}
124+
103125
/// @notice Transfers AMPLs from {msg.sender} and mints wAMPLs.
104126
///
105127
/// @param amples The amount of AMPLs to deposit.

test/unit/WAMPL.ts

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,102 @@ describe('WAMPL:withdrawTo', () => {
315315
})
316316
})
317317

318+
describe('WAMPL:withdrawAll', () => {
319+
beforeEach('setup WAMPL contract', setupContracts)
320+
321+
let r: any, amplesDeposited: BigNumber, wamplesMinted: BigNumber
322+
beforeEach(async function () {
323+
// 2% of AMPL total supply
324+
amplesDeposited = toAMPLFixedPt('1000000')
325+
326+
// 2 % of MAX_WAMPL_SUPPLY
327+
wamplesMinted = await wAMPL.underlyingToWrapper(amplesDeposited)
328+
329+
await ampl.connect(deployer).approve(wAMPL.address, amplesDeposited)
330+
await wAMPL.connect(deployer).deposit(amplesDeposited)
331+
332+
expect(await wAMPL.wrapperToUnderlying(wamplesMinted)).to.eq(
333+
amplesDeposited,
334+
)
335+
expect(await wAMPL.connect(deployer).callStatic.withdrawAll()).to.eq(
336+
wamplesMinted,
337+
)
338+
339+
r = wAMPL.connect(deployer).withdrawAll()
340+
await r
341+
})
342+
343+
it('should burn wamples', async function () {
344+
expect(await ampl.balanceOf(wAMPL.address)).to.eq('0')
345+
expect(await wAMPL.totalUnderlying()).to.eq('0')
346+
expect(await wAMPL.balanceOfUnderlying(deployerAddress)).to.eq('0')
347+
348+
expect(await wAMPL.totalSupply()).to.eq('0')
349+
expect(await wAMPL.balanceOf(deployerAddress)).to.eq('0')
350+
})
351+
352+
it('should log transfer', async function () {
353+
await expect(r)
354+
.to.emit(ampl, 'Transfer')
355+
.withArgs(wAMPL.address, deployerAddress, amplesDeposited)
356+
})
357+
358+
it('should log burn', async function () {
359+
await expect(r)
360+
.to.emit(wAMPL, 'Transfer')
361+
.withArgs(deployerAddress, ethers.constants.AddressZero, wamplesMinted)
362+
})
363+
})
364+
365+
describe('WAMPL:withdrawAllTo', () => {
366+
beforeEach('setup WAMPL contract', setupContracts)
367+
368+
let r: any, amplesDeposited: BigNumber, wamplesMinted: BigNumber
369+
beforeEach(async function () {
370+
// 2% of AMPL total supply
371+
amplesDeposited = toAMPLFixedPt('1000000')
372+
373+
// 2 % of MAX_WAMPL_SUPPLY
374+
wamplesMinted = await wAMPL.underlyingToWrapper(amplesDeposited)
375+
376+
await ampl.connect(deployer).approve(wAMPL.address, amplesDeposited)
377+
await wAMPL.connect(deployer).deposit(amplesDeposited)
378+
379+
expect(await wAMPL.wrapperToUnderlying(wamplesMinted)).to.eq(
380+
amplesDeposited,
381+
)
382+
expect(
383+
await wAMPL.connect(deployer).callStatic.withdrawAllTo(userBAddress),
384+
).to.eq(wamplesMinted)
385+
386+
r = wAMPL.connect(deployer).withdrawAllTo(userBAddress)
387+
await r
388+
})
389+
390+
it('should burn wamples', async function () {
391+
expect(await ampl.balanceOf(wAMPL.address)).to.eq('0')
392+
expect(await wAMPL.totalUnderlying()).to.eq('0')
393+
expect(await wAMPL.balanceOfUnderlying(userBAddress)).to.eq('0')
394+
expect(await wAMPL.balanceOfUnderlying(deployerAddress)).to.eq('0')
395+
396+
expect(await wAMPL.totalSupply()).to.eq('0')
397+
expect(await wAMPL.balanceOf(userBAddress)).to.eq('0')
398+
expect(await wAMPL.balanceOf(deployerAddress)).to.eq('0')
399+
})
400+
401+
it('should log transfer', async function () {
402+
await expect(r)
403+
.to.emit(ampl, 'Transfer')
404+
.withArgs(wAMPL.address, userBAddress, amplesDeposited)
405+
})
406+
407+
it('should log burn', async function () {
408+
await expect(r)
409+
.to.emit(wAMPL, 'Transfer')
410+
.withArgs(deployerAddress, ethers.constants.AddressZero, wamplesMinted)
411+
})
412+
})
413+
318414
describe('WAMPL:mint', () => {
319415
beforeEach('setup WAMPL contract', setupContracts)
320416

@@ -541,7 +637,7 @@ describe('WAMPL:burnTo', () => {
541637
})
542638
})
543639

544-
describe('WAMPL:withdrawAll', () => {
640+
describe('WAMPL:burnAll', () => {
545641
beforeEach('setup WAMPL contract', setupContracts)
546642

547643
let r: any, amplesDeposited: BigNumber, wamplesMinted: BigNumber
@@ -558,11 +654,11 @@ describe('WAMPL:withdrawAll', () => {
558654
expect(await wAMPL.wrapperToUnderlying(wamplesMinted)).to.eq(
559655
amplesDeposited,
560656
)
561-
expect(await wAMPL.connect(deployer).callStatic.withdrawAll()).to.eq(
562-
wamplesMinted,
657+
expect(await wAMPL.connect(deployer).callStatic.burnAll()).to.eq(
658+
amplesDeposited,
563659
)
564660

565-
r = wAMPL.connect(deployer).withdrawAll()
661+
r = wAMPL.connect(deployer).burnAll()
566662
await r
567663
})
568664

@@ -588,7 +684,7 @@ describe('WAMPL:withdrawAll', () => {
588684
})
589685
})
590686

591-
describe('WAMPL:withdrawAllTo', () => {
687+
describe('WAMPL:burnAllTo', () => {
592688
beforeEach('setup WAMPL contract', setupContracts)
593689

594690
let r: any, amplesDeposited: BigNumber, wamplesMinted: BigNumber
@@ -606,8 +702,8 @@ describe('WAMPL:withdrawAllTo', () => {
606702
amplesDeposited,
607703
)
608704
expect(
609-
await wAMPL.connect(deployer).callStatic.withdrawAllTo(userBAddress),
610-
).to.eq(wamplesMinted)
705+
await wAMPL.connect(deployer).callStatic.burnAllTo(userBAddress),
706+
).to.eq(amplesDeposited)
611707

612708
r = wAMPL.connect(deployer).withdrawAllTo(userBAddress)
613709
await r

0 commit comments

Comments
 (0)