ipld-eth-server/pkg/transformers/price_feeds/pep/fetcher.go

52 lines
1.2 KiB
Go
Raw Normal View History

2018-07-24 16:38:49 +00:00
package pep
import (
2018-07-26 18:57:38 +00:00
"math/big"
2018-07-24 16:38:49 +00:00
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
2018-07-26 18:57:38 +00:00
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/transformers/price_feeds"
2018-07-24 16:38:49 +00:00
)
type IPepFetcher interface {
FetchPepValue(header core.Header) (string, error)
}
type PepFetcher struct {
blockChain core.BlockChain
}
func NewPepFetcher(chain core.BlockChain) PepFetcher {
return PepFetcher{
blockChain: chain,
}
}
func (fetcher PepFetcher) FetchPepValue(header core.Header) (string, error) {
blockNumber := big.NewInt(header.BlockNumber)
query := ethereum.FilterQuery{
FromBlock: blockNumber,
ToBlock: blockNumber,
2018-07-26 18:57:38 +00:00
Addresses: []common.Address{common.HexToAddress(price_feeds.PepAddress)},
Topics: [][]common.Hash{{common.HexToHash(price_feeds.PepLogTopic0)}},
2018-07-24 16:38:49 +00:00
}
logs, err := fetcher.blockChain.GetEthLogsWithCustomQuery(query)
return fetcher.getLogValue(logs, err)
}
func (fetcher PepFetcher) getLogValue(logs []types.Log, err error) (string, error) {
if err != nil {
return "", err
}
if len(logs) < 1 {
2018-07-26 18:57:38 +00:00
return "", price_feeds.ErrNoMatchingLog
2018-07-24 16:38:49 +00:00
}
if len(logs) > 1 {
2018-07-26 18:57:38 +00:00
return "", price_feeds.ErrMultipleLogs
2018-07-24 16:38:49 +00:00
}
return string(logs[0].Data), nil
}