github.com/MetalBlockchain/subnet-evm@v0.4.9/contract-examples/contracts/ERC20NativeMinter.sol (about) 1 //SPDX-License-Identifier: MIT 2 pragma solidity ^0.8.0; 3 4 import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; 5 import "./AllowList.sol"; 6 import "./INativeMinter.sol"; 7 8 contract ERC20NativeMinter is ERC20, AllowList { 9 // Precompiled Native Minter Contract Address 10 address constant MINTER_ADDRESS = 0x0200000000000000000000000000000000000001; 11 // Designated Blackhole Address 12 address constant BLACKHOLE_ADDRESS = 0x0100000000000000000000000000000000000000; 13 string private constant TOKEN_NAME = "ERC20NativeMinterToken"; 14 string private constant TOKEN_SYMBOL = "XMPL"; 15 16 INativeMinter nativeMinter = INativeMinter(MINTER_ADDRESS); 17 18 event Deposit(address indexed dst, uint256 wad); 19 event Mintdrawal(address indexed src, uint256 wad); 20 21 constructor(uint256 initSupply) ERC20(TOKEN_NAME, TOKEN_SYMBOL) AllowList(MINTER_ADDRESS) { 22 // Mints INIT_SUPPLY to owner 23 _mint(_msgSender(), initSupply); 24 } 25 26 // Mints [amount] number of ERC20 token to [to] address. 27 function mint(address to, uint256 amount) external onlyOwner { 28 _mint(to, amount); 29 } 30 31 // Burns [amount] number of ERC20 token from [from] address. 32 function burn(address from, uint256 amount) external onlyOwner { 33 _burn(from, amount); 34 } 35 36 // Swaps [amount] number of ERC20 token for native coin. 37 function mintdraw(uint256 wad) external { 38 // Burn ERC20 token first. 39 _burn(_msgSender(), wad); 40 // Mints [amount] number of native coins (gas coin) to [msg.sender] address. 41 // Calls NativeMinter precompile through INativeMinter interface. 42 nativeMinter.mintNativeCoin(_msgSender(), wad); 43 emit Mintdrawal(_msgSender(), wad); 44 } 45 46 // Swaps [amount] number of native gas coins for ERC20 tokens. 47 function deposit() external payable { 48 // Burn native token by sending to BLACKHOLE_ADDRESS 49 payable(BLACKHOLE_ADDRESS).transfer(msg.value); 50 // Mint ERC20 token. 51 _mint(_msgSender(), msg.value); 52 emit Deposit(_msgSender(), msg.value); 53 } 54 55 function decimals() public view virtual override returns (uint8) { 56 return 18; 57 } 58 }