/
voting.go
146 lines (119 loc) · 4.45 KB
/
voting.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/rarimo/rarimo-core/x/rarimocore/crypto/pkg"
"github.com/rarimo/rarimo-core/x/rarimocore/types"
tokentypes "github.com/rarimo/rarimo-core/x/tokenmanager/types"
)
func (k Keeper) CreateVote(ctx sdk.Context, vote types.Vote) (bool, error) {
operation, ok := k.GetOperation(ctx, vote.Index.Operation)
if !ok {
return false, sdkerrors.Wrap(sdkerrors.ErrNotFound, "operation not found")
}
if _, ok := k.GetVote(ctx, vote.Index); ok {
return false, sdkerrors.Wrap(sdkerrors.ErrNotFound, "vote already exists")
}
if operation.Status != types.OpStatus_INITIALIZED {
return false, nil
}
k.SetVote(ctx, vote)
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventTypeVoted,
sdk.NewAttribute(types.AttributeKeyOperationId, operation.Index),
sdk.NewAttribute(types.AttributeKeyOperationType, operation.OperationType.String()),
sdk.NewAttribute(types.AttributeKeyVotingChoice, vote.Vote.String()),
))
return true, nil
}
func (k Keeper) UnapproveOperation(ctx sdk.Context, op types.Operation) error {
op.Status = types.OpStatus_NOT_APPROVED
k.SetOperation(ctx, op)
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventTypeOperationRejected,
sdk.NewAttribute(types.AttributeKeyOperationId, op.Index),
sdk.NewAttribute(types.AttributeKeyOperationType, op.OperationType.String()),
))
return nil
}
func (k Keeper) ApproveOperation(ctx sdk.Context, op types.Operation) error {
switch op.OperationType {
case types.OpType_TRANSFER:
transfer, _ := pkg.GetTransfer(op)
if err := k.ApproveTransferOperation(ctx, transfer); err != nil {
return err
}
default:
// Nothing to do
}
k.ApproveOperationDefault(ctx, op)
return nil
}
func (k Keeper) ApproveOperationDefault(ctx sdk.Context, op types.Operation) {
op.Status = types.OpStatus_APPROVED
k.SetOperation(ctx, op)
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventTypeOperationApproved,
sdk.NewAttribute(types.AttributeKeyOperationId, op.Index),
sdk.NewAttribute(types.AttributeKeyOperationType, op.OperationType.String()),
))
}
func (k Keeper) ApproveTransferOperation(ctx sdk.Context, transfer *types.Transfer) error {
data, ok := k.tm.GetCollectionData(ctx, &tokentypes.CollectionDataIndex{Chain: transfer.From.Chain, Address: transfer.From.Address})
if !ok {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "collection data does not exists")
}
from, ok := k.tm.GetOnChainItem(ctx, &transfer.From)
if !ok {
// Item also does not exist
item := tokentypes.Item{
Index: transfer.Origin,
Collection: data.Collection,
Meta: *transfer.Meta,
OnChain: []*tokentypes.OnChainItemIndex{&transfer.From},
}
k.tm.SetItem(ctx, item)
ctx.EventManager().EmitEvent(sdk.NewEvent(tokentypes.EventTypeItemCreated,
sdk.NewAttribute(tokentypes.AttributeKeyItemIndex, item.Index),
))
from = tokentypes.OnChainItem{
Index: &transfer.From,
Item: item.Index,
}
k.tm.SetOnChainItem(ctx, from)
ctx.EventManager().EmitEvent(sdk.NewEvent(tokentypes.EventTypeOnChainItemCreated,
sdk.NewAttribute(tokentypes.AttributeKeyItemIndex, from.Item),
sdk.NewAttribute(tokentypes.AttributeKeyOnChainItemChain, from.Index.Chain),
))
// Indexing seed and check if already exists. Meta not-nil validated during op creation
if item.Meta.Seed != "" {
if _, ok := k.tm.GetSeed(ctx, item.Meta.Seed); ok {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "seed already exists")
}
k.tm.SetSeed(ctx, tokentypes.Seed{
Seed: item.Meta.Seed,
Item: item.Index,
})
ctx.EventManager().EmitEvent(sdk.NewEvent(tokentypes.EventTypeSeedCreated,
sdk.NewAttribute(tokentypes.AttributeKeyItemIndex, item.Index),
sdk.NewAttribute(tokentypes.AttributeKeySeed, item.Meta.Seed),
))
}
}
to, ok := k.tm.GetOnChainItem(ctx, &transfer.To)
if !ok {
item, ok := k.tm.GetItem(ctx, from.Item)
if !ok {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "item does not exists but 'from' on chain item found")
}
item.OnChain = append(item.OnChain, &transfer.To)
k.tm.SetItem(ctx, item)
to = tokentypes.OnChainItem{
Index: &transfer.To,
Item: item.Index,
}
k.tm.SetOnChainItem(ctx, to)
ctx.EventManager().EmitEvent(sdk.NewEvent(tokentypes.EventTypeOnChainItemCreated,
sdk.NewAttribute(tokentypes.AttributeKeyItemIndex, to.Item),
sdk.NewAttribute(tokentypes.AttributeKeyOnChainItemChain, to.Index.Chain),
))
}
return nil
}