def Main(operation, args):
    """

    :param operation: str The name of the operation to perform
    :param args: list A list of arguments along with the operation
    :return:
        bytearray: The result of the operation
    """

    trigger = GetTrigger()
    token = Token()

    #print("Executing ICO Template")

    # This is used in the Verification portion of the contract
    # To determine whether a transfer of system assets ( NEO/Gas) involving
    # This contract's address can proceed
    if trigger == Verification:

        # check if the invoker is the owner of this contract
        is_owner = CheckWitness(token.owner)

        # If owner, proceed
        if is_owner:

            return True

        # Otherwise, we need to lookup the assets and determine
        # If attachments of assets is ok
        attachments = get_asset_attachments()  # type:Attachments

        storage = StorageAPI()

        crowdsale = Crowdsale()

        return crowdsale.can_exchange(token, attachments, storage, True)

    elif trigger == Application:

        if operation != None:

            nep = NEP5Handler()

            for op in nep.get_methods():
                if operation == op:
                    return nep.handle_nep51(operation, args, token)

            if operation == 'deploy':
                return deploy(token)

            if operation == 'circulation':
                storage = StorageAPI()
                return token.get_circulation(storage)

            # the following are handled by crowdsale

            sale = Crowdsale()

            if operation == 'mintTokens':
                return sale.exchange(token)

            # if operation == 'crowdsale_register':
            #     return sale.kyc_register(args, token)

            # if operation == 'crowdsale_status':
            #     return sale.kyc_status(args)

            if operation == 'crowdsale_available':
                return token.crowdsale_available_amount()

            if operation == 'crowdfunding_create':
                return crowdfunding_create(args)

            if operation == 'crowdfunding_total':
                storage = StorageAPI()
                crowdfunding_address = args[0]
                crowdfunding_total_key = storage.get_crowdfunding_total_key(
                    crowdfunding_address)
                crowdfunding_total = storage.get(crowdfunding_total_key)
                msg = ["crowdfunding_total", crowdfunding_total]
                Notify(msg)
                return crowdfunding_total

            if operation == 'crowdfunding_test':
                crowdfunding_address = args[0]
                member_addresses = crowdfunding_get_members(
                    crowdfunding_address)
                if not member_addresses:
                    return False

                Notify("Member addresses:")
                Notify(member_addresses)
                return True

            return 'unknown operation'

    return False
Ejemplo n.º 2
0
    def do_transfer(self, storage: StorageAPI, t_from, t_to, amount):

        if amount <= 0:
            return False

        if len(t_to) != 20:
            return False

        if CheckWitness(t_from):

            if t_from == t_to:
                print("transfer to self!")
                return True

            from_val = storage.get(t_from)

            if from_val < amount:
                print("insufficient funds")
                return False

            # Update sender balance
            if from_val == amount:
                storage.delete(t_from)

            else:
                difference = from_val - amount
                storage.put(t_from, difference)

            # Check if target is crowdfunding. If not, do normal transfer.
            is_crowdfunding = is_crowdfunding_address(t_to)
            if is_crowdfunding:
                Notify("is_crowdfunding!")
                members = crowdfunding_get_members(t_to)
                num_members = len(members)
                amount_per_member = amount / num_members
                msg = [
                    "Crowdfunding. Splitting amount across members:",
                    num_members, "amount per member:", amount_per_member
                ]
                Notify(msg)
                for member in members:
                    to_value = storage.get(member)
                    to_total = to_value + amount_per_member
                    storage.put(member, to_total)
                    OnTransfer(t_from, member, amount_per_member)

                # Increase the crowdfunding total amount
                crowdfunding_total_key = storage.get_crowdfunding_total_key(
                    t_to)
                crowdfunding_total = storage.get(crowdfunding_total_key)
                crowdfunding_total += amount
                storage.put(crowdfunding_total_key, crowdfunding_total)

                # Increase the crowdfunding number of contributions
                crowdfunding_numcontrib_key = storage.get_crowdfunding_numcontrib_key(
                    t_to)
                crowdfunding_numcontrib = storage.get(
                    crowdfunding_numcontrib_key)
                crowdfunding_numcontrib += 1
                storage.put(crowdfunding_numcontrib_key,
                            crowdfunding_numcontrib)

                # All done in case of crowdfunding
                return True

            to_value = storage.get(t_to)

            to_total = to_value + amount

            storage.put(t_to, to_total)

            OnTransfer(t_from, t_to, amount)

            return True
        else:
            print("from address is not the tx sender")

        return False