lotus/paychmgr/util.go
2020-09-21 15:24:45 -07:00

36 lines
1020 B
Go

package paychmgr
import (
"context"
"github.com/filecoin-project/go-address"
paych0 "github.com/filecoin-project/specs-actors/actors/builtin/paych"
)
type BestSpendableAPI interface {
PaychVoucherList(context.Context, address.Address) ([]*paych0.SignedVoucher, error)
PaychVoucherCheckSpendable(context.Context, address.Address, *paych0.SignedVoucher, []byte, []byte) (bool, error)
}
func BestSpendableByLane(ctx context.Context, api BestSpendableAPI, ch address.Address) (map[uint64]*paych0.SignedVoucher, error) {
vouchers, err := api.PaychVoucherList(ctx, ch)
if err != nil {
return nil, err
}
bestByLane := make(map[uint64]*paych0.SignedVoucher)
for _, voucher := range vouchers {
spendable, err := api.PaychVoucherCheckSpendable(ctx, ch, voucher, nil, nil)
if err != nil {
return nil, err
}
if spendable {
if bestByLane[voucher.Lane] == nil || voucher.Amount.GreaterThan(bestByLane[voucher.Lane].Amount) {
bestByLane[voucher.Lane] = voucher
}
}
}
return bestByLane, nil
}