# web3 and some math

There is a transaction that is in pending. In which the user gives 0.02 WBNB (2000000000000000000) to get at least 35460 TM (35460000000000000000000). If he gets less than that the transaction will be rejected. So we also have reserves until the end of his transaction _reserve0 (TM) uint112 : 357819415332186305286894 _reserve1 (WBNB) uint112 : 17171750682277891643 The task is to find the maximum amount of WBNB I can spend to buy TM tokens. Before user1, that user1's transaction should end successfully. I will be glad to get any information or formulas)

UPD tested for a couple of days, and this formula came up once in a while, I did the following way, maybe I did something wrong.

user wants to get a minimum of tokens A = minimum

the user sends some wbnb = wbnb

(reserv a - minimum) * (reserb b + wbnb) = k User

---------------- this is all about the user's transaction

i want to get a certain amount of tokens A = amount A

I send some WBNB tokens = WBNB

(reserv a - amount A)*(reserv B +WBNB) = k My

Then we do the following check

if(k User >k My ){

we send transaction

}else{

we dont send transaction

}

Using this algorithm, I often see that a user's transaction is very often rejected because he doesn't get his minimum tokens, what could I be wrong about?

For Uniswap, the automated market-making algorithm is `X * Y = K`, X and Y are amount of tokens and K is a constant.

In your case, (X+ 2000000000000000000)*(Y - 35460000000000000000000) < K is the condition for the transaction to execute.

Theoretically, the max amount `x2` token you can swap when `(X_reserve + 2000000000000000000 + x2)*(Y_reserve - 35460000000000000000000 - K/X * x2) = K`

• I will actually have to go through the values?
– user118276
Commented Mar 8, 2023 at 8:55
• In this case yes. Not sure if there is any other workaround though. Commented Mar 8, 2023 at 9:00
• I updated the post because long posts can't be left in the comments, could you look at it?
– user118276
Commented Mar 13, 2023 at 21:27