forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_paych.go
More file actions
50 lines (45 loc) · 1.23 KB
/
api_paych.go
File metadata and controls
50 lines (45 loc) · 1.23 KB
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
package api
import (
"context"
"fmt"
"github.com/ipfs/go-cid"
"github.com/textileio/powergate/ffs"
)
// ListPayChannels lists all payment channels associated with this FFS.
func (i *API) ListPayChannels(ctx context.Context) ([]ffs.PaychInfo, error) {
addrInfos := i.Addrs()
addrs := make([]string, len(addrInfos))
for i, info := range addrInfos {
addrs[i] = info.Addr
}
res, err := i.pm.List(ctx, addrs...)
if err != nil {
return nil, err
}
return res, nil
}
// CreatePayChannel creates a new payment channel.
func (i *API) CreatePayChannel(ctx context.Context, from string, to string, amount uint64) (ffs.PaychInfo, cid.Cid, error) {
if !i.isManagedAddress(from) {
return ffs.PaychInfo{}, cid.Undef, fmt.Errorf("%v is not managed by ffs instance", from)
}
return i.pm.Create(ctx, from, to, amount)
}
// RedeemPayChannel redeems a payment channel.
func (i *API) RedeemPayChannel(ctx context.Context, addr string) error {
channels, err := i.ListPayChannels(ctx)
if err != nil {
return err
}
managed := false
for _, channel := range channels {
if channel.Addr == addr {
managed = true
break
}
}
if !managed {
return fmt.Errorf("paych %v is not managed by ffs instance", addr)
}
return i.pm.Redeem(ctx, addr)
}