Skip to content

Commit

Permalink
Create order
Browse files Browse the repository at this point in the history
  • Loading branch information
kikakkz committed Sep 4, 2023
1 parent b625050 commit af0e698
Showing 1 changed file with 12 additions and 65 deletions.
77 changes: 12 additions & 65 deletions pkg/order/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,9 @@ type createHandler struct {
mainPaymentType *ordertypes.PaymentType

paymentCoinName string
paymentAddress string
paymentAddressStartAmount decimal.Decimal
paymentID string
paymentAccountID string

ledger *ledgermwpb.Ledger
paymentAccount *payaccmwpb.Account
ledger *ledgermwpb.Ledger
}

func tomorrowStart() time.Time {
Expand Down Expand Up @@ -452,7 +449,6 @@ func (h *createHandler) peekAddress(ctx context.Context) (*payaccmwpb.Account, e
for _, payment := range payments {
info, err := payaccmwcli.GetAccount(ctx, payment.ID)
if err != nil {
logger.Sugar().Infow("peekAddress", "payment", payment.Address, "ID", payment.ID, "error", err)
return nil, err
}

Expand All @@ -463,11 +459,6 @@ func (h *createHandler) peekAddress(ctx context.Context) (*payaccmwpb.Account, e
if info.AvailableAt > uint32(time.Now().Unix()) {
continue
}
if err := accountlock.Lock(payment.AccountID); err != nil {
logger.Sugar().Infow("peekAddress", "payment", payment.Address, "ID", payment.ID, "error", err)
continue
}

account = info
break
}
Expand Down Expand Up @@ -560,8 +551,8 @@ func (h *createHandler) withUpdateStock(dispose *dtmcli.SagaDispose) {
}
dispose.Add(
ordermwsvcname.ServiceDomain,
"good.middleware.app.good1.stock.v1.Middleware/SubStock",
"good.middleware.app.good1.stock.v1.Middleware/AddStock",
"good.middleware.app.good1.stock.v1.Middleware/SubStock",
&appgoodstockmwpb.AddStockRequest{
Info: req,
},
Expand All @@ -574,11 +565,9 @@ func (h *createHandler) withUpdateBalance(dispose *dtmcli.SagaDispose) {
return
}
req := &ledgermwpb.LedgerReq{
ID: &h.ledger.ID,
AppID: h.AppID,
UserID: h.UserID,
CoinTypeID: &h.ledger.CoinTypeID,
Locked: h.BalanceAmount,
Spendable: h.BalanceAmount,
}
dispose.Add(
Expand All @@ -591,47 +580,7 @@ func (h *createHandler) withUpdateBalance(dispose *dtmcli.SagaDispose) {
)
}

func (h *createHandler) CheckLedgerBalance(ctx context.Context) error {
if h.BalanceAmount == nil {
return nil
}

ba, err := decimal.NewFromString(*h.BalanceAmount)
if err != nil {
return err
}

if ba.Cmp(decimal.NewFromInt(0)) <= 0 {
return nil
}

general, err := ledgermwcli.GetLedgerOnly(ctx, &ledgermwpb.Conds{
AppID: &basetypes.StringVal{Op: cruder.EQ, Value: *h.AppID},
UserID: &basetypes.StringVal{Op: cruder.EQ, Value: *h.UserID},
CoinTypeID: &basetypes.StringVal{Op: cruder.EQ, Value: *h.PaymentCoinID},
})
if err != nil {
return err
}
if general == nil {
return fmt.Errorf("insufficient balance")
}

spendable, err := decimal.NewFromString(general.Spendable)
if err != nil {
return err
}

if spendable.Cmp(ba) < 0 {
return fmt.Errorf("insufficient balance")
}

h.ledger = general

return nil
}

func (h *createHandler) setCreateReqs() []*ordermwpb.OrderReq {
func (h *createHandler) orderReqs() []*ordermwpb.OrderReq {
paymentAmount := h.paymentAmountCoin.String()
startAmount := h.paymentAddressStartAmount.String()
paymentTransferAmount := h.paymentTransferAmount.String()
Expand Down Expand Up @@ -782,8 +731,14 @@ func (h *Handler) CreateOrder(ctx context.Context) (info *npool.Order, err error
case ordertypes.PaymentType_PayWithTransferAndBalance:
fallthrough //nolint
case ordertypes.PaymentType_PayWithOffline:
if err := handler.PeekAddress(ctx); err != nil {
return nil, err
for i := 0; i < 5; i++ {
if err := handler.PeekAddress(ctx); err != nil {
return nil, err
}
if err := accountlock.Lock(payment.AccountID); err != nil {

Check failure on line 738 in pkg/order/create.go

View workflow job for this annotation

GitHub Actions / test-cache

undefined: payment

Check failure on line 738 in pkg/order/create.go

View workflow job for this annotation

GitHub Actions / test (1.17.x, ubuntu-latest)

undefined: payment

Check failure on line 738 in pkg/order/create.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

undefined: payment
continue
}
break
}
defer func() {
accountlock.Unlock(handler.paymentAccountID) //nolint

Check failure on line 744 in pkg/order/create.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

handler.paymentAccountID undefined (type *createHandler has no field or method paymentAccountID)
Expand All @@ -793,10 +748,6 @@ func (h *Handler) CreateOrder(ctx context.Context) (info *npool.Order, err error
}
}

if err := handler.CheckLedgerBalance(ctx); err != nil {
return nil, err
}

createReqs := handler.setCreateReqs()

sagaDispose := dtmcli.NewSagaDispose(dtmimp.TransOptions{
Expand Down Expand Up @@ -879,10 +830,6 @@ func (h *Handler) CreateOrders(ctx context.Context) (infos []*npool.Order, err e
}
}

if err := handler.CheckLedgerBalance(ctx); err != nil {
return nil, err
}

createReqs := handler.setCreateReqs()

sagaDispose := dtmcli.NewSagaDispose(dtmimp.TransOptions{
Expand Down

0 comments on commit af0e698

Please sign in to comment.