blockchain

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2022 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const Zero = "0x0000000000000000000000000000000000000000"

Variables

View Source
var (
	ErrNoBuyTrade  = errors.New("no buy trade found")
	ErrNoSellTrade = errors.New("no sell trade found")
)
View Source
var (
	ErrNoPathFound     = errors.New("no path found")
	ErrNoPairsFound    = errors.New("no pairs found")
	ErrNoTradeFound    = errors.New("no trade found")
	ErrNoMidPriceFound = errors.New("no mid price found")
	ErrParsingPrice    = errors.New("failed to parse price")
)
View Source
var (
	ErrTxTimeout = errors.New("transaction timed out")
	ErrCancelNow = errors.New("canceling now")
)
View Source
var ErrExecutionReverted = errors.New("error no contracts")
View Source
var ErrNoContracts = errors.New("error no contracts")

Functions

func GetActualPriceImpact

func GetActualPriceImpact(trade *uniswap.Trade, dexFee *big.Int) float64

GetActualPriceImpact returns the price impact of the given trade. The dex fee gets subtracted from the impact for every trade in the route.

func GetChainID

func GetChainID(url string) (*big.Int, error)

GetChainID returns the chain id of the given node url.

func ValidateEndpointURL

func ValidateEndpointURL(url string, chainID uint32) bool

ValidateEndpointURL validates the given node url by trying to fetch the chain id.

Types

type Client

type Client struct {
	Client *ethclient.Client
	// contains filtered or unexported fields
}

func NewClient

func NewClient(node, multicallHex string) (*Client, error)

NewClient initilalizes the blockchain clients.

func (*Client) CheckListed

func (c *Client) CheckListed(token0, token1 *database.Token, dex *database.Dex, weth string, tokens []*database.Token) (*uniswap.Trade, error)

CheckListed checks if there is an available trading route for two given tokens. If there is no route, chain.ErrNoTradeFound is returned so you should check against that specific error when calling this functions, other errors can be retured too.

func (*Client) GetBalanceOf

func (c *Client) GetBalanceOf(address string, contract string) (*big.Int, error)

GetBalanceOf returns the balance of a given address in the given token or native currency if an empty string is passed.

func (*Client) GetBalanceOfToken

func (c *Client) GetBalanceOfToken(address string, token *database.Token) (*big.Int, error)

func (*Client) GetBestOrderTrades

func (c *Client) GetBestOrderTrades(token0, token1 *database.Token, buyAmount, sellAmount *big.Int, dex *database.Dex, tokens []*database.Token, maxHops int, weth string) (*uniswap.Trade, *uniswap.Trade, error)

func (*Client) GetBestTradeExactIn

func (c *Client) GetBestTradeExactIn(token0, token1 *database.Token, amount *big.Int, dex *database.Dex, tokens []*database.Token, maxHops int, weth string) (*uniswap.Trade, error)

func (*Client) GetBestTradeExactOut

func (c *Client) GetBestTradeExactOut(token0, token1 *database.Token, amount *big.Int, dex *database.Dex, tokens []*database.Token, maxHops int, weth string) (*uniswap.Trade, error)

func (*Client) GetPairInfo

func (c *Client) GetPairInfo(contracts ...string) (map[string]*Pair, error)

GetPairInfo returns a map of liquidity tokens with their pair info as values.

func (*Client) GetTokenInfo

func (c *Client) GetTokenInfo(contracts ...string) (map[string]*database.Token, error)

GetTokenSymbols returns a map of token addresses with their symbols as values.

func (*Client) GetValidPairTokens

func (c *Client) GetValidPairTokens(tokens []*database.Token, factory string) ([]string, error)

GetPairTokens returns a slice of liquidity tokens.

func (*Client) NewRouter

func (c *Client) NewRouter(contract string) (*uniswapv2router2.Uniswapv2router2, error)

NewRouter initializes the uniswapv2 router.

func (*Client) Swap

func (c *Client) Swap(wallet *database.Wallet, trade *database.Trade, target *database.Target) (*types.Transaction, error)

Swap triggers a swap of a target.

func (*Client) TradeDispatcher

func (c *Client) TradeDispatcher(ctx context.Context, cancel context.CancelFunc, wallet *database.Wallet, trade *database.Trade, price *Price, logStream chan<- string)

TradeDispatcher is a loop that checks if the price matches a target (considering the slippage) and executes the trade This function is blocking and should be run in a goroutine.

type Pair

type Pair struct {
	// contains filtered or unexported fields
}

func NewPair

func NewPair(address string, token0, token1 *database.Token, reserve0, reserve1, totalSupply *big.Int) *Pair

NewPair creates a new pair of tokens.

func (Pair) ToUniswap

func (p Pair) ToUniswap() (*uniswap.Pair, *uniswap.Token, *uniswap.Token, error)

ToUniswap converts a pair to a uniswap.Pair.

type Price

type Price struct {
	Heartbeat chan struct{}
	// contains filtered or unexported fields
}

func NewPrice

func NewPrice() *Price

New is the constructor for the PriceFeed.

func NewPriceWithContext

func NewPriceWithContext(ctx context.Context, cancel context.CancelFunc) *Price

func (*Price) BuyTrade

func (p *Price) BuyTrade() (*uniswap.Trade, error)

func (*Price) GetBuyAmount

func (p *Price) GetBuyAmount() *big.Int

func (*Price) GetBuyTrade

func (p *Price) GetBuyTrade() *uniswap.Trade

func (*Price) GetError

func (p *Price) GetError() error

func (*Price) GetHeartbeatRunning

func (p *Price) GetHeartbeatRunning() bool

func (*Price) GetRunning

func (p *Price) GetRunning() bool

func (*Price) GetSellAmount

func (p *Price) GetSellAmount() *big.Int

func (*Price) GetSellTrade

func (p *Price) GetSellTrade() *uniswap.Trade

func (*Price) GetTrades

func (p *Price) GetTrades() (*uniswap.Trade, *uniswap.Trade)

func (*Price) SellTrade

func (p *Price) SellTrade() (*uniswap.Trade, error)

func (*Price) SetBuyAmount

func (p *Price) SetBuyAmount(amount *big.Int)

func (*Price) SetHeartbeat

func (p *Price) SetHeartbeat(running bool)

func (*Price) SetPriceResult

func (p *Price) SetPriceResult(r PriceResult)

func (*Price) SetSellAmount

func (p *Price) SetSellAmount(amount *big.Int)

func (*Price) StartFeed

func (p *Price) StartFeed(c *Client, token0, token1 *database.Token, dex *database.Dex, tokens []*database.Token, interval time.Duration, maxHops int, weth string)

StartFeed starts a new price feed for the given token.

func (*Price) Stop

func (p *Price) Stop()

type PriceResult

type PriceResult struct {
	// contains filtered or unexported fields
}

type TokenPair

type TokenPair struct {
	Factory string
	Token0  string
	Token1  string
}

func NewTokenPair

func NewTokenPair(token0, token1, factory string) *TokenPair

NewTokenPair creates a new token pair.

Directories

Path Synopsis
abi

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL