/
msg_server_chain_link.go
90 lines (71 loc) · 2.97 KB
/
msg_server_chain_link.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
package keeper
import (
"context"
"time"
"cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/desmos/v7/x/profiles/types"
)
// LinkChainAccount defines a rpc method for MsgLinkChainAccount
func (k MsgServer) LinkChainAccount(goCtx context.Context, msg *types.MsgLinkChainAccount) (*types.MsgLinkChainAccountResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
srcAddrData, err := types.UnpackAddressData(k.cdc, msg.ChainAddress)
if err != nil {
return nil, err
}
link := types.NewChainLink(msg.Signer, srcAddrData, msg.Proof, msg.ChainConfig, ctx.BlockTime())
err = k.SaveChainLink(ctx, link)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeCreatedChainLink,
sdk.NewAttribute(types.AttributeKeyChainLinkExternalAddress, srcAddrData.GetValue()),
sdk.NewAttribute(types.AttributeKeyChainLinkChainName, msg.ChainConfig.Name),
sdk.NewAttribute(types.AttributeKeyChainLinkOwner, msg.Signer),
sdk.NewAttribute(types.AttributeKeyChainLinkCreationTime, link.CreationTime.Format(time.RFC3339Nano)),
),
})
return &types.MsgLinkChainAccountResponse{}, nil
}
// UnlinkChainAccount defines a rpc method for MsgUnlinkChainAccount
func (k MsgServer) UnlinkChainAccount(goCtx context.Context, msg *types.MsgUnlinkChainAccount) (*types.MsgUnlinkChainAccountResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Get the chain link
link, found := k.GetChainLink(ctx, msg.Owner, msg.ChainName, msg.Target)
if !found {
return nil, errors.Wrap(sdkerrors.ErrNotFound, "chain link not found")
}
// Delete the link
k.DeleteChainLink(ctx, link)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeDeletedChainLink,
sdk.NewAttribute(types.AttributeKeyChainLinkExternalAddress, msg.Target),
sdk.NewAttribute(types.AttributeKeyChainLinkChainName, msg.ChainName),
sdk.NewAttribute(types.AttributeKeyChainLinkOwner, msg.Owner),
),
})
return &types.MsgUnlinkChainAccountResponse{}, nil
}
// SetDefaultExternalAddress defines a rpc method for MsgSetDefaultExternalAddress
func (k MsgServer) SetDefaultExternalAddress(goCtx context.Context, msg *types.MsgSetDefaultExternalAddress) (*types.MsgSetDefaultExternalAddressResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Get the chain link
_, found := k.GetChainLink(ctx, msg.Signer, msg.ChainName, msg.Target)
if !found {
return nil, errors.Wrap(sdkerrors.ErrNotFound, "chain link not found")
}
k.SaveDefaultExternalAddress(ctx, msg.Signer, msg.ChainName, msg.Target)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeSetDefaultExternalAddress,
sdk.NewAttribute(types.AttributeKeyChainLinkChainName, msg.ChainName),
sdk.NewAttribute(types.AttributeKeyChainLinkExternalAddress, msg.Target),
sdk.NewAttribute(types.AttributeKeyChainLinkOwner, msg.Signer),
),
})
return &types.MsgSetDefaultExternalAddressResponse{}, nil
}