2018-08-22 17:44:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Vulcanize
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package bite
|
|
|
|
|
|
|
|
import (
|
2018-10-18 16:35:27 +00:00
|
|
|
"bytes"
|
2018-08-22 17:44:35 +00:00
|
|
|
"encoding/json"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2018-10-08 02:51:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-08-22 17:44:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
2018-09-11 19:01:45 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
2018-08-22 17:44:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Converter interface {
|
2018-10-02 22:25:38 +00:00
|
|
|
ToEntities(contractAbi string, ethLogs []types.Log) ([]BiteEntity, error)
|
|
|
|
ToModels(biteEntities []BiteEntity) ([]BiteModel, error)
|
2018-08-22 17:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type BiteConverter struct{}
|
|
|
|
|
2018-10-02 22:25:38 +00:00
|
|
|
func (BiteConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]BiteEntity, error) {
|
|
|
|
var entities []BiteEntity
|
|
|
|
for _, ethLog := range ethLogs {
|
2018-10-08 02:51:37 +00:00
|
|
|
entity := &BiteEntity{}
|
2018-10-02 22:25:38 +00:00
|
|
|
address := ethLog.Address
|
|
|
|
abi, err := geth.ParseAbi(contractAbi)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-22 17:44:35 +00:00
|
|
|
|
2018-10-02 22:25:38 +00:00
|
|
|
contract := bind.NewBoundContract(address, abi, nil, nil, nil)
|
2018-08-22 17:44:35 +00:00
|
|
|
|
2018-10-08 02:51:37 +00:00
|
|
|
err = contract.UnpackLog(entity, "Bite", ethLog)
|
2018-10-02 22:25:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
entity.Raw = ethLog
|
2018-10-19 16:15:01 +00:00
|
|
|
entity.LogIndex = ethLog.Index
|
2018-10-02 22:25:38 +00:00
|
|
|
entity.TransactionIndex = ethLog.TxIndex
|
2018-08-22 17:44:35 +00:00
|
|
|
|
2018-10-08 02:51:37 +00:00
|
|
|
entities = append(entities, *entity)
|
2018-10-02 22:25:38 +00:00
|
|
|
}
|
2018-08-22 17:44:35 +00:00
|
|
|
|
2018-10-02 22:25:38 +00:00
|
|
|
return entities, nil
|
2018-08-22 17:44:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 22:25:38 +00:00
|
|
|
func (converter BiteConverter) ToModels(entities []BiteEntity) ([]BiteModel, error) {
|
|
|
|
var models []BiteModel
|
|
|
|
for _, entity := range entities {
|
2018-10-18 16:35:27 +00:00
|
|
|
ilk := string(bytes.Trim(entity.Ilk[:], "\x00"))
|
|
|
|
urn := common.BytesToAddress(entity.Urn[:]).String()
|
2018-10-02 22:25:38 +00:00
|
|
|
ink := entity.Ink
|
|
|
|
art := entity.Art
|
|
|
|
iArt := entity.IArt
|
|
|
|
tab := entity.Tab
|
|
|
|
flip := entity.Flip
|
2018-10-19 16:15:01 +00:00
|
|
|
logIdx := entity.LogIndex
|
2018-10-02 22:25:38 +00:00
|
|
|
txIdx := entity.TransactionIndex
|
|
|
|
rawLogJson, err := json.Marshal(entity.Raw)
|
|
|
|
rawLogString := string(rawLogJson)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-22 17:44:35 +00:00
|
|
|
|
2018-10-02 22:25:38 +00:00
|
|
|
model := BiteModel{
|
|
|
|
Ilk: ilk,
|
|
|
|
Urn: urn,
|
|
|
|
Ink: shared.BigIntToString(ink),
|
|
|
|
Art: shared.BigIntToString(art),
|
|
|
|
IArt: shared.BigIntToString(iArt),
|
|
|
|
Tab: shared.BigIntToString(tab),
|
2018-10-16 19:51:26 +00:00
|
|
|
NFlip: shared.BigIntToString(flip),
|
2018-10-19 16:15:01 +00:00
|
|
|
LogIndex: logIdx,
|
2018-10-02 22:25:38 +00:00
|
|
|
TransactionIndex: txIdx,
|
|
|
|
Raw: rawLogString,
|
|
|
|
}
|
|
|
|
models = append(models, model)
|
|
|
|
}
|
|
|
|
return models, nil
|
2018-08-22 17:44:35 +00:00
|
|
|
}
|