• Stars
    star
    385
  • Rank 111,464 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Uniswap flash swap arbitrage solidity contracts

Uniswap Arbitrage Flash Swap Contract

You want to play with "millions" on the blockchain then try https://uniswap.org/docs/v2/smart-contract-integration/using-flash-swaps/. This contract is able to arbitrage between DEX on the blockchain without providing any own capital. Basically is usable on the Binance Smart Chain and provides callbacks for common DEX, feel free to extend.

This is not a full project, build your or own infrastructure around it!

Links

Transactions

transactions

Improvements

All examples are more or less overengineered for a quick start, so here we have a KISS solution.

  • Reduce overall contract size for low gas fee on deployment
  • Provide a on chain validation contract method to let nodes check arbitrage opportunities
  • Stop arbitrage opportunity execution as soon as possible if its already gone to reduce gas fee on failure
  • Interface callbacks for common DEX on "Binance Smart Chain" are included

Infrastructure

Basically arbitrage opportunity dont last long, your transaction must make it into the next block. So you have <3 seconds watching for opportunities, decide and execute transaction. Sometimes there are also a chance to 2-3 have block, see example below.

[7920960] [6/1/2021, 5:50:37 PM]: alive (bsc-ws-node.nariox.org) - took 308.42 ms
[7920991] [6/1/2021, 5:52:09 PM]: [bsc-ws-node.nariox.org] [BAKE/BNB ape>bakery] Arbitrage opportunity found! Expected profit: 0.007 $2.43 - 0.10%
[7920991] [6/1/2021, 5:52:09 PM] [bsc-ws-node.nariox.org]: [BAKE/BNB ape>bakery] and go:  {"profit":"$1.79","profitWithoutGasCost":"$2.43","gasCost":"$0.64","duration":"539.35 ms","provider":"bsc-ws-node.nariox.org"}
[7920992] [6/1/2021, 5:52:13 PM]: [bsc-ws-node.nariox.org] [BAKE/BNB ape>bakery] Arbitrage opportunity found! Expected profit: 0.007 $2.43 - 0.10%
[7920992] [6/1/2021, 5:52:13 PM] [bsc-ws-node.nariox.org]: [BAKE/BNB ape>bakery] and go:  {"profit":"$1.76","profitWithoutGasCost":"$2.43","gasCost":"$0.67","duration":"556.28 ms","provider":"bsc-ws-node.nariox.org"}
[7921000] [6/1/2021, 5:52:37 PM]: alive (bsc-ws-node.nariox.org) - took 280.54 ms

Requirements / Hints

  • You have a time window of 1000ms every 3 seconds (blocktime on BSC) and you should make it into the next transaction
  • Websocket connection is needed to listen directly for new incoming blocks
  • Public provided Websocket are useless bsc-ws-node.nariox.org simply they are way behind notify new blocks
  • Use a non public provider; or build your own node (light node helps) and better have multiple owns; let the fastest win
  • Spread your transaction execution around all possible providers, first one wins (in any case transactions are only execute once based on nonce)
  • Find suitable pairs with liquidity but not with much transaction
  • You can play with full pair liquidity, but dont be too greedy think of a price impact you would have
  • Common opportunities are just between 0,5 - 1%
  • Do not estimate transaction fee, just calculate it once and provide a static gas limit. Simply its takes to long
  • There is block parameter until the transaction is valid, so you can abort execution eg after +3 blocks
  • Payback is directly calculated by calling the foreign contracts so its project independent (no hardcoded fee calculation)
  • The profit is transferred to the owner / creator of the contract :)

Function

The contract is plain and simple [contracts/Flashswap.sol] some basic hints:

Check arbitrage opportunity between DEX. Read only method the one blockchain

    function check(
        address _tokenBorrow, // example: BUSD
        uint256 _amountTokenPay, // example: BNB => 10 * 1e18
        address _tokenPay, // example: BNB
        address _sourceRouter,
        address _targetRouter
    ) public view returns(int256, uint256) {

Starts the execution. You are able to estimate the gas usage of the function, its also directly validating the opportunity. Its slow depending on connected nodes.

    function start(
        uint _maxBlockNumber,
        address _tokenBorrow, // example BUSD
        uint256 _amountTokenPay,
        address _tokenPay, // our profit and what we will get; example BNB
        address _sourceRouter,
        address _targetRouter,
        address _sourceFactory
    ) external {

As all developers are lazy and just forking projects around without any rename a common implementation is possible. Basically the pair contract call the foreign method of the contract. You can find them the naming in any pair contract inside the swap() method.

Example: https://bscscan.com/address/0x0eD7e52944161450477ee417DE9Cd3a859b14fD0#code: if (data.length > 0) IPancakeCallee(to).pancakeCall(msg.sender, amount0Out, amount1Out, data);

Extend method if needed:

# internal callback 
function execute(address _sender, uint256 _amount0, uint256 _amount1, bytes calldata _data) internal

# foreign methods that get called
function pancakeCall(address _sender, uint256 _amount0, uint256 _amount1, bytes calldata _data) external
function uniswapV2Call(address _sender, uint256 _amount0, uint256 _amount1, bytes calldata _data) external

Run it

Its not a full infrastructure, but a working workflow, if you deploy the contract.

cp env.template .env # replace values inside ".env"
node watcher.js
started: wallet 0xXXXX - gasPrice 5000000000 - contract owner: 0xXXXX
[bsc-ws-node.nariox.org] You are connected on 0xXXXX
[8124531] [6/8/2021, 7:53:20 PM]: [bsc-ws-node.nariox.org] [BUSD/BNB pancake>panther] Arbitrage checked! Expected profit: -0.015 $-4.99 - -0.15%
[8124532] [6/8/2021, 7:53:21 PM]: [bsc-ws-node.nariox.org] [BUSD/BNB pancake>panther] Arbitrage checked! Expected profit: -0.015 $-4.99 - -0.15%
[8124533] [6/8/2021, 7:53:24 PM]: [bsc-ws-node.nariox.org] [BUSD/BNB pancake>panther] Arbitrage checked! Expected profit: -0.014 $-4.71 - -0.14%
[8124534] [6/8/2021, 7:53:27 PM]: [bsc-ws-node.nariox.org] [BUSD/BNB pancake>panther] Arbitrage checked! Expected profit: -0.014 $-4.61 - -0.14%

Hints

  • Designed to have multiple chain connectivities, play with some non public providers to be faster then the public once. Its all designed as "first win"

More Repositories

1

crypto-trading-bot

Cryptocurrency trading bot in javascript for Bitfinex, Bitmex, Binance, Bybit ... (public edition)
JavaScript
2,787
star
2

idea-php-symfony2-plugin

IntelliJ IDEA / PhpStorm Symfony Plugin
Java
896
star
3

idea-php-laravel-plugin

Laravel Framework Plugin for PhpStorm / IntelliJ IDEA
Java
571
star
4

idea-android-studio-plugin

Android Studio Plugin
Java
291
star
5

idea-php-annotation-plugin

Add PHP annotation support for PhpStorm and IntelliJ
Java
242
star
6

idea-php-toolbox

Collections of tools and improvements to make PhpStorm a little bit better
Java
157
star
7

idea-php-generics-plugin

Support generics types in PhpStorm via psalm / phpstan docblock
Java
144
star
8

idea-php-shopware-plugin

Shopware Plugin for PhpStorm which extends Symfony Plugin
Java
55
star
9

idea-php-symfony2-plugin-doc

Documentation only !!! for idea-php-symfony2-plugin
Python
49
star
10

idea-php-drupal-symfony2-bridge

PhpStorm plugin to support Symfony components inside Drupal 8
Java
34
star
11

idea-php-toolbox-json-files

PHP
25
star
12

torrent-announcer

Can simulate torrent client requests to a tracker and returns client ips
PHP
17
star
13

idea-php-oxid-plugin

Oxid Plugin for PhpStorm
Java
14
star
14

node-js-crypto-technical-analysis-candlestick-webserver

Candlestick Technical Analysis Http Server based on node.js
JavaScript
10
star
15

drupal-packagist

Provides a Composer packagist page for Drupal projects
PHP
8
star
16

behat-placeholder-extension

Behat placeholder extension
PHP
5
star
17

bittorrent-tracker

Simple BitTorrent Tracker / Proxy
PHP
5
star
18

solidity-multicall-contract

Contract to aggregate multiple contract web3-rpc requests into single request chunks
Solidity
4
star
19

idea-php-behat-plugin

IntelliJ IDEA / PhpStorm Behat Enhancement
Java
4
star
20

ZendDbBundle

Bundle that wraps ZendDb to Symfony2 and Doctrine
PHP
3
star
21

tgudy

TYPO3 Template and Extension
JavaScript
3
star
22

idea-localization-plugin

Extended translation / localization support for PhpStorm / IntelliJ for formats like XLF, XLIFF
Java
3
star
23

espend.de--Delicous-Bookmarks

Main Drupal module that works on b.espend.de to export HTML from Delicous Bookmarks
PHP
2
star
24

e2-fb

Facebook client for Enigma2 / Dreambox
Python
2
star
25

vavoo-iptv-stream-proxy

JavaScript
2
star
26

idea-twig-plugin

PhpStorm / IntelliJ Twig Extended Plugin improvements extracted from Symfony Plugin
Kotlin
2
star
27

Onlinestreams-for-D-Box2

Mediaportal für Neutrino (D-Box2) mit Interface für Popcorn Hour Geräte: einfacher "OnlineStream Proxy" auf Basis des internen Movieplayers. Enthalten Scripts: Youtube, ZDFMediathek, MyVideo, Apple Trailer, Spiegel.TV, kino.de, Tv-Total.de alle in PHP
PHP
2
star
28

idea-badge

Provides badges support for statistics of IntelliJ IDEA / PhpStorm Plugin Repository
PHP
2
star
29

jquery.csspanel

A panel to manipulate simple CSS styles
PHP
1
star
30

http-segmenter

Stream video to your iPhone, iPad, iPod, iOS over http
C
1
star
31

autodesk-inventor-plot-class

This class does some plotting stuff for Autodesk Inventor
Visual Basic
1
star
32

jetbrains-enterprise-plugin-repository

XML API Project for a custom JetBrains Enterprise Plugin Repository
PHP
1
star
33

MyTubeExt

MyTubeExt - Devel fork of MyTube Enigma2 Extension
Python
1
star
34

idea-php-code-usage

Exports a PHP file structure to JSON
Java
1
star
35

vbnettools

Some VB.NET projects like HamachiBroadcastFixGUI
Visual Basic
1
star
36

autodesk-inventor-printbuttons

Configurable toolbar for Autodesk Inventor for plot and print on single button click
Visual Basic
1
star
37

sharemybox.net-enigma2-client

Python Client for sharemybox.net Dreambox and Enigma2 based receiver
Python
1
star
38

idea-php-symfony2-plugin-test

Symfony2 Standard Edition to reflect features and provide tests of PhpStorm Plugin
PHP
1
star