Skip to content

Commit

Permalink
feature/v2-support (#1)
Browse files Browse the repository at this point in the history
* started working on v2

* slippage config object

* v2

* fixed dex names

* log

* bypass

* rm todo
  • Loading branch information
decanus authored Apr 4, 2024
1 parent b9b1c38 commit 53382f6
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 1 deletion.
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ module github.com/project-blanc/go-squid-router

go 1.21.7

require github.com/pkg/errors v0.9.1
require (
github.com/ethereum/go-ethereum v1.13.14
github.com/pkg/errors v0.9.1
)

require (
github.com/holiman/uint256 v1.2.4 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.16.0 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
github.com/ethereum/go-ethereum v1.13.14 h1:EwiY3FZP94derMCIam1iW4HFVrSgIcpsu0HwTQtm6CQ=
github.com/ethereum/go-ethereum v1.13.14/go.mod h1:TN8ZiHrdJwSe8Cb6x+p0hs5CxhJZPbqB7hHkaUXcmIU=
github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU=
github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
36 changes: 36 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"

squid "github.com/project-blanc/go-squid-router/v2"
)

func main() {
s := squid.NewClient("")

r, err := s.Route(squid.RouteRequestParameters{
FromChain: "42161",
ToChain: "42220",
FromToken: "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
ToToken: "0x765de816845861e75a25fca122bb6898b8b1282a",
ToAddress: "0x4254EbB758d60e3f15A93D53BDa6a760F0Ae24f9",
FromAmount: "10000000",
FromAddress: "0xcc70cbe5e7669E5c72E6C1c7b73a0da0855050AB",
Prefer: []squid.DexName{
squid.PANCAKESWAP,
squid.UNISWAP_V2,
squid.CURVE_V2,
},
SlippageConfig: squid.SlippageConfig{
Slippage: 1,
AutoMode: squid.NORMAL,
},
})

if err != nil {
panic(err)
}

fmt.Printf("%+v", r)
}
62 changes: 62 additions & 0 deletions v2/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package v2

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/pkg/errors"
)

type Client struct {
cli *http.Client
integratorID string
}

func NewClient(integratorID string) *Client {
return &Client{
integratorID: integratorID,
cli: &http.Client{}, // @TODO inject
}
}

// Route calls the route endpoint returning route information and the call data object
func (c *Client) Route(params RouteRequestParameters) (*RouteResponse, error) {
body, err := json.Marshal(params)
if err != nil {
return nil, err
}

req, err := http.NewRequest(http.MethodPost, "https://v2.api.squidrouter.com/v2/route", bytes.NewBuffer(body))
if err != nil {
return nil, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-integrator-id", c.integratorID)

resp, err := c.cli.Do(req)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
body, _ = io.ReadAll(resp.Body)
return nil, fmt.Errorf("request failed body %s", string(body)) // @TODO
}

buff, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body")
}

rr := map[string]*RouteResponse{}
err = json.Unmarshal(buff, &rr)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal response body")
}

return rr["route"], nil
}
135 changes: 135 additions & 0 deletions v2/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package v2

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)

type DexName string

const (
AGNI_V3 DexName = "Agni-v3"
AERODROME DexName = "Aerodrome"
APESWAP DexName = "Apeswap"
APESWAP_V3 DexName = "Apeswap-v3"
BALANCER_V2 DexName = "Balancer-v2"
BASESWAP DexName = "Baseswap"
BASESWAP_V3 DexName = "Baseswap-v3"
BEAMSWAP DexName = "Beamswap"
BEAMSWAP_V3 DexName = "Beamswap-v3"
CAMELOT DexName = "Camelot"
CURVE_V2 DexName = "Curve-v2"
ELLIPSIS DexName = "Ellipsis"
EQUILIBRE DexName = "Equilibre"
EQUALIZER DexName = "Equalizer"
FUSIONX_V2 DexName = "FusionX-v2"
FUSIONX_V3 DexName = "FusionX-v3"
GMX DexName = "GMX"
HORIZON DexName = "Horizon"
HORIZON_V3 DexName = "Horizon-v3"
KYBERSWAP DexName = "KyberSwap"
KYBERSWAP_AGGREGATOR DexName = "kyberswap-aggregator"
KINETIX_V3 DexName = "Kinetix-v3"
MENTO_V2 DexName = "Mento-v2"
OPENOCEAN DexName = "OpenOcean"
OSMOSIS DexName = "Osmosis"
PANCAKESWAP DexName = "Pancakeswap"
PANCAKESWAP_V3 DexName = "Pancakeswap-v3"
PANCAKESWAP_STABLE DexName = "Pancakeswap-stable"
PANGOLIN DexName = "Pangolin"
PLATYPUS DexName = "Platypus"
QUICKSWAP DexName = "Quickswap"
QUICKSWAP_V3 DexName = "Quickswap-v3"
RAMSES DexName = "Ramses"
SPOOKYSWAP DexName = "Spookyswap"
STELLASWAP DexName = "Stellaswap"
STELLASWAP_V3 DexName = "Stellaswap-v3"
STELLASWAP_SADDLE DexName = "Stellaswap-saddle"
SUSHISWAP DexName = "Sushiswap"
SUSHISWAP_V3 DexName = "Sushiswap-v3"
SWAPBASED DexName = "SwapBased"
SYNTHSWAP_V2 DexName = "SynthSwap-v2"
SYNTHSWAP_V3 DexName = "SynthSwap-v3"
SKYDROME DexName = "Skydrome"
THENA DexName = "Thena"
THENA_V3 DexName = "Thena-v3"
TRADERJOE DexName = "TraderJoe"
TRIDENT DexName = "Trident"
UBESWAP DexName = "Ubeswap"
UBESWAP_V3 DexName = "Ubeswap-v3"
UNISWAP_V2 DexName = "Uniswap-v2"
UNISWAP_V3 DexName = "Uniswap-v3"
WIGOSWAP DexName = "WigoSwap"
WOMBAT DexName = "Wombat"
VELODROME DexName = "Velodrome"
VELODROME_V2 DexName = "Velodrome-v2"
VELOCIMETER DexName = "Velocimeter"
ZYBERSWAP DexName = "Zyberswap"
ZYBERSWAP_V3 DexName = "Zyberswap-v3"
)

type SlippageMode int

const (
AGGRESSIVE SlippageMode = iota
NORMAL
FLEXIBLE
)

type SlippageConfig struct {
Slippage float64 `json:"slippage"`
AutoMode SlippageMode `json:"autoMode"`
}

type RouteRequestParameters struct {
// FromChain is the from chain id
FromChain string `json:"fromChain"`
// ToChain is the to chain id
ToChain string `json:"toChain"`
// FromToken is the from token address
FromToken string `json:"fromToken"`
// ToToken is the to token address
ToToken string `json:"toToken"`
// FromAmount is the amount to be sent from To Chain
FromAmount string `json:"fromAmount"`
// FromAddress EVM address for EVM to Cosmos, and Cosmos address for Cosmos to EVM.
FromAddress string `json:"fromAddress"`
// ToAddress is the ToChain recipient address
ToAddress string `json:"toAddress"`
// QuoteOnly returns only the route quote and excludes all call data
QuoteOnly bool `json:"quoteOnly"`
EnableBoost bool `json:"enableBoost"`
// Prefer array of supported DEXs for this trade
Prefer []DexName `json:"prefer,omitempty"`
// ReceiveGasOnDestination receive gas on destination chain
ReceiveGasOnDestination bool `json:"receiveGasOnDestination"`
SlippageConfig SlippageConfig `json:"slippageConfig"`
BypassGuardrails bool `json:"bypassGuardrails"`
}

type SquidRouteType string

const (
SquidRouteTypeCallBridgeCall SquidRouteType = "CALL_BRIDGE_CALL"
SquidRouteTypeCallBridge SquidRouteType = "CALL_BRIDGE"
SquidRouteTypeBridgeCall SquidRouteType = "BRIDGE_CALL"
SquidRouteTypeBridge SquidRouteType = "BRIDGE"
SquidRouteTypeEVMOnly SquidRouteType = "EVM_ONLY"
SquidRouteTypeCosmosOnly SquidRouteType = "COSMOS_ONLY"
)

type SquidData struct {
Target common.Address `json:"target"`
Data hexutil.Bytes `json:"data"`
Value string `json:"value"`
GasLimit string `json:"gasLimit"`
GasPrice string `json:"gasPrice"`
MaxFeePerGas string `json:"maxFeePerGas"`
MaxPriorityFeePerGas string `json:"maxPriorityFeePerGas"`
}

type RouteResponse struct {
// @TODO estimate
// @TodO params
TransactionRequest SquidData `json:"transactionRequest"`
}

0 comments on commit 53382f6

Please sign in to comment.