-
Notifications
You must be signed in to change notification settings - Fork 41
/
types.go
111 lines (93 loc) · 2.37 KB
/
types.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
100
101
102
103
104
105
106
107
108
109
110
111
package swap
import (
"encoding/hex"
"encoding/json"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type SwapStatus int8
const (
NULL SwapStatus = 0x00
Open SwapStatus = 0x01
Completed SwapStatus = 0x02
Expired SwapStatus = 0x03
)
func NewSwapStatusFromString(str string) SwapStatus {
switch str {
case "Open", "open":
return Open
case "Completed", "completed":
return Completed
case "Expired", "expired":
return Expired
default:
return NULL
}
}
func (status SwapStatus) String() string {
switch status {
case Open:
return "Open"
case Completed:
return "Completed"
case Expired:
return "Expired"
default:
return "NULL"
}
}
func (status SwapStatus) MarshalJSON() ([]byte, error) {
return json.Marshal(status.String())
}
func (status *SwapStatus) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
*status = NewSwapStatusFromString(s)
return nil
}
type SwapBytes []byte
func (bz SwapBytes) Marshal() ([]byte, error) {
return bz, nil
}
func (bz *SwapBytes) Unmarshal(data []byte) error {
*bz = data
return nil
}
func (bz SwapBytes) MarshalJSON() ([]byte, error) {
s := hex.EncodeToString(bz)
jbz := make([]byte, len(s)+2)
jbz[0] = '"'
copy(jbz[1:], []byte(s))
jbz[len(jbz)-1] = '"'
return jbz, nil
}
func (bz *SwapBytes) UnmarshalJSON(data []byte) error {
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
return fmt.Errorf("Invalid hex string: %s", data)
}
bz2, err := hex.DecodeString(string(data[1 : len(data)-1]))
if err != nil {
return err
}
*bz = bz2
return nil
}
type AtomicSwap struct {
From sdk.AccAddress `json:"from"`
To sdk.AccAddress `json:"to"`
OutAmount sdk.Coins `json:"out_amount"`
InAmount sdk.Coins `json:"in_amount"`
ExpectedIncome string `json:"expected_income"`
RecipientOtherChain string `json:"recipient_other_chain"`
RandomNumberHash SwapBytes `json:"random_number_hash"` // 32-length byte array, sha256(random_number, timestamp)
RandomNumber SwapBytes `json:"random_number"` // random_number is a 32-length random byte array
Timestamp int64 `json:"timestamp"`
CrossChain bool `json:"cross_chain"`
ExpireHeight int64 `json:"expire_height"`
Index int64 `json:"index"`
ClosedTime int64 `json:"closed_time"`
Status SwapStatus `json:"status"`
}