Wrapped Erc20 Tokendefi
WRAPPED ERC20 TOKEN
wrappedToken use for all ERC20 token, example: WETH for ETH.
deploy
- copy ERC20 address (for example deploy erc20 Mock)
- paste address as value in Wrapped contract constructor + name + symbol
- before anything: approve wrapped contract as/is spender from base erc20 token
deposit
: make wrapped token and locked base tokenwithdraw
: burn wrapped and return locked token to the life cycle- other functions same an ERC20
develop
- using
IWrapped
interface is custom/optional : if using this intrface, then inherit erc165 & inherite Wrapped contract. need to register
libraries
in this example use @openzeppelin/contracts --> sub --> erc20
if you like to use flat version, use this
ready/deployed
-
mock token here
Review
- Deposit
function deposit(uint256 amount) external returns (bool success) {
SafeERC20.safeTransferFrom(token, _msgSender(), address(this), amount);
mint(amount);
balances += amount;
require(isBalanced(), "check not pass");
emit Transfer(address(this), _msgSender(), amount);
success = true;
}
- Withdraw
function withdraw(uint256 amount) external returns (bool success) {
require(amount <= balances, "fund is low");
SafeERC20.safeTransfer(token, _msgSender(), amount);
burn(amount);
balances -= amount;
require(isBalanced(), "check not pass");
emit Transfer(address(this), _msgSender(), amount);
success = true;
}
function withdraw(address to, uint256 amount) external returns (bool success) {
require(amount <= balances, "fund is low");
SafeERC20.safeTransfer(token, to, amount);
burn(amount);
balances -= amount;
require(isBalanced(), "check not pass");
emit Transfer(address(this), to, amount);
success = true;
}
- Refund
function refund() external returns (bool success) {
// require(_terminate() == 0, "refund denied");
_terminate();
require(deposited[_msgSender()] == 0, "refund denied");
success = true;
}
function _terminate() internal returns (uint256) {
uint256 value = deposited[_msgSender()];
require(value <= balances, "fund is low");
require(value > 0, "gas spend for 0!");
_burn(_msgSender(), value);
deposited[_msgSender()] -= value;
balances -= value;
require(isBalanced(), "check not pass");
emit Transfer(address(this), _msgSender(), value);
return value;
}
- Mint
function mint(uint256 amount) internal {
uint256 tmp = deposited[_msgSender()];
_mint(_msgSender(), amount);
deposited[_msgSender()] += amount;
require(deposited[_msgSender()] > tmp, "amount can not be 0");
}
- Burn
function burn(uint256 amount) internal {
uint256 tmp = deposited[_msgSender()];
_burn(_msgSender(), amount);
deposited[_msgSender()] -= amount;
require(deposited[_msgSender()] < tmp, "amount can not be 0");
}