Skip to content

Add a GetABI() method to abigen v2 or expose abi as ABI. #34705

@mpolitzer

Description

@mpolitzer

Rationale

The current implementation requires that:

  1. either the generated binds share a package with their users; or that
  2. users call ParseABI() themselves to access the abi.

Having abi exposed as ABI or a GetABI() method would simplify client code.

Here is a section of the documentation on ethereum website showing direct abi access during Storage construction in a way that other packages are not allowed to do (abi accessible).

	...
	// create a BoundContract instance to interact with the pending contract
	storageABI, _ := StorageMetaData.ParseABI()
	contract := Storage{*storageABI}
	instance := contract.Instance(conn, address)
	...

ref. https://geth.ethereum.org/docs/developers/dapp-developer/native-bindings-v2

Why should this feature exist?

Better ergonomics. Make the API simpler to use.

What are the use-cases?

Simplify ABI access. For instance when filling the topics for a filter query with multiple contracts.
Here is a hypothetical call whith it implemented.

	contractA := ContractA.NewContractA()
	contractB := ContractB.NewContractB()

	fq := ethereum.FilterQuery{
		Topics: [][]common.Hash{
			contractA.GetABI().Events[ContractA.ContractAFooEventName].ID,
			contractB.GetABI().Events[ContractB.ContractBBarEventName].ID,
		},
	}

Implementation

Do you have ideas regarding the implementation of this feature? Yes, super simple.
Are you willing to implement this feature? Sure, here is a public domain patch.

diff --git a/accounts/abi/abigen/source2.go.tpl b/accounts/abi/abigen/source2.go.tpl
index 3d98cbb70..737d78a7f 100644
--- a/accounts/abi/abigen/source2.go.tpl
+++ b/accounts/abi/abigen/source2.go.tpl
@@ -59,6 +59,10 @@ var (
 		abi abi.ABI
 	}
 
+	func (c *{{.Type}}) GetABI() abi.ABI {
+		return c.abi
+	}
+
 	// New{{.Type}} creates a new instance of {{.Type}}.
 	func New{{.Type}}() *{{.Type}} {
 		parsed, err := {{.Type}}MetaData.ParseABI()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions