Skip to content

Commit

Permalink
Merge pull request #214 from UnitapApp/refactor/change-prizetap-web3u…
Browse files Browse the repository at this point in the history
…tils-inh-to-comp

Change prizetap web3_utils inh to comp
  • Loading branch information
PooyaFekri authored Dec 21, 2023
2 parents f8cc875 + 56596ed commit 3c400d9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 65 deletions.
8 changes: 4 additions & 4 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ def wait_for_transaction_receipt(self, tx_hash):
def current_block(self):
return self.w3.eth.block_number

def get_transaction_by_hash(self, hash):
return self.w3.eth.get_transaction(hash)
def get_transaction_by_hash(self, tx_hash):
return self.w3.eth.get_transaction(tx_hash)

def get_gas_price(self):
return self.w3.eth.gas_price
Expand All @@ -175,8 +175,8 @@ def from_wei(self, value: int, unit: str = "ether"):
def to_checksum_address(address: str):
return Web3.to_checksum_address(address.lower())

def get_transaction_receipt(self, hash):
return self.w3.eth.get_transaction_receipt(hash)
def get_transaction_receipt(self, tx_hash):
return self.w3.eth.get_transaction_receipt(tx_hash)

def get_balance(self, address):
return self.w3.eth.get_balance(address)
8 changes: 2 additions & 6 deletions prizetap/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ def __init__(self, user_profile: UserProfile) -> None:
def is_observed(self, *args, **kwargs):
chain = Chain.objects.get(chain_id=1)
self.unitappass_client = UnitapPassClient(chain)
user_address: str = self.user_profile.wallets.get(
wallet_type=chain.chain_type
).address
user_address = self.unitappass_client.w3.to_checksum_address(
user_address.lower()
)
user_address: str = self.user_profile.wallets.get(wallet_type=chain.chain_type).address
user_address = self.unitappass_client.to_checksum_address(user_address.lower())
return self.unitappass_client.is_holder(user_address)


Expand Down
4 changes: 1 addition & 3 deletions prizetap/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ def set_raffle_ids(self):
contract_client = PrizetapContractClient(raffle)

receipt = contract_client.get_transaction_receipt(raffle.tx_hash)
log = contract_client.contract.events.RaffleCreated().process_receipt(
receipt, errors=contract_client.LOG_DISCARD
)[0]
log = contract_client.process_raffle_receipt(receipt)

raffle.raffleId = log["args"]["raffleId"]
onchain_raffle = contract_client.get_raffle()
Expand Down
94 changes: 42 additions & 52 deletions prizetap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,23 @@
)


class PrizetapContractClient(Web3Utils):
class PrizetapContractClient:
def __init__(self, raffle) -> None:
super().__init__(raffle.chain.rpc_url_private, raffle.chain.poa)
self.raffle = raffle
self.web3_utils = Web3Utils(self.raffle.chain.rpc_url_private, self.raffle.chain.poa)
abi = PRIZETAP_ERC721_ABI if self.raffle.is_prize_nft else PRIZETAP_ERC20_ABI
self.set_contract(self.raffle.contract, abi)
self.set_account(self.raffle.chain.wallet.private_key)

def set_raffle_random_words(
self, expiration_time, random_words, reqId, muon_sig, gateway_sig
):
func = self.contract.functions.setRaffleRandomNumbers(
self.raffle.raffleId,
expiration_time,
random_words,
reqId,
muon_sig,
gateway_sig,
self.web3_utils.set_contract(self.raffle.contract, abi)
self.web3_utils.set_account(self.raffle.chain.wallet.private_key)

def set_raffle_random_words(self, expiration_time, random_words, reqId, muon_sig, gateway_sig):
func = self.web3_utils.contract.functions.setRaffleRandomNumbers(
self.raffle.raffleId, expiration_time, random_words, reqId, muon_sig, gateway_sig
)
return self.contract_txn(func)
return self.web3_utils.contract_txn(func)

def get_raffle(self):
func = self.contract.functions.raffles(self.raffle.raffleId)
output = self.contract_call(func)
func = self.web3_utils.contract.functions.raffles(self.raffle.raffleId)
output = self.web3_utils.contract_call(func)
return self.__process_raffle(output)

def get_last_winner_index(self):
Expand All @@ -47,31 +40,28 @@ def get_last_winner_index(self):
def set_winners(self):
winners_count = self.raffle.winners_count
last_winner_index = self.get_last_winner_index()
tx_hash = None
while last_winner_index < winners_count:
to_id = last_winner_index + 25
if to_id > winners_count:
to_id = winners_count
func = self.contract.functions.setWinners(self.raffle.raffleId, to_id)
func = self.web3_utils.contract.functions.setWinners(self.raffle.raffleId, to_id)
last_winner_index = to_id
tx_hash = self.contract_txn(func)
self.wait_for_transaction_receipt(tx_hash)
tx_hash = self.web3_utils.contract_txn(func)
self.web3_utils.wait_for_transaction_receipt(tx_hash)

return tx_hash

def get_raffle_winners(self):
func = self.contract.functions.getWinners(
self.raffle.raffleId, 1, self.raffle.winners_count
)
return self.contract_call(func)
func = self.web3_utils.contract.functions.getWinners(self.raffle.raffleId, 1, self.raffle.winners_count)
return self.web3_utils.contract_call(func)

def get_raffle_winners_count(self):
func = self.contract.functions.getWinnersCount(self.raffle.raffleId)
return self.contract_call(func)
func = self.web3_utils.contract.functions.getWinnersCount(self.raffle.raffleId)
return self.web3_utils.contract_call(func)

def __process_raffle(self, output):
raffles_abi = [
item for item in self.contract.abi if item.get("name") == "raffles"
]
raffles_abi = [item for item in self.web3_utils.contract.abi if item.get("name") == "raffles"]
assert len(raffles_abi) == 1, "The raffles abi not found"
raffles_abi = raffles_abi[0]
result = {}
Expand All @@ -80,45 +70,45 @@ def __process_raffle(self, output):
return result


class VRFClientContractClient(Web3Utils):

class VRFClientContractClient:
def __init__(self, chain) -> None:
super().__init__(chain.rpc_url_private, chain.poa)
address = (
VRF_CLIENT_POLYGON_ADDRESS
if DEPLOYMENT_ENV == "main"
else VRF_CLIENT_MUMBAI_ADDRESS
)
self.set_contract(address, VRF_CLIENT_ABI)
self.set_account(chain.wallet.private_key)
self.web3_utils = Web3Utils(chain.rpc_url_private, chain.poa)
address = VRF_CLIENT_POLYGON_ADDRESS if DEPLOYMENT_ENV == "main" else VRF_CLIENT_MUMBAI_ADDRESS
self.web3_utils.set_contract(address, VRF_CLIENT_ABI)
self.web3_utils.set_account(chain.wallet.private_key)

def get_last_request_id(self):
func = self.contract.functions.lastRequestId()
return self.contract_call(func)
func = self.web3_utils.contract.functions.lastRequestId()
return self.web3_utils.contract_call(func)

def get_last_request(self):
last_id = self.get_last_request_id()
func = self.contract.functions.vrfRequests(last_id)
return self.contract_call(func)
func = self.web3_utils.contract.functions.vrfRequests(last_id)
return self.web3_utils.contract_call(func)

def get_validity_period(self):
func = self.contract.functions.validityPeriod()
return self.contract_call(func)
func = self.web3_utils.contract.functions.validityPeriod()
return self.web3_utils.contract_call(func)

def request_random_words(self, num_words):
last_request = self.get_last_request()
expiration_time = last_request[0]
now = int(time.time())
if expiration_time < now:
func = self.contract.functions.requestRandomWords(num_words)
return self.contract_txn(func)
func = self.web3_utils.contract.functions.requestRandomWords(num_words)
return self.web3_utils.contract_txn(func)


class UnitapPassClient(Web3Utils):
class UnitapPassClient:
def __init__(self, chain: Chain) -> None:
super().__init__(chain.rpc_url_private, chain.poa)
self.set_contract("0x23826Fd930916718a98A21FF170088FBb4C30803", UNITAP_PASS_ABI)
self.web3_utils = Web3Utils(chain.rpc_url_private, chain.poa)
self.web3_utils.set_contract("0x23826Fd930916718a98A21FF170088FBb4C30803", UNITAP_PASS_ABI)

def is_holder(self, address: str):
func = self.contract.functions.balanceOf(address)
balance = self.contract_call(func)
func = self.web3_utils.contract.functions.balanceOf(address)
balance = self.web3_utils.contract_call(func)
return balance != 0

def to_checksum_address(self, address: str):
return self.web3_utils.w3.to_checksum_address(address)

0 comments on commit 3c400d9

Please sign in to comment.