# coding: utf8
from __future__ import unicode_literals
import logging
 
from backoffice.models import *
import requests
from tixsell import settings
import json
from web3 import Web3
 
def run():
    api_key = settings.ALCHEMY_ACCESS_KEY
    
    # choose the maximum number of retries to perform, default is 5
    max_retries = 3

    # create Alchemy object
    url = "https://matic-mumbai.g.alchemy.com/v2/"+api_key
    txHash="0x3e7665df531c0f75f2dcaa6177725c0d9ee02554438e79b99ce8d70a5f566c78"
    payload = {
    "id": 1,
    "jsonrpc": "2.0",
    "method": "eth_getTransactionReceipt",
    "params": [txHash]
    }
    headers = {
    "accept": "application/json",
    "content-type": "application/json"
    }

    response = requests.post(url, json=payload, headers=headers)
    if response.status_code==200:
        data = json.loads(response.text)
        #print(data)
        if data["result"]:
            result = data["result"]
            logs = result["logs"]
            for log in logs:
                topics = log["topics"]
                print("===============")
                if len(topics)==4:
                    name = topics[0]
                    source = topics[1]
                    dest = topics[2]
                    token=topics[3]
                    finalDest=web3.to_hex(dest)
                    if finalDest=="0xf234af46cd3c81637a9ad3c7829536880976870a":
                        print("envoi ",token," a ",dest)
    """
    finalPrice = Web3.from_wei(transaction["value"],'ether')
    ticket = Ticket.objects.get(id="e8f60c5a-9153-460e-95c0-4d08ceca9212")
    ticket.pricePaid = finalPrice
    ticket.save()
    print(finalPrice)
   
    block = alchemy.core.get_transaction_receipts(block_hash=blockHash)
    for transaction in block:
        if transaction["transactionHash"]==txHash:
            print(transaction)
            """
