Year 2021, crypto year.
Although Elon Musk crashed the market and I lost almost all my investments, I still think it’s worth learning a little bit on the Crypto.
First thing I want to do, is to see how can I do a Pancakeswap automatically? Yea, automatically.
Just a disclaimer – Please note this is not financial advice
It turns out that there are already a lot of references on the network, so I referred a lot.
The package I used was the most popular one – Web3.py
Using this package, and with some test using my wallet and real money, I successfully swapped some token using my BNBs
So, following’s the code that you need to do the same thing if you want – However, you take your own risk
The package and the scripts needs python3, you can use pip3 install web3 to install the package.
import time
import config as config
from web3 import Web3
PancakeABI = open('pancakeABI','r').read().replace('\n','')
bsc="https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
print(web3.isConnected())
#My own address to swap from
sender_address = "YOUR OWN WALLET PUBLIC ADDRESS"
#This is global Pancake V2 Swap router address
router_address = "0x10ED43C718714eb63d5aA57B78B54704E256024E"
#always spend using Wrapped BNB
#I guess you want to use other coins to swap you can do that, but for me I used Wrapped BNB
spend = web3.toChecksumAddress("0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c")
#This is your private key info
private="YOUR OWN WALLET PRIVATE KEY"
#Print out your balances just for checking
balance = web3.eth.get_balance(sender_address)
print(balance)
humanReadable = web3.fromWei(balance,'ether')
print(humanReadable)
#Contract id is the new token we are swaping to
contract_id = web3.toChecksumAddress("THE Contract ID of the destination Token")
#Setup the PancakeSwap contract
contract = web3.eth.contract(address=router_address, abi=PancakeABI)
nonce = web3.eth.get_transaction_count(sender_address)
start = time.time()
print(web3.toWei('0.02','ether'))
pancakeswap2_txn = contract.functions.swapExactETHForTokens(
1074184953676691292, # here setup the minimum destination token you want to have, you can do some math, or you can put a 0 if you don't want to care
[spend,contract_id],
sender_address,
(int(time.time()) + 1000000)
).buildTransaction({
'from': sender_address,
'value': web3.toWei(0.02,'ether'),#This is the Token(BNB) amount you want to Swap from
'gas': 250000,
'gasPrice': web3.toWei('5','gwei'),
'nonce': nonce,
})
signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key=private)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(web3.toHex(tx_token))
For the pancakeABI you need, you copy it from PancakeSWAP’s contract, or you can just use following simplified one since we only used 1 function defined in Pancake ABI, save the whole text to a file named “pancakeABI” in the same directory to the code and you should be fine to go
[{
"inputs": [{
"internalType": "uint256",
"name": "amountOutMin",
"type": "uint256"
}, {
"internalType": "address[]",
"name": "path",
"type": "address[]"
}, {
"internalType": "address",
"name": "to",
"type": "address"
}, {
"internalType": "uint256",
"name": "deadline",
"type": "uint256"
}
],
"name": "swapExactETHForTokens",
"outputs": [{
"internalType": "uint256[]",
"name": "amounts",
"type": "uint256[]"
}
],
"stateMutability": "payable",
"type": "function"
}
]