Skip to content

Instantly share code, notes, and snippets.

@Umiiii
Created October 29, 2024 13:35
Show Gist options
  • Save Umiiii/20449447d0ba2389793c3d07fc7fbf2f to your computer and use it in GitHub Desktop.
Save Umiiii/20449447d0ba2389793c3d07fc7fbf2f to your computer and use it in GitHub Desktop.
from web3 import Web3
from eth_account.messages import encode_defunct
import json
# Initialize Web3 connection
w3 = Web3(Web3.HTTPProvider("https://eth.llamarpc.com"))
YOUR_PRIVATE_KEY = ""
# Smart contract address and ABI
contract_address = "0x8143182a775C54578c8B7b3Ef77982498866945D"
abi = [
{
"inputs": [
{
"components": [
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "address",
"name": "primarySaleRecipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "quantity",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "price",
"type": "uint256"
},
{
"internalType": "address",
"name": "currency",
"type": "address"
},
{
"internalType": "uint128",
"name": "validityStartTimestamp",
"type": "uint128"
},
{
"internalType": "uint128",
"name": "validityEndTimestamp",
"type": "uint128"
},
{
"internalType": "bytes32",
"name": "uid",
"type": "bytes32"
}
],
"internalType": "struct ISignatureMintERC20.MintRequest",
"name": "_req",
"type": "tuple"
},
{
"internalType": "bytes",
"name": "_signature",
"type": "bytes"
}
],
"name": "mintWithSignature",
"outputs": [
{
"internalType": "address",
"name": "signer",
"type": "address"
}
],
"stateMutability": "payable",
"type": "function"
}
]
# Load contract
contract = w3.eth.contract(address=contract_address, abi=abi)
# Define a function to process each mint request
def process_mint_requests(requests_json):
# Load the list of requests from the input JSON
mint_requests = json.loads(requests_json)
# Iterate over each request
for mint_request in mint_requests:
try:
# Convert and construct parameters from the mint request
to = Web3.to_checksum_address(mint_request["to"])
primary_sale_recipient = Web3.to_checksum_address(mint_request["primarySaleRecipient"])
quantity = int(mint_request["quantity"])
price = int(mint_request["price"])
currency = Web3.to_checksum_address(mint_request["currency"])
validity_start_timestamp = int(mint_request["validityStartTimestamp"])
validity_end_timestamp = int(mint_request["validityEndTimestamp"])
uid = bytes.fromhex(mint_request["uid"][2:]) # Convert hex string to bytes
signature = bytes.fromhex(mint_request["signature"][2:]) # Convert hex string to bytes
# Construct the mint request tuple
mint_request_tuple = (
to,
primary_sale_recipient,
quantity,
price,
currency,
validity_start_timestamp,
validity_end_timestamp,
uid
)
# Send transaction
account = w3.eth.account.from_key(YOUR_PRIVATE_KEY)
# Get gas price dynamically
gas_price = w3.eth.gas_price
transaction = contract.functions.mintWithSignature(mint_request_tuple, signature).build_transaction({
'chainId': 1, # Mainnet
'gas': 120000,
'gasPrice': gas_price,
'nonce': w3.eth.get_transaction_count(account.address),
})
# Sign the transaction
signed_tx = w3.eth.account.sign_transaction(transaction, private_key=YOUR_PRIVATE_KEY)
try:
simulated_result = contract.functions.mintWithSignature(mint_request_tuple, signature).call({
'from': account.address
})
print(f"Simulation successful, signer: {simulated_result}")
except Exception as e:
print(f"Simulation failed: {e}")
continue
# Send the transaction
txn_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
# Get transaction receipt
print(f"Transaction hash: {txn_hash.hex()}")
txn_receipt = w3.eth.wait_for_transaction_receipt(txn_hash)
except Exception as e:
print(f"Error processing mint request: {e}")
# Example usage with a JSON array of mint requests
requests_json = '[{bridge-send-output}]'
process_mint_requests(requests_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment