1

When you try to use the functions of a specific contract in web3 you should add the .buildTransaction() to add the dictionary of parameters of the function. for example on the documentation of web3 one should call the function and after that the .buildTransaction() Example: contractFunction.buildTransaction(transaction). But I want to know where can I get the information that should contain this dictionary for a specific contract.

I´ve been working with the pancakeswap v2 contract, so I was able to get all the information correct:

pancake_eth_contract = w3.eth.contract(address=PANCAKEROUTER_Contract, abi=getABI(PANCAKEROUTER_Contract, driver))

But when I try to to use the .swapExactETHForTokens() I also noticed that I have to provide a .buildTransaction method. And I have searched in the internet with a lot of answer but I want to know where can I get this information by my own. Where should I search or what parte should I read of the contract or the ABI?

transaction_pancake = pancake_eth_contract.functions.swapExactETHForTokens(amountOutMin, path_addresses, address_to, deadline).buildTransaction(buid_Trans)

Here Is a example that I have used and works:

buid_Trans = {
    'from': My_Wallet_Address,
    'value': buy_amount,
    'gas': 125000,
    'gasPrice': w3.toWei('100', 'gwei'),
    'nonce': w3.eth.get_transaction_count(My_Wallet_Address)
}

I wanna know where can I get this information by my own.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Angel2207
  • 11
  • 1
  • 2

1 Answers1

1

Web3.py has some documentation about this. The data parameter is the relevant bit, and can contain either a ABI byte string containing the data of the function call on a contract, or in the case of a contract-creation transaction the initialisation code.

>>> math_contract.functions.increment(5).buildTransaction({'maxFeePerGas': 2000000000, 'maxPriorityFeePerGas': 1000000000})
{
    'to': '0x6Bc272FCFcf89C14cebFC57B8f1543F5137F97dE',
    'data': '0x7cf5dab00000000000000000000000000000000000000000000000000000000000000005',
    'value': 0,
    'gas': 43242,
    'maxFeePerGas': 2000000000,
    'maxPriorityFeePerGas': 1000000000,
    'chainId': 1
}

Also web3.js has better documentation for the equivalent function.

Pranab
  • 2,207
  • 5
  • 30
  • 50