/
transaction_keeper.go
99 lines (76 loc) · 3.64 KB
/
transaction_keeper.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
// Copyright [2021] - [2022], AssetMantle Pte. Ltd. and the code contributors
// SPDX-License-Identifier: Apache-2.0
package mutate
import (
"context"
"github.com/AssetMantle/schema/go/documents/base"
errorConstants "github.com/AssetMantle/schema/go/errors/constants"
baseLists "github.com/AssetMantle/schema/go/lists/base"
baseQualified "github.com/AssetMantle/schema/go/qualified/base"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/AssetMantle/modules/helpers"
"github.com/AssetMantle/modules/x/assets/key"
"github.com/AssetMantle/modules/x/assets/mappable"
"github.com/AssetMantle/modules/x/assets/record"
"github.com/AssetMantle/modules/x/classifications/auxiliaries/conform"
"github.com/AssetMantle/modules/x/identities/auxiliaries/authenticate"
"github.com/AssetMantle/modules/x/maintainers/auxiliaries/maintain"
)
type transactionKeeper struct {
mapper helpers.Mapper
authenticateAuxiliary helpers.Auxiliary
maintainAuxiliary helpers.Auxiliary
conformAuxiliary helpers.Auxiliary
}
var _ helpers.TransactionKeeper = (*transactionKeeper)(nil)
func (transactionKeeper transactionKeeper) Transact(context context.Context, message helpers.Message) (helpers.TransactionResponse, error) {
return transactionKeeper.Handle(context, message.(*Message))
}
func (transactionKeeper transactionKeeper) Handle(context context.Context, message *Message) (*TransactionResponse, error) {
fromAddress, err := sdkTypes.AccAddressFromBech32(message.From)
if err != nil {
panic("Could not get from address from Bech32 string")
}
if _, err := transactionKeeper.authenticateAuxiliary.GetKeeper().Help(context, authenticate.NewAuxiliaryRequest(fromAddress, message.FromID)); err != nil {
return nil, err
}
assets := transactionKeeper.mapper.NewCollection(context).Fetch(key.NewKey(message.AssetID))
Mappable := assets.GetMappable(key.NewKey(message.AssetID))
if Mappable == nil {
return nil, errorConstants.EntityNotFound.Wrapf("asset with ID %s not found", message.AssetID.AsString())
}
asset := mappable.GetAsset(Mappable)
mutables := baseQualified.NewMutables(message.MutableMetaProperties.Add(baseLists.AnyPropertiesToProperties(message.MutableProperties.Get()...)...))
if _, err := transactionKeeper.maintainAuxiliary.GetKeeper().Help(context, maintain.NewAuxiliaryRequest(asset.GetClassificationID(), message.FromID, mutables)); err != nil {
return nil, err
}
mutatedAsset := base.NewAsset(asset.GetClassificationID(), asset.GetImmutables(), asset.GetMutables().Mutate(baseLists.AnyPropertiesToProperties(mutables.GetMutablePropertyList().Get()...)...))
if _, err := transactionKeeper.conformAuxiliary.GetKeeper().Help(context, conform.NewAuxiliaryRequest(asset.GetClassificationID(), mutatedAsset.GetImmutables(), mutatedAsset.GetMutables())); err != nil {
return nil, err
}
if err := asset.ValidateBasic(); err != nil {
return nil, err
}
assets.Mutate(record.NewRecord(mutatedAsset))
return newTransactionResponse(), nil
}
func (transactionKeeper transactionKeeper) Initialize(mapper helpers.Mapper, _ helpers.ParameterManager, auxiliaries []interface{}) helpers.Keeper {
transactionKeeper.mapper = mapper
for _, auxiliary := range auxiliaries {
switch value := auxiliary.(type) {
case helpers.Auxiliary:
switch value.GetName() {
case conform.Auxiliary.GetName():
transactionKeeper.conformAuxiliary = value
case maintain.Auxiliary.GetName():
transactionKeeper.maintainAuxiliary = value
case authenticate.Auxiliary.GetName():
transactionKeeper.authenticateAuxiliary = value
}
}
}
return transactionKeeper
}
func keeperPrototype() helpers.TransactionKeeper {
return transactionKeeper{}
}