2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// 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-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"
|
2018-12-19 18:42:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2018-11-03 19:00:25 +00:00
|
|
|
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/shared/contract"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/shared/helpers"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/shared/types"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
2018-11-03 19:00:25 +00:00
|
|
|
)
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// ConverterInterface is used to convert watched event logs to
|
2018-11-03 19:02:31 +00:00
|
|
|
// custom logs containing event input name => value maps
|
2019-03-13 16:14:35 +00:00
|
|
|
type ConverterInterface 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
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// Converter is the underlying struct for the ConverterInterface
|
2019-03-13 16:14:35 +00:00
|
|
|
type Converter struct {
|
2018-11-23 18:12:24 +00:00
|
|
|
ContractInfo *contract.Contract
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// Update configures the converter for a specific contract
|
2019-03-13 16:14:35 +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
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// Convert converts the given watched event log into a types.Log for the given event
|
2019-03-13 16:14:35 +00:00
|
|
|
func (c *Converter) Convert(watchedEvent core.WatchedEvent, event types.Event) (*types.Log, error) {
|
2019-03-14 21:49:27 +00:00
|
|
|
boundContract := 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{})
|
|
|
|
log := helpers.ConvertToLog(watchedEvent)
|
2019-03-14 21:49:27 +00:00
|
|
|
err := boundContract.UnpackLogIntoMap(values, event.Name, log)
|
2018-11-03 19:00:25 +00:00
|
|
|
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
|
|
|
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)
|
2018-12-14 17:52:02 +00:00
|
|
|
strValues[fieldName] = hexutil.Encode(b)
|
2018-12-19 18:42:59 +00:00
|
|
|
if len(b) == 32 { // collect byte arrays of size 32 as hashes
|
2018-12-14 17:52:02 +00:00
|
|
|
seenHashes = append(seenHashes, common.HexToHash(strValues[fieldName]))
|
|
|
|
}
|
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:
|
2019-10-15 18:12:17 +00:00
|
|
|
return nil, fmt.Errorf("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{
|
2019-10-15 18:12:17 +00:00
|
|
|
ID: watchedEvent.LogID,
|
2018-11-07 21:50:43 +00:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|