go1inch

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2022 License: MIT Imports: 7 Imported by: 3

README

go-1inch

A golang API wrapper for the 1inch API

Getting started

testing

package main

import (
    "context"
    "fmt"
    "log"

    go1inch "github.com/jon4hz/go-1inch"
)

func main(){
    // create a new client
    client := go1inch.NewClient()

    // check the health status from the API pointing to the ethereum network
    health, _, err := client.Healthcheck(context.Background(), go1inch.Eth)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(health)
}

Ressources

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApproveAllowanceRes added in v0.0.7

type ApproveAllowanceRes struct {
	// address of 1inch contract
	Allowance string `json:"allowance"`
}

type ApproveSpenderRes

type ApproveSpenderRes struct {
	// address of 1inch contract
	Address string `json:"address"`
}

type ApproveTransactionOpts added in v0.0.7

type ApproveTransactionOpts struct {
	// Amount of tokens to be approved:
	// 0 — set approval to zero (lock a token)
	// >0 — approve exact amount of tokens.
	// if not set use infinity approve
	Amount string
}

type ApproveTransactionRes added in v0.0.7

type ApproveTransactionRes struct {
	// token contract address
	To string `json:"to"`
	// amount of eth to be sent (in wei)
	Value string `json:"value"`
	// recommended gas price (in wei)
	GasPrice string `json:"gasPrice"`
	// result calldata
	Data string `json:"data"`
}

type Client

type Client struct {
	Http *http.Client
}

func NewClient

func NewClient() *Client

func (*Client) ApproveAllowance added in v0.0.7

func (c *Client) ApproveAllowance(ctx context.Context, network Network, tokenAddress, walletAddress string) (*ApproveAllowanceRes, int, error)

ApproveAllowance gets the number of tokens that the 1inch router is allowed to spend

func (*Client) ApproveSpender

func (c *Client) ApproveSpender(ctx context.Context, network Network) (*ApproveSpenderRes, int, error)

ApproveSpender gets the address to which you need to approve before the swap transaction

func (*Client) ApproveTransaction added in v0.0.7

func (c *Client) ApproveTransaction(ctx context.Context, network Network, tokenAddress string, opts *ApproveTransactionOpts) (*ApproveTransactionRes, int, error)

ApproveTransaction gets calldata for approve transaction and spender address Do not combine amount parameter with infinity parameter, only one must be sent. infinity will overwrite amount amount is set in minimal divisible units: for example, to unlock 1 DAI, amount should be 1000000000000000000, to unlock 1.03 USDC, amount should be 1030000.

func (*Client) Healthcheck

func (c *Client) Healthcheck(ctx context.Context, network Network) (*HealthcheckRes, int, error)

Healthcheck checks if service is able to handle requests

func (*Client) LiquiditySouces added in v0.0.7

func (c *Client) LiquiditySouces(ctx context.Context, network Network) (*LiquiditySoucesRes, int, error)

ProtocolsImages gets names and images of all supported protocols

func (*Client) Quote

func (c *Client) Quote(ctx context.Context, network Network, fromTokenAddress, toTokenAddress, amount string, opts *QuoteOpts) (*QuoteRes, int, error)

Quote gets quote for an aggregated swap which can be used with a web3 provider to send the transaction

func (*Client) Swap

func (c *Client) Swap(ctx context.Context, network Network, fromTokenAddress, toTokenAddress, amount, fromAddress string, slippage int64, opts *SwapOpts) (*SwapRes, int, error)

Swap gets swap for an aggregated swap which can be used with a web3 provider to send the transaction

func (*Client) Tokens

func (c *Client) Tokens(ctx context.Context, network Network) (*TokensRes, int, error)

Tokens gets an array of all supported tokens (any erc20 token can be used in a quote and swap)

type HealthcheckRes

type HealthcheckRes struct {
	Status string `json:"status"`
}

type LiquiditySoucesRes added in v0.0.7

type LiquiditySoucesRes struct {
	Protocols []Protocols `json:"protocols"`
}

type Network added in v0.1.0

type Network string
const (
	Eth         Network = "eth"
	Bsc         Network = "bsc"
	Matic       Network = "matic"
	Optimism    Network = "optimism"
	Arbitrum    Network = "arbitrum"
	GnosisChain Network = "gnosis"
	Avalanche   Network = "avalanche"
	Fantom      Network = "fantom"
	Klaytn      Network = "klaytn"
	Auror       Network = "auror"
)

type Protocol

type Protocol [][]struct {
	Name             string  `json:"name"`
	Part             float64 `json:"part"`
	FromTokenAddress string  `json:"fromTokenAddress"`
	ToTokenAddress   string  `json:"toTokenAddress"`
}

type Protocols added in v0.0.7

type Protocols struct {
	Id    string `json:"id"`
	Title string `json:"title"`
	Img   string `json:"img"`
}

type QuoteOpts added in v0.0.5

type QuoteOpts struct {
	// referrer's fee in percentage
	// Ethereum: min: 0; max: 3; Binance: min: 0; max: 3; default: 0; !should be the same for quote and swap!
	Fee string
	// liquidity protocols that can be used in a swap
	Protocols string
	// gas price
	// default: fast from network
	GasPrice string
	// how many connectorTokens can be used
	// min: 0; max: 3; default: 2; !should be the same for quote and swap!
	ComplexityLevel string
	// contract addresses of connector tokens
	// max: 5; !should be the same for quote and swap!
	ConnectorTokens string
	// maximum amount of gas for a swap
	GasLimit string
	// virtual split parts. default: 50; max: 500; !should be the same for quote and swap!
	VirtualParts string
	// maximum number of parts each main route part can be split into
	// split parts. default: Ethereum: 50; Binance: 40 max: Ethereum: 100; Binance: 100; !should be the same for quote and swap!
	Parts string
	// maximum number of main route parts
	// default: Ethereum: 10, Binance: 10; max: Ethereum: 50, Binance: 50 !should be the same for quote and swap!
	MainRouteParts string
}

type QuoteRes

type QuoteRes struct {
	// parameters of a token to sell
	FromToken Token `json:"fromToken"`
	// parameters of a token to buy
	ToToken Token `json:"ToToken"`
	// input amount of fromToken in minimal divisible units
	ToTokenAmount string `json:"toTokenAmount"`
	// result amount of toToken in minimal divisible units
	FromTokenAmount string `json:"fromTokenAmount"`
	// route of the trade
	Protocols []Protocol `json:"protocols"`
	// rough estimated amount of the gas limit for used protocols;
	// do not use estimatedGas from the quote method as the gas limit of a transaction
	EstimatedGas int64 `json:"estimatedGas"`
}

type SwapOpts added in v0.0.5

type SwapOpts struct {
	// protocols that can be used in a swap
	Protocols string
	// address that will receive a purchased token
	// Receiver of destination currency. default: fromAddress
	DestReceiver string
	// referrer's address
	ReferrerAddress string
	// referrer's fee in percentage
	// Ethereum: min: 0; max: 3; Binance: min: 0; max: 3; default: 0; !should be the same for quote and swap!
	Fee string
	// gas price
	// default: fast from network
	GasPrice string
	// if true, CHI will be burned from fromAddress to compensate gas
	// default: false; Suggest to check user's balance and allowance before set this flag; CHI should be approved to spender address
	BurnChi bool
	// how many connectorTokens can be used
	// min: 0; max: 3; default: 2; !should be the same for quote and swap!
	ComplexityLevel string
	// contract addresses of connector tokens
	// max: 5; !should be the same for quote and swap!
	ConnectorTokens string
	// if true, accept the partial order execution
	AllowPartialFill bool
	// if true, checks of the required quantities are disabled
	DisableEstimate bool
	// maximum amount of gas for a swap
	GasLimit string
	// virtual split parts. default: 50; max: 500; !should be the same for quote and swap!
	VirtualParts string
	// maximum number of parts each main route part can be split into
	// split parts. default: Ethereum: 50; Binance: 40 max: Ethereum: 100; Binance: 100; !should be the same for quote and swap!
	Parts string
	// maximum number of main route parts
	// default: Ethereum: 10, Binance: 10; max: Ethereum: 50, Binance: 50 !should be the same for quote and swap!
	MainRouteParts string
}

type SwapRes

type SwapRes struct {
	// parameters of a token to sell
	FromToken Token `json:"fromToken"`
	// parameters of a token to buy
	ToToken Token `json:"ToToken"`
	// input amount of fromToken in minimal divisible units
	ToTokenAmount string `json:"toTokenAmount"`
	// result amount of toToken in minimal divisible units
	FromTokenAmount string `json:"fromTokenAmount"`
	// route of the trade
	Protocols []Protocol `json:"protocols"`
	// transaction data
	Tx Tx `json:"tx"`
}

type Token

type Token struct {
	Symbol   string `json:"symbol"`
	Name     string `json:"name"`
	Address  string `json:"address"`
	Decimals int64  `json:"decimals"`
	LogoURI  string `json:"logoURI"`
}

type TokensRes

type TokensRes struct {
	Tokens map[string]Token `json:"tokens"`
}

type Tx

type Tx struct {
	// transactions will be sent from this address
	From string `json:"from"`
	// transactions will be sent to our contract address
	To string `json:"to"`
	// call data
	Data string `json:"data"`
	// amount of ETH (in wei) will be sent to the contract address
	Value string `json:"value"`
	// gas price in wei
	GasPrice string `json:"gasPrice"`
	// estimated amount of the gas limit, increase this value by 25%
	Gas int64 `json:"gas"`
}

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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