Skip to content

Instantly share code, notes, and snippets.

@a2468834
Last active June 18, 2022 19:35
Show Gist options
  • Save a2468834/8ed70083a80b4459227e4df2f765d5cd to your computer and use it in GitHub Desktop.
Save a2468834/8ed70083a80b4459227e4df2f765d5cd to your computer and use it in GitHub Desktop.
from web3 import Web3 # Use version 5.29.2
from web3.middleware import geth_poa_middleware
import json
class CONST:
def ABI():
with open("./WrappedEther.json") as f:
return json.load(f)
NodeProvider = lambda : "https://"
WrappedEther = lambda : ""
PubKey = lambda : ""
PriKey = lambda : ""
Amount = lambda : 10000
ChainId = lambda : 4 # At Rinkeby
if __name__ == "__main__":
# Connect provider
w3 = Web3(Web3.HTTPProvider(CONST.NodeProvider()))
w3.middleware_onion.inject(geth_poa_middleware, layer=0) # Could disable this line for Mainnet
# Prepare helper objects
contract = w3.eth.contract(address=CONST.WrappedEther(), abi=CONST.ABI())
nonce = w3.eth.get_transaction_count(CONST.PubKey())
# Prepare unsigned txn
unsigned_txn = contract.functions.approve(
CONST.PubKey(),
CONST.Amount()
).buildTransaction({
"chainId": CONST.ChainId(),
"gas": 10000000,
"maxFeePerGas": w3.toWei("100", "gwei"),
"maxPriorityFeePerGas": w3.toWei("10", "gwei"),
"nonce": nonce
})
# Sign the txn
signed_txn = w3.eth.account.sign_transaction(unsigned_txn, private_key=CONST.PriKey())
# Send signed txn
txn_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
txn_hash = w3.toHex(txn_hash)
print(txn_hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment