i am newbie in Python. I am deciphering some lines of code using the Web3.py package and I came across some lines that do not have said package or do not locate it on the web page. I interpret that it is to finally buy the token in question.

Does anyone know which package contract.functions.swapExactETHforTokens belongs to or if it has any binding of the properties?

I tried to look for web3.py and i could not get any information. Someone told me that is a contract function in pancakeswap contract (0x10ED43C718714eb63d5aA57B78B54704E256024E) but i could not find any sintaxis or literature about other codes to use.

I'll be thankfull if you provide me any link to read all properties or how to interpret this parameters linked to Pancakeswap code contract.

I used these links trying to look for an answer:

Web3.py Documentation

Uniswap Docs

contract = web3.eth.contract(address=panRouterContractAddress, abi=panabi)

nonce = web3.eth.get_transaction_count(sender_address)
start = time.time()

pancakeswap2_txn = contract.functions.swapExactETHForTokens(
10000000000, # set to 0, or specify minimum amount of tokeny you want to receive - consider decimals!!!
(int(time.time()) + 10000)
'from': sender_address,
'value': web3.toWei(0.001,'ether'),#This is the Token(BNB) amount you want to Swap from
'gas': 250000,
'gasPrice': web3.toWei('5','gwei'),
'nonce': nonce,

This is part of the code i found on Pancakeswap Contract Source Code related to that but i don't know is they are similar to use.

function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
    returns (uint[] memory amounts)
    require(path[0] == WETH, 'PancakeRouter: INVALID_PATH');
    amounts = PancakeLibrary.getAmountsOut(factory, msg.value, path);
    require(amounts[amounts.length - 1] >= amountOutMin, 'PancakeRouter: INSUFFICIENT_OUTPUT_AMOUNT');
    IWETH(WETH).deposit{value: amounts[0]}();
    assert(IWETH(WETH).transfer(PancakeLibrary.pairFor(factory, path[0], path[1]), amounts[0]));
    _swap(amounts, path, to);

1 Answer 1


It does not belong to web3.py itself, but web3.py accesses the smart contract from the uniswap router which is deployed on the ethereum blockchain with the address: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D


The uniswap router source code, including the function swapExactETHForTokens, can be found here https://github.com/Uniswap/v2-periphery/blob/master/contracts/UniswapV2Router02.sol

Pancakeswap is just a copy of uniswap on the binance smart chain, the pancakeswap router address is the following: 0x10ED43C718714eb63d5aA57B78B54704E256024E


Edit based on Joses comment:

So, let's first distinguish the call to the smart contract swapExactETHForTokens with 4 parameters, and the built transaction with 5 parameters. The transaction wraps the call to the smart contract. Every call to a method of a smart contract, meaning every transaction, which changes the state of the blockchain has to be paid for with wei. Wei is a fraction of Ether (or on BSC it is BNB), the smallest unit thereof.

One has to pay for a transaction, because there are many people competing for their own transaction to be in the next block of the chain. The competing/pending transactions are placed in a transaction pool (also called the Mempool). Whether your transaction wins the competition of being in the next block, depends on whether the node which adds this next block (decided via mining/stacking) chooses your transaction among other transactions.

A node is rewarded with whatever people decide to pay for their transaction. This introduces an incentive for the nodes to choose transactions from the mempool where the owner of the transaction is willing to pay more wei per computation. Gas is the unit of computation.

Now, let's have a look at the transaction parameters: from, value, gas, gasPrice and nonce.

from, is the address/"public key" of the person who wants the execute a smart contract's method and builds the transaction. Transactions are then signed with help of the private key. If you use ganache or geth the signing is done for you.

value. Some smart contract methods are payable, which means they can receive Ether/BNB. For instance, the WETH/WBNB token contract has a deposit method, which is payable and converts the value of Ether/BNB into corresponding WETH/BNB tokens.

gasPrice, the amount of Wei you are willing to pay for each unit of gas burned during the execution of your transaction.

gas, the max amount of gas you are willing to pay for. So gas * gasPrice is the max Wei you are willing to pay for. If the computations of the transaction reach this limit the transaction fails.

nonce, rolling number of transactions of the address from.

The parameters from and value are accessible via msg.sender and msg.value within the smart contract, e.g. within the deposit function in the WBNB contract.

Now, that all the transaction parameters are in place, one can enter the smart contract method call, swapExactETHForTokens. The parameters are explained in the uniswap documentary you linked to.

  • Hi, i understand that. However, The structure in the Uniswap contract (or the one I posted in my question which is the same) does not match the format in which the Pancakeswap code is written. In the code you have 9 required elements separated by a comma (Min tokens to receive, [spend, tokenToBuy], sender_address, Unixstamp runtime, token, gas, gas to spend, nonce ?!). Whereas in the mother code of the Uniswap contract it only has 4 elements to fill: "swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)" Why this disparity? Dec 25, 2021 at 1:01
  • I really appreciate your help. I understanded everything. I deduced that line of code is this structure: contract.functions.swapExactETHForTokens("explained").buildTransaction("xxx"). Last question: do you have any link to understand ".buildTransaction" method and its components following the same estructure here? I found this link but comparing with this code that you explained to me has differents things or maybe is a new version of the code: Contract Method Dec 27, 2021 at 5:21
  • it is not web3py, but web3pyin the truffle docs the transaction build is explained here: trufflesuite.com/docs/truffle/getting-started/… here is an example of a swap in web3py github.com/NikolajevZj/Bocik/blob/main/Pancake_b.py
    – rassi
    Dec 27, 2021 at 15:04
  • Very thank you. Your last answer responded my final query. I can see why "to" and "data" are not in my line code. They've already implicit in lines above. Your example made me feel safe that my line code is well structured. Merry Christmas and a Happy New Year. Knowledge always is the best gift. Dec 27, 2021 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.