Skip to content

Commit 0c1075d

Browse files
committed
Merge branch 'last-min-flash-arb'
2 parents 628d446 + 5e5b997 commit 0c1075d

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

lib/getEthPrice.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
async function getEthPriceInUSD() {
2+
let price;
3+
let gasPrice;
4+
5+
try {
6+
const response = await fetch(
7+
`https://cors-anywhere.herokuapp.com/https://ethgas.watch/api/gas`
8+
);
9+
const data = await response.json();
10+
11+
price = data?.ethPrice;
12+
gasPrice = data?.fast;
13+
console.log("gasPrice", gasPrice);
14+
} catch (err) {
15+
console.log("error fetching eth price", err);
16+
price = 1747;
17+
}
18+
19+
return { ethPrice: price, gasPrice };
20+
}
21+
22+
export default getEthPriceInUSD;

svelte/flasharb.svelte

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<script>
2+
import { onMount } from "svelte";
3+
import { ethers } from "ethers";
24
import Container from "./container.svelte";
35
import getPriceData from "../lib/getPriceData.mjs";
46
import getSpreadData from "../lib/getSpreadData.mjs";
7+
import getEthPriceInUSD from "../lib/getEthPrice.mjs";
58
69
const NUMBER_PRICE_DIGITS_SHOWN = 8;
710
@@ -16,17 +19,23 @@
1619
// let crypto_comPrice = 0;
1720
let priceData;
1821
22+
// to remove after demo
23+
let ethPrice = 1747;
24+
let gasPrice = 0.0000001225;
25+
let unitOfGasInUSD = 0.00021347788;
1926
let amountToBorrow = 0;
2027
let grossProfit = 0;
2128
let flashloanFee = 0;
2229
let gasCost = 0;
2330
2431
let selectedSpread = { spread: 0, dex1: "", dex2: "" };
2532
26-
$: grossProfit = (amountToBorrow * Math.abs(selectedSpread.spread)) / 100;
27-
$: flashloanFee = amountToBorrow * 0.0009;
28-
$: tradingFee1 = amountToBorrow * getTradeFee(selectedSpread.dex1);
29-
$: tradingFee2 = amountToBorrow * getTradeFee(selectedSpread.dex2);
33+
$: amountToBorrowUSD = amountToBorrow * ethPrice;
34+
$: grossProfit = (amountToBorrowUSD * Math.abs(selectedSpread.spread)) / 100;
35+
$: flashloanFee = amountToBorrowUSD * 0.0009;
36+
$: gasCost = 600000 * unitOfGasInUSD;
37+
$: tradingFee1 = amountToBorrowUSD * getTradeFee(selectedSpread.dex1);
38+
$: tradingFee2 = amountToBorrowUSD * getTradeFee(selectedSpread.dex2);
3039
$: estimatedProfit =
3140
grossProfit - flashloanFee - gasCost - tradingFee1 - tradingFee2;
3241
@@ -70,10 +79,8 @@
7079
if (!spread) return "fs-black-spread";
7180
7281
const absoluteSpread = Math.abs(spread);
73-
if (absoluteSpread < 0.1) {
82+
if (absoluteSpread < 0.4) {
7483
return "fs-red-spread";
75-
} else if (absoluteSpread < 0.2) {
76-
return "fs-black-spread";
7784
} else {
7885
return "fs-green-spread";
7986
}
@@ -96,6 +103,17 @@
96103
97104
let selectedPair = pairs[1];
98105
106+
onMount(async () => {
107+
const res = await getEthPriceInUSD();
108+
if (res) {
109+
ethPrice = res.ethPrice;
110+
gasPrice = ethers.utils.formatUnits(res.gasPrice.gwei.toString(), "gwei");
111+
unitOfGasInUSD = gasPrice * ethPrice;
112+
}
113+
114+
console.log("ethPrice", ethPrice);
115+
});
116+
99117
async function getPrices() {
100118
const data = await getPriceData({ pair: selectedPair });
101119
console.log("priceData", data);
@@ -269,7 +287,7 @@
269287

270288
<div class="fs-simulation-section">
271289
<div class="fs-simulation-left columntitlebar amount">
272-
<h3 class="columnTitle">Amount to borrow</h3>
290+
<h3 class="columnTitle">Amount to borrow (in WETH)</h3>
273291
<input
274292
bind:value={amountToBorrow}
275293
class="inputtextfield faflashloan w-embed fs-amount-field"
@@ -279,31 +297,28 @@
279297
<div id="DepositPosition" class="columnpricecost w-col">
280298
<div class="columntitlebar" style="padding-left: 0;">
281299
<h2 id="columnTitle">Cost-Profit analysis</h2>
282-
<div class="textlightmode rates">(in {selectedPair.asset1})</div>
300+
<div class="textlightmode rates">(in USD)</div>
283301
</div>
284302
<div class="w-layout-grid gridcosts">
285303
<div class="textlightmode label02">Gross profit</div>
286304
<div id="costArbitrage" class="textlightmode numbers">
287-
{grossProfit === 0 ? "Select a spread" : grossProfit}
305+
{grossProfit === 0 ? "Select a spread" : grossProfit.toFixed(5) + " $"}
288306
</div>
289307
<div class="textlightmode label02">Flashloan Fee</div>
290308
<div id="costFlashLoan" class="textlightmode numbers">
291-
{flashloanFee}
309+
{flashloanFee.toFixed(5)} $
292310
</div>
293311
<div class="textlightmode label02">Gas Cost</div>
294312
<div id="costGas" class="textlightmode numbers">
295-
<input
296-
bind:value={gasCost}
297-
class="inputtextfield faflashloan w-embed fs-amount-field fs-cost-field"
298-
/>
313+
{gasCost.toFixed(5)} $
299314
</div>
300315
<div class="textlightmode label02">Trading Fees (1)</div>
301316
<div id="costPlatform01" class="textlightmode numbers">
302-
{tradingFee1.toFixed(5)}
317+
{tradingFee1.toFixed(5)} $
303318
</div>
304319
<div class="textlightmode label02">Trading Fees (2)</div>
305320
<div id="costPlatform02" class="textlightmode numbers">
306-
{tradingFee2.toFixed(5)}
321+
{tradingFee2.toFixed(5)} $
307322
</div>
308323
</div>
309324
<div
@@ -313,10 +328,10 @@
313328
>
314329
<div class="columntitlebar profit" style="padding-left: 0;">
315330
<h2 id="columnTitle" style="font-size: 16px">
316-
Estimated Profit 🤑
331+
Estimated Net Profit 🤑
317332
</h2>
318333
<div id="differenceProfit" class="textlightmode numbers big">
319-
{estimatedProfit.toFixed(5)}
334+
{estimatedProfit.toFixed(5)} $
320335
</div>
321336
</div>
322337
</div>

0 commit comments

Comments
 (0)