github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/vmbridge/keeper/testdata/testerc20.sol (about) 1 // SPDX-License-Identifier: MIT 2 3 pragma solidity 0.8.7; 4 5 contract Exchange is ERC20 { 6 address public constant moduleAddress = 7 address(0xc63cf6c8E1f3DF41085E9d8Af49584dae1432b4f); 8 9 string public wasmContractAddress = "ex14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s6fqu27"; 10 11 event __OKCSendToWasm(string wasmAddr, string recipient, uint256 amount); 12 13 function initialize(string memory denom_, uint8 decimals_) public { 14 __ERC20_init(denom_, denom_, decimals_); 15 } 16 17 function native_denom() public view returns (string memory) { 18 return symbol(); 19 } 20 21 function updatewasmContractAddress(string memory addr) public { 22 wasmContractAddress = addr; 23 } 24 25 function mint(address recipient,uint256 amount) public { 26 _mint(recipient, amount); 27 } 28 29 30 function mintERC20(string calldata caller, address recipient,uint256 amount) public returns (bool) { 31 require(msg.sender == moduleAddress); 32 require(keccak256(abi.encodePacked(caller)) == keccak256(abi.encodePacked(wasmContractAddress))); 33 _mint(recipient, amount); 34 return true; 35 } 36 37 38 // send an "amount" of the contract token to recipient on wasm 39 function send_to_wasm(string memory recipient,string memory wasmContract , uint256 amount) public { 40 _burn(msg.sender, amount); 41 emit __OKCSendToWasm(wasmContract,recipient, amount); 42 } 43 } 44 45 contract ERC20 { 46 bool private initialized; 47 48 string private _name; 49 string private _symbol; 50 uint8 private _decimals; 51 uint256 private _totalSupply; 52 53 mapping(address => uint256) private _balances; 54 mapping(address => mapping(address => uint256)) private _allowances; 55 56 event Transfer(address indexed from, address indexed to, uint256 value); 57 event Approval( 58 address indexed owner, 59 address indexed spender, 60 uint256 value 61 ); 62 63 function __ERC20_init( 64 string memory name_, 65 string memory symbol_, 66 uint8 decimals_ 67 ) internal { 68 require(!initialized, "ERC20: already initialized;"); 69 initialized = true; 70 71 _name = name_; 72 _symbol = symbol_; 73 _decimals = decimals_; 74 } 75 76 function name() public view virtual returns (string memory) { 77 return _name; 78 } 79 80 function symbol() public view virtual returns (string memory) { 81 return _symbol; 82 } 83 84 function decimals() public view virtual returns (uint8) { 85 return _decimals; 86 } 87 88 function totalSupply() public view virtual returns (uint256) { 89 return _totalSupply; 90 } 91 92 function balanceOf(address account) public view virtual returns (uint256) { 93 return _balances[account]; 94 } 95 96 function transfer(address to, uint256 amount) 97 public 98 virtual 99 returns (bool) 100 { 101 address owner = msg.sender; 102 _transfer(owner, to, amount); 103 return true; 104 } 105 106 function allowance(address owner, address spender) 107 public 108 view 109 virtual 110 returns (uint256) 111 { 112 return _allowances[owner][spender]; 113 } 114 115 function approve(address spender, uint256 amount) 116 public 117 virtual 118 returns (bool) 119 { 120 address owner = msg.sender; 121 _approve(owner, spender, amount); 122 return true; 123 } 124 125 function transferFrom( 126 address from, 127 address to, 128 uint256 amount 129 ) public virtual returns (bool) { 130 address spender = msg.sender; 131 _spendAllowance(from, spender, amount); 132 _transfer(from, to, amount); 133 return true; 134 } 135 136 function increaseAllowance(address spender, uint256 addedValue) 137 public 138 virtual 139 returns (bool) 140 { 141 address owner = msg.sender; 142 _approve(owner, spender, _allowances[owner][spender] + addedValue); 143 return true; 144 } 145 146 function decreaseAllowance(address spender, uint256 subtractedValue) 147 public 148 virtual 149 returns (bool) 150 { 151 address owner = msg.sender; 152 uint256 currentAllowance = _allowances[owner][spender]; 153 require( 154 currentAllowance >= subtractedValue, 155 "ERC20: decreased allowance below zero" 156 ); 157 unchecked { 158 _approve(owner, spender, currentAllowance - subtractedValue); 159 } 160 161 return true; 162 } 163 164 function _transfer( 165 address from, 166 address to, 167 uint256 amount 168 ) internal virtual { 169 require(from != address(0), "ERC20: transfer from the zero address"); 170 require(to != address(0), "ERC20: transfer to the zero address"); 171 172 uint256 fromBalance = _balances[from]; 173 require( 174 fromBalance >= amount, 175 "ERC20: transfer amount exceeds balance" 176 ); 177 unchecked { 178 _balances[from] = fromBalance - amount; 179 } 180 _balances[to] += amount; 181 182 emit Transfer(from, to, amount); 183 } 184 185 function _mint(address account, uint256 amount) internal virtual { 186 require(account != address(0), "ERC20: mint to the zero address"); 187 188 _totalSupply += amount; 189 _balances[account] += amount; 190 emit Transfer(address(0), account, amount); 191 } 192 193 function _burn(address account, uint256 amount) internal virtual { 194 require(account != address(0), "ERC20: burn from the zero address"); 195 196 uint256 accountBalance = _balances[account]; 197 require(accountBalance >= amount, "ERC20: burn amount exceeds balance"); 198 unchecked { 199 _balances[account] = accountBalance - amount; 200 } 201 _totalSupply -= amount; 202 203 emit Transfer(account, address(0), amount); 204 } 205 206 function _approve( 207 address owner, 208 address spender, 209 uint256 amount 210 ) internal virtual { 211 require(owner != address(0), "ERC20: approve from the zero address"); 212 require(spender != address(0), "ERC20: approve to the zero address"); 213 214 _allowances[owner][spender] = amount; 215 emit Approval(owner, spender, amount); 216 } 217 218 function _spendAllowance( 219 address owner, 220 address spender, 221 uint256 amount 222 ) internal virtual { 223 uint256 currentAllowance = allowance(owner, spender); 224 if (currentAllowance != type(uint256).max) { 225 require( 226 currentAllowance >= amount, 227 "ERC20: insufficient allowance" 228 ); 229 unchecked { 230 _approve(owner, spender, currentAllowance - amount); 231 } 232 } 233 } 234 }