-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wip): introduce wrapped ip and predeploy (#282)
Introduce Wrapped IP and predeploy WIP in genesis issue: none
- Loading branch information
1 parent
1dddd27
commit ae61c40
Showing
8 changed files
with
206 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
pragma solidity ^0.8.23; | ||
|
||
import { ERC20 } from "solady/src/tokens/ERC20.sol"; | ||
/// @notice Wrapped IP implementation. | ||
/// @author Inspired by WETH9 (https://github.com/dapphub/ds-weth/blob/master/src/weth9.sol) | ||
contract WIP is ERC20 { | ||
/// @notice emitted when IP is deposited in exchange for WIP | ||
event Deposit(address indexed from, uint amount); | ||
/// @notice emitted when WIP is withdrawn in exchange for IP | ||
event Withdrawal(address indexed to, uint amount); | ||
/// @notice emitted when a transfer of IP fails | ||
error IPTransferFailed(); | ||
|
||
/// @notice triggered when IP is deposited in exchange for WIP | ||
receive() external payable { | ||
deposit(); | ||
} | ||
|
||
/// @notice deposits IP in exchange for WIP | ||
/// @dev the amount of IP deposited is equal to the amount of WIP minted | ||
function deposit() public payable { | ||
_mint(msg.sender, msg.value); | ||
emit Deposit(msg.sender, msg.value); | ||
} | ||
|
||
/// @notice withdraws WIP in exchange for IP | ||
/// @dev the amount of IP minted is equal to the amount of WIP burned | ||
/// @param value the amount of WIP to burn and withdraw | ||
function withdraw(uint value) external { | ||
_burn(msg.sender, value); | ||
(bool success, ) = msg.sender.call{ value: value }(""); | ||
if (!success) { | ||
revert IPTransferFailed(); | ||
} | ||
emit Withdrawal(msg.sender, value); | ||
} | ||
|
||
/// @notice returns the name of the token | ||
function name() public view override returns (string memory) { | ||
return "Wrapped IP"; | ||
} | ||
|
||
/// @notice returns the symbol of the token | ||
function symbol() public view override returns (string memory) { | ||
return "WIP"; | ||
} | ||
|
||
/// @dev Sets Permit2 contract's allowance to infinity. | ||
function _givePermit2InfiniteAllowance() internal pure override returns (bool) { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.23; | ||
|
||
import { Test } from "../utils/Test.sol"; | ||
import { WIP } from "../../src/token/WIP.sol"; | ||
|
||
contract ContractWithoutReceive {} | ||
|
||
contract WIPTest is Test { | ||
function testMetadata() public view { | ||
assertEq(wip.name(), "Wrapped IP"); | ||
assertEq(wip.symbol(), "WIP"); | ||
assertEq(wip.decimals(), 18); | ||
} | ||
|
||
function testFallbackDeposit() public { | ||
assertEq(wip.balanceOf(address(this)), 0); | ||
assertEq(wip.totalSupply(), 0); | ||
|
||
(bool success, ) = address(wip).call{ value: 1 ether }(""); | ||
assertTrue(success); | ||
|
||
assertEq(wip.balanceOf(address(this)), 1 ether); | ||
assertEq(wip.totalSupply(), 1 ether); | ||
} | ||
|
||
function testDeposit() public { | ||
assertEq(wip.balanceOf(address(this)), 0); | ||
assertEq(wip.totalSupply(), 0); | ||
|
||
wip.deposit{ value: 1 ether }(); | ||
|
||
assertEq(wip.balanceOf(address(this)), 1 ether); | ||
assertEq(wip.totalSupply(), 1 ether); | ||
} | ||
|
||
function testWithdraw() public { | ||
uint256 startingBalance = address(this).balance; | ||
|
||
wip.deposit{ value: 1 ether }(); | ||
|
||
wip.withdraw(1 ether); | ||
|
||
uint256 balanceAfterWithdraw = address(this).balance; | ||
|
||
assertEq(balanceAfterWithdraw, startingBalance); | ||
assertEq(wip.balanceOf(address(this)), 0); | ||
assertEq(wip.totalSupply(), 0); | ||
} | ||
|
||
function testPartialWithdraw() public { | ||
wip.deposit{ value: 1 ether }(); | ||
|
||
uint256 balanceBeforeWithdraw = address(this).balance; | ||
|
||
wip.withdraw(0.5 ether); | ||
|
||
uint256 balanceAfterWithdraw = address(this).balance; | ||
|
||
assertEq(balanceAfterWithdraw, balanceBeforeWithdraw + 0.5 ether); | ||
assertEq(wip.balanceOf(address(this)), 0.5 ether); | ||
assertEq(wip.totalSupply(), 0.5 ether); | ||
} | ||
|
||
function testWithdrawToContractWithoutReceiveReverts() public { | ||
address owner = address(new ContractWithoutReceive()); | ||
|
||
vm.deal(owner, 1 ether); | ||
|
||
vm.prank(owner); | ||
wip.deposit{ value: 1 ether }(); | ||
|
||
assertEq(wip.balanceOf(owner), 1 ether); | ||
|
||
vm.expectRevert(WIP.IPTransferFailed.selector); | ||
vm.prank(owner); | ||
wip.withdraw(1 ether); | ||
} | ||
|
||
function testFallbackDeposit(uint256 amount) public { | ||
amount = _bound(amount, 0, address(this).balance); | ||
|
||
assertEq(wip.balanceOf(address(this)), 0); | ||
assertEq(wip.totalSupply(), 0); | ||
|
||
(bool success, ) = address(wip).call{ value: amount }(""); | ||
assertTrue(success); | ||
|
||
assertEq(wip.balanceOf(address(this)), amount); | ||
assertEq(wip.totalSupply(), amount); | ||
} | ||
|
||
function testDeposit(uint256 amount) public { | ||
amount = _bound(amount, 0, address(this).balance); | ||
|
||
assertEq(wip.balanceOf(address(this)), 0); | ||
assertEq(wip.totalSupply(), 0); | ||
|
||
wip.deposit{ value: amount }(); | ||
|
||
assertEq(wip.balanceOf(address(this)), amount); | ||
assertEq(wip.totalSupply(), amount); | ||
} | ||
|
||
function testWithdraw(uint256 depositAmount, uint256 withdrawAmount) public { | ||
depositAmount = _bound(depositAmount, 0, address(this).balance); | ||
withdrawAmount = _bound(withdrawAmount, 0, depositAmount); | ||
|
||
wip.deposit{ value: depositAmount }(); | ||
|
||
uint256 balanceBeforeWithdraw = address(this).balance; | ||
|
||
wip.withdraw(withdrawAmount); | ||
|
||
uint256 balanceAfterWithdraw = address(this).balance; | ||
|
||
assertEq(balanceAfterWithdraw, balanceBeforeWithdraw + withdrawAmount); | ||
assertEq(wip.balanceOf(address(this)), depositAmount - withdrawAmount); | ||
assertEq(wip.totalSupply(), depositAmount - withdrawAmount); | ||
} | ||
|
||
receive() external payable {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters