[#365] settlement/basic: Check amount of collected assets

Some transfers from container owners into bank account may
fail due to lack of assets, so we have to deal with remaining
amount of assets in banking account.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-02-02 16:45:13 +03:00 committed by Alex Vanin
parent f85e5f0238
commit 4433448645
4 changed files with 28 additions and 2 deletions

View file

@ -18,8 +18,6 @@ func (inc *IncomeSettlementContext) Collect() {
inc.mu.Lock()
defer inc.mu.Unlock()
// todo: save state of bank wallet
cachedRate, err := inc.rate.BasicRate()
if err != nil {
inc.log.Error("can't get basic income rate",

View file

@ -1,6 +1,7 @@
package basic
import (
"math/big"
"sync"
"github.com/mr-tron/base58"
@ -21,6 +22,11 @@ type (
BasicRate() (uint64, error)
}
// BalanceFetcher uses NEP-17 compatible balance contract
BalanceFetcher interface {
Balance(id *owner.ID) (*big.Int, error)
}
IncomeSettlementContext struct {
mu sync.Mutex // lock to prevent collection and distribution in the same time
@ -29,6 +35,7 @@ type (
rate RateFetcher
estimations EstimationFetcher
balances BalanceFetcher
container common.ContainerStorage
placement common.PlacementCalculator
exchange common.Exchanger
@ -42,6 +49,7 @@ type (
Epoch uint64
Rate RateFetcher
Estimations EstimationFetcher
Balances BalanceFetcher
Container common.ContainerStorage
Placement common.PlacementCalculator
Exchange common.Exchanger
@ -59,6 +67,7 @@ func NewIncomeSettlementContext(p *IncomeSettlementContextPrms) (*IncomeSettleme
epoch: p.Epoch,
rate: p.Rate,
estimations: p.Estimations,
balances: p.Balances,
container: p.Container,
placement: p.Placement,
exchange: p.Exchange,

View file

@ -1,6 +1,20 @@
package basic
import (
"go.uber.org/zap"
)
func (inc *IncomeSettlementContext) Distribute() {
inc.mu.Lock()
defer inc.mu.Unlock()
bankBalance, err := inc.balances.Balance(inc.bankOwner)
if err != nil {
inc.log.Error("can't fetch balance of banking account",
zap.String("error", err.Error()))
return
}
_ = bankBalance
}

View file

@ -259,6 +259,10 @@ func (b basicIncomeSettlementDeps) Estimations(epoch uint64) ([]*wrapper.Estimat
return result, nil
}
func (b basicIncomeSettlementDeps) Balance(id *owner.ID) (*big.Int, error) {
return b.balanceClient.BalanceOf(id)
}
func (s *auditSettlementCalculator) ProcessAuditSettlements(epoch uint64) {
(*audit.Calculator)(s).Calculate(&audit.CalculatePrm{
Epoch: epoch,
@ -271,6 +275,7 @@ func (b *basicSettlementConstructor) CreateContext(epoch uint64) (*basic.IncomeS
Epoch: epoch,
Rate: b.dep,
Estimations: b.dep,
Balances: b.dep,
Container: b.dep,
Placement: b.dep,
Exchange: b.dep,