github.com/0xsequence/ethkit@v1.25.0/ethartifact/registry.go (about) 1 package ethartifact 2 3 import ( 4 "fmt" 5 "sort" 6 7 "github.com/0xsequence/ethkit/ethcontract" 8 "github.com/0xsequence/ethkit/go-ethereum/accounts/abi" 9 ) 10 11 func NewContractRegistry() *ContractRegistry { 12 return &ContractRegistry{ 13 contracts: map[string]Artifact{}, 14 names: []string{}, 15 } 16 } 17 18 type ContractRegistry struct { 19 contracts map[string]Artifact 20 names []string // index of contract names in the map 21 } 22 23 func (c *ContractRegistry) Add(artifact Artifact) error { 24 if c.contracts == nil { 25 c.contracts = map[string]Artifact{} 26 } 27 if artifact.ContractName == "" { 28 return fmt.Errorf("unable to register contract with empty name") 29 } 30 c.contracts[artifact.ContractName] = artifact 31 c.names = append(c.names, artifact.ContractName) 32 sort.Sort(sort.StringSlice(c.names)) 33 return nil 34 } 35 36 func (c *ContractRegistry) Register(contractName string, contractABI abi.ABI, contractBin []byte) (Artifact, error) { 37 r := Artifact{ContractName: contractName, ABI: contractABI, Bin: contractBin} 38 err := c.Add(r) 39 if err != nil { 40 return Artifact{}, err 41 } 42 return r, nil 43 } 44 45 func (s *ContractRegistry) RegisterJSON(contractName string, contractABIJSON string, contractBin []byte) (Artifact, error) { 46 parsedABI, err := ethcontract.ParseABI(contractABIJSON) 47 if err != nil { 48 return Artifact{}, err 49 } 50 return s.Register(contractName, parsedABI, contractBin) 51 } 52 53 func (c *ContractRegistry) MustAdd(contractABI Artifact) { 54 err := c.Add(contractABI) 55 if err != nil { 56 panic(err) 57 } 58 } 59 60 func (c *ContractRegistry) MustRegister(contractName string, contractABI abi.ABI, contractBin []byte) Artifact { 61 r, err := c.Register(contractName, contractABI, contractBin) 62 if err != nil { 63 panic(err) 64 } 65 return r 66 } 67 68 func (c *ContractRegistry) MustRegisterJSON(contractName string, contractABIJSON string, contractBin []byte) Artifact { 69 r, err := c.RegisterJSON(contractName, contractABIJSON, contractBin) 70 if err != nil { 71 panic(err) 72 } 73 return r 74 } 75 76 func (c *ContractRegistry) MustGet(name string) Artifact { 77 artifact, ok := c.Get(name) 78 if !ok { 79 panic(fmt.Sprintf("ethartifact: ContractRegistry#MustGet failed to get '%s'", name)) 80 } 81 return artifact 82 } 83 84 func (c *ContractRegistry) ContractNames() []string { 85 return c.names 86 } 87 88 func (c *ContractRegistry) Get(name string) (Artifact, bool) { 89 artifact, ok := c.contracts[name] 90 return artifact, ok 91 } 92 93 func (c *ContractRegistry) Encode(contractName, method string, args ...interface{}) ([]byte, error) { 94 if c.contracts == nil { 95 return nil, fmt.Errorf("contract registry cannot find contract %s", contractName) 96 } 97 artifact, ok := c.contracts[contractName] 98 if !ok { 99 return nil, fmt.Errorf("contract registry cannot find contract %s", contractName) 100 } 101 return artifact.ABI.Pack(method, args...) 102 }