-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommitment.go
201 lines (150 loc) · 3.62 KB
/
commitment.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package commitment
import (
"errors"
"time"
splitter_token "github.com/code-payments/code-server/pkg/solana/splitter"
)
type State uint8
const (
StateUnknown State = iota
StatePayingDestination
StateReadyToOpen
StateOpening
StateOpen
StateClosing
StateClosed
// Theoretical states that don't have any logic right now, but will be needed
// later for GC.
StateReadyToRemoveFromMerkleTree
StateRemovedFromMerkleTree
)
type Record struct {
Id uint64
DataVersion splitter_token.DataVersion
Address string
Bump uint8
Pool string
PoolBump uint8
RecentRoot string
Transcript string
Destination string
Amount uint64
Vault string
VaultBump uint8
Intent string
ActionId uint32
Owner string
// Has the treasury been repaid for advancing Record.Amount to Record.Destination?
// Not to be confused with payments being diverted to this commitment and then
// being closed.
TreasuryRepaid bool
// The commitment vault where repayment for Record.Amount will be diverted to.
RepaymentDivertedTo *string
State State
CreatedAt time.Time
}
func (r *Record) Validate() error {
if r.DataVersion != splitter_token.DataVersion1 {
return errors.New("commitment data version must be 1")
}
if len(r.Address) == 0 {
return errors.New("address is required")
}
if len(r.Pool) == 0 {
return errors.New("pool is required")
}
if len(r.RecentRoot) == 0 {
return errors.New("recent root is required")
}
if len(r.Transcript) == 0 {
return errors.New("transcript is required")
}
if len(r.Destination) == 0 {
return errors.New("destination is required")
}
if r.Amount == 0 {
return errors.New("settlement amount must be positive")
}
if len(r.Vault) == 0 {
return errors.New("vault is required")
}
if len(r.Intent) == 0 {
return errors.New("intent is required")
}
if len(r.Owner) == 0 {
return errors.New("owner is required")
}
return nil
}
func (r *Record) Clone() *Record {
var repaymentDivertedTo *string
if r.RepaymentDivertedTo != nil {
value := *r.RepaymentDivertedTo
repaymentDivertedTo = &value
}
return &Record{
Id: r.Id,
DataVersion: r.DataVersion,
Address: r.Address,
Bump: r.Bump,
Pool: r.Pool,
PoolBump: r.PoolBump,
RecentRoot: r.RecentRoot,
Transcript: r.Transcript,
Destination: r.Destination,
Amount: r.Amount,
Vault: r.Vault,
VaultBump: r.VaultBump,
Intent: r.Intent,
ActionId: r.ActionId,
Owner: r.Owner,
TreasuryRepaid: r.TreasuryRepaid,
RepaymentDivertedTo: repaymentDivertedTo,
State: r.State,
CreatedAt: r.CreatedAt,
}
}
func (r *Record) CopyTo(dst *Record) {
dst.Id = r.Id
dst.DataVersion = r.DataVersion
dst.Address = r.Address
dst.Bump = r.Bump
dst.Pool = r.Pool
dst.PoolBump = r.PoolBump
dst.RecentRoot = r.RecentRoot
dst.Transcript = r.Transcript
dst.Destination = r.Destination
dst.Amount = r.Amount
dst.Vault = r.Vault
dst.VaultBump = r.VaultBump
dst.Intent = r.Intent
dst.ActionId = r.ActionId
dst.Owner = r.Owner
dst.TreasuryRepaid = r.TreasuryRepaid
dst.RepaymentDivertedTo = r.RepaymentDivertedTo
dst.State = r.State
dst.CreatedAt = r.CreatedAt
}
func (s State) String() string {
switch s {
case StateUnknown:
return "unknown"
case StatePayingDestination:
return "paying_destination"
case StateReadyToOpen:
return "ready_to_open"
case StateOpening:
return "opening"
case StateOpen:
return "open"
case StateClosing:
return "closing"
case StateClosed:
return "closed"
case StateReadyToRemoveFromMerkleTree:
return "ready_to_remove_from_merkle_tree"
case StateRemovedFromMerkleTree:
return "removed_from_merkle_tree"
}
return "unknown"
}