Hello everybody I need your help.

I know this is possible but I think only with Solidity.

Actually, to sell tokens the fastest possible, I use the function balanceOf only after BUY so if it returns value = 0, it makes a bool if value > 0, it sends my SELL function.

I know it's possible to buy and immediately after sell tokens in Solidity, but how to do with Javascript or Python ? Do I need to send a signed Tx? I don't know is there a tutorial please?

  • Sorry for the question, but what are you trying to do? Something like a front run? Commented Nov 3, 2022 at 14:29
  • Im trying to buy and sell fastest possible in solidity with 2 différent Tx , can you help me ?
    – Banlieu451
    Commented Nov 3, 2022 at 15:13
  • I think you already thought about it but my idea is: 1 - Get AmountsOut. 2 - Send Buy Tx (swapETHfor ExactTokens) with a little bit higher Gwei. 3 - At the same time (or with a little delay) send Sell Tx (swapExactTokensForETH) with normal Gwei...- The main idea is send two transactions at the same time but "force" the buy tx to approve first. Commented Nov 4, 2022 at 18:57
  • Ok im gonna try it : I have create function Buy and function Sell(Get amount at start of function ) in my solidity contract im gonna call buy and sell in same time in my JAVASCRIPT with différents gas, i am comming back
    – Banlieu451
    Commented Nov 4, 2022 at 21:35

1 Answer 1


Make one function in your smart contract that buy, check if balance > 0, and if yes, sells the token.

Here is an example of pseudo code

 function buyAndSellInOneFunction(uint256 erc20Amount) public  {
       uint myBal = erc20.balanceOf(address(this));
       if(myBal > 0){

That will buy and sell in one block

Then call that function from your javascript backend/script.

  • Thanks for your answer but this is not what im seaching for if i follow your step it sell after some block ? Im seaching a way to buy and sell in same block in few time .. if i call function balanceOf i need wait the token come into my account and sell that take like 3 sec its too long there is a way to buy get the amountOut and immediatly sell the token amount ..
    – Banlieu451
    Commented Oct 14, 2022 at 15:59
  • I have edited my comment to include some pseudo code, let me know if that helps. Commented Oct 14, 2022 at 16:13

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.