Advertisement
so_hard

Untitled

Jan 21st, 2022
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1.  
  2. Storages = {}
  3.  
  4. function create(_name,_symbol,_totalSupply)
  5.     if Storages.publisher==nil then
  6.         Storages.publisher = sender;
  7.         Storages.name    = _name;
  8.         Storages.symbol  = _symbol;
  9.         Storages.decimals  = 8;
  10.         Storages.totalSupply  = _totalSupply;
  11.  
  12.         luaDB.SetValue("balances",sender,_totalSupply);
  13.  
  14.         lualib.TransferEvent(sender, "", "create: " .. addressThis);
  15.     end
  16. end
  17.  
  18. --返回ERC20代币的名字
  19. function name()
  20.     return Storages.name;
  21. end
  22.  
  23. --返回代币的简称
  24. function symbol()
  25.     return Storages.symbol;
  26. end
  27.  
  28. --返回token使用的小数点后几位。比如如果设置为3,就是支持0.001表示。
  29. function decimals()
  30.     return Storages.decimals;
  31. end
  32.  
  33. --返回token的总供应量
  34. function totalSupply()
  35.     return Storages.totalSupply;
  36. end
  37.  
  38. --返回某个地址(账户)的账户余额
  39. function balanceOf(_owner)
  40.     return luaDB.GetValue("balances",_owner);
  41. end
  42.  
  43. --从代币合约的调用者地址上转移 _value的数量token到的地址 _to
  44. function transfer(_to, _value)
  45.     local value_sender = luaDB.GetValue("balances",sender);
  46.     local value_to     = luaDB.GetValue("balances",_to);
  47.  
  48.     lualib.Assert( sender ~= _to , "transfer: sender == _to" );
  49.     lualib.Assert( biglib.Greater(value_sender,_value,true) , "transfer: balances not enough" );
  50.     lualib.Assert( biglib.Greater( biglib.Add(value_to , _value) , value_to,true) , "transfer: balances overflow" );
  51.  
  52.     value_sender = biglib.Sub(value_sender,_value);
  53.     value_to     = biglib.Add(value_to,_value);
  54.  
  55.     luaDB.SetValue("balances",sender,value_sender);
  56.     luaDB.SetValue("balances",_to,value_to);
  57.  
  58.     lualib.TransferEvent(sender, _to, _value);
  59.     return true;
  60. end
  61.  
  62. --允许 _spender多次从您的帐户转账,最高达 _value金额。 如果再次调用此函数,它将以 _value覆盖当前的余量
  63. function approve(_spender, _value)
  64.     local allowed_sender = luaDB.GetValue("allowed",sender);
  65.  
  66.     if allowed_sender == nil then
  67.         allowed_sender = {}
  68.     end
  69.  
  70.     allowed_sender[_spender] = _value;
  71.     luaDB.SetValue("allowed",sender,allowed_sender);
  72.  
  73.     lualib.TransferEvent(sender, _spender, _value);
  74.     return true;
  75. end
  76.  
  77. --从地址 _from发送数量为 _value的token到地址 _to
  78. function transferFrom(_from, _to, _value)
  79.  
  80.     local allowed_from = luaDB.GetValue("allowed",_from);
  81.     if allowed_from == nil then
  82.         allowed_from = {}
  83.     end
  84.  
  85.     lualib.Assert( _from ~= _to , "transfer: _from == _to" );
  86.     lualib.Assert( allowed_from[_to] ~= nil , "transfer: _from == nil" );
  87.  
  88.     local value_from = luaDB.GetValue("balances",_from);
  89.     local value_to   = luaDB.GetValue("balances",_to);
  90.  
  91.     if biglib.Greater(value_from,_value,true) and biglib.Greater(allowed_from[_to],_value,true)
  92.             and biglib.Greater( biglib.Add(value_to , _value) , value_to,true) then
  93.  
  94.         value_from = biglib.Sub(value_from,_value);
  95.         value_to   = biglib.Add(value_to,_value);
  96.         allowed_from[_to]  = biglib.Sub(allowed_from[_to],_value);
  97.  
  98.         luaDB.SetValue("balances",_from,value_from);
  99.         luaDB.SetValue("balances",_to,value_to);
  100.         luaDB.SetValue("allowed",_from,allowed_from);
  101.  
  102.         lualib.TransferEvent(sender, _to, _value);
  103.         return true;
  104.     end
  105.     lualib.Assert( false , "transferFrom: error" );
  106. end
  107.  
  108. --返回 _spender仍然被允许从 _owner提取的金额
  109. function allowance(_from, _spender)
  110.     local allowed_from = luaDB.GetValue("allowed",_from);
  111.     if allowed_from == nil then
  112.         allowed_from = {}
  113.     end
  114.  
  115.     return allowed_from[_spender]
  116. end
  117.  
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement