Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #932 from seanyoung/vent-abi-changes
Browse files Browse the repository at this point in the history
Allow reading abi from abi file or bin file and merging of abis
  • Loading branch information
gregdhill committed Oct 8, 2018
2 parents bff9a96 + e027ea3 commit 8e5e378
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion execution/evm/abi/abi.go
Expand Up @@ -846,6 +846,9 @@ func readArgSpec(argsJ []ArgumentJSON) ([]Argument, error) {
args[i].EVM = EVMBytes{M: 0}
case "string":
args[i].EVM = EVMString{}
default:
// Assume it is a type of Contract
args[i].EVM = EVMAddress{}
}
}

Expand All @@ -856,7 +859,15 @@ func ReadAbiSpec(specBytes []byte) (*AbiSpec, error) {
var specJ []AbiSpecJSON
err := json.Unmarshal(specBytes, &specJ)
if err != nil {
return nil, err
// The abi spec file might a bin file, with the Abi under the Abi field in json
var binFile struct {
Abi []AbiSpecJSON
}
err = json.Unmarshal(specBytes, &binFile)
if err != nil {
return nil, err
}
specJ = binFile.Abi
}

abiSpec := AbiSpec{
Expand Down Expand Up @@ -919,6 +930,32 @@ func ReadAbiSpecFile(filename string) (*AbiSpec, error) {
return ReadAbiSpec(specBytes)
}

// MergeAbiSpec takes multiple AbiSpecs and merges them into once structure. Note that
// the same function name or event name can occur in different abis, so there might be
// some information loss.
func MergeAbiSpec(abiSpec []*AbiSpec) *AbiSpec {
newSpec := AbiSpec{
Events: make(map[string]EventSpec),
EventsById: make(map[EventID]EventSpec),
Functions: make(map[string]FunctionSpec),
}

for _, s := range abiSpec {
for n, f := range s.Functions {
newSpec.Functions[n] = f
}

// Different Abis can have the Event name, but with a different signature
// Loop over the signatures, as these are less likely to have collisions
for _, e := range s.EventsById {
newSpec.Events[e.Name] = e
newSpec.EventsById[e.EventID] = e
}
}

return &newSpec
}

func EVMTypeFromReflect(v reflect.Type) Argument {
arg := Argument{Name: v.Name()}

Expand Down

0 comments on commit 8e5e378

Please sign in to comment.