# Path: hydrachain/nc_utils.py
# def isaddress(x):
#     return len(x) == 20 and x != '\0' * 20
# 
# STATUS = 'uint16'
# 
# FORBIDDEN = 403
# 
# OK = 200
# 
# INSUFFICIENTFUNDS = PAYMENTREQUIRED = 402


import ethereum.utils as utils
import ethereum.slogging as slogging
import hydrachain.native_contracts as nc
from hydrachain.nc_utils import isaddress, STATUS, FORBIDDEN, OK, INSUFFICIENTFUNDS
    https://github.com/ethereum/wiki/wiki/Standardized_Contract_APIs
    """
    address = utils.int_to_addr(5000)
    events = [Transfer, Approval]

    owner = nc.Scalar('address')
    supply = nc.Scalar('uint256')
    # mapping (address => uint256)
    #   here mapping betw address => balances
    accounts = nc.IterableDict('uint256')
    allowances = nc.Dict(nc.Dict('uint256'))

    def init(ctx, _supply='uint256', returns=STATUS):
        log.DEV('In Fungible.init')
        if isaddress(ctx.owner):
            return FORBIDDEN
        ctx.owner = ctx.tx_origin
        ctx.accounts[ctx.tx_origin] = _supply
        ctx.supply = _supply
        return OK

    def transfer(ctx, _to='address', _value='uint256', returns=STATUS):
        """ Standardized Contract API:
        function transfer(address _to, uint256 _value) returns (bool _success)
        """
        log.DEV('In Fungible.transfer')
        if ctx.accounts[ctx.msg_sender] >= _value:
            ctx.accounts[ctx.msg_sender] -= _value
            ctx.accounts[_to] += _value
            ctx.Transfer(ctx.msg_sender, _to, _value)

# Path: hydrachain/nc_utils.py
# def isaddress(x):
#     return len(x) == 20 and x != '\0' * 20
# 
# STATUS = 'uint16'
# 
# FORBIDDEN = 403
# 
# OK = 200
# 
# INSUFFICIENTFUNDS = PAYMENTREQUIRED = 402


# Path: hydrachain/examples/native/fungible/fungible_contract.py
import ethereum.utils as utils
import ethereum.slogging as slogging
import hydrachain.native_contracts as nc
from hydrachain.nc_utils import isaddress, STATUS, FORBIDDEN, OK, INSUFFICIENTFUNDS

    https://github.com/ethereum/wiki/wiki/Standardized_Contract_APIs
    """
    address = utils.int_to_addr(5000)
    events = [Transfer, Approval]

    owner = nc.Scalar('address')
    supply = nc.Scalar('uint256')
    # mapping (address => uint256)
    #   here mapping betw address => balances
    accounts = nc.IterableDict('uint256')
    allowances = nc.Dict(nc.Dict('uint256'))

    def init(ctx, _supply='uint256', returns=STATUS):
        log.DEV('In Fungible.init')
        if isaddress(ctx.owner):
            return FORBIDDEN
        ctx.owner = ctx.tx_origin
        ctx.accounts[ctx.tx_origin] = _supply
        ctx.supply = _supply
        return OK

    def transfer(ctx, _to='address', _value='uint256', returns=STATUS):
        """ Standardized Contract API:
        function transfer(address _to, uint256 _value) returns (bool _success)
        """
        log.DEV('In Fungible.transfer')
        if ctx.accounts[ctx.msg_sender] >= _value:
            ctx.accounts[ctx.msg_sender] -= _value
            ctx.accounts[_to] += _value
            ctx.Transfer(ctx.msg_sender, _to, _value)

            return OK