From 58625ffdd6e20f161f93e23dce1e224b308a749d Mon Sep 17 00:00:00 2001 From: Henrique Marlon Date: Sat, 30 Nov 2024 21:06:39 -0300 Subject: [PATCH] feat: cancel order logic (#3) --- cmd/tribes-auth/.gitkeep | 0 cmd/tribes-ramp/.gitkeep | 0 cmd/tribes-rollup/root/root.go | 1 + cmd/tribes-web3-provider/.gitkeep | 0 coverage.md | 96 ++++++++++++++----- internal/domain/entity/crowdfunding.go | 4 + internal/domain/entity/order.go | 1 + .../advance_handler/order_advance_handlers.go | 36 +++++++ .../usecase/order_usecase/cancel_order.go | 71 ++++++++++++++ 9 files changed, 186 insertions(+), 23 deletions(-) delete mode 100644 cmd/tribes-auth/.gitkeep delete mode 100644 cmd/tribes-ramp/.gitkeep delete mode 100644 cmd/tribes-web3-provider/.gitkeep create mode 100644 internal/usecase/order_usecase/cancel_order.go diff --git a/cmd/tribes-auth/.gitkeep b/cmd/tribes-auth/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/cmd/tribes-ramp/.gitkeep b/cmd/tribes-ramp/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/cmd/tribes-rollup/root/root.go b/cmd/tribes-rollup/root/root.go index 3dc0292..e060242 100644 --- a/cmd/tribes-rollup/root/root.go +++ b/cmd/tribes-rollup/root/root.go @@ -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"})) diff --git a/cmd/tribes-web3-provider/.gitkeep b/cmd/tribes-web3-provider/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/coverage.md b/coverage.md index 8f80763..65a045f 100644 --- a/coverage.md +++ b/coverage.md @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/internal/domain/entity/crowdfunding.go b/internal/domain/entity/crowdfunding.go index ad42479..1c91a20 100644 --- a/internal/domain/entity/crowdfunding.go +++ b/internal/domain/entity/crowdfunding.go @@ -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) } diff --git a/internal/domain/entity/order.go b/internal/domain/entity/order.go index 56b9a13..3a6287a 100644 --- a/internal/domain/entity/order.go +++ b/internal/domain/entity/order.go @@ -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" diff --git a/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go b/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go index 8793c71..dc5360b 100644 --- a/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go +++ b/internal/infra/cartesi/handler/advance_handler/order_advance_handlers.go @@ -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 +} diff --git a/internal/usecase/order_usecase/cancel_order.go b/internal/usecase/order_usecase/cancel_order.go new file mode 100644 index 0000000..fb4a270 --- /dev/null +++ b/internal/usecase/order_usecase/cancel_order.go @@ -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 +}