The contracts, EmpressToken
and WrappedEmpressToken
, work together to create a system where a native token (EmpressToken
) can be "wrapped" into a different token (WrappedEmpressToken
or WEMP
).
EmpressToken
is a basic ERC-20 token contract representing a token named "Empress Token" with the symbol "EMP". It allows users to buy the tokens and transfer them.
constructor() ERC20("Empress Token", "EMP") {
_mint(address(this), 1000*(10 ** uint256(decimals())));
}
The constructor initializes the contract by minting 1,000 EMP
tokens to the contract itself. The tokens are assigned to the contract’s address (address(this)
).
- BuyToken:
Allows users to buy
EMP
tokens by transferring tokens from the contract’s balance to the user’s address.
function BuyToken(uint _amount) public {
require(balanceOf(address(this)) >= _amount, "Not enough tokens in contract");
transferToken(address(this), msg.sender, _amount);
}
- Logic:
- The function checks if the contract has enough tokens to fulfill the purchase.
- If enough tokens are available, it transfers the specified amount of
EMP
tokens from the contract to the caller (buyer).
- transferToken:
Transfers
EMP
tokens between two addresses.
function transferToken(address from, address to, uint amount) public {
_transfer(from, to, amount*(10 ** uint256(decimals())));
}
- Logic:
- The function multiplies the token amount by the token's decimal factor and then calls the internal
_transfer
function of the ERC-20 contract to move the tokens from one address to another.
- The function multiplies the token amount by the token's decimal factor and then calls the internal
WrappedEmpressToken
(symbol "WEMP") allows users to wrap and unwrap EmpressToken
(EMP) into WEMP
tokens. This is a common practice in decentralized finance (DeFi) to wrap tokens into a new token format, which might have different uses or functionality.
constructor(address _empressToken) ERC20("Wrapped Empress Token", "WEMP") {
empressToken = EmpressToken(_empressToken);
}
- Purpose:
The constructor initializes the
WrappedEmpressToken
contract and links it to theEmpressToken
contract by passing its address as_empressToken
.
- wrap:
Allows users to wrap their
EMP
tokens intoWEMP
tokens.
function wrap(uint256 _amount) external {
require(_amount > 0, "Amount must be greater than zero");
// Transfer EMP tokens from the user to this contract
empressToken.transferToken(msg.sender, address(this), _amount);
// Mint WEMP tokens to the user
_mint(msg.sender, _amount*(10 ** uint256(decimals())));
}
- Logic:
- First, it checks that the amount of tokens being wrapped is greater than zero.
- The function transfers
EMP
tokens from the user’s address to the contract’s address (address(this)
). - After receiving the
EMP
tokens, the contract mints the same amount ofWEMP
tokens for the user, scaled by the token’s decimal factor.
- unwrap:
Allows users to unwrap their
WEMP
tokens back intoEMP
tokens.
function unwrap(uint256 _amount) external {
require(_amount > 0, "Amount must be greater than zero");
// Burn WEMP tokens from the user
_burn(msg.sender, _amount*(10 ** uint256(decimals())));
// Transfer the corresponding amount of EMP tokens back to the user
empressToken.transferToken(address(this),msg.sender, _amount);
}
- Logic:
- First, it checks that the amount of tokens to be unwrapped is greater than zero.
- The contract burns the specified amount of
WEMP
tokens from the user. - Then, the contract transfers the same amount of
EMP
tokens back to the user from the contract’s balance.
-
Wrapping (EMP to WEMP):
- The user sends
EMP
tokens to theWrappedEmpressToken
contract. - For every 1
EMP
sent, the contract mints 1WEMP
token to the user, thus "wrapping" the tokens.
- The user sends
-
Unwrapping (WEMP to EMP):
- The user sends
WEMP
tokens back to the contract. - The contract burns these
WEMP
tokens and returns the corresponding amount ofEMP
tokens to the user, effectively "unwrapping" the tokens.
- The user sends
- EmpressToken : https://holesky.etherscan.io/address/0x30F22Ac0698f882Fb6544B98B66E5e00eC7B6A67
- WrappedEmpress (Main contract) : https://holesky.etherscan.io/address/0xef744d1cfF05d93832a3CEbA2821d835018EDD41