Skip to content

Commit

Permalink
feat: cancel order logic (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemarlon authored Dec 1, 2024
1 parent 9092c3a commit 58625ff
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 23 deletions.
Empty file removed cmd/tribes-auth/.gitkeep
Empty file.
Empty file removed cmd/tribes-ramp/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions cmd/tribes-rollup/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func NewDApp(ah *AdvanceHandlers, ih *InspectHandlers, ms *Middlewares) *router.
r.HandleAdvance("deleteContract", ms.RBAC.Middleware(ah.ContractAdvanceHandlers.DeleteContractHandler, []string{"admin"}))

r.HandleAdvance("createOrder", ms.RBAC.Middleware(ah.OrderAdvanceHandlers.CreateOrderHandler, []string{"non_qualified_investor", "qualified_investor"}))
r.HandleAdvance("cancelOrder", ms.RBAC.Middleware(ah.OrderAdvanceHandlers.CancelOrderHandler, []string{"non_qualified_investor", "qualified_investor"}))

r.HandleAdvance("createCrowdfunding", ms.TLSN.Middleware(ah.CrowdfundingAdvanceHandlers.CreateCrowdfundingHandler))
r.HandleAdvance("deleteCrowdfunding", ms.RBAC.Middleware(ah.CrowdfundingAdvanceHandlers.DeleteCrowdfundingHandler, []string{"admin"}))
Expand Down
Empty file removed cmd/tribes-web3-provider/.gitkeep
Empty file.
96 changes: 73 additions & 23 deletions coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ github.com/tribeshq/tribes/cmd/tribes-rollup/root/root.go:100.16,102.49 2 0
github.com/tribeshq/tribes/cmd/tribes-rollup/root/root.go:103.21,103.21 0 0
github.com/tribeshq/tribes/cmd/tribes-rollup/root/root.go:107.2,107.54 1 0
github.com/tribeshq/tribes/cmd/tribes-rollup/root/root.go:107.54,110.3 2 0
github.com/tribeshq/tribes/cmd/tribes-rollup/root/root.go:113.88,151.2 28 0
github.com/tribeshq/tribes/cmd/tribes-rollup/root/root.go:113.88,152.2 29 0
github.com/tribeshq/tribes/cmd/tribes-rollup/root/wire_gen.go:21.60,30.2 5 0
github.com/tribeshq/tribes/cmd/tribes-rollup/root/wire_gen.go:32.68,48.2 10 0
github.com/tribeshq/tribes/cmd/tribes-rollup/root/wire_gen.go:50.68,66.2 10 0
Expand Down Expand Up @@ -62,27 +62,27 @@ github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:81.2,81.21 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:81.21,83.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:84.2,84.39 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:84.39,86.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:87.2,87.22 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:87.22,89.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:90.2,90.31 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:90.31,92.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:93.2,93.23 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:93.23,95.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:96.2,96.12 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:49.142,58.41 2 0
github.com/tribeshq/tribes/internal/domain/entity/order.go:58.41,60.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/order.go:61.2,61.19 1 0
github.com/tribeshq/tribes/internal/domain/entity/order.go:64.34,65.27 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:65.27,67.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:68.2,68.38 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:68.38,70.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:71.2,71.26 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:71.26,73.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:74.2,74.32 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:74.32,76.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:77.2,77.22 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:77.22,79.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:80.2,80.12 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:91.2,91.22 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:91.22,93.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:94.2,94.31 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:94.31,96.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:97.2,97.23 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:97.23,99.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:100.2,100.12 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:50.142,59.41 2 0
github.com/tribeshq/tribes/internal/domain/entity/order.go:59.41,61.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/order.go:62.2,62.19 1 0
github.com/tribeshq/tribes/internal/domain/entity/order.go:65.34,66.27 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:66.27,68.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:69.2,69.38 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:69.38,71.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:72.2,72.26 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:72.26,74.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:75.2,75.32 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:75.32,77.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:78.2,78.22 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:78.22,80.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:81.2,81.12 1 1
github.com/tribeshq/tribes/internal/domain/entity/user.go:45.84,53.40 2 1
github.com/tribeshq/tribes/internal/domain/entity/user.go:53.40,55.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/user.go:56.2,56.18 1 1
Expand Down Expand Up @@ -190,6 +190,19 @@ github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:53.2,54.16 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:54.16,56.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:57.2,58.12 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:61.154,63.56 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:63.56,65.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:66.2,69.16 4 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:69.16,71.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:73.2,74.12 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:74.12,76.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:77.2,78.16 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:78.16,80.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:81.2,86.16 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:86.16,88.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:89.2,90.16 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:90.16,92.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:93.2,94.12 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/user_advance_handler.go:18.134,23.2 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/user_advance_handler.go:25.152,27.56 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/user_advance_handler.go:27.56,29.3 1 0
Expand Down Expand Up @@ -654,6 +667,18 @@ github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/update_crowdfun
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/update_crowdfunding.go:46.170,57.16 2 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/update_crowdfunding.go:57.16,59.3 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/update_crowdfunding.go:60.2,72.8 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:34.180,40.2 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:42.149,44.16 2 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:44.16,46.3 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:47.2,47.42 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:47.42,49.3 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:50.2,51.16 2 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:51.16,53.3 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:54.2,54.53 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:54.53,56.3 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:57.2,58.16 2 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:58.16,60.3 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:61.2,70.8 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:35.226,42.2 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:44.178,46.9 2 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:46.9,48.3 1 0
Expand Down Expand Up @@ -1103,7 +1128,7 @@ github.com/tribeshq/tribes/cmd/tribes-rollup/root/root.go:100.16,102.49 2 0
github.com/tribeshq/tribes/cmd/tribes-rollup/root/root.go:103.21,103.21 0 0
github.com/tribeshq/tribes/cmd/tribes-rollup/root/root.go:107.2,107.54 1 0
github.com/tribeshq/tribes/cmd/tribes-rollup/root/root.go:107.54,110.3 2 0
github.com/tribeshq/tribes/cmd/tribes-rollup/root/root.go:113.88,151.2 28 1
github.com/tribeshq/tribes/cmd/tribes-rollup/root/root.go:113.88,152.2 29 1
github.com/tribeshq/tribes/cmd/tribes-rollup/root/wire_gen.go:21.60,30.2 5 1
github.com/tribeshq/tribes/cmd/tribes-rollup/root/wire_gen.go:32.68,48.2 10 1
github.com/tribeshq/tribes/cmd/tribes-rollup/root/wire_gen.go:50.68,66.2 10 1
Expand Down Expand Up @@ -1207,6 +1232,19 @@ github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:53.2,54.16 2 1
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:54.16,56.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:57.2,58.12 2 1
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:61.154,63.56 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:63.56,65.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:66.2,69.16 4 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:69.16,71.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:73.2,74.12 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:74.12,76.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:77.2,78.16 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:78.16,80.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:81.2,86.16 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:86.16,88.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:89.2,90.16 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:90.16,92.3 1 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go:93.2,94.12 2 0
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/user_advance_handler.go:18.134,23.2 1 1
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/user_advance_handler.go:25.152,27.56 2 1
github.com/tribeshq/tribes/internal/infra/cartesi/handler/advance_handler/user_advance_handler.go:27.56,29.3 1 0
Expand Down Expand Up @@ -1671,6 +1709,18 @@ github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/update_crowdfun
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/update_crowdfunding.go:46.170,57.16 2 1
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/update_crowdfunding.go:57.16,59.3 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/update_crowdfunding.go:60.2,72.8 1 1
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:34.180,40.2 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:42.149,44.16 2 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:44.16,46.3 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:47.2,47.42 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:47.42,49.3 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:50.2,51.16 2 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:51.16,53.3 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:54.2,54.53 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:54.53,56.3 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:57.2,58.16 2 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:58.16,60.3 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/cancel_order.go:61.2,70.8 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:35.226,42.2 1 1
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:44.178,46.9 2 1
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:46.9,48.3 1 0
Expand Down
4 changes: 4 additions & 0 deletions internal/domain/entity/crowdfunding.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (a *Crowdfunding) Validate() error {
if a.ClosesAt > a.CreatedAt+15552000 {
return fmt.Errorf("%w: expiration date cannot be greater than 6 months", ErrInvalidCrowdfunding)
}
// TODO: Add this when in production
// if a.ClosesAt < 604800 {
// return fmt.Errorf("%w: cannot close crowdfunding campaign in less than 7 days", ErrInvalidCrowdfunding)
// }
if a.CreatedAt == 0 {
return fmt.Errorf("%w: creation date is missing", ErrInvalidCrowdfunding)
}
Expand Down
1 change: 1 addition & 0 deletions internal/domain/entity/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type OrderState string
const (
OrderStatePending OrderState = "pending"
OrderStateAccepted OrderState = "accepted"
OrderCancelled OrderState = "cancelled"
OrderStatePartiallyAccepted OrderState = "partially_accepted"
OrderStateRejected OrderState = "rejected"
OrderStateSettled OrderState = "settled"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,39 @@ func (h *OrderAdvanceHandlers) CreateOrderHandler(env rollmelette.Env, metadata
env.Notice(append([]byte("order created - "), order...))
return nil
}

func (h *OrderAdvanceHandlers) CancelOrderHandler(env rollmelette.Env, metadata rollmelette.Metadata, deposit rollmelette.Deposit, payload []byte) error {
var input order_usecase.CancelOrderInputDTO
if err := json.Unmarshal(payload, &input); err != nil {
return err
}
ctx := context.Background()
cancelOrder := order_usecase.NewCancelOrderUseCase(h.UserRepository, h.OrderRepository, h.CrowdfundingRepository)
res, err := cancelOrder.Execute(ctx, &input, metadata)
if err != nil {
return err
}
// TODO: remove this check when update to V2
appAddress, isSet := env.AppAddress()
if !isSet {
return fmt.Errorf("no application address defined yet, contact the Tribes support")
}
contract, err := h.ContractRepository.FindContractBySymbol(ctx, "STABLECOIN")
if err != nil {
return err
}
if err := env.ERC20Transfer(
contract.Address,
appAddress,
metadata.MsgSender,
res.Amount.ToBig(),
); err != nil {
return err
}
order, err := json.Marshal(res)
if err != nil {
return err
}
env.Notice(append([]byte("order canceled - "), order...))
return nil
}
71 changes: 71 additions & 0 deletions internal/usecase/order_usecase/cancel_order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package order_usecase

import (
"context"
"errors"

"github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
"github.com/rollmelette/rollmelette"
"github.com/tribeshq/tribes/internal/domain/entity"
)

type CancelOrderInputDTO struct {
Id uint
}

type CancelOrderOutputDTO struct {
Id uint
CrowdfundingId uint
Investor common.Address
Amount *uint256.Int
InterestRate *uint256.Int
State string
CreatedAt int64
UpdatedAt int64
}

type CancelOrderUseCase struct {
UserRepository entity.UserRepository
OrderRepository entity.OrderRepository
CrowdfundingRepository entity.CrowdfundingRepository
}

func NewCancelOrderUseCase(userRepository entity.UserRepository, orderRepository entity.OrderRepository, crowdfundingRepository entity.CrowdfundingRepository) *CancelOrderUseCase {
return &CancelOrderUseCase{
UserRepository: userRepository,
OrderRepository: orderRepository,
CrowdfundingRepository: crowdfundingRepository,
}
}

func (c *CancelOrderUseCase) Execute(ctx context.Context, input *CancelOrderInputDTO, metadata rollmelette.Metadata) (*CancelOrderOutputDTO, error) {
order, err := c.OrderRepository.FindOrderById(ctx, input.Id)
if err != nil {
return nil, err
}
if order.Investor != metadata.MsgSender {
return nil, errors.New("only the investor can cancel the order")
}
crowdfunding, err := c.CrowdfundingRepository.FindCrowdfundingById(ctx, order.CrowdfundingId)
if err != nil {
return nil, err
}
if crowdfunding.ClosesAt < metadata.BlockTimestamp {
return nil, errors.New("cannot cancel order after crowdfunding closes")
}
err = c.OrderRepository.DeleteOrder(ctx, input.Id)
if err != nil {
return nil, err
}
return &CancelOrderOutputDTO{
Id: order.Id,
CrowdfundingId: order.CrowdfundingId,
Investor: order.Investor,
Amount: order.Amount,
InterestRate: order.InterestRate,
State: string(order.State),
CreatedAt: order.CreatedAt,
UpdatedAt: order.UpdatedAt,
}, nil
}

0 comments on commit 58625ff

Please sign in to comment.