2024-04-01 13:39:17 +00:00
|
|
|
package paraswap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
netUrl "net/url"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
|
|
|
)
|
|
|
|
|
|
|
|
const pricesURL = "https://apiv5.paraswap.io/prices"
|
|
|
|
|
|
|
|
type Route struct {
|
|
|
|
GasCost *bigint.BigInt `json:"gasCost"`
|
|
|
|
SrcAmount *bigint.BigInt `json:"srcAmount"`
|
|
|
|
SrcTokenAddress common.Address `json:"srcToken"`
|
|
|
|
SrcTokenDecimals uint `json:"srcDecimals"`
|
|
|
|
DestAmount *bigint.BigInt `json:"destAmount"`
|
|
|
|
DestTokenAddress common.Address `json:"destToken"`
|
|
|
|
DestTokenDecimals uint `json:"destDecimals"`
|
|
|
|
RawPriceRoute json.RawMessage `json:"rawPriceRoute"`
|
2024-06-12 20:14:30 +00:00
|
|
|
Side SwapSide `json:"side"`
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PriceRouteResponse struct {
|
|
|
|
PriceRoute json.RawMessage `json:"priceRoute"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientV5) FetchPriceRoute(ctx context.Context, srcTokenAddress common.Address, srcTokenDecimals uint,
|
|
|
|
destTokenAddress common.Address, destTokenDecimals uint, amountWei *big.Int, addressFrom common.Address,
|
2024-06-12 20:14:30 +00:00
|
|
|
addressTo common.Address, side SwapSide) (Route, error) {
|
2024-04-01 13:39:17 +00:00
|
|
|
|
|
|
|
params := netUrl.Values{}
|
|
|
|
params.Add("srcToken", srcTokenAddress.Hex())
|
|
|
|
params.Add("srcDecimals", strconv.Itoa(int(srcTokenDecimals)))
|
|
|
|
params.Add("destToken", destTokenAddress.Hex())
|
|
|
|
params.Add("destDecimals", strconv.Itoa(int(destTokenDecimals)))
|
|
|
|
params.Add("userAddress", addressFrom.Hex())
|
|
|
|
// params.Add("receiver", addressTo.Hex()) // at this point paraswap doesn't allow swap and transfer transaction
|
|
|
|
params.Add("network", strconv.FormatUint(c.chainID, 10))
|
|
|
|
params.Add("amount", amountWei.String())
|
2024-06-12 20:14:30 +00:00
|
|
|
params.Add("side", string(side))
|
2024-07-18 20:14:42 +00:00
|
|
|
params.Add("partner", c.partnerID)
|
2024-07-24 20:29:54 +00:00
|
|
|
params.Add("excludeContractMethodsWithoutFeeModel", "true")
|
|
|
|
params.Add("excludeDEXS", "AugustusRFQ") // This DEX causes issues when creating the transaction
|
2024-04-01 13:39:17 +00:00
|
|
|
|
|
|
|
url := pricesURL
|
2024-07-30 13:48:22 +00:00
|
|
|
response, err := c.httpClient.DoGetRequest(ctx, url, params, nil)
|
2024-04-01 13:39:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return Route{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return handlePriceRouteResponse(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePriceRouteResponse(response []byte) (Route, error) {
|
|
|
|
var priceRouteResponse PriceRouteResponse
|
|
|
|
err := json.Unmarshal(response, &priceRouteResponse)
|
|
|
|
if err != nil {
|
|
|
|
return Route{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if priceRouteResponse.Error != "" {
|
|
|
|
return Route{}, errors.New(priceRouteResponse.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var route Route
|
|
|
|
err = json.Unmarshal(priceRouteResponse.PriceRoute, &route)
|
|
|
|
if err != nil {
|
|
|
|
return Route{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
route.RawPriceRoute = priceRouteResponse.PriceRoute
|
|
|
|
|
|
|
|
return route, nil
|
|
|
|
}
|