2019-01-24 20:41:30 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2018 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-22 17:44:35 +00:00
|
|
|
|
|
|
|
package bite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-10-26 18:26:10 +00:00
|
|
|
"fmt"
|
2018-08-22 17:44:35 +00:00
|
|
|
"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 BiteConverter struct{}
|
|
|
|
|
2018-10-26 18:26:10 +00:00
|
|
|
func (BiteConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]interface{}, error) {
|
|
|
|
var entities []interface{}
|
2018-10-02 22:25:38 +00:00
|
|
|
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-26 18:26:10 +00:00
|
|
|
func (converter BiteConverter) ToModels(entities []interface{}) ([]interface{}, error) {
|
|
|
|
var models []interface{}
|
2018-10-02 22:25:38 +00:00
|
|
|
for _, entity := range entities {
|
2018-10-26 18:26:10 +00:00
|
|
|
biteEntity, ok := entity.(BiteEntity)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("entity of type %T, not %T", entity, BiteEntity{})
|
|
|
|
}
|
|
|
|
|
2019-01-28 22:52:47 +00:00
|
|
|
ilk := common.Bytes2Hex(biteEntity.Ilk[:])
|
2018-10-26 18:26:10 +00:00
|
|
|
urn := common.BytesToAddress(biteEntity.Urn[:]).String()
|
|
|
|
ink := biteEntity.Ink
|
|
|
|
art := biteEntity.Art
|
|
|
|
iArt := biteEntity.IArt
|
|
|
|
tab := biteEntity.Tab
|
|
|
|
flip := biteEntity.Flip
|
|
|
|
logIdx := biteEntity.LogIndex
|
|
|
|
txIdx := biteEntity.TransactionIndex
|
2018-11-12 20:59:16 +00:00
|
|
|
rawLog, err := json.Marshal(biteEntity.Raw)
|
2018-10-02 22:25:38 +00:00
|
|
|
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,
|
2018-11-12 20:59:16 +00:00
|
|
|
Raw: rawLog,
|
2018-10-02 22:25:38 +00:00
|
|
|
}
|
|
|
|
models = append(models, model)
|
|
|
|
}
|
|
|
|
return models, nil
|
2018-08-22 17:44:35 +00:00
|
|
|
}
|