/
chequebook.go
340 lines (288 loc) · 10.7 KB
/
chequebook.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package chequebook
import (
"context"
"errors"
"fmt"
"math/big"
"strings"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/newswarm-lab/new-bee/pkg/nswabi"
"github.com/newswarm-lab/new-bee/pkg/sctx"
"github.com/newswarm-lab/new-bee/pkg/settlement/swap/erc20"
"github.com/newswarm-lab/new-bee/pkg/storage"
"github.com/newswarm-lab/new-bee/pkg/transaction"
)
// SendChequeFunc is a function to send cheques.
type SendChequeFunc func(cheque *SignedCheque) error
const (
lastIssuedChequeKeyPrefix = "swap_chequebook_last_issued_cheque_"
totalIssuedKey = "swap_chequebook_total_issued_"
)
var (
// ErrOutOfFunds is the error when the chequebook has not enough free funds for a cheque
ErrOutOfFunds = errors.New("chequebook out of funds")
// ErrInsufficientFunds is the error when the chequebook has not enough free funds for a user action
ErrInsufficientFunds = errors.New("insufficient token balance")
chequebookABI = transaction.ParseABIUnchecked(nswabi.RewardChequeBookABIv0_1_0)
chequeCashedEventType = chequebookABI.Events["ChequeCashed"]
chequeBouncedEventType = chequebookABI.Events["ChequeBounced"]
)
// Service is the main interface for interacting with the nodes chequebook.
type Service interface {
// Deposit starts depositing erc20 token into the chequebook. This returns once the transactions has been broadcast.
Deposit(ctx context.Context, amount *big.Int) (hash common.Hash, err error)
// Withdraw starts withdrawing erc20 token from the chequebook. This returns once the transactions has been broadcast.
Withdraw(ctx context.Context, amount *big.Int) (hash common.Hash, err error)
// WaitForDeposit waits for the deposit transaction to confirm and verifies the result.
WaitForDeposit(ctx context.Context, txHash common.Hash) error
// Balance returns the token balance of the chequebook.
Balance(ctx context.Context) (*big.Int, error)
// AvailableBalance returns the token balance of the chequebook which is not yet used for uncashed cheques.
AvailableBalance(ctx context.Context) (*big.Int, error)
// Address returns the address of the used chequebook contract.
Address() common.Address
// Issue a new cheque for the beneficiary with an cumulativePayout amount higher than the last.
Issue(ctx context.Context, beneficiary common.Address, amount *big.Int, sendChequeFunc SendChequeFunc) (*big.Int, error)
// LastCheque returns the last cheque we issued for the beneficiary.
LastCheque(beneficiary common.Address) (*SignedCheque, error)
// LastCheque returns the last cheques for all beneficiaries.
LastCheques() (map[common.Address]*SignedCheque, error)
}
type service struct {
lock sync.Mutex
transactionService transaction.Service
address common.Address
contract *chequebookContract
ownerAddress common.Address
erc20Service erc20.Service
store storage.StateStorer
chequeSigner ChequeSigner
totalIssuedReserved *big.Int
}
// New creates a new chequebook service for the provided chequebook contract.
func New(transactionService transaction.Service, address, ownerAddress common.Address, store storage.StateStorer, chequeSigner ChequeSigner, erc20Service erc20.Service) (Service, error) {
return &service{
transactionService: transactionService,
address: address,
contract: newChequebookContract(address, transactionService),
ownerAddress: ownerAddress,
erc20Service: erc20Service,
store: store,
chequeSigner: chequeSigner,
totalIssuedReserved: big.NewInt(0),
}, nil
}
// Address returns the address of the used chequebook contract.
func (s *service) Address() common.Address {
return s.address
}
// Deposit starts depositing erc20 token into the chequebook. This returns once the transactions has been broadcast.
func (s *service) Deposit(ctx context.Context, amount *big.Int) (hash common.Hash, err error) {
balance, err := s.erc20Service.BalanceOf(ctx, s.ownerAddress)
if err != nil {
return common.Hash{}, err
}
// check we can afford this so we don't waste gas
if balance.Cmp(amount) < 0 {
return common.Hash{}, ErrInsufficientFunds
}
return s.erc20Service.Transfer(ctx, s.address, amount)
}
// Balance returns the token balance of the chequebook.
func (s *service) Balance(ctx context.Context) (*big.Int, error) {
return s.contract.Balance(ctx)
}
// AvailableBalance returns the token balance of the chequebook which is not yet used for uncashed cheques.
func (s *service) AvailableBalance(ctx context.Context) (*big.Int, error) {
totalIssued, err := s.totalIssued()
if err != nil {
return nil, err
}
balance, err := s.Balance(ctx)
if err != nil {
return nil, err
}
totalPaidOut, err := s.contract.TotalPaidOut(ctx)
if err != nil {
return nil, err
}
// balance plus totalPaidOut is the total amount ever put into the chequebook (ignoring deposits and withdrawals which cancelled out)
// minus the total amount we issued from this chequebook this gives use the portion of the balance not covered by any cheques
availableBalance := big.NewInt(0).Add(balance, totalPaidOut)
availableBalance = availableBalance.Sub(availableBalance, totalIssued)
return availableBalance, nil
}
// WaitForDeposit waits for the deposit transaction to confirm and verifies the result.
func (s *service) WaitForDeposit(ctx context.Context, txHash common.Hash) error {
receipt, err := s.transactionService.WaitForReceipt(ctx, txHash)
if err != nil {
return err
}
if receipt.Status != 1 {
return transaction.ErrTransactionReverted
}
return nil
}
// lastIssuedChequeKey computes the key where to store the last cheque for a beneficiary.
func lastIssuedChequeKey(beneficiary common.Address) string {
return fmt.Sprintf("%s%x", lastIssuedChequeKeyPrefix, beneficiary)
}
func (s *service) reserveTotalIssued(ctx context.Context, amount *big.Int) (*big.Int, error) {
s.lock.Lock()
defer s.lock.Unlock()
availableBalance, err := s.AvailableBalance(ctx)
if err != nil {
return nil, err
}
if amount.Cmp(big.NewInt(0).Sub(availableBalance, s.totalIssuedReserved)) > 0 {
return nil, ErrOutOfFunds
}
s.totalIssuedReserved = s.totalIssuedReserved.Add(s.totalIssuedReserved, amount)
return big.NewInt(0).Sub(availableBalance, amount), nil
}
func (s *service) unreserveTotalIssued(amount *big.Int) {
s.lock.Lock()
defer s.lock.Unlock()
s.totalIssuedReserved = s.totalIssuedReserved.Sub(s.totalIssuedReserved, amount)
}
// Issue issues a new cheque and passes it to sendChequeFunc.
// The cheque is considered sent and saved when sendChequeFunc succeeds.
// The available balance which is available after sending the cheque is passed
// to the caller for it to be communicated over metrics.
func (s *service) Issue(ctx context.Context, beneficiary common.Address, amount *big.Int, sendChequeFunc SendChequeFunc) (*big.Int, error) {
availableBalance, err := s.reserveTotalIssued(ctx, amount)
if err != nil {
return nil, err
}
defer s.unreserveTotalIssued(amount)
var cumulativePayout *big.Int
lastCheque, err := s.LastCheque(beneficiary)
if err != nil {
if err != ErrNoCheque {
return nil, err
}
cumulativePayout = big.NewInt(0)
} else {
cumulativePayout = lastCheque.CumulativePayout
}
// increase cumulativePayout by amount
cumulativePayout = cumulativePayout.Add(cumulativePayout, amount)
// create and sign the new cheque
cheque := Cheque{
Chequebook: s.address,
CumulativePayout: cumulativePayout,
Beneficiary: beneficiary,
}
sig, err := s.chequeSigner.Sign(&Cheque{
Chequebook: s.address,
CumulativePayout: cumulativePayout,
Beneficiary: beneficiary,
})
if err != nil {
return nil, err
}
// actually send the check before saving to avoid double payment
err = sendChequeFunc(&SignedCheque{
Cheque: cheque,
Signature: sig,
})
if err != nil {
return nil, err
}
err = s.store.Put(lastIssuedChequeKey(beneficiary), cheque)
if err != nil {
return nil, err
}
s.lock.Lock()
defer s.lock.Unlock()
totalIssued, err := s.totalIssued()
if err != nil {
return nil, err
}
totalIssued = totalIssued.Add(totalIssued, amount)
return availableBalance, s.store.Put(totalIssuedKey, totalIssued)
}
// returns the total amount in cheques issued so far
func (s *service) totalIssued() (totalIssued *big.Int, err error) {
err = s.store.Get(totalIssuedKey, &totalIssued)
if err != nil {
if err != storage.ErrNotFound {
return nil, err
}
return big.NewInt(0), nil
}
return totalIssued, nil
}
// LastCheque returns the last cheque we issued for the beneficiary.
func (s *service) LastCheque(beneficiary common.Address) (*SignedCheque, error) {
var lastCheque *SignedCheque
err := s.store.Get(lastIssuedChequeKey(beneficiary), &lastCheque)
if err != nil {
if err != storage.ErrNotFound {
return nil, err
}
return nil, ErrNoCheque
}
return lastCheque, nil
}
func keyBeneficiary(key []byte, prefix string) (beneficiary common.Address, err error) {
k := string(key)
split := strings.SplitAfter(k, prefix)
if len(split) != 2 {
return common.Address{}, errors.New("no beneficiary in key")
}
return common.HexToAddress(split[1]), nil
}
// LastCheque returns the last cheques for all beneficiaries.
func (s *service) LastCheques() (map[common.Address]*SignedCheque, error) {
result := make(map[common.Address]*SignedCheque)
err := s.store.Iterate(lastIssuedChequeKeyPrefix, func(key, val []byte) (stop bool, err error) {
addr, err := keyBeneficiary(key, lastIssuedChequeKeyPrefix)
if err != nil {
return false, fmt.Errorf("parse address from key: %s: %w", string(key), err)
}
if _, ok := result[addr]; !ok {
lastCheque, err := s.LastCheque(addr)
if err != nil {
return false, err
}
result[addr] = lastCheque
}
return false, nil
})
if err != nil {
return nil, err
}
return result, nil
}
func (s *service) Withdraw(ctx context.Context, amount *big.Int) (hash common.Hash, err error) {
availableBalance, err := s.AvailableBalance(ctx)
if err != nil {
return common.Hash{}, err
}
// check we can afford this so we don't waste gas and don't risk bouncing cheques
if availableBalance.Cmp(amount) < 0 {
return common.Hash{}, ErrInsufficientFunds
}
callData, err := chequebookABI.Pack("withdraw", amount)
if err != nil {
return common.Hash{}, err
}
request := &transaction.TxRequest{
To: &s.address,
Data: callData,
GasPrice: sctx.GetGasPrice(ctx),
GasLimit: 95000,
Value: big.NewInt(0),
Description: fmt.Sprintf("chequebook withdrawal of %d BZZ", amount),
}
txHash, err := s.transactionService.Send(ctx, request)
if err != nil {
return common.Hash{}, err
}
return txHash, nil
}