2018-11-03 19:00:25 +00:00
|
|
|
// Copyright 2018 Vulcanize
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/types"
|
|
|
|
)
|
|
|
|
|
2018-11-03 19:02:31 +00:00
|
|
|
// Parser is used to fetch and parse contract ABIs
|
|
|
|
// It is dependent on etherscan's api
|
2018-11-03 19:00:25 +00:00
|
|
|
type Parser interface {
|
|
|
|
Parse(contractAddr string) error
|
2018-11-03 19:02:31 +00:00
|
|
|
Abi() string
|
|
|
|
ParsedAbi() abi.ABI
|
2018-11-03 19:00:25 +00:00
|
|
|
GetMethods() map[string]*types.Method
|
|
|
|
GetEvents() map[string]*types.Event
|
|
|
|
}
|
|
|
|
|
|
|
|
type parser struct {
|
|
|
|
client *geth.EtherScanAPI
|
|
|
|
abi string
|
|
|
|
parsedAbi abi.ABI
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewParser(network string) *parser {
|
|
|
|
url := geth.GenURL(network)
|
|
|
|
|
|
|
|
return &parser{
|
|
|
|
client: geth.NewEtherScanClient(url),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 19:02:31 +00:00
|
|
|
func (p *parser) Abi() string {
|
2018-11-03 19:00:25 +00:00
|
|
|
return p.abi
|
|
|
|
}
|
|
|
|
|
2018-11-03 19:02:31 +00:00
|
|
|
func (p *parser) ParsedAbi() abi.ABI {
|
2018-11-03 19:00:25 +00:00
|
|
|
return p.parsedAbi
|
|
|
|
}
|
|
|
|
|
2018-11-03 19:02:31 +00:00
|
|
|
// Retrieves and parses the abi string
|
2018-11-04 21:26:39 +00:00
|
|
|
// for the given contract address
|
2018-11-03 19:00:25 +00:00
|
|
|
func (p *parser) Parse(contractAddr string) error {
|
|
|
|
abiStr, err := p.client.GetAbi(contractAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.abi = abiStr
|
|
|
|
p.parsedAbi, err = geth.ParseAbi(abiStr)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-03 19:02:31 +00:00
|
|
|
// Parses methods into our custom method type and returns
|
2018-11-03 19:00:25 +00:00
|
|
|
func (p *parser) GetMethods() map[string]*types.Method {
|
|
|
|
methods := map[string]*types.Method{}
|
|
|
|
|
|
|
|
for _, m := range p.parsedAbi.Methods {
|
|
|
|
method := types.NewMethod(m)
|
|
|
|
methods[m.Name] = method
|
|
|
|
}
|
|
|
|
|
|
|
|
return methods
|
|
|
|
}
|
|
|
|
|
2018-11-03 19:02:31 +00:00
|
|
|
// Parses events into our custom event type and returns
|
2018-11-03 19:00:25 +00:00
|
|
|
func (p *parser) GetEvents() map[string]*types.Event {
|
|
|
|
events := map[string]*types.Event{}
|
|
|
|
|
|
|
|
for _, e := range p.parsedAbi.Events {
|
|
|
|
event := types.NewEvent(e)
|
|
|
|
events[e.Name] = event
|
|
|
|
}
|
|
|
|
|
|
|
|
return events
|
|
|
|
}
|