Skip to content

Commit

Permalink
Add disabled field in withdrawal config. Fix reopener.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaptoss committed May 22, 2024
1 parent f4bc100 commit 0b49dff
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Config interface {
evtypes.EventTypeser
sbtcheck.SbtChecker

PointPrice() int64
PointPrice() PointsPrice
}

type config struct {
Expand Down
15 changes: 9 additions & 6 deletions internal/config/point_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"gitlab.com/distributed_lab/kit/kv"
)

func (c *config) PointPrice() int64 {
type PointsPrice struct {
PointPriceURMO int64 `fig:"point_price_urmo,required"`
Disabled bool `fig:"disabled"`
}

func (c *config) PointPrice() PointsPrice {
return c.pointPrice.Do(func() interface{} {
var cfg struct {
PointPriceURMO int64 `fig:"point_price_urmo,required"`
}
var cfg PointsPrice

err := figure.Out(&cfg).
From(kv.MustGetStringMap(c.getter, "withdrawal")).
Expand All @@ -20,6 +23,6 @@ func (c *config) PointPrice() int64 {
panic(fmt.Errorf("failed to figure out withdrawal point price: %w", err))
}

return cfg.PointPriceURMO
}).(int64)
return cfg
}).(PointsPrice)
}
2 changes: 1 addition & 1 deletion internal/data/pg/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (q *balances) GetWithRank(nullifier string) (*data.Balance, error) {
SELECT b1.*, COALESCE(b2.rank, 0) AS rank FROM %s AS b1
LEFT JOIN (SELECT nullifier, ROW_NUMBER() OVER (ORDER BY amount DESC, updated_at DESC) AS rank FROM %s WHERE referred_by IS NOT NULL) AS b2
ON b1.nullifier = b2.nullifier
WHERE b1.nullifier = ?;
WHERE b1.nullifier = ?
`, balancesTable, balancesTable)

if err := q.db.GetRaw(&res, stmt, nullifier); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions internal/service/handlers/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/rarimo/decentralized-auth-svc/resources"
"github.com/rarimo/rarime-points-svc/internal/config"
"github.com/rarimo/rarime-points-svc/internal/data"
"github.com/rarimo/rarime-points-svc/internal/data/evtypes"
"github.com/rarimo/saver-grpc-lib/broadcaster"
Expand Down Expand Up @@ -105,12 +106,12 @@ func Broadcaster(r *http.Request) broadcaster.Broadcaster {
return r.Context().Value(broadcasterCtxKey).(broadcaster.Broadcaster)
}

func CtxPointPrice(price int64) func(context.Context) context.Context {
func CtxPointPrice(price config.PointsPrice) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, pointPriceCtxKey, price)
}
}

func PointPrice(r *http.Request) int64 {
return r.Context().Value(pointPriceCtxKey).(int64)
func PointPrice(r *http.Request) config.PointsPrice {
return r.Context().Value(pointPriceCtxKey).(config.PointsPrice)
}
2 changes: 1 addition & 1 deletion internal/service/handlers/point_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func GetPointPrice(w http.ResponseWriter, r *http.Request) {
Type: resources.POINT_PRICE,
},
Attributes: resources.PointPriceAttributes{
Urmo: PointPrice(r),
Urmo: PointPrice(r).PointPriceURMO,
},
},
})
Expand Down
8 changes: 7 additions & 1 deletion internal/service/handlers/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import (
)

func Withdraw(w http.ResponseWriter, r *http.Request) {

if PointPrice(r).Disabled {
w.WriteHeader(http.StatusNoContent)
return
}

req, err := requests.NewWithdraw(r)
if err != nil {
ape.RenderErr(w, problems.BadRequest(err)...)
Expand Down Expand Up @@ -131,7 +137,7 @@ func isEligibleToWithdraw(balance *data.Balance, amount int64) error {
}

func broadcastWithdrawalTx(req resources.WithdrawRequest, r *http.Request) error {
urmo := req.Data.Attributes.Amount * PointPrice(r)
urmo := req.Data.Attributes.Amount * PointPrice(r).PointPriceURMO
tx := &bank.MsgSend{
FromAddress: Broadcaster(r).Sender(),
ToAddress: req.Data.Attributes.Address,
Expand Down
4 changes: 4 additions & 0 deletions internal/service/workers/reopener/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (c *initCollector) collect() ([]data.ReopenableEvent, error) {
func (c *initCollector) selectReopenable(freq evtypes.Frequency, before int64) ([]data.ReopenableEvent, error) {
types := c.types.Names(evtypes.FilterByFrequency(freq), evtypes.FilterInactive)

if len(types) == 0 {
return []data.ReopenableEvent{}, nil
}

res, err := c.q.New().FilterByType(types...).
FilterByUpdatedAtBefore(before).
SelectReopenable()
Expand Down

0 comments on commit 0b49dff

Please sign in to comment.