Example #1
0
def burn(account: UInt160, amount: int):
    """
    Burns zNEO tokens.

    :param account: the address of the account that is pulling out cryptocurrency of this contract
    :type account: UInt160
    :param amount: the amount of gas to be refunded
    :type amount: int
    :raise AssertionError: raised if `account` length is not 20, amount is less than than 0 or the account doesn't have
    enough zNEO to burn
    """
    assert len(account) == 20
    assert amount >= 0
    if check_witness(account):
        if amount != 0:
            current_total_supply = totalSupply()
            account_balance = balanceOf(account)

            assert account_balance >= amount

            put(SUPPLY_KEY, current_total_supply - amount)

            if account_balance == amount:
                delete(account)
            else:
                put(account, account_balance - amount)

            on_transfer(account, None, amount)
            post_transfer(account, None, amount, None, False)

            call_contract(NEO, 'transfer',
                          [executing_script_hash, account, amount, None])
Example #2
0
def set_balance(owner: UInt160, amount: int):
    old = balanceOf(owner)
    new = old + (amount)
    debug(['set_balance: ', amount])

    key = mk_balance_key(owner)
    if (new > 0):
        put(key, new)
    else:
        delete(key)
Example #3
0
def transfer(from_address: UInt160, to_address: UInt160, amount: int,
             data: Any) -> bool:
    """
    Transfers a specified amount of NEP17 tokens from one account to another

    If the method succeeds, it must fire the `transfer` event and must return true, even if the amount is 0,
    or from and to are the same address.

    :param from_address: the address to transfer from
    :type from_address: UInt160
    :param to_address: the address to transfer to
    :type to_address: UInt160
    :param amount: the amount of NEP17 tokens to transfer
    :type amount: int
    :param data: whatever data is pertinent to the onPayment method
    :type data: Any

    :return: whether the transfer was successful
    :raise AssertionError: raised if `from_address` or `to_address` length is not 20 or if `amount` if less than zero.
    """
    # the parameters from and to should be 20-byte addresses. If not, this method should throw an exception.
    assert len(from_address) == 20 and len(to_address) == 20
    # the parameter amount must be greater than or equal to 0. If not, this method should throw an exception.
    assert amount >= 0

    # The function MUST return false if the from account balance does not have enough tokens to spend.
    from_balance = storage.get(from_address).to_int()
    if from_balance < amount:
        return False

    # The function should check whether the from address equals the caller contract hash.
    # If so, the transfer should be processed;
    # If not, the function should use the check_witness to verify the transfer.
    if from_address != runtime.calling_script_hash:
        if not runtime.check_witness(from_address):
            return False

    # skip balance changes if transferring to yourself or transferring 0 cryptocurrency
    if from_address != to_address and amount != 0:
        if from_balance == amount:
            storage.delete(from_address)
        else:
            storage.put(from_address, from_balance - amount)

        to_balance = storage.get(to_address).to_int()
        storage.put(to_address, to_balance + amount)

    # if the method succeeds, it must fire the transfer event
    on_transfer(from_address, to_address, amount)
    # if the to_address is a smart contract, it must call the contracts onPayment
    post_transfer(from_address, to_address, amount, data)
    # and then it must return true
    return True
Example #4
0
def transfer(from_address: bytes, to_address: bytes, amount: int) -> bool:
    """
    Transfers a specified amount of NEP5 tokens from one account to another

    If the method succeeds, it must fire the `transfer` event and must return true, even if the amount is 0,
    or from and to are the same address.

    :param from_address: the address to transfer from
    :type from_address: bytes
    :param to_address: the address to transfer to
    :type to_address: bytes
    :param amount: the amount of NEP5 tokens to transfer
    :type amount: int

    :return: whether the transfer was successful
    :raise AssertionError: raised if `from_address` or `to_address` length is not 20 or if `amount` if less than zero.
    """
    # the parameters from and to should be 20-byte addresses. If not, this method should throw an exception.
    assert len(from_address) == 20 and len(to_address) == 20
    # the parameter amount must be greater than or equal to 0. If not, this method should throw an exception.
    assert amount >= 0

    # The function MUST return false if the from account balance does not have enough tokens to spend.
    from_balance = get(from_address).to_int()
    if from_balance < amount:
        return False

    # The function should check whether the from address equals the caller contract hash.
    # If so, the transfer should be processed;
    # If not, the function should use the check_witness to verify the transfer.
    if from_address != calling_script_hash:
        if not check_witness(from_address):
            return False

    # if the `to_address` is a deployed contract, the function should check the payable flag of this contract
    # TODO: include example when objects are implemented

    if from_address == to_address:
        # transfer to self
        return True

    if from_balance == amount:
        delete(from_address)
    else:
        put(from_address, from_balance - amount)

    to_balance = get(to_address).to_int()
    put(to_address, to_balance + amount)

    # if the method succeeds, it must fire the transfer event, and must return true
    on_transfer(from_address, to_address, amount)
    return True
Example #5
0
def cancel_player_bet(player: UInt160, bet_id: UInt256):
    if len(get(POOL_OWNER_KEY + bet_id)) == 0:
        raise Exception("Pool doesn't exist.")
    if not check_witness(player):
        raise Exception('No authorization.')
    if len(get(POOL_RESULT_KEY + bet_id)) > 0:
        raise Exception('Pool is finished already')
    if len(get(POOL_BET_KEY + bet_id + player)) == 0:
        raise Exception("Player didn't bet on this pool")

    # 5% fee of the bet for cancelling
    refund_value = PRICE_IN_GAS - PRICE_IN_GAS * 5 // 100
    transfer_gas(executing_script_hash, player, refund_value)

    delete(POOL_BET_KEY + bet_id + player)
Example #6
0
def kyc_remove(addresses: List[UInt160]) -> int:
    """
    Removes the given addresses from the kyc whitelist

    :param addresses: a list with the addresses to be removed
    :return: the number of removed addresses
    """
    removed_addresses = 0
    if is_administrator():
        for address in addresses:
            if len(address) == 20:
                kyc_key = KYC_WHITELIST_PREFIX + address
                storage.delete(kyc_key)
                removed_addresses += 1

    return removed_addresses
Example #7
0
def main(operation: str, arg: str, val: str) -> Any:

    print("context")

    if operation == 'sget':

        return get(arg)

    elif operation == 'sput':

        put(arg, val)
        return True

    elif operation == 'sdel':

        delete(arg)
        return True

    return 'unknown operation'
Example #8
0
def main(operation: str, arg: str, val: str) -> Any:

    storage_context = get_context()
    print("context")

    if operation == 'sget':

        return get(arg, storage_context)

    elif operation == 'sput':

        put(arg, val, storage_context)
        return True

    elif operation == 'sdel':

        delete(arg, storage_context)
        return True

    return 'unknown operation'
Example #9
0
def burn(account: UInt160, amount: int):
    """
    Burns DSTONE tokens.

    :param account: the address of the account that is sending cryptocurrency to this contract
    :param amount: the amount of gas to be refunded
    :raise AssertionError: raised if amount is less than than 0 or the check_witness using ADMIN goes wrong
    """
    assert amount >= 0 and check_witness(ADMIN)
    if amount != 0:
        current_total_supply = totalSupply()
        account_balance = balanceOf(account)

        assert account_balance >= amount
        put(TOTAL_SUPPLY, current_total_supply - amount)

        if account_balance == amount:
            delete(account)
        else:
            put(account, account_balance - amount)

        on_transfer(account, None, amount)
        post_transfer(account, None, amount, None)
Example #10
0
def deleteStream(stream: Dict[str, Any]):
    """
    Delete a stream from storage

    Args:
        stream (Dict[str, Any]): Stream object
    """
    b_id = cast(bytes, stream['id'])

    delete(b'streams/' + b_id)

    sender_key = cast(bytes, stream['sender']) + b':' + b_id
    recipient_key = cast(bytes, stream['recipient']) + b':' + b_id

    delete(b'bysender/' + sender_key)
    delete(b'byrecipient/' + recipient_key)
def Main(key: Union[bytes, str]):
    context = get_context()
    delete(key, context)
Example #12
0
def transferFrom(spender: UInt160, from_address: UInt160, to_address: UInt160,
                 amount: int, data: Any) -> bool:
    """
    A spender transfers an amount of zNEO tokens allowed from one account to another.

    If the method succeeds, it must fire the `Transfer` event and must return true, even if the amount is 0,
    or from and to are the same address.

    :param spender: the address that is trying to transfer zNEO tokens
    :type spender: UInt160
    :param from_address: the address to transfer from
    :type from_address: UInt160
    :param to_address: the address to transfer to
    :type to_address: UInt160
    :param amount: the amount of zNEO tokens to transfer
    :type amount: int
    :param data: whatever data is pertinent to the onPayment method
    :type data: Any

    :return: whether the transfer was successful
    :raise AssertionError: raised if `spender`, `from_address` or `to_address` length is not 20 or if `amount` if less
    than zero.
    """
    # the parameters from and to should be 20-byte addresses. If not, this method should throw an exception.
    assert len(spender) == 20 and len(from_address) == 20 and len(
        to_address) == 20
    # the parameter amount must be greater than or equal to 0. If not, this method should throw an exception.
    assert amount >= 0

    # The function MUST return false if the from account balance does not have enough tokens to spend.
    from_balance = get(from_address).to_int()
    if from_balance < amount:
        return False

    # The function MUST return false if the from account balance does not allow enough tokens to be spent by the spender.
    allowed = allowance(from_address, spender)
    if allowed < amount:
        return False

    # The function should check whether the spender address equals the caller contract hash.
    # If so, the transfer should be processed;
    # If not, the function should use the check_witness to verify the transfer.
    if spender != calling_script_hash:
        if not check_witness(spender):
            return False

    if allowed == amount:
        delete(ALLOWANCE_PREFIX + from_address + spender)
    else:
        put(ALLOWANCE_PREFIX + from_address + spender, allowed - amount)

    # skip balance changes if transferring to yourself or transferring 0 cryptocurrency
    if from_address != to_address and amount != 0:
        if from_balance == amount:
            delete(from_address)
        else:
            put(from_address, from_balance - amount)

        to_balance = get(to_address).to_int()
        put(to_address, to_balance + amount)

    # if the method succeeds, it must fire the transfer event
    on_transfer(from_address, to_address, amount)
    # if the to_address is a smart contract, it must call the contracts onPayment
    post_transfer(from_address, to_address, amount, data, True)
    # and then it must return true
    return True
Example #13
0
def Main(key: bytes):
    delete(key)
Example #14
0
def remove_locked_view_counter(tokenId: bytes):
    key = mk_lv_key(tokenId)
    debug(['remove_locked_view_counter: ', key, tokenId])
    delete(key)
Example #15
0
def Main(key: int):
    delete(key)
Example #16
0
def delete_value(key: str):
    storage.delete(key)
Example #17
0
def remove_owner_of(tokenId: bytes):
    key = mk_token_key(tokenId)
    debug(['remove_owner_of: ', key, tokenId])
    delete(key)
 def remove_token(self, token_id: bytes):
     storage.delete(account_prefix_key(self.address) + token_id)
     self._balance = self._balance - 1
     self.set_account()
Example #19
0
def Main(key: ByteString):
    context = get_context()
    delete(key, context)
def Main(key: str):
    delete(key)
Example #21
0
def remove_meta(tokenId: bytes):
    key = mk_meta_key(tokenId)
    debug(['remove_meta: ', key, tokenId])
    delete(key)
Example #22
0
def transfer_from(originator: UInt160, from_address: UInt160,
                  to_address: UInt160, amount: int, data: Any) -> bool:
    """
    Transfers an amount from the `from` account to the `to` account if the `originator` has been approved to transfer
    the requested amount.

    :param originator: the address where the actual token is
    :type originator: UInt160
    :param from_address: the address to transfer from with originator's approval
    :type from_address: UInt160
    :param to_address: the address to transfer to
    :type to_address: UInt160
    :param amount: the amount of NEP17 tokens to transfer
    :type amount: int
    :param data: any pertinent data that might validate the transaction
    :type data: Any

    :return: whether the transfer was successful
    :raise AssertionError: raised if `from_address` or `to_address` length is not 20 or if `amount` if less than zero.
    """
    # the parameters from and to should be 20-byte addresses. If not, this method should throw an exception.
    assert len(originator) == 20 and len(from_address) == 20 and len(
        to_address) == 20
    # the parameter amount must be greater than or equal to 0. If not, this method should throw an exception.
    assert amount >= 0

    # The function should check whether the from address equals the caller contract hash.
    # If so, the transfer should be processed;
    # If not, the function should use the check_witness to verify the transfer.
    if from_address != runtime.calling_script_hash:
        if not runtime.check_witness(from_address):
            return False

    approved_transfer_amount = allowance(originator, from_address)
    if approved_transfer_amount < amount:
        return False

    originator_balance = balance_of(originator)
    if originator_balance < amount:
        return False

    # update allowance between originator and from
    if approved_transfer_amount == amount:
        storage.delete(TRANSFER_ALLOWANCE_PREFIX + originator + from_address)
    else:
        storage.put(TRANSFER_ALLOWANCE_PREFIX + originator + from_address,
                    approved_transfer_amount - amount)

    # skip balance changes if transferring to yourself or transferring 0 cryptocurrency
    if amount != 0 and from_address != to_address:
        # update originator's balance
        if originator_balance == amount:
            storage.delete(originator)
        else:
            storage.put(originator, originator_balance - amount)

        # updates to's balance
        to_balance = storage.get(to_address).to_int()
        storage.put(to_address, to_balance + amount)

    # if the method succeeds, it must fire the transfer event
    on_transfer(from_address, to_address, amount)
    # if the to_address is a smart contract, it must call the contracts onPayment
    post_transfer(from_address, to_address, amount, data)
    # and then it must return true
    return True
Example #23
0
def remove_locked_content(tokenId: bytes):
    key = mk_locked_key(tokenId)
    debug(['remove_locked_content: ', key, tokenId])
    delete(key)
Example #24
0
def remove_royalties(tokenId: bytes):
    key = mk_royalties_key(tokenId)
    debug(['remove_royalties: ', key, tokenId])
    delete(key)
Example #25
0
def transferFrom(originator: bytes, from_address: bytes, to_address: bytes,
                 amount: int) -> bool:
    """
    Transfers an amount from the `from` account to the `to` account if the `originator` has been approved to transfer
    the requested amount.

    :param originator: the address where the actual token is
    :type originator: bytes
    :param from_address: the address to transfer from with originator's approval
    :type from_address: bytes
    :param to_address: the address to transfer to
    :type to_address: bytes
    :param amount: the amount of NEP5 tokens to transfer
    :type amount: int

    :return: whether the transfer was successful
    :raise AssertionError: raised if `from_address` or `to_address` length is not 20 or if `amount` if less than zero.
    """
    # the parameters from and to should be 20-byte addresses. If not, this method should throw an exception.
    assert len(originator) == 20 and len(from_address) == 20 and len(
        to_address) == 20
    # the parameter amount must be greater than or equal to 0. If not, this method should throw an exception.
    assert amount >= 0

    # The function should check whether the from address equals the caller contract hash.
    # If so, the transfer should be processed;
    # If not, the function should use the check_witness to verify the transfer.
    if from_address != calling_script_hash:
        if not check_witness(from_address):
            return False

    # if the `to_address` is a deployed contract, the function should check the payable flag of this contract
    # TODO: include example when objects are implemented

    approved_transfer_amount = allowance(originator, from_address)
    if approved_transfer_amount < amount:
        return False

    originator_balance = balanceOf(originator)
    if originator_balance < amount:
        return False

    # don't accept a meaningless value
    if amount == 0 or originator == to_address or from_address == to_address:
        return True  # nep5 standard: return true when amount is 0 or from == to

    # update allowance between originator and from
    if approved_transfer_amount == amount:
        delete(TRANSFER_ALLOWANCE_PREFIX + originator + from_address)
    else:
        put(TRANSFER_ALLOWANCE_PREFIX + originator + from_address,
            approved_transfer_amount - amount)

    # update originator's balance
    if originator_balance == amount:
        delete(originator)
    else:
        put(originator, originator_balance - amount)

    # updates to's balance
    to_balance = get(to_address).to_int()
    put(to_address, to_balance + amount)

    # if the method succeeds, it must fire the transfer event, and must return true
    on_transfer(from_address, to_address, amount)
    return True