Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
14 kB
3
Indexable
Never
import json
from web3 import Web3
import pandas as pd


def raffle_ticket_logic(email, wallet_addresses):
    emails_wallets_total_balance_raffle_nft = []
    try:
        # desired block number
        block_number = 17773538  # (Jul-25-2023 11:59:59 PM +UTC)

        # -------------------------------- web3 provider instance --------------------------------
        # Initialize a Web3 provider, such as Infura.
        eth_w3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/b9cadf0f28404c2ba44283bc9cbd018d'))
        pol_w3 = Web3(Web3.HTTPProvider('https://polygon-mainnet.infura.io/v3/b9cadf0f28404c2ba44283bc9cbd018d'))
        # -------------------------------- web3 provider instance }}} --------------------------------

        # -------------------------------- contract addresses --------------------------------
        # Convert contract addresses to checksum format
        ads_contract_address = Web3.to_checksum_address("0x3106a0a076bedae847652f42ef07fd58589e001f")
        eth_alex_contract_address = Web3.to_checksum_address("0xb2d4F230298Cf68164F6C5DD994068Cbb4C3D335")
        pol_mutated_alex_contract_address = Web3.to_checksum_address("0xCe375B8f2b1113d77D89Fd522955C19A2B7f6460")
        pol_alexa_contract_address = Web3.to_checksum_address("0xCB0d870CE67Eb99Ec371e6d65c9350748E6E22F8")
        lattice_12mths_pool_contract_address = Web3.to_checksum_address("0x79ddc8af1ad063f65cb12639de82fd33495392ae")

        # -------------------------------- LOAD THE ABI FILES --------------------------------
        # Load the eth_alex NFT contract ABI
        with open('raffle_api/api_data/eth_ads_token_abi.json') as eth_ads_f:
            eth_ads_contract_abi = json.load(eth_ads_f)

        # Load the NFT contract ABI
        with open('raffle_api/api_data/eth_alex_sc_abi.json') as eth_alex_f:
            eth_alex_contract_abi = json.load(eth_alex_f)

        # Load the pol_mutated_alex NFT contract ABI
        with open('raffle_api/api_data/polygon_AlkimiMutantAlex.json') as pol_mutated_alex_f:
            pol_mutated_alex_contract_abi = json.load(pol_mutated_alex_f)

        # Load the pol_alexa NFT contract ABI
        with open('raffle_api/api_data/polygon_Alexa.json') as pol_alexa_f:
            pol_alexa_contract_abi = json.load(pol_alexa_f)

        # Load the lattice_12mths_pool NFT contract ABI
        with open('raffle_api/api_data/lattice_12mths_pool_abi.json') as lattice_12mths_pool_f:
            lattice_12mths_pool_contract_abi = json.load(lattice_12mths_pool_f)

        # -------------------------------- {{{ GET THE CONTRACT INSTANCES --------------------------------
        # Get the eth_alex contract instance
        eth_ads_contract = eth_w3.eth.contract(
            address=ads_contract_address, abi=eth_ads_contract_abi)

        # Get the NFT contract instance
        eth_alex_contract = eth_w3.eth.contract(
            address=eth_alex_contract_address, abi=eth_alex_contract_abi)

        # Get the pol_mutated_alex contract instance
        pol_mutated_alex_contract = pol_w3.eth.contract(
            address=pol_mutated_alex_contract_address, abi=pol_mutated_alex_contract_abi)

        # Get the pol_alexa contract instance
        pol_alexa_contract = pol_w3.eth.contract(
            address=pol_alexa_contract_address, abi=pol_alexa_contract_abi)

        # Create a contract instance
        lattice_12mths_pool_contract = eth_w3.eth.contract(
            address=lattice_12mths_pool_contract_address, abi=lattice_12mths_pool_contract_abi)

        # -------------------------------- {{{ Get the wallet list from the alkimi pools --------------------------------
        # Read SEP 2022 JSON file
        with open("raffle_api/api_data/2022-Sep_AlkimiPool.json", "r") as file:
            sep_2022_alkimi_pool_data = json.load(file)
        # Create a set of wallets from the user data in the JSON file
        sep_2022_alkimi_pool_wallets_list_1 = {data['wallet'] for data in sep_2022_alkimi_pool_data}
        sep_2022_alkimi_pool_wallets_list = {Web3.to_checksum_address(wallet) for wallet in
                                             sep_2022_alkimi_pool_wallets_list_1}

        # Read OCT 2022 JSON file
        with open("raffle_api/api_data/2022-Oct_AlkimiPool.json", "r") as file:
            oct_2022_alkimi_pool_data = json.load(file)
        # Create a set of wallets from the user data in the JSON file
        oct_2022_alkimi_pool_wallets_list_1 = {data['wallet'] for data in oct_2022_alkimi_pool_data}
        oct_2022_alkimi_pool_wallets_list = {Web3.to_checksum_address(wallet) for wallet in
                                             oct_2022_alkimi_pool_wallets_list_1}

        # Read NOV 2022 JSON file
        with open("raffle_api/api_data/2022-Nov_AlkimiPool.json", "r") as file:
            nov_2022_alkimi_pool_data = json.load(file)
        # Create a set of wallets from the user data in the JSON file
        nov_2022_alkimi_pool_wallets_list_1 = {data['wallet'] for data in nov_2022_alkimi_pool_data}
        nov_2022_alkimi_pool_wallets_list = {Web3.to_checksum_address(wallet) for wallet in
                                             nov_2022_alkimi_pool_wallets_list_1}

        # Read DEC 2022 JSON file
        with open("raffle_api/api_data/2022-Dec_AlkimiPool.json", "r") as file:
            dec_2022_alkimi_pool_data = json.load(file)
        # Create a set of wallets from the user data in the JSON file
        dec_2022_alkimi_pool_wallets_list_1 = {data['wallet'] for data in dec_2022_alkimi_pool_data}
        dec_2022_alkimi_pool_wallets_list = {Web3.to_checksum_address(wallet) for wallet in
                                             dec_2022_alkimi_pool_wallets_list_1}

        # Read Q1 2023 JSON file
        with open("raffle_api/api_data/2023-Q1_AlkimiPool.json", "r") as file:
            q1_2023_alkimi_pool_data = json.load(file)
        # Create a set of wallets from the user data in the JSON file
        q1_2023_alkimi_pool_wallets_list_1 = {data['wallet'] for data in q1_2023_alkimi_pool_data}
        q1_2023_alkimi_pool_wallets_list = {Web3.to_checksum_address(wallet) for wallet in
                                            q1_2023_alkimi_pool_wallets_list_1}

        # Read Q2 2023 JSON file
        with open("raffle_api/api_data/2023-Q2_AlkimiPool.json", "r") as file:
            q2_2023_alkimi_pool_data = json.load(file)
        # Create a set of wallets from the user data in the JSON file
        q2_2023_alkimi_pool_wallets_list_1 = {data['wallet'] for data in q2_2023_alkimi_pool_data}
        q2_2023_alkimi_pool_wallets_list = {Web3.to_checksum_address(wallet) for wallet in
                                            q2_2023_alkimi_pool_wallets_list_1}

        # -------------------------------- {{{ Get the wallet list from the 12 mths Lattice pool --------------------------------
        # print("~~~~~~~~~~~~~~~~  LATTICE 12 mths POOL ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")

        # Get all events related to the contract
        events = lattice_12mths_pool_contract.events.Staked.get_logs(
            fromBlock=0, toBlock='latest')

        # Set to store unique wallet addresses
        lattice_12mths_wallet_addresses_1 = set()

        # Iterate through each event
        for event in events:
            # Add the transaction sender wallet address to the set
            lattice_12mths_wallet_addresses_1.add(event['args']['user'])

        lattice_12mths_wallet_addresses = {Web3.to_checksum_address(wallet) for wallet in
                                           lattice_12mths_wallet_addresses_1}

        # print("Total Number of wallets staked in Lattice 12 mths pool : ", len(lattice_12mths_wallet_addresses))
        # -------------------------------- Get the wallet list from the 12 mths Lattice pool }}} --------------------------------

        # -------------------------------- {{{ Get the wallet list from the 3 mths Lattice pool --------------------------------
        # Read the Excel file and create a DataFrame
        df = pd.read_excel('raffle_api/api_data/ALKIMI LABS - Lattice 3-month Soft Staking Pool - 2022.xlsx',
                           header=None, skiprows=2, usecols='A')

        # Convert the column to a list
        lattice_3mths_wallet_addresses_1 = df[0].tolist()
        lattice_3mths_wallet_addresses = {Web3.to_checksum_address(wallet) for wallet in
                                          lattice_3mths_wallet_addresses_1}
        # print("Total Number of wallets staked in Lattice 3 mths pool : ", len(lattice_3mths_wallet_addresses))

        # print("Total Number of wallets staked in Lattice 3 mths pool : ", len(lattice_3mths_wallet_addresses))
        # -------------------------------- Get the wallet list from the 3 mths Lattice pool }}}
        # -------------------------------- Create a list to store the emails, wallets, total balance, eligibility flag
        # and eth Alex NFT count.

        # Iterate over the rows in the CSV file.

        # Check if kycVerified is true for the current row.
        # if ticket_dt['kycVerified'].lower() == 'true':
        # Get the email and wallets from the current row.
        # email = ticket_details['email']
        # wallet_addresses = ticket_details['wallet']  # Assuming the wallet addresses are in a JSON array.

        # Initialize total balance for the user
        total_balance = 0

        # List to store individual wallet data
        wallets_data = []

        # Process each wallet address in the user's wallets
        for wallet_address in wallet_addresses:

            wallet_address_sc = Web3.to_checksum_address(wallet_address)
            balance = eth_ads_contract.functions.balanceOf(wallet_address_sc).call(block_identifier=block_number) / (
                        10 ** 18)

            # Get the number of NFTs held
            eth_alex_nft_count = eth_alex_contract.functions.balanceOf(wallet_address_sc).call()
            pol_mutated_alex_nft_count = pol_mutated_alex_contract.functions.balanceOf(wallet_address_sc).call()
            pol_alexa_nft_count = pol_alexa_contract.functions.balanceOf(wallet_address_sc).call()

            # check the pools
            sep_2022_val = 0;
            oct_2022_val = 0;
            nov_2022_val = 0;
            dec_2022_val = 0;
            q1_2023_val = 0;
            q2_2023_val = 0
            lattice_12mths_val = 0;
            lattice_3mths_val = 0

            if wallet_address_sc in sep_2022_alkimi_pool_wallets_list:
                sep_2022_val = 1
            if wallet_address_sc in oct_2022_alkimi_pool_wallets_list:
                oct_2022_val = 1
            if wallet_address_sc in nov_2022_alkimi_pool_wallets_list:
                nov_2022_val = 1
            if wallet_address_sc in dec_2022_alkimi_pool_wallets_list:
                dec_2022_val = 1
            if wallet_address_sc in q1_2023_alkimi_pool_wallets_list:
                q1_2023_val = 1
            if wallet_address_sc in q2_2023_alkimi_pool_wallets_list:
                q2_2023_val = 1
            if wallet_address_sc in lattice_12mths_wallet_addresses:
                lattice_12mths_val = 1
            if wallet_address_sc in lattice_3mths_wallet_addresses:
                lattice_3mths_val = 1

            total_tickets_nft_pools = eth_alex_nft_count + pol_mutated_alex_nft_count + pol_alexa_nft_count + \
                                      sep_2022_val + oct_2022_val + nov_2022_val + dec_2022_val + q1_2023_val + q2_2023_val + \
                                      lattice_12mths_val + lattice_3mths_val

            # assign this for now.... later for the first wallet this will be updated based on the $ADS balance
            total_tickets = total_tickets_nft_pools

            wallets_data.append({'wallet_id': wallet_address_sc, \
                                 'balance': balance, \
                                 'eligibility': 0, \
                                 'eth_alex_nft_count': eth_alex_nft_count, \
                                 'pol_alexa_nft_count': pol_alexa_nft_count, \
                                 'pol_mutated_alex_nft_count': pol_mutated_alex_nft_count, \
                                 'sep_2022': sep_2022_val, \
                                 'oct_2022': oct_2022_val, \
                                 'nov_2022': nov_2022_val, \
                                 'dec_2022': dec_2022_val, \
                                 'q1_2023': q1_2023_val, \
                                 'q2_2023': q2_2023_val, \
                                 'lattice_12mths': lattice_12mths_val, \
                                 'lattice_3mths': lattice_3mths_val, \
                                 'total_tickets_nft_pools': total_tickets_nft_pools, \
                                 'total_tickets_based_on_balance': 0, \
                                 'total_tickets': total_tickets
                                 })
            total_balance += balance

        # Determine the eligibility flag based on the total_balance
        eligibility_flag = total_balance >= 10000

        if total_balance >= 200000:
            tickets_based_on_balance = 10
        elif total_balance >= 50000:
            tickets_based_on_balance = 5
        else:
            tickets_based_on_balance = 0

        # Update total_tickets_based_on_balance for the first wallet
        first_wallet = wallets_data[0]
        first_wallet['total_tickets_based_on_balance'] = tickets_based_on_balance
        if eligibility_flag:
            first_wallet['eligibility'] = 1
        first_wallet['total_tickets'] = first_wallet['total_tickets_based_on_balance'] + first_wallet[
            'total_tickets_nft_pools'] + first_wallet['eligibility']

        # Add the total_balance, eligibility flag, NFT ownership flag, NFT count, and individual wallet data to the dictionary and store it in the list.
        emails_wallets_total_balance_raffle_nft.append(
            {'email': email, 'wallets': wallets_data, 'total_balance': total_balance,
             'eligibility_flag': eligibility_flag})

        return emails_wallets_total_balance_raffle_nft
    except Exception as e:
        return str(e)