111 lines
3.9 KiB
Python
111 lines
3.9 KiB
Python
from web3 import Web3
|
|
import time
|
|
|
|
# ================= 配置区 =================
|
|
# 1. 填入你提取出的私钥 (以 0x 开头)
|
|
PRIVATE_KEY = "0xb759f8d56102c2e932d968c9a64fa79e8645fd5c28bef737c218ae3f3a13c480"
|
|
|
|
# 2. Polygon RPC 节点 (如果失效可以换成 https://polygon.drpc.org)
|
|
RPC_URL = "https://polygon-rpc.com"
|
|
|
|
# 3. 相关合约地址 (Polygon 网络)
|
|
USDC_ADDRESS = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174" # 官方 USDC (Bridged)
|
|
CLOB_EXCHANGE = "0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E" # Polymarket 交易授权合约
|
|
|
|
|
|
# =========================================
|
|
|
|
def manual_approve():
|
|
# 连接网络
|
|
w3 = Web3(Web3.HTTPProvider(RPC_URL))
|
|
|
|
if not w3.is_connected():
|
|
print("❌ 无法连接到 Polygon 网络,请检查网络或代理设置。")
|
|
return
|
|
|
|
try:
|
|
# 加载账户
|
|
account = w3.eth.account.from_key(PRIVATE_KEY)
|
|
my_address = account.address
|
|
print(f"👤 钱包地址: {my_address}")
|
|
|
|
# 1. 检查 MATIC 余额
|
|
balance = w3.eth.get_balance(my_address)
|
|
matic_balance = w3.from_wei(balance, 'ether')
|
|
print(f"⛽ 当前 MATIC 余额: {matic_balance:.4f} POL")
|
|
|
|
if matic_balance < 0.01:
|
|
print("❌ 余额不足以支付 Gas 费,请先充值 MATIC。")
|
|
return
|
|
|
|
# 2. 实例化 USDC 合约
|
|
abi = [
|
|
{
|
|
"constant": False,
|
|
"inputs": [{"name": "_spender", "type": "address"}, {"name": "_value", "type": "uint256"}],
|
|
"name": "approve",
|
|
"outputs": [{"name": "", "type": "bool"}],
|
|
"type": "function"
|
|
},
|
|
{
|
|
"constant": True,
|
|
"inputs": [{"name": "_owner", "type": "address"}, {"name": "_spender", "type": "address"}],
|
|
"name": "allowance",
|
|
"outputs": [{"name": "", "type": "uint256"}],
|
|
"type": "function"
|
|
}
|
|
]
|
|
usdc_contract = w3.eth.contract(address=w3.to_checksum_address(USDC_ADDRESS), abi=abi)
|
|
|
|
# 3. 检查是否已经授权过
|
|
current_allowance = usdc_contract.functions.allowance(my_address, CLOB_EXCHANGE).call()
|
|
if current_allowance > 10 ** 12: # 如果授权额度大于 1,000,000 USDC
|
|
print("✅ 已经拥有足够的授权额度,无需重复操作。")
|
|
return
|
|
|
|
# 4. 构造授权交易 (无限额度)
|
|
print("⏳ 正在构建授权交易...")
|
|
max_amount = 2 ** 256 - 1
|
|
|
|
gas_price = w3.eth.gas_price
|
|
# 增加 20% 以确保快速成交
|
|
adjusted_gas_price = int(gas_price * 1.2)
|
|
|
|
tx = usdc_contract.functions.approve(
|
|
w3.to_checksum_address(CLOB_EXCHANGE),
|
|
max_amount
|
|
).build_transaction({
|
|
'from': my_address,
|
|
'nonce': w3.eth.get_transaction_count(my_address),
|
|
'gas': 100000,
|
|
'gasPrice': adjusted_gas_price,
|
|
'chainId': 137
|
|
})
|
|
|
|
# 5. 签名并发送
|
|
signed_tx = w3.eth.account.sign_transaction(tx, PRIVATE_KEY)
|
|
|
|
# 处理新旧版本 web3.py 的属性差异
|
|
raw_tx = getattr(signed_tx, 'raw_transaction', getattr(signed_tx, 'rawTransaction', None))
|
|
|
|
print("📡 正在发送交易到区块链...")
|
|
tx_hash = w3.eth.send_raw_transaction(raw_tx)
|
|
print(f"✅ 交易已发出! Hash: {w3.to_hex(tx_hash)}")
|
|
|
|
# 6. 等待确认
|
|
print("⏳ 正在等待区块确认...")
|
|
receipt = w3.eth.wait_for_transaction_receipt(tx_hash, timeout=120)
|
|
|
|
if receipt.status == 1:
|
|
print("-" * 30)
|
|
print("🎊 授权成功!你现在可以运行【购买.py】了。")
|
|
print("-" * 30)
|
|
else:
|
|
print("❌ 交易被拒绝,请检查 PolygonScan 确认原因。")
|
|
|
|
except Exception as e:
|
|
print(f"❌ 发生错误: {str(e)}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
manual_approve() |