def save(self, request): errors = [] normal_addresses = [] file_name = libs.rand(16) file = open("{0}/{1}".format(settings.TMP_ROOT, file_name), "wb") file.write(self.files["file"].read()) file.close() with open("{0}/{1}".format(settings.TMP_ROOT, file_name), "r") as file: csv_file = csv.reader(file) for row in csv_file: try: address = row[0] except IndexError: continue else: address = address.strip()[:-41:-1][::-1] if re.match(ADDRESS, address): if address not in normal_addresses: normal_addresses.append(address) else: messages.info( request, _("Duplicate the value: ") + "{}".format(address)) else: errors.append(", ".join(row)) if normal_addresses: data = { "command": "cost", "count": [len(normal_addresses), settings.COUNT_ADDRESSES], "call": settings.COST_CALL } order = sm.SenderOrder( user_id=libs.who_signin(request)[0], contract=self.data["contract"][:-41:-1][::-1], cost=str(libs.get_data(data, "cost")), amount=str(int(self.data["amount"]) * len(normal_addresses))) order.save() data = {"command": "create", "order": order.id} order.address = libs.get_data(data, "address") order.save() for address in normal_addresses: recipient = sm.RecipientData(order=order, address=address, amount=self.data["amount"]) recipient.save() if errors: for error in errors: messages.info(request, _("Error string: ") + error) os.remove("{0}/{1}".format(settings.TMP_ROOT, file_name))
def run_order(data): """ установить таймаут при деплое или хранить адрес размещенного контракта, а потом обращаться за ним если ошибка размещения - выслать письмо """ if not data.get("work_contract"): deploy_contract(data) data["command"] = "task" del data["address"] del data["contract"] del data["amount"] addresses = [] order = sm.SenderOrder.objects.get(id=data["order"]) all_addresses = order.recipient_odrer.all().values("address") for rec in all_addresses: addresses.append(rec.address) iterations = len(addresses) // settings.COUNT_ADDRESSES sizes_arrays = settings.COUNT_ADDRESSES * iterations staff = (len(addresses) - (settings.COUNT_ADDRESSES * iterations)) if staff: sizes_arrays.append(staff) start = 0 end = 0 for size_array in sizes_arrays: end += size_array data["addresses"] = addresses[start:start + end] tx_receipt = libs.get_data(data, "tx_receipt") # libs.add_statistic( # order=data["order"], # event="Call method 'transfer'", # tx_hash=tx_receipt.transactionHash # ) start += size_array
def deploy_contract(data): tx_receipt = libs.get_data(data, "tx_receipt") work_contract = tx_receipt.contractAddress # libs.add_statistic( # order=data["order"], # event="Deploy copntract", # tx_hash=tx_receipt.transactionHash # ) order = sm.SenderOrder.objects.filter(id=data["order"]) order.update(work_contract=work_contract[2:]) data["work_contract"] = work_contract[2:]
def clean(self): errors = False if not re.match(ADDRESS, self.cleaned_data.get("contract")[:-41:-1][::-1]): errors = True self.add_error("contract", _("Invalid smart address address format")) data = { "command": "contract", "contract": self.cleaned_data.get("contract") } if not libs.get_data(data, "contract"): errors = True self.add_error("contract", _("Not smart contract")) if int(self.cleaned_data.get("amount")) <= 0: errors = True self.add_error("amount", _("Only positive number is greater than zero")) if errors: raise forms.ValidationError(_("Data entry error")) else: return self.cleaned_data
def check_token(request): if libs.is_signin(request): if request.method == "POST" and request.is_ajax(): address = request.POST.get("address") contract = request.POST.get("contract") order_id = request.POST.get("order_id") try: order_id = int(order_id) except TypeError: return HttpResponse( json.dumps({ "result": "error", "description": "Order not found" })) if address and contract and order_id: if re.match(ADDRESS, address) and re.match(ADDRESS, contract): order = rm.SenderOrder.objects.filter(address=address, id=order_id) if order and len(order) == 1: data = {"reload": False} data["result"] = libs.get_data( { "command": "token", "address": address, "contract": contract }, "balance") if not order[0].tokens and data["result"] \ and data["result"] >= int(order[0].amount): order.update(tokens=True) data["reload"] = True return HttpResponse(json.dumps(data)) else: return HttpResponse( json.dumps({ "result": "error", "description": "Address not found" })) else: return HttpResponse( json.dumps({ "result": "error", "description": "Invalid address format" })) else: return HttpResponse( json.dumps({ "result": "error", "description": "Not found field 'address'" })) else: return HttpResponse( json.dumps({ "result": "error", "description": "Only POST request" })) else: return HttpResponse( json.dumps({ "result": "error", "description": "Only for authorized users" }))