Skip to content

Instantly share code, notes, and snippets.

@halaprix
Created October 16, 2024 10:15
Show Gist options
  • Select an option

  • Save halaprix/e4777718a379f9da4ea3e0ea30d368be to your computer and use it in GitHub Desktop.

Select an option

Save halaprix/e4777718a379f9da4ea3e0ea30d368be to your computer and use it in GitHub Desktop.
Claim rewards calldata - spark
import json
from web3 import Web3
from eth_abi import encode
import csv
# Configure these variables
RPC_URL = "" # Replace with your Infura project ID or other RPC URL
CONTRACT_ADDRESS = "0x4370D3b6C9588E02ce9D22e684387859c7Ff5b34" # Replace with the actual contract address
ASSET_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" # Replace with the actual asset address
REWARD_ADDRESS = "0x7f39c581f595b53c5cb19bd0b3f8da6c935e2ca0" # Replace with the actual reward token address
CLAIMER_PRIVATE_KEY = "" # Replace with the private key of the authorized claimer
def load_addresses(filename):
addresses = []
with open(filename, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
addresses.append((row['proxyAddress'], row['walletOwner']))
return addresses
def encode_claim_data(assets, user, reward):
return encode(['address[]', 'address', 'address'], [assets, user, reward])
def claim_rewards(w3, contract, proxy_address, wallet_owner, dry_run=True):
assets = [Web3.to_checksum_address(ASSET_ADDRESS)]
user = Web3.to_checksum_address(wallet_owner)
reward = Web3.to_checksum_address(REWARD_ADDRESS)
encoded_data = encode_claim_data(assets, user, reward)
function_selector = Web3.keccak(text="claimAllRewardsOnBehalf(address[],address,address)")[:4]
transaction = {
'to': contract.address,
'data': function_selector + encoded_data,
'gas': 300000, # Adjust as needed
'gasPrice': w3.eth.gas_price,
'nonce': w3.eth.get_transaction_count(w3.eth.default_account),
}
if dry_run:
print(f"Dry run for {proxy_address} (owner: {wallet_owner}):")
print(f"Transaction data: {transaction['data'].hex()}")
# simulate the transaction
print("Transaction successful")
else:
signed_txn = w3.eth.account.sign_transaction(transaction, CLAIMER_PRIVATE_KEY)
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(f"Transaction for {proxy_address} (owner: {wallet_owner}) sent. Hash: {tx_hash.hex()}")
print(f"Transaction status: {'Success' if receipt['status'] == 1 else 'Failed'}")
def main():
w3 = Web3(Web3.HTTPProvider(RPC_URL))
contract = w3.eth.contract(address=Web3.to_checksum_address(CONTRACT_ADDRESS), abi=[]) # We don't need the ABI for this
w3.eth.default_account = w3.eth.account.from_key(CLAIMER_PRIVATE_KEY).address
addresses = load_addresses('addresses_spark.txt')
dry_run = input("Do you want to perform a dry run? (yes/no): ").lower() == 'yes'
for proxy_address, wallet_owner in addresses:
claim_rewards(w3, contract, proxy_address, wallet_owner, dry_run)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment