status-go/services/wallet/thirdparty/paraswap/request_price_route.go

85 lines
2.6 KiB
Go
Raw Normal View History

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://api.paraswap.io/prices"
2024-04-01 13:39:17 +00:00
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"`
Side SwapSide `json:"side"`
ContractAddress common.Address `json:"contractAddress"`
TokenTransferProxy common.Address `json:"tokenTransferProxy"`
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))
params.Add("partner", c.partnerID)
params.Add("excludeContractMethodsWithoutFeeModel", "true")
params.Add("version", "6.2")
2024-04-01 13:39:17 +00:00
url := pricesURL
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
}