Web3js

PancakeSwap 執行恢復

  • June 18, 2021

我一直在嘗試編寫一個在 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)  

我試過批准和不批准,結果是一樣的。我在幣安測試網上執行,我的測試網錢包上有 BNB。

嗯,我想通了。原來我用的汽油太少了。

引用自:https://ethereum.stackexchange.com/questions/99231