token.sol 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /// SPDX-License-Identifier: Apache-2.0
  2. contract token {
  3. address public admin;
  4. uint32 public decimals;
  5. string public name;
  6. string public symbol;
  7. constructor(
  8. address _admin,
  9. string memory _name,
  10. string memory _symbol,
  11. uint32 _decimals
  12. ) {
  13. admin = _admin;
  14. name = _name;
  15. symbol = _symbol;
  16. decimals = _decimals;
  17. }
  18. mapping(address => int128) public balances;
  19. mapping(address => mapping(address => int128)) public allowances;
  20. function mint(address to, int128 amount) public {
  21. require(amount >= 0, "Amount must be non-negative");
  22. admin.requireAuth();
  23. setBalance(to, balance(to) + amount);
  24. }
  25. function approve(address owner, address spender, int128 amount) public {
  26. require(amount >= 0, "Amount must be non-negative");
  27. owner.requireAuth();
  28. allowances[owner][spender] = amount;
  29. }
  30. function transfer(address from, address to, int128 amount) public {
  31. require(amount >= 0, "Amount must be non-negative");
  32. from.requireAuth();
  33. require(balance(from) >= amount, "Insufficient balance");
  34. setBalance(from, balance(from) - amount);
  35. setBalance(to, balance(to) + amount);
  36. }
  37. function transfer_from(
  38. address spender,
  39. address from,
  40. address to,
  41. int128 amount
  42. ) public {
  43. require(amount >= 0, "Amount must be non-negative");
  44. spender.requireAuth();
  45. require(balance(from) >= amount, "Insufficient balance");
  46. require(allowance(from, spender) >= amount, "Insufficient allowance");
  47. setBalance(from, balance(from) - amount);
  48. setBalance(to, balance(to) + amount);
  49. allowances[from][spender] -= amount;
  50. }
  51. function burn(address from, int128 amount) public {
  52. require(amount >= 0, "Amount must be non-negative");
  53. require(balance(from) >= amount, "Insufficient balance");
  54. from.requireAuth();
  55. setBalance(from, balance(from) - amount);
  56. }
  57. function burn_from(address spender, address from, int128 amount) public {
  58. require(amount >= 0, "Amount must be non-negative");
  59. spender.requireAuth();
  60. require(balance(from) >= amount, "Insufficient balance");
  61. require(allowance(from, spender) >= amount, "Insufficient allowance");
  62. setBalance(from, balance(from) - amount);
  63. allowances[from][spender] -= amount;
  64. }
  65. function setBalance(address addr, int128 amount) internal {
  66. balances[addr] = amount;
  67. }
  68. function balance(address addr) public view returns (int128) {
  69. return balances[addr];
  70. }
  71. function allowance(
  72. address owner,
  73. address spender
  74. ) public view returns (int128) {
  75. return allowances[owner][spender];
  76. }
  77. }