Skip to content

Commit

Permalink
client: external-match-client: Add GetFees method to fetch match fee (
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut authored Feb 15, 2025
1 parent b658a53 commit 2abb908
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 136 deletions.
6 changes: 6 additions & 0 deletions client/api_types/api_external_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,9 @@ type ApiSettlementTransaction struct { //nolint:revive
Data string `json:"data"`
Value string `json:"value"`
}

// ApiExternalMatchFee represents the fees for a given asset in external matches
type ApiExternalMatchFee struct { //nolint:revive
RelayerFee string `json:"relayer_fee"`
ProtocolFee string `json:"protocol_fee"`
}
7 changes: 7 additions & 0 deletions client/api_types/request_response_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const (
// --- Orderbook Endpoints --- //
// GetSupportedTokensPath is the path for the GetSupportedTokens action
GetSupportedTokensPath = "/v0/supported-tokens"
// GetFeeForAssetPath is the path for the GetFeeForAsset action
GetFeeForAssetPath = "/v0/order_book/external-match-fee"

// --- Wallet Endpoints --- //
// GetWalletPath is the path for the GetWallet action
Expand Down Expand Up @@ -66,6 +68,11 @@ type WalletUpdateAuthorization struct {
NewRootKey *string `json:"new_root_key"`
}

// BuildGetFeeForAssetPath builds the path for the GetFeeForAsset action
func BuildGetFeeForAssetPath(mint string) string {
return fmt.Sprintf("%s?mint=%s", GetFeeForAssetPath, mint)
}

// BuildGetWalletPath builds the path for the GetWallet action
func BuildGetWalletPath(walletID uuid.UUID) string {
return fmt.Sprintf(GetWalletPath, walletID)
Expand Down
164 changes: 28 additions & 136 deletions client/external_match_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package external_match_client //nolint:revive
import (
"encoding/json"
"fmt"
"math/big"
"net/http"

geth_common "github.com/ethereum/go-ethereum/common"

"github.com/renegade-fi/golang-sdk/client"
"github.com/renegade-fi/golang-sdk/client/api_types"
"github.com/renegade-fi/golang-sdk/wallet"
Expand All @@ -22,139 +19,6 @@ const (
apiKeyHeader = "X-Renegade-Api-Key" //nolint:gosec
)

// -----------------
// | Request Types |
// -----------------

// ExternalMatchBundle is the application level analog to the ApiExternalMatchBundle
type ExternalMatchBundle struct {
MatchResult *api_types.ApiExternalMatchResult
Fees *api_types.ApiFee
Receive *api_types.ApiExternalAssetTransfer
Send *api_types.ApiExternalAssetTransfer
SettlementTx *SettlementTransaction
// Whether the match has received gas sponsorship
//
// If `true`, the bundle is routed through a gas rebate contract that
// refunds the gas used by the match to the configured address
GasSponsored bool
}

// SettlementTransaction is the application level analog to the ApiSettlementTransaction
type SettlementTransaction struct {
Type string
To geth_common.Address
Data []byte
Value *big.Int
}

// toSettlementTransaction converts an ApiSettlementTransaction to a SettlementTransaction
func toSettlementTransaction(tx *api_types.ApiSettlementTransaction) *SettlementTransaction {
// Parse a geth address and bytes data from hex strings
to := geth_common.HexToAddress(tx.To)
data := geth_common.FromHex(tx.Data)
valueBytes := geth_common.FromHex(tx.Value)
value := big.NewInt(0).SetBytes(valueBytes)

return &SettlementTransaction{
Type: tx.Type,
To: to,
Data: data,
Value: value,
}
}

// AssembleExternalMatchOptions represents the options for an assembly request
type AssembleExternalMatchOptions struct {
ReceiverAddress *string
DoGasEstimation bool
UpdatedOrder *api_types.ApiExternalOrder
// RequestGasSponsorship is a flag to request gas sponsorship for the settlement tx
//
// This is subject to rate limit by the auth server, but if approved will refund the gas spent
// on the settlement tx to the address specified in `GasRefundAddress`. If no refund address is
// specified, the refund is directed to `tx.origin`
RequestGasSponsorship bool
// GasRefundAddress is the address to refund the gas to
//
// This is ignored if `RequestGasSponsorship` is false
GasRefundAddress *string
}

// WithReceiverAddress sets the receiver address for the assembly options
func (o *AssembleExternalMatchOptions) WithReceiverAddress(address *string) *AssembleExternalMatchOptions {
o.ReceiverAddress = address
return o
}

// WithGasEstimation sets whether to perform gas estimation
func (o *AssembleExternalMatchOptions) WithGasEstimation(estimate bool) *AssembleExternalMatchOptions {
o.DoGasEstimation = estimate
return o
}

// WithUpdatedOrder sets the updated order for the assembly options
func (o *AssembleExternalMatchOptions) WithUpdatedOrder(order *api_types.ApiExternalOrder) *AssembleExternalMatchOptions {
o.UpdatedOrder = order
return o
}

// WithRequestGasSponsorship sets whether to request gas sponsorship
func (o *AssembleExternalMatchOptions) WithRequestGasSponsorship(request bool) *AssembleExternalMatchOptions {
o.RequestGasSponsorship = request
return o
}

// WithGasRefundAddress sets the gas refund address for the assembly options
func (o *AssembleExternalMatchOptions) WithGasRefundAddress(address *string) *AssembleExternalMatchOptions {
o.GasRefundAddress = address
return o
}

// BuildRequestPath builds the request path for the assembly options
func (o *AssembleExternalMatchOptions) BuildRequestPath() string {
path := api_types.AssembleExternalQuotePath
path += fmt.Sprintf("?%s=%t", api_types.RequestGasSponsorshipParam, o.RequestGasSponsorship)
if o.GasRefundAddress != nil {
path += fmt.Sprintf("&%s=%s", api_types.GasRefundAddressParam, *o.GasRefundAddress)
}

return path
}

// NewAssembleExternalMatchOptions creates a new AssembleExternalMatchOptions with default values
func NewAssembleExternalMatchOptions() *AssembleExternalMatchOptions {
return &AssembleExternalMatchOptions{
ReceiverAddress: nil,
DoGasEstimation: false,
UpdatedOrder: nil,
}
}

// ExternalMatchOptions represents the options for an external match request
//
// Deprecated: Use AssembleExternalMatchOptions instead
type ExternalMatchOptions struct {
AssembleExternalMatchOptions
}

// BuildRequestPath builds the request path for the external match options
func (o *ExternalMatchOptions) BuildRequestPath() string {
path := api_types.GetExternalMatchBundlePath
path += fmt.Sprintf("?%s=%t", api_types.RequestGasSponsorshipParam, o.RequestGasSponsorship)
if o.GasRefundAddress != nil {
path += fmt.Sprintf("&%s=%s", api_types.GasRefundAddressParam, *o.GasRefundAddress)
}
return path
}

// NewExternalMatchOptions creates a new ExternalMatchOptions with default values
func NewExternalMatchOptions() *ExternalMatchOptions {
return &ExternalMatchOptions{
AssembleExternalMatchOptions: *NewAssembleExternalMatchOptions(),
}
}

// -------------------------
// | Client Implementation |
// -------------------------
Expand Down Expand Up @@ -194,6 +58,10 @@ func NewExternalMatchClient(
}
}

// ----------------
// | Metadata API |
// ----------------

// GetSupportedTokens requests the list of supported tokens from the relayer
func (c *ExternalMatchClient) GetSupportedTokens() ([]api_types.ApiToken, error) {
var response api_types.GetSupportedTokensResponse
Expand All @@ -209,6 +77,30 @@ func (c *ExternalMatchClient) GetSupportedTokens() ([]api_types.ApiToken, error)
return response.Tokens, nil
}

// GetFeeForAsset requests the fees for a given base token
func (c *ExternalMatchClient) GetFeeForAsset(addr *string) (*ExternalMatchFee, error) {
var response api_types.ApiExternalMatchFee
err := c.relayerHttpClient.GetJSON(
api_types.BuildGetFeeForAssetPath(*addr),
nil, // body
&response,
)
if err != nil {
return nil, err
}

parsedFee, err := toExternalMatchFee(&response)
if err != nil {
return nil, err
}

return parsedFee, nil
}

// ------------------------
// | Quote + Assembly API |
// ------------------------

// GetExternalMatchQuote requests a quote from the relayer
// returns nil if no match is found
func (c *ExternalMatchClient) GetExternalMatchQuote(
Expand Down
Loading

0 comments on commit 2abb908

Please sign in to comment.