coccoinomane / crabada.py

Crabada bot with automatic reinforce. Snib snib! 🦀
MIT License
60 stars 54 forks source link

Estimate gas to prevent failed reinforcement #40

Closed coccoinomane closed 2 years ago

coccoinomane commented 2 years ago

@coccoinomane Came across this question and tried to implement it. I couldn't be able to detect a transaction that will fail but maybe it helps you to figure it out.

In Web3Client.py / buildContractTransaction()

baseTx = self.buildBaseTransaction()
try:
    result = contractFunction.estimateGas(baseTx, "pending")
    logger.debug(f"Estimate gas result: {result}")
except Exception as err:
    logger.warning(f"Transaction will probably fail!", exc_info=True)

return contractFunction.buildTransaction(baseTx)

without "pending":

result = contractFunction.estimateGas(baseTx)

and without "baseTx" parameters when estimating gas:

try:
    result = contractFunction.estimateGas()
    logger.debug(f"Estimate gas result: {result}")
except Exception as err:
    logger.warning(f"Transaction will probably fail!", exc_info=True)

baseTx = self.buildBaseTransaction()
return contractFunction.buildTransaction(baseTx)

All raised the same exception : execution reverted: GAME:NOT OWNER

Originally posted by @yigitest in https://github.com/coccoinomane/crabada.py/issues/12#issuecomment-1081652424

coccoinomane commented 2 years ago

Hi @yigitest !

I opened a new issue from your comment in https://github.com/coccoinomane/crabada.py/issues/12#issuecomment-1081652424

I'll do some tests and see where that leads us :-)

coccoinomane commented 2 years ago

@yigitest

I am getting the GAME:NOT OWNER error, too.

I could not find anything related to this error either in the Discord or by inspecting the JS code in play.crabada.com.

We have to assume that the error is generated by some internal check in the smart contract which prevents any kind of execution without signing the transaction.

If this is the case, we will probably need to focus in finding the endpoint used by play.crabada.com to reinforce. This endpoint has the desirable property of preventing any loss of gas, because it shows a "FAILED" modal if the chosen crab was already borrowed.

coccoinomane commented 2 years ago

I have inspected my Metamask transactions in the browser, and Crabada does indeed use estimate_gas() before sending the transaction.

Therefore, I have further debugged my code and found that the client omitted the from field in certain transactions, which in turn generated the NOT TEAM OWNER error.

Me: 🤦🏻‍♂️

I have fixed the bug in https://github.com/coccoinomane/crabada.py/tree/2022-04-03_web3-client-refresh.

Now, when you try to reinforce with a crab that has already been taken, the bot returns an error without sending the transactions, just like the UI does.

At the moment, the error is not handled, meaning that the bot will just exit without attempting to borrow other crabs.

An improvement would be for the strategy to automatically pick the next-best available crab, up to a certain number of attempts.

Let me know if you have further thoughts about this :-)

yigitest commented 2 years ago

Awesome find 👏.

I am now wondering which block should we use, "latest" or "pending". I will repeat my original tests and report back my findings in a day or two.

coccoinomane commented 2 years ago

Hi @yigitest, and thanks for your feedback!

Right now, I do not use estimateGas explicitly.

Instead, I let web3.py do the estimation for me, by omitting the gas parameter in the tx (code).

In turn, web3.py calls estimateGas, without the optional "block" parameter.

I am not sure whether the node will select the "latest" or the "pending" block, but, based on the tests I have done so far, it seems that it does the right thing for us.

Let me know if your tests suggest otherwise :-)

Cheers, Cocco

coccoinomane commented 2 years ago

Hi @yigitest, thanks for your feedback!

Right now, I do not use estimateGas explicitly.

Instead, I let web3.py do the estimation for me, by omitting the gas parameter in the tx (code https://github.com/coccoinomane/crabada.py/blob/2022-04-03_web3-client-refresh/src/libs/Web3Client/Web3Client.py#L88-L92).

In turn, web3.py calls estimateGas without the optional "block" parameter.

I am not sure whether the mode will select the "latest" or the "pending" block, but based on the tests I have done so far, it seems that it does the right thing for us.

Let me know if your tests suggest otherwise :-)

Cheers, Cocco

On 4 Apr 2022, at 12:27, yigiter @.***> wrote:

Awesome find 👏.

I am now wondering which block should we use, "latest" or "pending". I will repeat my original tests and report back my findings in a day or two.

— Reply to this email directly, view it on GitHub https://github.com/coccoinomane/crabada.py/issues/40#issuecomment-1087381774, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA55ENBCAXSEXBTMKPLE7SDVDK7XXANCNFSM5SNMYLDQ. You are receiving this because you were mentioned.