def Deploy(): """ Constructor of this contract. Only deployer hard-coded can call this function and cannot call this function after called once. Followings are initialization list for this token 1. Transfer the owner to the deployer. (Owner can mint and burn the token) 2. Supply initial coin to the deployer. """ ctx = GetContext() Require(CheckWitness(DEPLOYER)) # only can be initialized by deployer Require(not Get(ctx, 'DEPLOYED')) # only can deploy once # disable to deploy again Put(ctx, 'DEPLOYED', 1) # the first owner is the deployer # can transfer ownership to other by calling `transferOwner` function Put(ctx, OWNER_KEY, DEPLOYER) # supply the coin. All coin will belong to deployer. Put(ctx, MST_SUPPLY_KEY, INIT_SUPPLY * FACTOR) Put(ctx, concat(OWN_PREFIX, DEPLOYER), INIT_SUPPLY * FACTOR) return True
def _approve(_context, _from, _to, _amount): RequireScriptHash(_to) # to-address validation Require(_amount >= 0) # amount must be not minus value from_val = _balanceOf(_context, _from) approved_val = _allowance(_context, _from, _to) approve_val = Add(approved_val, _amount) Require(from_val >= approve_val) # the token owner must have the amount over approved approve_key = concat(ALLOWANCE_PREFIX, concat(_from, _to)) SafePut(_context, approve_key, approve_val) return True
def Add(a, b): """ Adds two numbers, throws on overflow. """ c = a + b Require(c >= a) return c
def Div(a, b): """ Integer division of two numbers, truncating the quotient. """ Require(b > 0) c = a / b return c
def Sub(a, b): """ Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend). :param a: operand a :param b: operand b :return: a - b if a - b > 0 or revert the transaction. """ Require(a >= b) return a - b
def _burn(_context, _account, _amount): Require(_amount > 0) # the amount to burn should be over 0 account_val = _balanceOf(_context, _account) total_supply = _totalSupply(_context) Require(_amount < total_supply) # should be not over total supply # burn the token from account. It also subtract the total supply account_val = Sub(account_val, _amount) total_supply = Sub(total_supply, _amount) SafePut(_context, concat(OWN_PREFIX, _account), account_val) SafePut(_context, MST_SUPPLY_KEY, total_supply) Notify(["burn", _account, _amount]) return True
def Mul(a, b): """ Multiplies two numbers, throws on overflow. :param a: operand a :param b: operand b :return: a - b if a - b > 0 or revert the transaction. """ if a == 0: return 0 c = a * b Require(c / a == b) return c
def _transfer(_context, _from, _to, _value): Require(_value > 0) # transfer value must be over 0 RequireScriptHash(_to) # to-address validation from_val = _balanceOf(_context, _from) to_val = _balanceOf(_context, _to) from_val = Sub(from_val, _value) to_val = Add(to_val, _value) SafePut(_context, concat(OWN_PREFIX, _from), from_val) SafePut(_context, concat(OWN_PREFIX, _to), to_val) Notify(["transfer", from_val, to_val, _value]) return True
def _mint(_context, _to, _amount): Require(_amount > 0) # mint value must be over 0 RequireScriptHash(_to) # to address should total_supply = _totalSupply(_context) to_val = _balanceOf(_context, _to) # Add total supply value and give the token to the to-address total_supply += _amount to_val += _amount SafePut(_context, MST_SUPPLY_KEY, total_supply) SafePut(_context, concat(OWN_PREFIX, _to), to_val) Notify(["mint", _to, _amount]) return True
def _transferFrom(_context, _owner, _spender, _to, _amount): RequireWitness(_owner) RequireScriptHash(_spender) RequireScriptHash(_to) Require(_amount > 0) approve_key = concat(ALLOWANCE_PREFIX, concat(_spender, _owner)) approve_amount = Get(_context, approve_key) approve_amount = Sub(approve_amount, _amount) if not _transfer(_context, _spender, _to, _amount): return False SafePut(_context, approve_key, approve_amount) Notify(["transferFrom", _owner, _spender, _to, _amount]) return True