def do_approve(self, storage: StorageAPI, t_owner, t_spender, amount):

        if not CheckWitness(t_owner):
            print("Incorrect permission")
            return False

        if amount < 0:
            print("Negative amount")
            return False

        from_balance = storage.get(t_owner)

        # cannot approve an amount that is
        # currently greater than the from balance
        if from_balance >= amount:

            approval_key = concat(t_owner, t_spender)

            if amount == 0:
                storage.delete(approval_key)
            else:
                storage.put(approval_key, amount)

            OnApprove(t_owner, t_spender, amount)

            return True

        return False
Beispiel #2
0
    def do_transfer_from(self, storage: StorageAPI, t_from, t_to, amount):

        if amount <= 0:
            return False

        available_key = concat(t_from, t_to)

        if len(available_key) != 40:
            return False

        if not CheckWitness(t_to):
            print("to address is not the tx sender")
            return False

        available_to_to_addr = storage.get(available_key)

        if available_to_to_addr < amount:
            print("Insufficient funds approved")
            return False

        from_balance = storage.get(t_from)

        if from_balance < amount:
            print("Insufficient tokens in from balance")
            return False

        to_balance = storage.get(t_to)

        new_from_balance = from_balance - amount

        new_to_balance = to_balance + amount

        storage.put(t_to, new_to_balance)
        storage.put(t_from, new_from_balance)

        print("transfer complete")

        new_allowance = available_to_to_addr - amount

        if new_allowance == 0:
            print("removing all balance")
            storage.delete(available_key)
        else:
            print("updating allowance to new allowance")
            storage.put(available_key, new_allowance)

        OnTransfer(t_from, t_to, amount)

        return True
    def do_transfer_from(self, storage: StorageAPI, t_from, t_to, amount):

        if amount <= 0:
            return False

        available_key = concat(t_from, t_to)

        if len(available_key) != 40:
            return False

        available_to_to_addr = storage.get(available_key)

        if available_to_to_addr < amount:
            print("Insufficient funds approved")
            return False

        from_balance = storage.get(t_from)

        if from_balance < amount:
            print("Insufficient tokens in from balance")
            return False

        # TODO: Check if target is crowdfunding
        # If no crowdfunding, do normal transfer

        to_balance = storage.get(t_to)

        new_from_balance = from_balance - amount

        new_to_balance = to_balance + amount

        storage.put(t_to, new_to_balance)
        storage.put(t_from, new_from_balance)

        print("transfer complete")

        new_allowance = available_to_to_addr - amount

        if new_allowance == 0:
            print("removing all balance")
            storage.delete(available_key)
        else:
            print("updating allowance to new allowance")
            storage.put(available_key, new_allowance)

        OnTransfer(t_from, t_to, amount)

        return True
Beispiel #4
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

            if from_val == amount:
                storage.delete(t_from)

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

            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
    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