-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadata.go
96 lines (82 loc) · 2.37 KB
/
metadata.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
package types
import (
"encoding/binary"
"encoding/hex"
fmt "fmt"
"math/big"
)
///////////////////////////////////
// Implement Metadata //
///////////////////////////////////
type EvmContractMetadata struct {
DestGasLimit uint64 `json:"destGasLimit"`
DestGasPrice uint64 `json:"destGasPrice"`
AckGasLimit uint64 `json:"ackGasLimit"`
AckGasPrice uint64 `json:"ackGasPrice"`
RelayerFees *big.Int `json:"relayerFees"`
AckType uint8 `json:"ackType"`
IsReadCall bool `json:"isReadCall"`
AsmAddress string `json:"asmAddress"`
}
type IContractMetadata interface {
GetRequestMetadata() []byte
}
func DecodeEvmContractMetadata(msg IContractMetadata) *EvmContractMetadata {
metadata := &EvmContractMetadata{
RelayerFees: big.NewInt(0),
}
requestMetadataStr := hex.EncodeToString(msg.GetRequestMetadata())
if len(requestMetadataStr) < 100 {
return metadata
}
var (
chunk1 = requestMetadataStr[:16]
chunk2 = requestMetadataStr[16:32]
chunk3 = requestMetadataStr[32:48]
chunk4 = requestMetadataStr[48:64]
chunk5 = requestMetadataStr[64:96]
chunk6 = requestMetadataStr[96:98]
chunk7 = requestMetadataStr[98:100]
chunk8 = requestMetadataStr[100:]
)
destGasLimit, err := hex.DecodeString(chunk1)
if err != nil {
destGasLimit = make([]byte, 8)
}
destGasPrice, err := hex.DecodeString(chunk2)
if err != nil {
destGasPrice = make([]byte, 8)
}
ackGasLimit, err := hex.DecodeString(chunk3)
if err != nil {
ackGasLimit = make([]byte, 8)
}
ackGasPrice, err := hex.DecodeString(chunk4)
if err != nil {
ackGasPrice = make([]byte, 8)
}
if _, ok := metadata.RelayerFees.SetString(chunk5, 16); !ok {
metadata.RelayerFees.SetInt64(0) // Set to zero if there's an issue
}
var ackType uint8
if _, err := fmt.Sscanf(chunk6, "%x", &ackType); err != nil {
ackType = 0
}
var isReadCall bool
if chunk7 == "01" {
isReadCall = true
}
asmAddressByte, err := hex.DecodeString(chunk8)
if err != nil {
asmAddressByte = make([]byte, 0)
}
asmAddress := string(asmAddressByte)
metadata.DestGasLimit = binary.BigEndian.Uint64(destGasLimit)
metadata.DestGasPrice = binary.BigEndian.Uint64(destGasPrice)
metadata.AckGasLimit = binary.BigEndian.Uint64(ackGasLimit)
metadata.AckGasPrice = binary.BigEndian.Uint64(ackGasPrice)
metadata.AckType = ackType
metadata.IsReadCall = isReadCall
metadata.AsmAddress = asmAddress
return metadata
}