-
Notifications
You must be signed in to change notification settings - Fork 2
/
msg_server_order_delivered.go
executable file
·147 lines (119 loc) · 5.09 KB
/
msg_server_order_delivered.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package keeper
import (
"context"
"strconv"
"time"
"github.com/Harry-027/deal/x/deal/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
func (k msgServer) OrderDelivered(goCtx context.Context, msg *types.MsgOrderDelivered) (*types.MsgOrderDeliveredResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
logger := k.Logger(ctx)
deal, found := k.Keeper.GetNewDeal(ctx, msg.DealId)
if !found {
return nil, types.ErrDealNotFound
}
// validate before processing the message
contract, found := k.Keeper.GetNewContract(ctx, msg.DealId, msg.ContractId)
if !found {
return nil, types.ErrContractNotFound
}
if msg.Creator != contract.Consumer {
return nil, types.ErrInvalidConsumer
}
if contract.Status != types.INDELIVERY || contract.Status == types.COMPLETED {
return nil, types.ErrNotShipped
}
startTime, err := time.Parse(types.TIME_FORMAT, contract.StartTime)
if err != nil {
panic("invalid start time")
}
deliveryExpectedTime := startTime.Add(time.Duration(contract.OwnerETA))
deliveryActualTime := ctx.BlockTime()
logger.Debug("deliveryExpectedTime :: ", deliveryExpectedTime)
logger.Debug("deliveryActualTime :: ", deliveryActualTime)
// calculate the delivery delay in case if any
if deliveryActualTime.After(deliveryExpectedTime) {
deliveryTimeDelay := deliveryActualTime.Sub(deliveryExpectedTime).Minutes()
logger.Debug("deliveryTimeDelay :: ", deliveryTimeDelay)
if contract.ShippingDelay != 0 {
// subtract the shipping delay if any
deliveryTimeDelay = deliveryTimeDelay - float64(contract.ShippingDelay)
logger.Debug("deliveryTimeDelay after subtracting shipping delay", deliveryTimeDelay)
}
contract.DeliveryDelay = uint32(deliveryTimeDelay)
}
timeTaken := deliveryActualTime.Sub(startTime).Minutes()
logger.Debug("timeTaken :: ", timeTaken)
var refundAmount float64 = 0
orderPayment := float64(contract.Fees)
// calculate the penalty for late delivery/shipping for owner & vendor respectively
if timeTaken != 0 {
vendorSlashPercent := float64(contract.ShippingDelay) / timeTaken
logger.Debug("vendorSlashPercent :: ", vendorSlashPercent)
ownerSlashPercent := float64(contract.DeliveryDelay) / timeTaken
logger.Debug("ownerSlashPercent :: ", ownerSlashPercent)
refundAmount = (vendorSlashPercent + ownerSlashPercent) * orderPayment * 0.01
logger.Debug("refundAmount :: ", refundAmount)
}
//deduct delay charges from payment
totalPay := uint64(orderPayment - refundAmount)
logger.Debug("TotalPay :: ", totalPay)
moduleAccount := k.auth.GetModuleAddress(types.ModuleName)
moduleBalance := k.bank.GetBalance(ctx, moduleAccount, types.TOKEN)
if moduleBalance.IsLT(contract.GetCoin(totalPay)) {
panic("Escrow account insufficient balance")
}
// calculate the vendor payment for given order based on deal commission
vendorPay := uint64(0.01 * float64(deal.Commission*totalPay))
logger.Debug("vendorPay :: ", vendorPay)
// calculate the owner payment
ownerPay := totalPay - vendorPay
logger.Debug("ownerPay :: ", ownerPay)
// validate the addresses for different parties involved in the contract
consumerAddress, err := contract.GetConsumerAddress()
if err != nil {
panic("Invalid consumer address")
}
ownerAddress, err := deal.GetOwnerAddress()
if err != nil {
panic("Invalid owner address")
}
vendorAddress, err := deal.GetVendorAddress()
if err != nil {
panic("Invalid vendor address")
}
// process the payment for all the parties
// refund the delay charges to consumer
err = k.bank.SendCoinsFromModuleToAccount(ctx, types.ModuleName, consumerAddress, sdk.NewCoins(contract.GetCoin(uint64(refundAmount))))
if err != nil {
return nil, sdkerrors.Wrapf(err, types.ErrPaymentFailed.Error())
}
err = k.bank.SendCoinsFromModuleToAccount(ctx, types.ModuleName, ownerAddress, sdk.NewCoins(contract.GetCoin(ownerPay)))
if err != nil {
return nil, sdkerrors.Wrapf(err, types.ErrPaymentFailed.Error())
}
err = k.bank.SendCoinsFromModuleToAccount(ctx, types.ModuleName, vendorAddress, sdk.NewCoins(contract.GetCoin(vendorPay)))
if err != nil {
return nil, sdkerrors.Wrapf(err, types.ErrPaymentFailed.Error())
}
// mark the contract status as completed as order has been delivered to consumer and settlement is complete
contract.Status = types.COMPLETED
k.Keeper.SetNewContract(ctx, contract)
ctx.GasMeter().ConsumeGas(types.SETTLEMENT_GAS, "Order delivered")
ctx.EventManager().EmitEvent(
sdk.NewEvent(sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeyAction, types.COMPLETED),
sdk.NewAttribute(types.IDVALUE, contract.ContractId),
sdk.NewAttribute(types.CONSUMER, contract.Consumer),
sdk.NewAttribute(types.OWNER, deal.Owner),
sdk.NewAttribute(types.VENDOR, deal.Vendor),
sdk.NewAttribute(types.REFUND_PAY, strconv.FormatUint(uint64(refundAmount), 10)),
sdk.NewAttribute(types.OWNER_PAY, strconv.FormatUint(ownerPay, 10)),
sdk.NewAttribute(types.VENDOR_PAY, strconv.FormatUint(vendorPay, 10)),
),
)
return &types.MsgOrderDeliveredResponse{IdValue: contract.ContractId, ContractStatus: contract.Status}, nil
}