【问题标题】:Sending tokens out on coinpayments success payment using Web3py使用 Web3py 发送代币支付成功支付
【发布时间】:2020-03-28 18:01:54
【问题描述】:

我正在编写 Django 应用程序,并希望在 Coinpayments 向我发送有关成功付款的回调后使用 Web3 发送令牌。问题是 Coinpayments 一次发送多个回调,并且仅在一种情况下发送令牌,其他回调得到replacement transaction underpriced error。我已经尝试使用解决方案,例如将 +1 添加到nonce 或删除此参数,但这对我没有帮助,因为交易仍在使用相同的随机数构建。如何解决这个问题或者我做错了什么?

class CoinpaymentsIPNPaymentView(BaseCoinpaymentsIPNView):
    def post(self, request, order_id, *args, **kwargs):
        status = int(request.POST.get('status'))
        order = Order.objects.get(id=order_id)
        order.status = request.POST.get("status_text")
        if not status >= 100:
            order.save()
            return JsonResponse({"status": status})
        amount = Decimal(request.POST.get('amount1'))
        record = Record.objects.create(
            user=order.user,
            type='i',
            amount=amount,
        )
        order.record = record
        order.save()

        gold_record = GoldRecord.objects.get(from_record=record)

        contract = w3.eth.contract(address=CONTRACT_ADDRESS, abi=ABI_JSON)
        transaction = contract.functions.transfer(order.user.wallet.address, int(gold_record.amount * 10 ** 18)).buildTransaction({
            'chainId': 1,
            'gas': 70000,
            'nonce': w3.eth.getTransactionCount(WALLET_ADDRESS)                      # address where all tokens are stored
        })
        signed_tx = w3.eth.account.signTransaction(transaction, WALLET_PRIVATE_KEY)  # signing with wallet's above private key
        tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
        print(tx_hash.hex())
        tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)

        return JsonResponse({"status": status})

附:我已经在 Ethereum StackExchange 上问过它,但没有人回答或评论它:https://ethereum.stackexchange.com/questions/80961/sending-tokens-out-on-coinpayments-success-payment-using-web3py

【问题讨论】:

    标签: django ethereum web3py coinpayments-api


    【解决方案1】:

    好的,让网络知道我自己找到的答案和解决方案

    每个事务都应该有唯一的随机数,所以我注意到如果我执行一个循环来发送事务并将nonce 设置为w3.eth.getTransactionCount(WALLET_ADDRESS) + index,那么它会发送所有事务而不会出现任何错误。所以我删除了即时硬币发送(甚至删除了waitForTransactionReceipt 以加快它),并在我处理所有支付的地方发出管理命令,如果成功发送,我分配它的tx_hash 并使用 Heroku Scheduler 每 10 分钟运行一次

    【讨论】:

      猜你喜欢
      • 2020-06-01
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多