Skip to content

Commit

Permalink
update: linter fixes, typos, optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mhrynenko committed May 31, 2024
1 parent e9158f4 commit c98e499
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Config struct {
identity.VerifierProvider
Broadcasterer
AirdropConfiger
PriceApiConfiger
PriceAPIConfiger

airdrop comfig.Once
verifier comfig.Once
Expand All @@ -30,6 +30,6 @@ func New(getter kv.Getter) *Config {
VerifierProvider: identity.NewVerifierProvider(getter),
Broadcasterer: NewBroadcaster(getter),
AirdropConfiger: NewAirdropConfiger(getter),
PriceApiConfiger: NewPriceApiConfiger(getter),
PriceAPIConfiger: NewPriceApiConfiger(getter),
}
}
26 changes: 13 additions & 13 deletions internal/config/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,47 @@ import (
"gitlab.com/distributed_lab/logan/v3/errors"
)

const priceApiYamlKey = "price_api"
const priceAPIYamlKey = "price_api"

var (
ErrPriceApiRequestFailed = errors.New("failed to fetch price api")
ErrPriceAPIRequestFailed = errors.New("failed to fetch price api")
ErrEmptyPrice = errors.New("dollar price in ETH is empty")
)

type PriceApiConfiger interface {
type PriceAPIConfiger interface {
PriceApiConfig() PriceApiConfig
}

type PriceApiConfig struct {
URL *url.URL `fig:"url,required"`
Key string `fig:"key,required"`
CurrencyId string `fig:"currency_id,required"`
CurrencyID string `fig:"currency_id,required"`
QuoteTag string `fig:"quote_tag,required"`
}

type priceApi struct {
type priceAPI struct {
once comfig.Once
getter kv.Getter
}

func NewPriceApiConfiger(getter kv.Getter) PriceApiConfiger {
return &priceApi{
func NewPriceApiConfiger(getter kv.Getter) PriceAPIConfiger {
return &priceAPI{
getter: getter,
}
}

func (v *priceApi) PriceApiConfig() PriceApiConfig {
func (v *priceAPI) PriceApiConfig() PriceApiConfig {
return v.once.Do(func() interface{} {
var result PriceApiConfig

err := figure.
Out(&result).
With(figure.BaseHooks).
From(kv.MustGetStringMap(v.getter, priceApiYamlKey)).
From(kv.MustGetStringMap(v.getter, priceAPIYamlKey)).
Please()
if err != nil {
panic(errors.Wrap(err, "failed to figure out config", logan.F{
"yaml_key": priceApiYamlKey,
"yaml_key": priceAPIYamlKey,
}))
}

Expand Down Expand Up @@ -84,7 +84,7 @@ func (cfg PriceApiConfig) ConvertPrice() (*big.Float, error) {
URL := cfg.URL.JoinPath("/v2/cryptocurrency/quotes/latest")

query := URL.Query()
query.Set("id", cfg.CurrencyId)
query.Set("id", cfg.CurrencyID)
query.Set("convert", cfg.QuoteTag)

URL.RawQuery = query.Encode()
Expand All @@ -109,7 +109,7 @@ func (cfg PriceApiConfig) ConvertPrice() (*big.Float, error) {
return nil, errors.Wrap(err, "failed to read response body")
}

return nil, errors.From(ErrPriceApiRequestFailed, logan.F{
return nil, errors.From(ErrPriceAPIRequestFailed, logan.F{
"status": response.StatusCode,
"body": string(body),
})
Expand All @@ -120,7 +120,7 @@ func (cfg PriceApiConfig) ConvertPrice() (*big.Float, error) {
return nil, errors.Wrap(err, "failed to decode response body")
}

dollarInEth := big.NewFloat(body.Data[cfg.CurrencyId].Quote[cfg.QuoteTag].Price)
dollarInEth := big.NewFloat(body.Data[cfg.CurrencyID].Quote[cfg.QuoteTag].Price)
if dollarInEth.Cmp(big.NewFloat(0)) == 0 {
return nil, ErrEmptyPrice
}
Expand Down
6 changes: 3 additions & 3 deletions internal/service/api/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
broadcasterCtxKey
erc20PermitCtxKey
erc20PermitTransferCtxKey
priceApiConfigCtxKey
priceAPIConfigCtxKey
)

func CtxLog(entry *logan.Entry) func(context.Context) context.Context {
Expand Down Expand Up @@ -107,10 +107,10 @@ func ERC20PermitTransfer(r *http.Request) *contracts.ERC20TransferWithPermit {

func CtxPriceApiConfig(entry config.PriceApiConfig) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, priceApiConfigCtxKey, entry)
return context.WithValue(ctx, priceAPIConfigCtxKey, entry)
}
}

func PriceApiConfig(r *http.Request) config.PriceApiConfig {
return r.Context().Value(priceApiConfigCtxKey).(config.PriceApiConfig)
return r.Context().Value(priceAPIConfigCtxKey).(config.PriceApiConfig)
}
5 changes: 4 additions & 1 deletion internal/service/api/requests/transfer_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func VerifyPermitSignature(r *http.Request, attrs resources.TransferErc20TokenAt
}
recoveredAddr := crypto.PubkeyToAddress(*pubKey)

if bytes.Compare(recoveredAddr.Bytes(), attrs.Sender.Bytes()) != 0 {
if !bytes.Equal(recoveredAddr.Bytes(), attrs.Sender.Bytes()) {
fmt.Println(recoveredAddr.Hex())
fmt.Println(attrs.Sender.Hex())
return errors.New("recovered pubkey is invalid")
Expand Down Expand Up @@ -140,6 +140,9 @@ func buildMessage(r *http.Request, attrs resources.TransferErc20TokenAttributes)
nonce,
attrs.Deadline,
)
if err != nil {
return nil, errors.Wrap(err, "failed to pack permit args")
}

structHash := crypto.Keccak256(packed)

Expand Down

0 comments on commit c98e499

Please sign in to comment.