Web3.js Tokens – Identifying the Library for ‘.functions.swapExactETHforTokens’

pancakeswappythontokensweb3.pyweb3js

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!!!
[spend,tokenToBuy],
sender_address,
(int(time.time()) + 10000)
).buildTransaction({
'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)
    external
    virtual
    override
    payable
    ensure(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);
}

Best Answer

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

https://etherscan.io/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

https://bscscan.com/address/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.

Related Topic