-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.py
38 lines (31 loc) · 1.24 KB
/
contract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
from web3 import Web3
# from web3 import exceptions
from cryptography.fernet import Fernet
from dotenv import load_dotenv
# Load environment variables (.env file)
load_dotenv()
rpc_url = "https://bsc-dataseed.binance.org:443"
web3 = Web3(Web3.HTTPProvider(rpc_url))
def connect_to_contract(contract_addr, contract_abi):
contract = web3.eth.contract(address=contract_addr, abi=contract_abi)
return contract
def send_txn(txn, private_key_encrypt):
# try:
# get key from environment variable
fernet_key = os.environ['FERNET_KEY']
# decode encrypted key
private_key = Fernet(fernet_key.encode()).decrypt(private_key_encrypt.encode()).decode()
txn_signed = web3.eth.account.signTransaction(txn, private_key)
tx_hash = web3.eth.sendRawTransaction(txn_signed.rawTransaction)
tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash)
return tx_receipt
# except exceptions.SolidityError as error:
# print(error)
def get_tx_options(public_address, gas=500000):
return {
"nonce": web3.eth.getTransactionCount(public_address),
"from": public_address,
"gas": int(os.environ['GAS_FEE']),
"gasPrice": web3.toWei(5, "gwei"),
}