forked from ChainSafe/ChainBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
84 lines (69 loc) · 2.35 KB
/
events.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
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only
package substrate
import (
"fmt"
"math/big"
events "github.com/PolkaFoundry/chainbridge-substrate-events"
"github.com/PolkaFoundry/chainbridge-utils/msg"
"github.com/ChainSafe/log15"
)
type eventName string
type eventHandler func(interface{}, log15.Logger) (msg.Message, error)
const FungibleTransfer eventName = "FungibleTransfer"
const NonFungibleTransfer eventName = "NonFungibleTransfer"
const GenericTransfer eventName = "GenericTransfer"
var Subscriptions = []struct {
name eventName
handler eventHandler
}{
{FungibleTransfer, fungibleTransferHandler},
{NonFungibleTransfer, nonFungibleTransferHandler},
{GenericTransfer, genericTransferHandler},
}
func fungibleTransferHandler(evtI interface{}, log log15.Logger) (msg.Message, error) {
evt, ok := evtI.(events.EventFungibleTransfer)
if !ok {
return msg.Message{}, fmt.Errorf("failed to cast EventFungibleTransfer type")
}
resourceId := msg.ResourceId(evt.ResourceId)
log.Info("Got fungible transfer event!", "destination", evt.Destination, "resourceId", resourceId.Hex(), "amount", evt.Amount)
return msg.NewFungibleTransfer(
0, // Unset
msg.ChainId(evt.Destination),
msg.Nonce(evt.DepositNonce),
evt.Amount.Int,
resourceId,
evt.Recipient,
), nil
}
func nonFungibleTransferHandler(evtI interface{}, log log15.Logger) (msg.Message, error) {
evt, ok := evtI.(events.EventNonFungibleTransfer)
if !ok {
return msg.Message{}, fmt.Errorf("failed to cast EventNonFungibleTransfer type")
}
log.Info("Got non-fungible transfer event!", "destination", evt.Destination, "resourceId", evt.ResourceId)
return msg.NewNonFungibleTransfer(
0, // Unset
msg.ChainId(evt.Destination),
msg.Nonce(evt.DepositNonce),
msg.ResourceId(evt.ResourceId),
big.NewInt(0).SetBytes(evt.TokenId[:]),
evt.Recipient,
evt.Metadata,
), nil
}
func genericTransferHandler(evtI interface{}, log log15.Logger) (msg.Message, error) {
evt, ok := evtI.(events.EventGenericTransfer)
if !ok {
return msg.Message{}, fmt.Errorf("failed to cast EventGenericTransfer type")
}
log.Info("Got generic transfer event!", "destination", evt.Destination, "resourceId", evt.ResourceId)
return msg.NewGenericTransfer(
0, // Unset
msg.ChainId(evt.Destination),
msg.Nonce(evt.DepositNonce),
msg.ResourceId(evt.ResourceId),
evt.Metadata,
), nil
}