-
Notifications
You must be signed in to change notification settings - Fork 0
/
MEVictim.ts
138 lines (91 loc) · 4.18 KB
/
MEVictim.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// _ _____ ______ _____ _
// /\ (_) | __ \| ____| __ \| |
// / \ __ ___ ___ _ __ ___ | |__) | |__ | |__) | |
// / /\ \ \ \/ / |/ _ \| '_ ` _ \| _ /| __| | ___/| |
// / ____ \ > <| | (_) | | | | | | | \ \| |____| | | |____
// /_/ \_\/_/\_\_|\___/|_| |_| |_|_| \_\______|_| |______|
//
//
let idxFrontTx = getReceipt(txHashFrontRun)
.txIdx()
.toCircuitValue();
let idxVictimTx = getReceipt(txHashVictim)
.txIdx()
.toCircuitValue();
let idxBackTx = getReceipt(txHashBackRun)
.txIdx()
.toCircuitValue();
let gasPriceFrontTx = getTx(txHashFrontRun)
.gasLimit()
.toCircuitValue();
let gasPriceVictim = getTx(txHashVictim)
.gasLimit()
.toCircuitValue();
let gasPriceBackTx = getTx(txHashBackRun)
.gasLimit()
.toCircuitValue();
let blockNumberFrontRun = getReceipt(txHashFrontRun).blockNumber().toCircuitValue()
let blockNumberVictim = getReceipt(txHashVictim).blockNumber().toCircuitValue()
const receiptFrontRunner = getReceipt(txHashFrontRun);
const receiptVictim = getReceipt(txHashVictim);
const receiptBackRunner = getReceipt(txHashBackRun);
// retrieves a log entry in the receipt
const logFrontRunnerTransactionPool = receiptFrontRunner.log(0);
const logBackRunnerTransactionPool = receiptBackRunner.log(1);
// gets the address from which the event was emitted from
const logFrontRunnerTransactionPoolAddress = logFrontRunnerTransactionPool.address();
const logBackRunnerTransactionPoolAddress = logBackRunnerTransactionPool.address();
// check front runner gas is greater than victim
checkLessThan(gasPriceVictim, gasPriceFrontTx);
// check victim index less than back runner
checkLessThan(idxVictimTx, idxBackTx);
// // check front runner index less than victim
checkLessThan(idxFrontTx, idxVictimTx);
// // check front runner gas is greater than victim
checkLessThan(gasPriceVictim, gasPriceFrontTx);
// // check front runner gas is equal to victim
assertEqual(blockNumberVictim, blockNumberFrontRun);
// check that from for front runner is equal to back runner
// assertEqual(logFrontRunnerTransactionPoolAddress.toCircuitValue(), logBackRunnerTransactionPoolAddress.toCircuitValue());
// price check
const logFrontRunner = receiptFrontRunner.log(logIdxFrontRun);
const logVictim= receiptVictim.log(logIdxVictim);
const logBackRunner = receiptBackRunner.log(logIdxBackrunner);
const priorAbs = receiptFrontRunner.log(logIdxFrontRun).data(0).toCircuitValue();
const priorDiv = receiptFrontRunner.log(logIdxFrontRun).data(1);
const pdLo = priorDiv.lo();
const b128max = BigInt(0xffffffffffffffffffffffffffffffff);
const bigLo = pdLo.value();
let v = sub(constant(b128max), constant(bigLo));
const currentAbs = receiptVictim.log(logIdxVictim).data(0).toCircuitValue();
const currentDiv = receiptVictim.log(logIdxVictim).data(1);
const pdLoCurrent = currentDiv.lo();
const bigLoCurrent = pdLoCurrent.value();
let vCurrent = sub(constant(b128max), constant(bigLoCurrent));
const nextAbs = receiptBackRunner.log(logIdxBackrunner).data(0).toCircuitValue();
const nextDiv = receiptBackRunner.log(logIdxBackrunner).data(1);
const pdLoNext = nextDiv.lo();
const bigLoNext = pdLoNext.value();
let vNext = sub(constant(b128max), constant(bigLoNext));
// const nextAbs = receiptBackRunner.log(logIdxBackrunner).data(0).toCircuitValue();
// const nextDiv = receiptBackRunner.log(logIdxBackrunner).data(1).toCircuitValue();
// check prior price < current price
let priorPrice = constant(0);
let currentPrice = constant(0);
let nextPrice = constant(0);
if (isLessThan(priorAbs, v)) {
//reverse ratio and comparison because division must be greater than 1
priorPrice = div(mul(v, constant(100000)), mul(priorAbs, constant(100000)));
currentPrice = div(mul(vCurrent, constant(100000)), mul(currentAbs, constant(100000)));
nextPrice = div(mul(vNext, constant(100000)), mul(nextAbs, constant(100000)));
checkLessThan(currentPrice, priorPrice);
checkLessThan(nextPrice, currentPrice);
} else {
priorPrice = div(mul(priorAbs, constant(100000)), mul(v, constant(100000)));
currentPrice = div(mul(currentAbs, constant(100000)), mul(vCurrent, constant(100000)));
nextPrice = div(mul(nextAbs, constant(100000)), mul(vNext, constant(100000)));
checkLessThan(priorPrice, currentPrice);
checkLessThan(currentPrice, nextPrice);
}
addToCallback(constant(1));