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

64 lines
1.6 KiB
Go
Raw Normal View History

2018-10-04 21:49:09 +00:00
package vat_tune
import (
"bytes"
"encoding/json"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
2018-10-04 21:49:09 +00:00
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
2018-10-04 21:49:09 +00:00
)
type VatTuneConverter struct{}
2018-10-24 15:55:55 +00:00
func (VatTuneConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
var models []interface{}
2018-10-04 21:49:09 +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())
v := common.BytesToAddress(ethLog.Topics[3].Bytes())
2018-10-04 21:49:09 +00:00
wBytes := shared.GetDataBytesAtIndex(-3, ethLog.Data)
w := common.BytesToAddress(wBytes)
2018-10-04 21:49:09 +00:00
dinkBytes := shared.GetDataBytesAtIndex(-2, ethLog.Data)
2018-10-24 15:55:55 +00:00
dink := big.NewInt(0).SetBytes(dinkBytes)
2018-10-04 21:49:09 +00:00
dartBytes := shared.GetDataBytesAtIndex(-1, ethLog.Data)
2018-10-24 15:55:55 +00:00
dart := big.NewInt(0).SetBytes(dartBytes)
2018-10-04 21:49:09 +00:00
raw, err := json.Marshal(ethLog)
if err != nil {
return nil, err
}
model := VatTuneModel{
Ilk: ilk,
Urn: urn.String(),
V: v.String(),
W: w.String(),
2018-10-24 15:55:55 +00:00
Dink: dink.String(),
Dart: dart.String(),
2018-10-04 21:49:09 +00:00
TransactionIndex: ethLog.TxIndex,
LogIndex: ethLog.Index,
2018-10-04 21:49:09 +00:00
Raw: raw,
}
models = append(models, model)
}
return models, nil
}
func verifyLog(log types.Log) error {
if len(log.Topics) < 4 {
return errors.New("log missing topics")
}
if len(log.Data) < constants.DataItemLength {
2018-10-04 21:49:09 +00:00
return errors.New("log missing data")
}
return nil
}