ipld-eth-server/pkg/transformers/vat_toll/converter.go

49 lines
1.1 KiB
Go
Raw Normal View History

2018-10-04 19:46:04 +00:00
package vat_toll
import (
"bytes"
"encoding/json"
"errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
type VatTollConverter struct{}
2018-10-25 00:21:14 +00:00
func (VatTollConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
var models []interface{}
2018-10-04 19:46:04 +00:00
for _, ethLog := range ethLogs {
err := verifyLog(ethLog)
if err != nil {
return nil, err
}
ilk := string(bytes.Trim(ethLog.Topics[1].Bytes(), "\x00"))
urn := common.BytesToAddress(ethLog.Topics[2].Bytes()[:common.AddressLength])
take := ethLog.Topics[3].Big()
raw, err := json.Marshal(ethLog)
if err != nil {
return nil, err
}
model := VatTollModel{
Ilk: ilk,
Urn: urn.String(),
Take: take.String(),
TransactionIndex: ethLog.TxIndex,
LogIndex: ethLog.Index,
2018-10-04 19:46:04 +00:00
Raw: raw,
}
models = append(models, model)
}
return models, nil
}
func verifyLog(log types.Log) error {
numTopicInValidLog := 4
if len(log.Topics) < numTopicInValidLog {
return errors.New("log missing topics")
}
return nil
}