100% found this document useful (1 vote)
637 views7 pages

Import Json

The summary provides the high level information about the document in 3 sentences or less: The document defines a function to buy a token through the PancakeSwap V2 exchange on the Binance Smart Chain network. It imports the necessary libraries, sets up a Web3 connection, and defines the PancakeRouter contract address and ABI. The function will execute a swap on PancakeSwap to purchase a given amount of tokens using the supplied private key and sender address.

Uploaded by

Nguyễn Kim Nam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
637 views7 pages

Import Json

The summary provides the high level information about the document in 3 sentences or less: The document defines a function to buy a token through the PancakeSwap V2 exchange on the Binance Smart Chain network. It imports the necessary libraries, sets up a Web3 connection, and defines the PancakeRouter contract address and ABI. The function will execute a swap on PancakeSwap to purchase a given amount of tokens using the supplied private key and sender address.

Uploaded by

Nguyễn Kim Nam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

import json

from web3 import Web3

import time

def buy_token_through_pancake_v2(private_key,sender_address,nb_buy,tokenToBuy,nb_gas):

bsc = 'https://bsc-dataseed.binance.org/'

web3 = Web3(Web3.HTTPProvider(bsc))

web3.isConnected()

# pancacke v2 sawp (gg r len bsc copy dia chi nay, mixed upper and lower case)

pandRouterContactAddress = '0x10ED43C718714eb63d5aA57B78B54704E256024E'

panabi = '[{"inputs":[{"internalType":"address","name":"_factory","type":"address"},
{"internalType":"address","name":"_WETH","type":"address"}],"stateMutability":"nonpayable","type":"
constructor"},{"inputs":[],"name":"WETH","outputs":
[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},
{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},
{"internalType":"address","name":"tokenB","type":"address"},
{"internalType":"uint256","name":"amountADesired","type":"uint256"},
{"internalType":"uint256","name":"amountBDesired","type":"uint256"},
{"internalType":"uint256","name":"amountAMin","type":"uint256"},
{"internalType":"uint256","name":"amountBMin","type":"uint256"},
{"internalType":"address","name":"to","type":"address"},
{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidity","outputs":
[{"internalType":"uint256","name":"amountA","type":"uint256"},
{"internalType":"uint256","name":"amountB","type":"uint256"},
{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"nonpayable","type":"
function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},
{"internalType":"uint256","name":"amountTokenDesired","type":"uint256"},
{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},
{"internalType":"uint256","name":"amountETHMin","type":"uint256"},
{"internalType":"address","name":"to","type":"address"},
{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidityETH","outputs":
[{"internalType":"uint256","name":"amountToken","type":"uint256"},
{"internalType":"uint256","name":"amountETH","type":"uint256"},
{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"payable","type":"fun
ction"},{"inputs":[],"name":"factory","outputs":
[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},
{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},
{"internalType":"uint256","name":"reserveIn","type":"uint256"},
{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountIn","outputs":
[{"internalType":"uint256","name":"amountIn","type":"uint256"}],"stateMutability":"pure","type":"func
tion"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},
{"internalType":"uint256","name":"reserveIn","type":"uint256"},
{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountOut","outputs":
[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"pure","type":"fu
nction"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},
{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsIn","outputs":
[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"fu
nction"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},
{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsOut","outputs":
[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"fu
nction"},{"inputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},
{"internalType":"uint256","name":"reserveA","type":"uint256"},
{"internalType":"uint256","name":"reserveB","type":"uint256"}],"name":"quote","outputs":
[{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"pure","type":"func
tion"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},
{"internalType":"address","name":"tokenB","type":"address"},
{"internalType":"uint256","name":"liquidity","type":"uint256"},
{"internalType":"uint256","name":"amountAMin","type":"uint256"},
{"internalType":"uint256","name":"amountBMin","type":"uint256"},
{"internalType":"address","name":"to","type":"address"},
{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidity","outputs":
[{"internalType":"uint256","name":"amountA","type":"uint256"},
{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type"
:"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},
{"internalType":"uint256","name":"liquidity","type":"uint256"},
{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},
{"internalType":"uint256","name":"amountETHMin","type":"uint256"},
{"internalType":"address","name":"to","type":"address"},
{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETH","outputs
":[{"internalType":"uint256","name":"amountToken","type":"uint256"},
{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","typ
e":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},
{"internalType":"uint256","name":"liquidity","type":"uint256"},
{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},
{"internalType":"uint256","name":"amountETHMin","type":"uint256"},
{"internalType":"address","name":"to","type":"address"},
{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETHSupportin
gFeeOnTransferTokens","outputs":
[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","ty
pe":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},
{"internalType":"uint256","name":"liquidity","type":"uint256"},
{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},
{"internalType":"uint256","name":"amountETHMin","type":"uint256"},
{"internalType":"address","name":"to","type":"address"},
{"internalType":"uint256","name":"deadline","type":"uint256"},
{"internalType":"bool","name":"approveMax","type":"bool"},
{"internalType":"uint8","name":"v","type":"uint8"},
{"internalType":"bytes32","name":"r","type":"bytes32"},
{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermit","out
puts":[{"internalType":"uint256","name":"amountToken","type":"uint256"},
{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","typ
e":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},
{"internalType":"uint256","name":"liquidity","type":"uint256"},
{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},
{"internalType":"uint256","name":"amountETHMin","type":"uint256"},
{"internalType":"address","name":"to","type":"address"},
{"internalType":"uint256","name":"deadline","type":"uint256"},
{"internalType":"bool","name":"approveMax","type":"bool"},
{"internalType":"uint8","name":"v","type":"uint8"},
{"internalType":"bytes32","name":"r","type":"bytes32"},
{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermitSupp
ortingFeeOnTransferTokens","outputs":
[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","ty
pe":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},
{"internalType":"address","name":"tokenB","type":"address"},
{"internalType":"uint256","name":"liquidity","type":"uint256"},
{"internalType":"uint256","name":"amountAMin","type":"uint256"},
{"internalType":"uint256","name":"amountBMin","type":"uint256"},
{"internalType":"address","name":"to","type":"address"},
{"internalType":"uint256","name":"deadline","type":"uint256"},
{"internalType":"bool","name":"approveMax","type":"bool"},
{"internalType":"uint8","name":"v","type":"uint8"},
{"internalType":"bytes32","name":"r","type":"bytes32"},
{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityWithPermit","output
s":[{"internalType":"uint256","name":"amountA","type":"uint256"},
{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type"
:"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},
{"internalType":"address[]","name":"path","type":"address[]"},
{"internalType":"address","name":"to","type":"address"},
{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapETHForExactTokens","ou
tputs":
[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":
"function"},{"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","ou
tputs":
[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":
"function"},{"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":"swapExactETHForTokensSupp
ortingFeeOnTransferTokens","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":
[{"internalType":"uint256","name":"amountIn","type":"uint256"},
{"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":"swapExactTokensForETH","ou
tputs":
[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","ty
pe":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},
{"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":"swapExactTokensForETHSupp
ortingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":
[{"internalType":"uint256","name":"amountIn","type":"uint256"},
{"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":"swapExactTokensForTokens","
outputs":
[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","ty
pe":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},
{"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":"swapExactTokensForTokensSu
pportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},
{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},
{"internalType":"uint256","name":"amountInMax","type":"uint256"},
{"internalType":"address[]","name":"path","type":"address[]"},
{"internalType":"address","name":"to","type":"address"},
{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactETH","ou
tputs":
[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","ty
pe":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},
{"internalType":"uint256","name":"amountInMax","type":"uint256"},
{"internalType":"address[]","name":"path","type":"address[]"},
{"internalType":"address","name":"to","type":"address"},
{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactTokens","
outputs":
[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","ty
pe":"function"},{"stateMutability":"payable","type":"receive"}]'

# STEP2 : PREPARE TO SEND TO PANCAKE

# sender_address = '0x8b950156DB55fd7Bb60Be578F63bC9E3796C332F'

#sender_address = '0x'+sender_address[2:].upper()

balance = web3.eth.get_balance(sender_address)

balance = web3.fromWei(balance,'ether')

# input('zzz')

tokenToBuy = web3.toChecksumAddress(tokenToBuy)

# don vi chuyen tien la WBNB

spend = web3.toChecksumAddress('0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c')

contract = web3.eth.contract(address = pandRouterContactAddress,abi = json.loads(panabi))

nonce = web3.eth.get_transaction_count(sender_address)

start = time.time()

pancakeswap2_txn = contract.functions.swapExactETHForTokens(

0,

[spend, tokenToBuy],

sender_address,
(int(time.time())+10000),

).buildTransaction({

'from':sender_address,

'value':web3.toWei(nb_buy,'ether'),

'gasPrice':web3.toWei(nb_gas,'gwei'),

'nonce':nonce

})

signed_txt = web3.eth.account.sign_transaction(pancakeswap2_txn,private_key=private_key)

tx_token = web3.eth.send_raw_transaction(signed_txt.rawTransaction)

print(web3.toHex(tx_token))

return tx_token

if __name__ =="__main__":

nb_try = 2000

private_key = 'ff06b981a183ce245396f11c654402ffb458f04e19597753f4586ac7a3353238'

sender_address = '0x3f8E0E8E70212f9c1E5ae03d7573B523c6e52bAE'

nb_buy = 0.13 # unit WBNB

# dia chi coin muon mua

#PVU

# tokenToBuy = '0x31471e0791fcdbe82fbf4c44943255e923f1b794'

tokenToBuy = '0xE485b2780C3822A62dA88857FD6481018EA8CB95'

nb_gas = 30

for i in range(nb_try):

print('i',i)

try:
tx_token =
buy_token_through_pancake_v2(private_key,sender_address,nb_buy,tokenToBuy,nb_gas)

input("DONE")

except Exception as e:

print('err %s'%e)

time.sleep(1)

You might also like