我一直在尝试编写在pancakeSwap上运行的机器人代码,由于某种原因,我尝试使用这个脚本的所有事务都会被恢复。
API_URL = 'https://data-seed-prebsc-1-s1.binance.org:8545/' #testnet
CONTRACT_ID = Web3.toChecksumAddress('0xc3f9c723ce06ee0fa420c8c88cee115fa0147748')
SENDER_ADDRESS = '00'
SENDER_PRIVATE_KEY = '00'
ROUTER_ADDRESS = '0xD99D1c33F9fC3444f8101754aBC46c52416550D1' #pancakeswap V2 router tesnet
Spend = Web3.toChecksumAddress('0xed24fc36d5ee211ea25a80239fb8c4cfd80f12ee') #spend binance
Receive = CONTRACT_ID
max_approval_hex = f"0x{64 * 'f'}"
max_approval_int = int(max_approval_hex, 16)
w3 = Web3(Web3.HTTPProvider(API_URL))
contract = w3.eth.contract(address=ROUTER_ADDRESS, abi=ABI)
erc_20_contract = w3.eth.contract(address=Spend, abi=ERC20_ABI)
nonce = w3.eth.get_transaction_count(SENDER_ADDRESS)
#send coin to router
approve_txn = erc_20_contract.functions.transferFrom(SENDER_ADDRESS, ROUTER_ADDRESS, 1).buildTransaction({
'from': SENDER_ADDRESS,
'gas': w3.toWei('250000', 'wei'),
'nonce': nonce,
})
signed_txn = w3.eth.account.sign_transaction(approve_txn, private_key=SENDER_PRIVATE_KEY)
w3.eth.send_raw_transaction(signed_txn.rawTransaction)
#approve the coin
approve_txn = erc_20_contract.functions.approve(ROUTER_ADDRESS, max_approval_int).buildTransaction({
'from': SENDER_ADDRESS,
'gas': w3.toWei('250000', 'wei'),
'nonce': nonce + 1,
})
signed_txn = w3.eth.account.sign_transaction(approve_txn, private_key=SENDER_PRIVATE_KEY)
w3.eth.send_raw_transaction(signed_txn.rawTransaction)
start = time.time()
pancakeswap_txn = contract.functions.swapExactTokensForTokens(
100,
10,
[Spend, Receive],
SENDER_ADDRESS,
(int(time.time()) + 1000000)
).buildTransaction({
'from': SENDER_ADDRESS,
'gas': 100000,
'gasPrice': w3.toWei('100', 'gwei'),
'nonce': nonce + 2,
})
signed_txn = w3.eth.account.sign_transaction(approve_txn,
private_key=SENDER_PRIVATE_KEY)
w3.eth.send_raw_transaction(signed_txn.rawTransaction) 我试过批准和不批准,结果是一样的。我正在运行二进制TestNet,我的测试网钱包上有BNB。
发布于 2021-05-19 03:48:16
嗯,我想出来了。原来我用的汽油太少了。
https://ethereum.stackexchange.com/questions/99231
复制相似问题