2018-10-04 21:49:09 +00:00
|
|
|
package vat_tune
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
2018-11-08 15:14:35 +00:00
|
|
|
"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
|
|
|
|
}
|
2019-01-28 22:52:47 +00:00
|
|
|
ilk := shared.GetHexWithoutPrefix(ethLog.Topics[1].Bytes())
|
2019-02-05 21:43:20 +00:00
|
|
|
urn := shared.GetHexWithoutPrefix(ethLog.Topics[2].Bytes())
|
|
|
|
v := shared.GetHexWithoutPrefix(ethLog.Topics[3].Bytes())
|
2018-10-04 21:49:09 +00:00
|
|
|
wBytes := shared.GetDataBytesAtIndex(-3, ethLog.Data)
|
2019-02-05 21:43:20 +00:00
|
|
|
w := shared.GetHexWithoutPrefix(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,
|
2019-02-05 21:43:20 +00:00
|
|
|
Urn: urn,
|
|
|
|
V: v,
|
|
|
|
W: w,
|
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,
|
2018-10-23 21:33:04 +00:00
|
|
|
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")
|
|
|
|
}
|
2018-11-08 15:14:35 +00:00
|
|
|
if len(log.Data) < constants.DataItemLength {
|
2018-10-04 21:49:09 +00:00
|
|
|
return errors.New("log missing data")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|