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
|
|
|
|
2018-11-04 21:26:39 +00:00
|
|
|
package contract
|
2018-11-03 19:00:25 +00:00
|
|
|
|
|
|
|
import (
|
2018-11-04 21:26:39 +00:00
|
|
|
"errors"
|
2019-02-13 19:04:07 +00:00
|
|
|
|
2018-11-03 19:00:25 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2018-12-07 15:38:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2018-11-03 19:00:25 +00:00
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/filters"
|
2018-11-23 18:12:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/types"
|
2018-11-03 19:00:25 +00:00
|
|
|
)
|
|
|
|
|
2018-11-23 18:12:24 +00:00
|
|
|
// Contract object to hold our contract data
|
2018-11-04 21:26:39 +00:00
|
|
|
type Contract struct {
|
2018-11-23 18:12:24 +00:00
|
|
|
Name string // Name of the contract
|
|
|
|
Address string // Address of the contract
|
|
|
|
Network string // Network on which the contract is deployed; default empty "" is Ethereum mainnet
|
|
|
|
StartingBlock int64 // Starting block of the contract
|
|
|
|
LastBlock int64 // Most recent block on the network
|
|
|
|
Abi string // Abi string
|
|
|
|
ParsedAbi abi.ABI // Parsed abi
|
2018-12-19 18:42:59 +00:00
|
|
|
Events map[string]types.Event // List of events to watch
|
|
|
|
Methods []types.Method // List of methods to poll
|
2018-12-18 17:31:21 +00:00
|
|
|
Filters map[string]filters.LogFilter // Map of event filters to their event names; used only for full sync watcher
|
2018-12-07 15:38:46 +00:00
|
|
|
FilterArgs map[string]bool // User-input list of values to filter event logs for
|
|
|
|
MethodArgs map[string]bool // User-input list of values to limit method polling to
|
|
|
|
EmittedAddrs map[interface{}]bool // List of all unique addresses collected from converted event logs
|
|
|
|
EmittedHashes map[interface{}]bool // List of all unique hashes collected from converted event logs
|
|
|
|
CreateAddrList bool // Whether or not to persist address list to postgres
|
2018-12-14 17:52:02 +00:00
|
|
|
CreateHashList bool // Whether or not to persist hash list to postgres
|
|
|
|
Piping bool // Whether or not to pipe method results forward as arguments to subsequent methods
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
// If we will be calling methods that use addr, hash, or byte arrays
|
2018-12-14 17:52:02 +00:00
|
|
|
// as arguments then we initialize maps to hold these types of values
|
2018-12-07 15:38:46 +00:00
|
|
|
func (c Contract) Init() *Contract {
|
|
|
|
for _, method := range c.Methods {
|
|
|
|
for _, arg := range method.Args {
|
|
|
|
switch arg.Type.T {
|
|
|
|
case abi.AddressTy:
|
|
|
|
c.EmittedAddrs = map[interface{}]bool{}
|
2018-12-14 17:52:02 +00:00
|
|
|
case abi.HashTy, abi.BytesTy, abi.FixedBytesTy:
|
2018-12-07 15:38:46 +00:00
|
|
|
c.EmittedHashes = map[interface{}]bool{}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are creating an address list in postgres
|
|
|
|
// we initialize the map despite what method call, if any
|
|
|
|
if c.CreateAddrList {
|
|
|
|
c.EmittedAddrs = map[interface{}]bool{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use contract info to generate event filters - full sync omni watcher only
|
2018-11-07 21:50:43 +00:00
|
|
|
func (c *Contract) GenerateFilters() error {
|
2018-11-04 21:26:39 +00:00
|
|
|
c.Filters = map[string]filters.LogFilter{}
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-11-04 21:26:39 +00:00
|
|
|
for name, event := range c.Events {
|
2018-11-07 21:50:43 +00:00
|
|
|
c.Filters[name] = filters.LogFilter{
|
2019-02-13 19:04:07 +00:00
|
|
|
Name: c.Address + "_" + event.Name,
|
2018-11-07 21:50:43 +00:00
|
|
|
FromBlock: c.StartingBlock,
|
|
|
|
ToBlock: -1,
|
2019-01-04 18:15:22 +00:00
|
|
|
Address: common.HexToAddress(c.Address).Hex(),
|
2018-12-18 17:31:21 +00:00
|
|
|
Topics: core.Topics{event.Sig().Hex()},
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-23 18:12:24 +00:00
|
|
|
// If no filters were generated, throw an error (no point in continuing with this contract)
|
2018-11-04 21:26:39 +00:00
|
|
|
if len(c.Filters) == 0 {
|
|
|
|
return errors.New("error: no filters created")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
// Returns true if address is in list of arguments to
|
2018-11-07 21:50:43 +00:00
|
|
|
// filter events for or if no filtering is specified
|
2018-12-07 15:38:46 +00:00
|
|
|
func (c *Contract) WantedEventArg(arg string) bool {
|
|
|
|
if c.FilterArgs == nil {
|
2018-11-07 21:50:43 +00:00
|
|
|
return false
|
2018-12-07 15:38:46 +00:00
|
|
|
} else if len(c.FilterArgs) == 0 {
|
2018-11-07 21:50:43 +00:00
|
|
|
return true
|
2018-12-07 15:38:46 +00:00
|
|
|
} else if a, ok := c.FilterArgs[arg]; ok {
|
2018-11-07 21:50:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
// Returns true if address is in list of arguments to
|
|
|
|
// poll methods with or if no filtering is specified
|
|
|
|
func (c *Contract) WantedMethodArg(arg interface{}) bool {
|
|
|
|
if c.MethodArgs == nil {
|
2018-11-07 21:50:43 +00:00
|
|
|
return false
|
2018-12-07 15:38:46 +00:00
|
|
|
} else if len(c.MethodArgs) == 0 {
|
2018-11-07 21:50:43 +00:00
|
|
|
return true
|
2018-12-07 15:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolve interface to one of the three types we handle as arguments
|
|
|
|
str := StringifyArg(arg)
|
|
|
|
|
|
|
|
// See if it's hex string has been filtered for
|
|
|
|
if a, ok := c.MethodArgs[str]; ok {
|
2018-11-07 21:50:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
// Returns true if any mapping value matches filtered for address or if no filter exists
|
2018-11-07 21:50:43 +00:00
|
|
|
// Used to check if an event log name-value mapping should be filtered or not
|
|
|
|
func (c *Contract) PassesEventFilter(args map[string]string) bool {
|
|
|
|
for _, arg := range args {
|
2018-12-07 15:38:46 +00:00
|
|
|
if c.WantedEventArg(arg) {
|
2018-11-03 19:00:25 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
// Add event emitted address to our list if it passes filter and method polling is on
|
|
|
|
func (c *Contract) AddEmittedAddr(addresses ...interface{}) {
|
|
|
|
for _, addr := range addresses {
|
|
|
|
if c.WantedMethodArg(addr) && c.Methods != nil {
|
|
|
|
c.EmittedAddrs[addr] = true
|
|
|
|
}
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-07 15:38:46 +00:00
|
|
|
|
|
|
|
// Add event emitted hash to our list if it passes filter and method polling is on
|
|
|
|
func (c *Contract) AddEmittedHash(hashes ...interface{}) {
|
|
|
|
for _, hash := range hashes {
|
|
|
|
if c.WantedMethodArg(hash) && c.Methods != nil {
|
|
|
|
c.EmittedHashes[hash] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StringifyArg(arg interface{}) (str string) {
|
|
|
|
switch arg.(type) {
|
|
|
|
case string:
|
|
|
|
str = arg.(string)
|
|
|
|
case common.Address:
|
|
|
|
a := arg.(common.Address)
|
|
|
|
str = a.String()
|
|
|
|
case common.Hash:
|
|
|
|
a := arg.(common.Hash)
|
|
|
|
str = a.String()
|
|
|
|
case []byte:
|
|
|
|
a := arg.([]byte)
|
|
|
|
str = hexutil.Encode(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|