Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #125

Merged
merged 6 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytz
from time import time
from web3 import Web3
from web3.middleware import geth_poa_middleware
from web3.contract.contract import Contract, ContractFunction
from web3.types import Type, TxParams
from django.utils import timezone
Expand Down Expand Up @@ -50,23 +51,30 @@ def get_first_day_of_the_month():


class Web3Utils:
def __init__(self, rpc_url) -> None:
def __init__(self, rpc_url, poa = False) -> None:
self._rpc_url = rpc_url
self._w3 = None
self._account = None
self._contract = None
self._poa = poa

@property
def w3(self) -> Web3:
if self._w3 and self._w3.is_connected():
return self._w3

self._w3 = Web3(Web3.HTTPProvider(self._rpc_url))
if self.poa:
self._w3.middleware_onion.inject(geth_poa_middleware, layer=0)

if self._w3.is_connected():
return self._w3

raise Exception(f"RPC provider is not connected ({self._rpc_url})")

@property
def poa(self):
return self._poa

@property
def account(self):
Expand All @@ -83,7 +91,7 @@ def set_contract(self, address, abi):
self._contract = self.w3.eth.contract(address=address, abi=abi)

def contract_txn(self, func: Type[ContractFunction]):
signed_tx = self.build_contract_call(func)
signed_tx = self.build_contract_txn(func)
txn_hash = self.send_raw_tx(signed_tx)
return txn_hash.hex()

Expand All @@ -92,8 +100,10 @@ def contract_call(self, func: Type[ContractFunction], from_address=None):
return func.call({"from": from_address})
return func.call()

def build_contract_call(self, func: Type[ContractFunction]):
tx_data = func.build_transaction({"from": self.account.address})
def build_contract_txn(self, func: Type[ContractFunction]):
nonce = self.w3.eth.get_transaction_count(self.account.address)
tx_data = func.build_transaction(
{"from": self.account.address, "nonce": nonce})
return self.sign_tx(tx_data)

def sign_tx(self, tx_data: TxParams):
Expand Down
Loading
Loading