This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
coin.go
79 lines (66 loc) · 1.73 KB
/
coin.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
package types
import (
"fmt"
"strconv"
)
const (
mojoInChia int64 = 1000000000000
)
// Mojo is a special type for Mojos, to keep track of what unit an amount is
type Mojo int64
// MarshalJSON marshals Mojo into json
func (m Mojo) MarshalJSON() ([]byte, error) {
s := strconv.FormatInt(int64(m), 10)
return []byte(s), nil
}
// UnmarshalJSON unmarshals json data into Mojo
func (m *Mojo) UnmarshalJSON(data []byte) error {
mojo, err := strconv.ParseUint(string(data), 10, 64)
if err != nil {
return err
}
*m = Mojo(mojo)
return nil
}
// XCH is a special type for Chia, to keep track of what unit an amount is
type XCH float64
// MarshalJSON marshals XCH into json
func (xch XCH) MarshalJSON() ([]byte, error) {
s := fmt.Sprintf("%f", xch)
return []byte(s), nil
}
// UnmarshalJSON unmarshals json data into XCH
func (xch *XCH) UnmarshalJSON(data []byte) error {
x, err := strconv.ParseFloat(string(data), 64)
if err != nil {
return err
}
*xch = XCH(x)
return nil
}
// ToMojo converts chia to mojos
func (xch XCH) ToMojo() Mojo {
return Mojo(xch * XCH(mojoInChia))
}
// ToChia converts mojo to chia
func (m Mojo) ToChia() XCH {
return XCH(m) / XCH(mojoInChia)
}
// Coin is a coin
type Coin struct {
Amount Mojo `json:"amount"`
ParentCoinInfo string `json:"parent_coin_info"`
PuzzleHash string `json:"puzzle_hash"`
}
// CoinSolution solution to a coin
type CoinSolution struct {
Coin *Coin `json:"coin"`
PuzzleReveal *SerializedProgram `json:"puzzle_reveal"`
Solution *SerializedProgram `json:"solution"`
}
// CoinAddedEvent data from coin-added websocket event
type CoinAddedEvent struct {
Success bool `json:"success"`
State string `json:"state"`
WalletID uint32 `json:"wallet_id"`
}