2020-08-18 15:27:00 +00:00
|
|
|
package paychmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-21 22:24:45 +00:00
|
|
|
|
2020-09-28 21:25:58 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/paych"
|
2020-08-18 15:27:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type BestSpendableAPI interface {
|
2020-09-28 21:25:58 +00:00
|
|
|
PaychVoucherList(context.Context, address.Address) ([]*paych.SignedVoucher, error)
|
|
|
|
PaychVoucherCheckSpendable(context.Context, address.Address, *paych.SignedVoucher, []byte, []byte) (bool, error)
|
2020-08-18 15:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 21:25:58 +00:00
|
|
|
func BestSpendableByLane(ctx context.Context, api BestSpendableAPI, ch address.Address) (map[uint64]*paych.SignedVoucher, error) {
|
2020-08-18 15:27:00 +00:00
|
|
|
vouchers, err := api.PaychVoucherList(ctx, ch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-28 21:25:58 +00:00
|
|
|
bestByLane := make(map[uint64]*paych.SignedVoucher)
|
2020-08-18 15:27:00 +00:00
|
|
|
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
|
|
|
|
}
|