Created
June 27, 2023 02:51
-
-
Save L-Kov/21e4b8c9fae7598f48e3d639ab639670 to your computer and use it in GitHub Desktop.
Web3.py transfer CTF positions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from web3 import Web3 | |
import os | |
from dotenv import load_dotenv | |
load_dotenv() | |
pk = os.getenv('PRIVATE_KEY') | |
rpc = os.getenv('RPC_URL') | |
wallet_address = os.getenv('WALLET_ADDRESS') | |
new_wallet_address = os.getenv('NEW_WALLET_ADDRESS') | |
private_key = os.getenv('PRIVATE_KEY') | |
tokens = [11015470973684177829729219287262166995141465048508201953575582100565462316088] | |
w3 = Web3(Web3.HTTPProvider(rpc)) | |
abi = [{"constant": False,"inputs": [{"name": "from","type": "address"},{"name": "to","type": "address"},{"name": "ids","type": "uint256[]"},{"name": "values","type": "uint256[]"},{"name": "data","type": "bytes"}],"name":"safeBatchTransferFrom","outputs": [],"payable": False,"stateMutability": "nonpayable","type": "function"},{"constant": True,"inputs": [{"name": "owners","type": "address[]"},{"name": "ids","type": "uint256[]"}],"name": "balanceOfBatch","outputs": [{"name": "","type": "uint256[]"}],"payable": False,"stateMutability": "view","type": "function"}] | |
ctf_address = "0x4D97DCd97eC945f40cF65F87097ACe5EA0476045" | |
ctf = w3.eth.contract(address=ctf_address, abi=abi) | |
balances = ctf.functions.balanceOfBatch([wallet_address], tokens).call() | |
function = "safeBatchTransferFrom" | |
args = [wallet_address, new_wallet_address, tokens, balances, b''] | |
print(args) | |
nonce = w3.eth.get_transaction_count(wallet_address) | |
gas_estimate = ctf.functions[function](*args).estimate_gas({"from": wallet_address}) | |
transaction = ctf.functions[function](*args).build_transaction({ | |
'from': wallet_address, | |
'nonce': nonce, | |
'gas': gas_estimate, | |
'gasPrice': w3.eth.gas_price, | |
}) | |
signed_transaction = w3.eth.account.sign_transaction(transaction, private_key) | |
transaction_hash = w3.eth.send_raw_transaction(signed_transaction.rawTransaction) | |
print(transaction_hash.hex()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment