-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
56 lines (44 loc) · 1.66 KB
/
parser.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
package txs
// --------------------------------------------------------
// Parser
//
// Parses structs containing event information into
// unsigned transactions for validators to sign, then
// relays the data packets as transactions on the
// Cosmos Bridge.
// --------------------------------------------------------
import (
"errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/peggy/cmd/ebrelayer/events"
ethbridgeTypes "github.com/cosmos/peggy/x/ethbridge/types"
)
// ETH : ETH constant specifies a token type of Ethereum
const (
ETH string = "eth"
)
// ParsePayload : parses and packages a LockEvent struct with a validator address in an EthBridgeClaim msg
func ParsePayload(valAddr sdk.ValAddress, event *events.LockEvent) (ethbridgeTypes.EthBridgeClaim, error) {
witnessClaim := ethbridgeTypes.EthBridgeClaim{}
// Nonce type casting (*big.Int -> int)
nonce := int(event.Nonce.Int64())
// Sender type casting (address.common -> string)
sender := ethbridgeTypes.NewEthereumAddress(event.From.Hex())
// Recipient type casting ([]bytes -> sdk.AccAddress)
recipient, err := sdk.AccAddressFromBech32(string(event.To[:]))
if err != nil {
return witnessClaim, err
}
if recipient.Empty() {
return witnessClaim, errors.New("empty recipient address")
}
// Amount type casting (*big.Int -> sdk.Coins)
coins := sdk.Coins{sdk.NewInt64Coin(ETH, event.Value.Int64())}
// Package the information in a unique EthBridgeClaim
witnessClaim.Nonce = nonce
witnessClaim.EthereumSender = sender
witnessClaim.ValidatorAddress = valAddr
witnessClaim.CosmosReceiver = recipient
witnessClaim.Amount = coins
return witnessClaim, nil
}