decred.org/dcrdex@v1.0.5/dex/networks/erc20/contracts/TestToken.sol (about) 1 // SPDX-License-Identifier: MIT 2 3 // This is a simplified version of OpenZeppelin Contracts v4.4.0 (token/ERC20/ERC20.sol). 4 5 pragma solidity = 0.8.18; 6 7 contract TestToken { 8 event Transfer(address indexed _from, address indexed _to, uint256 _value); 9 10 mapping(address => uint256) private _balances; 11 mapping(address => mapping(address => uint256)) private _allowances; 12 13 uint256 private _totalSupply; 14 15 string private _name; 16 string private _symbol; 17 uint8 private _decimals; 18 19 /** 20 * @dev Sets the values for {name} and {symbol}. 21 * 22 * The default value of {decimals} is 18. To select a different value for 23 * {decimals} you should overload it. 24 * 25 * All two of these values are immutable: they can only be set once during 26 * construction. 27 */ 28 constructor(uint8 dec) { 29 _name = "TestToken"; 30 _symbol = "TST"; 31 _decimals = dec; 32 33 _totalSupply = 44000000000000000000000; 34 _balances[0x18D65FB8d60c1199bb1Ad381bE47aA692b482605] = 11000000000000000000000; // alpha 35 _balances[0x4F8eF3892B65ED7fc356fF473a2eF2aE5EC27A06] = 11000000000000000000000; // beta 36 _balances[0xd12aB7cf72CCf1f3882eC99DDc53CD415635C3bE] = 11000000000000000000000; // delta 37 } 38 39 /** 40 * @dev Returns the name of the token. 41 */ 42 function name() public view virtual returns (string memory) { 43 return _name; 44 } 45 46 /** 47 * @dev Returns the symbol of the token, usually a shorter version of the 48 * name. 49 */ 50 function symbol() public view virtual returns (string memory) { 51 return _symbol; 52 } 53 54 /** 55 * @dev Returns the number of decimals used to get its user representation. 56 * For example, if `decimals` equals `2`, a balance of `505` tokens should 57 * be displayed to a user as `5.05` (`505 / 10 ** 2`). 58 * 59 * Tokens usually opt for a value of 18, imitating the relationship between 60 * Ether and Wei. This is the value {ERC20} uses, unless this function is 61 * overridden; 62 * 63 * NOTE: This information is only used for _display_ purposes: it in 64 * no way affects any of the arithmetic of the contract, including 65 * {IERC20-balanceOf} and {IERC20-transfer}. 66 */ 67 function decimals() public view virtual returns (uint8) { 68 return _decimals; 69 } 70 71 /** 72 * @dev See {IERC20-totalSupply}. 73 */ 74 function totalSupply() public view virtual returns (uint256) { 75 return _totalSupply; 76 } 77 78 /** 79 * @dev See {IERC20-balanceOf}. 80 */ 81 function balanceOf(address account) public view virtual returns (uint256) { 82 return _balances[account]; 83 } 84 85 /** 86 * @dev See {IERC20-transfer}. 87 * 88 * Requirements: 89 * 90 * - `recipient` cannot be the zero address. 91 * - the caller must have a balance of at least `amount`. 92 */ 93 function transfer(address recipient, uint256 amount) public virtual returns (bool) { 94 _transfer(msg.sender, recipient, amount); 95 return true; 96 } 97 98 /** 99 * @dev See {IERC20-allowance}. 100 */ 101 function allowance(address owner, address spender) public view virtual returns (uint256) { 102 return _allowances[owner][spender]; 103 } 104 105 /** 106 * @dev See {IERC20-approve}. 107 * 108 * Requirements: 109 * 110 * - `spender` cannot be the zero address. 111 */ 112 function approve(address spender, uint256 amount) public virtual returns (bool) { 113 _approve(msg.sender, spender, amount); 114 return true; 115 } 116 117 function testApprove(address user, address spender, uint256 amount) public returns (bool) { 118 _approve(user, spender, amount); 119 return true; 120 } 121 122 /** 123 * @dev See {IERC20-transferFrom}. 124 * 125 * Emits an {Approval} event indicating the updated allowance. This is not 126 * required by the EIP. See the note at the beginning of {ERC20}. 127 * 128 * Requirements: 129 * 130 * - `sender` and `recipient` cannot be the zero address. 131 * - `sender` must have a balance of at least `amount`. 132 * - the caller must have allowance for ``sender``'s tokens of at least 133 * `amount`. 134 */ 135 function transferFrom( 136 address sender, 137 address recipient, 138 uint256 amount 139 ) public virtual returns (bool) { 140 _transfer(sender, recipient, amount); 141 142 uint256 currentAllowance = _allowances[sender][msg.sender]; 143 require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance"); 144 unchecked { 145 _approve(sender, msg.sender, currentAllowance - amount); 146 } 147 148 return true; 149 } 150 151 /** 152 * Airdrop is used to mint new tokens and give them to a user. 153 */ 154 function airdrop(address recipient, uint256 amount) public virtual { 155 _totalSupply += amount; 156 _balances[recipient] += amount; 157 } 158 159 /** 160 * @dev Moves `amount` of tokens from `sender` to `recipient`. 161 * 162 * This internal function is equivalent to {transfer}, and can be used to 163 * e.g. implement automatic token fees, slashing mechanisms, etc. 164 * 165 * Requirements: 166 * 167 * - `sender` cannot be the zero address. 168 * - `recipient` cannot be the zero address. 169 * - `sender` must have a balance of at least `amount`. 170 */ 171 function _transfer( 172 address sender, 173 address recipient, 174 uint256 amount 175 ) internal virtual { 176 require(sender != address(0), "ERC20: transfer from the zero address"); 177 require(recipient != address(0), "ERC20: transfer to the zero address"); 178 179 uint256 senderBalance = _balances[sender]; 180 require(senderBalance >= amount, "ERC20: transfer amount exceeds balance"); 181 unchecked { 182 _balances[sender] = senderBalance - amount; 183 } 184 _balances[recipient] += amount; 185 emit Transfer(sender, recipient, amount); 186 } 187 188 /** 189 * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens. 190 * 191 * This internal function is equivalent to `approve`, and can be used to 192 * e.g. set automatic allowances for certain subsystems, etc. 193 * 194 * Requirements: 195 * 196 * - `owner` cannot be the zero address. 197 * - `spender` cannot be the zero address. 198 */ 199 function _approve( 200 address owner, 201 address spender, 202 uint256 amount 203 ) internal virtual { 204 require(owner != address(0), "ERC20: approve from the zero address"); 205 require(spender != address(0), "ERC20: approve to the zero address"); 206 207 _allowances[owner][spender] = amount; 208 } 209 }