npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sandwich-scanner

v0.1.3

Published

Library to detect on-chain sandwich attacks

Downloads

33

Readme

Sandwich-scanner

Continuous Integrations License Package dependency count Npm package version

Description

Sandwich-scanner is a simple library to scanning the ethereum blockchain for sandwich attacks.

A sandwich attack is a type of arbitrage attack that occurs when a user is able to buy an asset at a low price and sell it at a higher price, all within the same transaction.

This tool is in beta and might not be 100% accurate. Only Uniswap V2 is supported for now.

Installation

This library is published in the NPM registry and can be installed using any compatible package manager.

npm install sandwich-scanner --save

# For Yarn, use the command below.
yarn add sandwich-scanner

Installation from CDN

This module has an UMD bundle available through JSDelivr and Unpkg CDNs.

<!-- For UNPKG use the code below. -->
<script src="https://unpkg.com/sandwich-scanner"></script>

<!-- For JSDelivr use the code below. -->
<script src="https://cdn.jsdelivr.net/npm/sandwich-scanner"></script>

<script>
  // UMD module is exposed through the "sandwich-scanner" global variable.
  console.log(sandwich-scanner);
</script>

Usage

Instantiation

const provider = new ethers.providers.JsonRpcProvider(INFURA_MAINNET_RPC);
const sandwichDetector = new SandwichDetector(provider, covalentApiKey);

The library has implemented a provider wrapper for retrying failed requests. This is useful for handling rate limiting errors from the RPC node.

In order to leverage the retry option, you can pass an array of providers to the SandwichDetector constructor.

const provider1 = new ethers.providers.JsonRpcProvider(INFURA_MAINNET_RPC);
const provider2 = new ethers.providers.JsonRpcProvider(ANKR_MAINNET_RPC);
const provider3 = new ethers.providers.JsonRpcProvider(BLASTAPI_MAINNET_RPC);

const sandwichDetector = new SandwichDetector(
  provider1, 
  covalentApiKey, 
  [provider2, provider3]);

provider1 will be consider as the main provider. In case of failure, the wrapper will route the calls to the next provider in the array.

Get sandwich transactions

// Get all sandwich transactions for a specific block and Dex
const sandwichesForBlock = await sandwichDetector.getSandwichesForBlock(11698481, "UniswapV2");

// Get all sandwich transactions for a specific address block and Dex
const sandwichesOnAddress = await sandwichDetector.getSandwichesForAddress('"0x1234..."', "UniswapV2");

// Get sandwich transactions with on offset for a specific address block and Dex
const sandwichesOnAddressWithOffset = await sandwichDetector.getSwandwichesforAddressWithOffset("0x1234...", 1, "UniswapV2");

offset: considering the tx in block y at position n. The detector will fetch txs in block y at position [n-offset ; n] and [n ; n+offset]. The detection logic will be applied on theses txs.

As most of the sandwiches are happening at the index before and after the swap transaction, it is recommended to opt for an offset of 1.

Note that getSandwichesForAddress is very resource intensive and will hit hard your RPC. It is recommended to use getSandwichesForAddressWithOffset instead.

Documentation

Documentation generated from source files by Typedoc.

License

Released under MIT License.