2018-11-07 21:50:43 +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-11-03 19:00:25 +00:00
|
|
|
|
|
|
|
package converter
|
|
|
|
|
|
|
|
import (
|
2018-11-07 21:50:43 +00:00
|
|
|
"errors"
|
2018-11-23 18:12:24 +00:00
|
|
|
"fmt"
|
2018-11-07 21:50:43 +00:00
|
|
|
"math/big"
|
|
|
|
"strconv"
|
|
|
|
|
2018-11-03 19:00:25 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-11-23 18:12:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/contract"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/helpers"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/types"
|
2018-11-03 19:00:25 +00:00
|
|
|
)
|
|
|
|
|
2018-11-03 19:02:31 +00:00
|
|
|
// Converter is used to convert watched event logs to
|
|
|
|
// custom logs containing event input name => value maps
|
2018-11-03 19:00:25 +00:00
|
|
|
type Converter interface {
|
2018-11-20 16:38:23 +00:00
|
|
|
Convert(watchedEvent core.WatchedEvent, event types.Event) (*types.Log, error)
|
2018-11-07 21:50:43 +00:00
|
|
|
Update(info *contract.Contract)
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type converter struct {
|
2018-11-23 18:12:24 +00:00
|
|
|
ContractInfo *contract.Contract
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
func NewConverter(info *contract.Contract) *converter {
|
2018-11-03 19:00:25 +00:00
|
|
|
return &converter{
|
2018-11-23 18:12:24 +00:00
|
|
|
ContractInfo: info,
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
func (c *converter) Update(info *contract.Contract) {
|
2018-11-23 18:12:24 +00:00
|
|
|
c.ContractInfo = info
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 19:02:31 +00:00
|
|
|
// Convert the given watched event log into a types.Log for the given event
|
2018-11-20 16:38:23 +00:00
|
|
|
func (c *converter) Convert(watchedEvent core.WatchedEvent, event types.Event) (*types.Log, error) {
|
2018-11-23 18:12:24 +00:00
|
|
|
contract := bind.NewBoundContract(common.HexToAddress(c.ContractInfo.Address), c.ContractInfo.ParsedAbi, nil, nil, nil)
|
2018-11-03 19:00:25 +00:00
|
|
|
values := make(map[string]interface{})
|
|
|
|
|
|
|
|
for _, field := range event.Fields {
|
|
|
|
var i interface{}
|
|
|
|
values[field.Name] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
log := helpers.ConvertToLog(watchedEvent)
|
|
|
|
err := contract.UnpackLogIntoMap(values, event.Name, log)
|
|
|
|
if err != nil {
|
2018-11-20 16:38:23 +00:00
|
|
|
return nil, err
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
strValues := make(map[string]string, len(values))
|
2018-12-07 15:38:46 +00:00
|
|
|
seenBytes := make([]interface{}, 0, len(values))
|
|
|
|
seenAddrs := make([]interface{}, 0, len(values))
|
|
|
|
seenHashes := make([]interface{}, 0, len(values))
|
2018-11-15 18:53:08 +00:00
|
|
|
for fieldName, input := range values {
|
2018-11-07 21:50:43 +00:00
|
|
|
// Postgres cannot handle custom types, resolve to strings
|
|
|
|
switch input.(type) {
|
|
|
|
case *big.Int:
|
2018-11-24 04:26:07 +00:00
|
|
|
b := input.(*big.Int)
|
2018-11-15 18:53:08 +00:00
|
|
|
strValues[fieldName] = b.String()
|
2018-11-07 21:50:43 +00:00
|
|
|
case common.Address:
|
2018-11-24 04:26:07 +00:00
|
|
|
a := input.(common.Address)
|
2018-11-15 18:53:08 +00:00
|
|
|
strValues[fieldName] = a.String()
|
2018-12-07 15:38:46 +00:00
|
|
|
seenAddrs = append(seenAddrs, a)
|
2018-11-07 21:50:43 +00:00
|
|
|
case common.Hash:
|
2018-11-24 04:26:07 +00:00
|
|
|
h := input.(common.Hash)
|
2018-11-15 18:53:08 +00:00
|
|
|
strValues[fieldName] = h.String()
|
2018-12-07 15:38:46 +00:00
|
|
|
seenHashes = append(seenHashes, h)
|
2018-11-07 21:50:43 +00:00
|
|
|
case string:
|
2018-11-15 18:53:08 +00:00
|
|
|
strValues[fieldName] = input.(string)
|
2018-11-07 21:50:43 +00:00
|
|
|
case bool:
|
2018-11-15 18:53:08 +00:00
|
|
|
strValues[fieldName] = strconv.FormatBool(input.(bool))
|
2018-11-24 04:26:07 +00:00
|
|
|
case []byte:
|
|
|
|
b := input.([]byte)
|
|
|
|
strValues[fieldName] = string(b)
|
2018-12-07 15:38:46 +00:00
|
|
|
seenBytes = append(seenBytes, b)
|
2018-11-24 04:26:07 +00:00
|
|
|
case byte:
|
|
|
|
b := input.(byte)
|
|
|
|
strValues[fieldName] = string(b)
|
2018-11-07 21:50:43 +00:00
|
|
|
default:
|
2018-11-23 18:12:24 +00:00
|
|
|
return nil, errors.New(fmt.Sprintf("error: unhandled abi type %T", input))
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
// Only hold onto logs that pass our address filter, if any
|
2018-11-23 18:12:24 +00:00
|
|
|
if c.ContractInfo.PassesEventFilter(strValues) {
|
2018-11-20 16:38:23 +00:00
|
|
|
eventLog := &types.Log{
|
2018-11-07 21:50:43 +00:00
|
|
|
Id: watchedEvent.LogID,
|
|
|
|
Values: strValues,
|
|
|
|
Block: watchedEvent.BlockNumber,
|
|
|
|
Tx: watchedEvent.TxHash,
|
|
|
|
}
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
// Cache emitted values if their caching is turned on
|
|
|
|
if c.ContractInfo.EmittedAddrs != nil {
|
|
|
|
c.ContractInfo.AddEmittedAddr(seenAddrs...)
|
|
|
|
}
|
|
|
|
if c.ContractInfo.EmittedHashes != nil {
|
|
|
|
c.ContractInfo.AddEmittedHash(seenHashes...)
|
|
|
|
}
|
|
|
|
if c.ContractInfo.EmittedBytes != nil {
|
|
|
|
c.ContractInfo.AddEmittedBytes(seenBytes...)
|
|
|
|
}
|
|
|
|
|
2018-11-23 18:12:24 +00:00
|
|
|
return eventLog, nil
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
2018-11-03 19:00:25 +00:00
|
|
|
|
2018-11-20 16:38:23 +00:00
|
|
|
return nil, nil
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|