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"
|
|
|
|
"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-04 21:26:39 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/contract"
|
2018-11-07 21:50:43 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/helpers"
|
2018-11-03 19:00:25 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/types"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
Convert(watchedEvent core.WatchedEvent, event *types.Event) 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-07 21:50:43 +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{
|
|
|
|
contractInfo: info,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
func (c *converter) Update(info *contract.Contract) {
|
2018-11-03 19:00:25 +00:00
|
|
|
c.contractInfo = info
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
func (c *converter) CheckInfo() *contract.Contract {
|
|
|
|
return c.contractInfo
|
|
|
|
}
|
|
|
|
|
2018-11-03 19:02:31 +00:00
|
|
|
// Convert the given watched event log into a types.Log for the given event
|
|
|
|
func (c *converter) Convert(watchedEvent core.WatchedEvent, event *types.Event) error {
|
2018-11-03 19:00:25 +00:00
|
|
|
contract := bind.NewBoundContract(common.HexToAddress(c.contractInfo.Address), c.contractInfo.ParsedAbi, nil, nil, nil)
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
strValues := make(map[string]string, len(values))
|
|
|
|
|
|
|
|
for eventName, input := range values {
|
|
|
|
// Postgres cannot handle custom types, resolve to strings
|
|
|
|
switch input.(type) {
|
|
|
|
case *big.Int:
|
|
|
|
var b *big.Int
|
|
|
|
b = input.(*big.Int)
|
|
|
|
strValues[eventName] = b.String()
|
|
|
|
case common.Address:
|
|
|
|
var a common.Address
|
|
|
|
a = input.(common.Address)
|
|
|
|
strValues[eventName] = a.String()
|
|
|
|
c.contractInfo.AddTokenHolderAddress(a.String()) // cache address in a list of contract's token holder addresses
|
|
|
|
case common.Hash:
|
|
|
|
var h common.Hash
|
|
|
|
h = input.(common.Hash)
|
|
|
|
strValues[eventName] = h.String()
|
|
|
|
case string:
|
|
|
|
strValues[eventName] = input.(string)
|
|
|
|
case bool:
|
|
|
|
strValues[eventName] = strconv.FormatBool(input.(bool))
|
|
|
|
default:
|
|
|
|
return errors.New("error: unhandled abi type")
|
|
|
|
}
|
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
|
|
|
|
// Persist log here and don't hold onto it
|
|
|
|
if c.contractInfo.PassesEventFilter(strValues) {
|
|
|
|
eventLog := types.Log{
|
|
|
|
Id: watchedEvent.LogID,
|
|
|
|
Values: strValues,
|
|
|
|
Block: watchedEvent.BlockNumber,
|
|
|
|
Tx: watchedEvent.TxHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Logs[watchedEvent.LogID] = eventLog
|
|
|
|
}
|
2018-11-03 19:00:25 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|