ipld-eth-server/pkg/transformers/price_feeds/pip/transformer.go

44 lines
1.1 KiB
Go
Raw Normal View History

2018-07-26 18:57:38 +00:00
package pip
import (
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers/price_feeds"
)
type PipTransformer struct {
fetcher IPipFetcher
repository IPipRepository
}
func NewPipTransformer(chain core.BlockChain, db *postgres.DB) PipTransformer {
fetcher := NewPipFetcher(chain)
repository := NewPipRepository(db)
return PipTransformer{
fetcher: fetcher,
repository: repository,
}
}
func (transformer PipTransformer) Execute(header core.Header, headerID int64) error {
value, err := transformer.fetcher.FetchPipValue(header)
if err != nil {
if err == price_feeds.ErrNoMatchingLog {
return nil
}
return err
}
pip := getPip(value, header, headerID)
return transformer.repository.CreatePip(pip)
}
func getPip(logValue string, header core.Header, headerID int64) price_feeds.PriceUpdate {
valueInUSD := price_feeds.Convert("wad", logValue, 15)
pep := price_feeds.PriceUpdate{
BlockNumber: header.BlockNumber,
HeaderID: headerID,
UsdValue: valueInUSD,
}
return pep
}