Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
fix: handle fetching conversion rate for same token (#38)
Browse files Browse the repository at this point in the history
* Expand average strategy to support case when base and foreign currency are equal

* add logs for errors
  • Loading branch information
MakMuftic authored Mar 15, 2023
1 parent 0ec622a commit 4029375
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (h *Handler) getRate(c *gin.Context) {

gp, err := h.consensus.FilterLocalGasPriceData(h.gasPriceStore, toDomain.Name)
if err != nil {
h.log.Errorf("get gasprice process failed: %v", err)
ginErrorReturn(c, http.StatusInternalServerError, newReturnErrorResp(&oracleErrors.InternalServerError, err))
return
}
Expand All @@ -81,13 +82,15 @@ func (h *Handler) getRate(c *gin.Context) {
toDomain.BaseCurrencySymbol,
fromDomain.BaseCurrencySymbol)
if err != nil {
h.log.Errorf("calculate ber process failed: %v", err)
ginErrorReturn(c, http.StatusInternalServerError, newReturnErrorResp(&oracleErrors.InternalServerError, err))
return
}
h.log.Debugf("base rate calculation: to: %s, from: %s\n", toDomain.BaseCurrencySymbol, fromDomain.BaseCurrencySymbol)

ter, err := h.calculateTokenRate(resource, ber, fromDomain, toDomain)
if err != nil {
h.log.Errorf("calculate ter process failed: %v", err)
ginErrorReturn(c, http.StatusInternalServerError, newReturnErrorResp(&oracleErrors.InternalServerError, err))
return
}
Expand All @@ -108,6 +111,7 @@ func (h *Handler) getRate(c *gin.Context) {
}
rate.Signature, err = signature.RateSignature(h.conf, rate, h.identity, fromDomain.ID, resource.ID)
if err != nil {
h.log.Errorf("signature process failed: %v", err)
ginErrorReturn(c, http.StatusInternalServerError, newReturnErrorResp(&oracleErrors.InternalServerError, err))
return
}
Expand Down
10 changes: 10 additions & 0 deletions consensus/strategy/average.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"math"
"strconv"
"time"

"github.com/ChainSafe/sygma-fee-oracle/store"
"github.com/ChainSafe/sygma-fee-oracle/types"
Expand Down Expand Up @@ -61,6 +62,15 @@ func (a *Average) GasPrice(store *store.GasPriceStore, domainName string) (*type
}

func (a *Average) ConversionRate(store *store.ConversionRateStore, base, foreign string) (*types.ConversionRate, error) {
if base == foreign {
return &types.ConversionRate{
Base: base,
Foreign: foreign,
Rate: 1,
Time: time.Now().Unix(),
}, nil
}

re, err := store.GetConversionRatesByCurrencyPair(base, foreign)
if err != nil {
return nil, err
Expand Down

0 comments on commit 4029375

Please sign in to comment.