-
Notifications
You must be signed in to change notification settings - Fork 2
/
msg_server_commit_contract.go
executable file
·68 lines (55 loc) · 1.92 KB
/
msg_server_commit_contract.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
package keeper
import (
"context"
"strconv"
"time"
"github.com/Harry-027/deal/x/deal/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// CommitContract is the tx handler to handle commit contract messages
func (k msgServer) CommitContract(goCtx context.Context, msg *types.MsgCommitContract) (*types.MsgCommitContractResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
deal, found := k.Keeper.GetNewDeal(ctx, msg.DealId)
if !found {
return nil, types.ErrDealNotFound
}
// validate is the transaction is coming from vendor
if msg.Creator != deal.Vendor {
return nil, types.ErrInvalidVendor
}
contract, found := k.Keeper.GetNewContract(ctx, msg.DealId, msg.ContractId)
if !found {
return nil, types.ErrContractNotFound
}
expiry, err := time.Parse(types.TIME_FORMAT, contract.Expiry)
if err != nil {
panic("invalid expiry time")
}
// don't process the expired contracts
if ctx.BlockTime().After(expiry) {
return nil, types.ErrContractExpired
}
etaInMins, err := strconv.Atoi(msg.VendorETA)
if err != nil {
return nil, types.ErrInvalidTime
}
// validate the vendor ETA
if (contract.OwnerETA / 2) < uint32(etaInMins) {
return nil, types.ErrVendorETA
}
// process and emit the custom event
contract.Status = types.COMMITTED
contract.VendorETA = uint32(etaInMins)
k.Keeper.SetNewContract(ctx, contract)
ctx.GasMeter().ConsumeGas(types.PROCESS_GAS, "Commit Contract")
ctx.EventManager().EmitEvent(
sdk.NewEvent(sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeyAction, types.COMMITTED),
sdk.NewAttribute(types.IDVALUE, contract.ContractId),
sdk.NewAttribute(types.VENDOR_ETA, strconv.FormatUint(uint64(contract.VendorETA), 10)),
sdk.NewAttribute(types.OWNER_ETA, strconv.FormatUint(uint64(contract.OwnerETA), 10)),
),
)
return &types.MsgCommitContractResponse{IdValue: contract.ContractId, ContractStatus: contract.Status}, nil
}