forked from denuoweb/solar
-
Notifications
You must be signed in to change notification settings - Fork 2
/
contractsRepo.go
203 lines (157 loc) · 4.09 KB
/
contractsRepo.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
202
203
package contract
import (
"encoding/json"
"log"
"os"
"sort"
"time"
"github.com/pkg/errors"
)
type CompiledContracts map[string]*CompiledContract
type DeployedContracts map[string]*DeployedContract
type DeployedContract struct {
CompiledContract
Name string `json:"name"`
DeployName string `json:"deployName"`
Address Bytes `json:"address"`
TransactionID Bytes `json:"txid"`
CreatedAt time.Time `json:"createdAt"`
Confirmed bool `json:"confirmed"`
// htmlcoin
Sender string `json:"sender,omitempty"`
SenderHex string `json:"senderHex,omitempty"`
}
type ContractsRepository struct {
filepath string
Contracts DeployedContracts `json:"contracts"`
Libraries DeployedContracts `json:"libraries"`
// ABI definitions related to the contracts, but not deployed.
Related CompiledContracts `json:"related"`
}
func OpenContractsRepository(filepath string) (repo *ContractsRepository, err error) {
f, err := os.Open(filepath)
if os.IsNotExist(err) {
return &ContractsRepository{
filepath: filepath,
Contracts: make(DeployedContracts),
Libraries: make(DeployedContracts),
Related: make(CompiledContracts),
}, nil
}
if err != nil {
return
}
defer f.Close()
dec := json.NewDecoder(f)
repo = &ContractsRepository{
filepath: filepath,
}
err = dec.Decode(&repo)
if err != nil {
return
}
if repo.Libraries == nil {
repo.Libraries = make(DeployedContracts)
}
if repo.Contracts == nil {
repo.Contracts = make(DeployedContracts)
}
if repo.Related == nil {
repo.Related = make(CompiledContracts)
}
return
}
func (r *ContractsRepository) Get(name string) (*DeployedContract, bool) {
contract, ok := r.Contracts[name]
return contract, ok
}
func (r *ContractsRepository) GetLib(name string) (*DeployedContract, bool) {
contract, ok := r.Libraries[name]
return contract, ok
}
func (r *ContractsRepository) Len() int {
return len(r.Contracts)
}
func (r *ContractsRepository) UnconfirmedContracts() []*DeployedContract {
var contracts []*DeployedContract
for _, contract := range r.Libraries {
if !contract.Confirmed {
contracts = append(contracts, contract)
}
}
for _, contract := range r.Contracts {
if !contract.Confirmed {
contracts = append(contracts, contract)
}
}
return contracts
}
func (r *ContractsRepository) SortedContracts() []*DeployedContract {
var contracts []*DeployedContract
for _, contract := range r.Contracts {
contracts = append(contracts, contract)
}
sort.Slice(contracts, func(i, j int) bool {
c1 := contracts[i]
c2 := contracts[j]
return c1.CreatedAt.Unix() < c2.CreatedAt.Unix()
})
return contracts
}
func (r *ContractsRepository) Exists(name string) bool {
_, found := r.Contracts[name]
return found
}
func (r *ContractsRepository) LibExists(name string) bool {
_, found := r.Libraries[name]
return found
}
func (r *ContractsRepository) Confirm(name string) (err error) {
c, found := r.Contracts[name]
if !found {
return errors.Errorf("Cannot unconfirm unknown contract %s", name)
}
c.Confirmed = true
return nil
}
func (r *ContractsRepository) Set(name string, c *DeployedContract) {
r.Contracts[name] = c
}
func (r *ContractsRepository) SetLib(name string, c *DeployedContract) {
r.Libraries[name] = c
}
func (r *ContractsRepository) Commit() (err error) {
// TODO. do write & swap instead of truncat?
f, err := os.Create(r.filepath)
if err != nil {
return
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
return enc.Encode(r)
}
// Confirm checks the RPC server to see if all the contracts
// are confirmed by the blockchain.
func (r *ContractsRepository) ConfirmAll(updateProgress func(i, total int), confirmer func(c *DeployedContract) error) (err error) {
contracts := r.UnconfirmedContracts()
total := len(contracts)
if updateProgress != nil {
updateProgress(0, total)
}
for i, contract := range contracts {
contract := contract
err := confirmer(contract)
if err != nil {
log.Println("err", err)
}
if updateProgress != nil {
updateProgress(i+1, total)
}
}
err = r.Commit()
if err != nil {
return
}
return
}