You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Failure when swapping from BNB to token on BSC chain #332
Well, first things first, I haven't used BSC for 1y+, but the fee value seems extremely weird. Default values are 500, 3000, and 10000 (0.05%, 0.3%, and 1%, respectively). They might vote for other fee levels, obv. However, this value is still way too big. I would double-check.
Regarding your question, please provide a complete error message, as it is challenging to locate the problem.
so I was able to debug this a bit more and it looks like a discrepancy between bsc and ethereum... the failure is in this command w3.eth.get_block("latest")["baseFeePerGas"]
when running these lines of code using a bsc provider I get the Keyerror but it doesn't generate an error (it returns a value) when using an ethereum provider
from web3 import Web3
import config # this has the provider information
web3 = Web3(Web3.HTTPProvider(config.provider))
web3.eth.get_block("latest")["baseFeePerGas"] # this generates the error on bsc but not on ethereum
Seems like we need to make a fix or smth. In _build_and_send_tx()
add smth like transaction["gasPrice"] = self.w3.eth.generate_gas_price()
Should be fine IMO
Also, you might need to add the following after the middleware injection
def _build_and_send_tx(
self, function: ContractFunction, tx_params: Optional[TxParams] = None
) -> HexBytes:
"""Build and send a transaction."""
if not tx_params:
tx_params = self._get_tx_params()
transaction = function.build_transaction(tx_params)
# This is a test... needs to be removed. 5-14-2023
################################################################
transaction["gasPrice"] = self.w3.eth.generate_gas_price()
################################################################
if "gas" not in tx_params:
# `use_estimate_gas` needs to be True for networks like Arbitrum (can't assume 250000 gas),
# but it breaks tests for unknown reasons because estimate_gas takes forever on some tx's.
# Maybe an issue with ganache? (got GC warnings once...)
if self.use_estimate_gas:
# The Uniswap V3 UI uses 20% margin for transactions
transaction["gas"] = Wei(
int(self.w3.eth.estimate_gas(transaction) * 1.2)
)
else:
transaction["gas"] = Wei(250_000)
signed_txn = self.w3.eth.account.sign_transaction(
transaction, private_key=self.private_key
)
# TODO: This needs to get more complicated if we want to support replacing a transaction
# FIXME: This does not play nice if transactions are sent from other places using the same wallet.
try:
return self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
finally:
logger.debug(f"nonce: {tx_params['nonce']}")
self.last_nonce = Nonce(tx_params["nonce"] + 1)
I was able to get past the error after I added that line above as you suggested and also the lines of code post middleware injection. Had to tweak things a bit in order to get a successful swap thro. One of the issues I ran into is the assumption that BNB (equivalent to ETH - i.e. native token) has an address of all zeros in BSC - but that's not the case. I had to get creative in my code to make it work.
Nice to see you've managed to resolve this issue. Regarding native tokens, I prefer to use wrapped versions anywhere if possible. This approach allows us to avoid extra wrap/unwrap calls during swaps. That saves some gas, making transactions cheaper. Regardless of the used network, you can get the wrapped token contract with the self.router.functions.WETH call.
Anything to keep in mind when doing swaps on Optimism? I’m getting all kinds of errors when running this on Op. any advice, gotchas, minefields? I’ll collect my thoughts and post detailed errors etc tomorrow
I do believe that most underwater stones regarding Optimism have been discussed already. Search here/in the discussion section. Also, I'd like to ask you to create a separate thread/issue if questions arise.
@liquid-8@nisilag excuse me, i have extremely low knowledge in python. with eth network everything works fine, but now i'm tryting to make swap on bsc network and getting same error with 'baseFeePerGas'. could you tell where exectly and how i need to add those lines so that the exchange works on bsc?
Activity
liquid-8 commentedon May 13, 2023
Well, first things first, I haven't used BSC for 1y+, but the fee value seems extremely weird. Default values are 500, 3000, and 10000 (0.05%, 0.3%, and 1%, respectively). They might vote for other fee levels, obv. However, this value is still way too big. I would double-check.
Regarding your question, please provide a complete error message, as it is challenging to locate the problem.
nisilag commentedon May 13, 2023
thx for your input. I tried with 10000 which is the fee (1%) and it failed with the same error message. here's the full error
nisilag commentedon May 13, 2023
so I was able to debug this a bit more and it looks like a discrepancy between bsc and ethereum... the failure is in this command w3.eth.get_block("latest")["baseFeePerGas"]
when running these lines of code using a bsc provider I get the Keyerror but it doesn't generate an error (it returns a value) when using an ethereum provider
liquid-8 commentedon May 14, 2023
Seems like we need to make a fix or smth. In
_build_and_send_tx()
add smth like
transaction["gasPrice"] = self.w3.eth.generate_gas_price()Should be fine IMO
Also, you might need to add the following after the middleware injection
Not sure tho
nisilag commentedon May 14, 2023
so I added it but am still getting the same error
nisilag commentedon May 15, 2023
I was able to get past the error after I added that line above as you suggested and also the lines of code post middleware injection. Had to tweak things a bit in order to get a successful swap thro. One of the issues I ran into is the assumption that BNB (equivalent to ETH - i.e. native token) has an address of all zeros in BSC - but that's not the case. I had to get creative in my code to make it work.
liquid-8 commentedon May 15, 2023
Nice to see you've managed to resolve this issue. Regarding native tokens, I prefer to use wrapped versions anywhere if possible. This approach allows us to avoid extra wrap/unwrap calls during swaps. That saves some gas, making transactions cheaper. Regardless of the used network, you can get the wrapped token contract with the
self.router.functions.WETHcall.nisilag commentedon May 16, 2023
Anything to keep in mind when doing swaps on Optimism? I’m getting all kinds of errors when running this on Op. any advice, gotchas, minefields? I’ll collect my thoughts and post detailed errors etc tomorrow
liquid-8 commentedon May 16, 2023
I do believe that most underwater stones regarding Optimism have been discussed already. Search here/in the discussion section. Also, I'd like to ask you to create a separate thread/issue if questions arise.
vadimkalolo commentedon Jun 13, 2023
@liquid-8 @nisilag excuse me, i have extremely low knowledge in python. with eth network everything works fine, but now i'm tryting to make swap on bsc network and getting same error with 'baseFeePerGas'. could you tell where exectly and how i need to add those lines so that the exchange works on bsc?