First pass at tracking balance

This commit is contained in:
Patrick O'Grady 2020-10-27 09:58:56 -07:00
parent ae2ff20bd2
commit ae78470b83
No known key found for this signature in database
GPG key ID: 8DE11C985C0C8D85
3 changed files with 86 additions and 1 deletions

View file

@ -0,0 +1,31 @@
package indexer
import (
"context"
"github.com/coinbase/rosetta-sdk-go/parser"
"github.com/coinbase/rosetta-sdk-go/storage"
"github.com/coinbase/rosetta-sdk-go/types"
)
var _ storage.BalanceStorageHandler = (*BalanceStorageHandler)(nil)
type BalanceStorageHandler struct{}
// BlockAdded is called whenever a block is committed to BlockStorage.
func (h *BalanceStorageHandler) BlockAdded(
ctx context.Context,
block *types.Block,
changes []*parser.BalanceChange,
) error {
return nil
}
// BlockRemoved is called whenever a block is removed from BlockStorage.
func (h *BalanceStorageHandler) BlockRemoved(
ctx context.Context,
block *types.Block,
changes []*parser.BalanceChange,
) error {
return nil
}

View file

@ -0,0 +1,47 @@
package indexer
import (
"context"
"github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/parser"
"github.com/coinbase/rosetta-sdk-go/storage"
"github.com/coinbase/rosetta-sdk-go/types"
)
var _ storage.BalanceStorageHelper = (*BalanceStorageHelper)(nil)
type BalanceStorageHelper struct {
a *asserter.Asserter
}
// AccountBalance attempts to fetch the balance
// for a missing account in storage.
func (h *BalanceStorageHelper) AccountBalance(
ctx context.Context,
account *types.AccountIdentifier,
currency *types.Currency,
block *types.BlockIdentifier,
) (*types.Amount, error) {
return &types.Amount{
Value: "0",
Currency: currency,
}, nil
}
// Asserter returns a *asserter.Asserter.
func (h *BalanceStorageHelper) Asserter() *asserter.Asserter {
return h.a
}
// BalanceExemptions returns a list of *types.BalanceExemption.
func (h *BalanceStorageHelper) BalanceExemptions() []*types.BalanceExemption {
return []*types.BalanceExemption{}
}
// ExemptFunc returns a parser.ExemptOperation.
func (h *BalanceStorageHelper) ExemptFunc() parser.ExemptOperation {
return func(op *types.Operation) bool {
return false
}
}

View file

@ -199,7 +199,14 @@ func Initialize(
coinStorage := storage.NewCoinStorage(localStore, i, asserter)
i.coinStorage = coinStorage
i.workers = []storage.BlockWorker{coinStorage}
balanceStorage := storage.NewBalanceStorage(localStore)
balanceStorage.Initialize(
&BalanceStorageHelper{asserter},
&BalanceStorageHandler{},
)
i.workers = []storage.BlockWorker{coinStorage, balanceStorage}
return i, nil
}