Skip to content

Commit

Permalink
fix codegen on failing dynamic contract ABI fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
sduchesneau committed Jul 3, 2024
1 parent 3fcb39a commit 06f47e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ethfull/apicalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func getContractABI(ctx context.Context, address string, endpoint string) (*eth.

res, err := httpClient.Do(req)
if err != nil {
return nil, "", nil, fmt.Errorf("getting contract abi from etherscan: %w", err)
return nil, "", nil, fmt.Errorf("getting contract abi: %w", err)
}
defer res.Body.Close()

Expand Down
22 changes: 19 additions & 3 deletions ethfull/convo.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Supported networks: `+strings.Join(supportedChains, ", "),

func NewWithSQL(factory *codegen.MsgWrapFactory) codegen.Conversation {
h := &Convo{
state: &Project{},
state: &Project{currentContractIdx: -1},
factory: factory,
outputType: outputTypeSQL,
remoteBuildState: &codegen.RemoteBuildState{},
Expand All @@ -72,7 +72,7 @@ func NewWithSQL(factory *codegen.MsgWrapFactory) codegen.Conversation {

func NewWithSubgraph(factory *codegen.MsgWrapFactory) codegen.Conversation {
h := &Convo{
state: &Project{},
state: &Project{currentContractIdx: -1},
factory: factory,
outputType: outputTypeSubgraph,
remoteBuildState: &codegen.RemoteBuildState{},
Expand All @@ -94,7 +94,7 @@ func cmd(msg any) loop.Cmd {
// This function does NOT mutate anything. Only reads.

func (c *Convo) contextContract() *Contract {
if c.state.currentContractIdx > len(c.state.Contracts)-1 {
if c.state.currentContractIdx == -1 || c.state.currentContractIdx > len(c.state.Contracts)-1 {
return nil
}
return c.state.Contracts[c.state.currentContractIdx]
Expand Down Expand Up @@ -208,6 +208,9 @@ func (p *Project) NextStep() (out loop.Cmd) {
if dynContract.abi == nil {
if dynContract.RawABI == nil {
if dynContract.referenceContractAddress == "" {
if p.ChainConfig().ApiEndpoint == "" {
return notifyContext(cmd(AskDynamicContractABI{}))
}
return notifyContext(cmd(AskDynamicContractAddress{}))
}
return notifyContext(cmd(FetchDynamicContractABI{}))
Expand Down Expand Up @@ -389,6 +392,15 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd {
return c.action(InputContractABI{}).TextInput("Please paste the contract ABI", "Submit").
Cmd()

case AskDynamicContractABI:
contract := c.contextContract()
if contract == nil {
return QuitInvalidContext
}

return c.action(InputDynamicContractABI{}).TextInput(fmt.Sprintf("Please paste the ABI for contracts that will be created by the event %q", contract.FactoryCreationEventName()), "Submit").
Cmd()

case InputContractABI:
// FIXME: dedupe all these QuitInvalidContext!
contract := c.contextContract()
Expand Down Expand Up @@ -480,6 +492,10 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd {
return QuitInvalidContext
}
contract := c.state.dynamicContractOf(factory.Name)
config := c.state.ChainConfig()
if config.ApiEndpoint == "" {
return cmd(AskDynamicContractABI{})
}
return func() loop.Msg {
abi, err := contract.FetchABI(c.state.ChainConfig())
return ReturnFetchDynamicContractABI{abi: abi, err: err}
Expand Down
4 changes: 4 additions & 0 deletions ethfull/convo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func TestContractNameAlreadyExists(t *testing.T) {
f := &codegen.MsgWrapFactory{}
conv := NewWithSQL(f).(*Convo)
p := conv.state
p.currentContractIdx = 0
p.Contracts = append(p.Contracts, &Contract{
BaseContract: BaseContract{
Name: "test",
Expand All @@ -239,6 +240,7 @@ func TestDynamicContractNameAlreadyExists(t *testing.T) {
f := &codegen.MsgWrapFactory{}
conv := NewWithSQL(f).(*Convo)
p := conv.state
p.currentContractIdx = 0
p.Contracts = append(p.Contracts, &Contract{
BaseContract: BaseContract{
Name: "test",
Expand All @@ -259,6 +261,7 @@ func TestContractAddressAlreadyExists(t *testing.T) {
f := &codegen.MsgWrapFactory{}
conv := NewWithSQL(f).(*Convo)
p := conv.state
p.currentContractIdx = 0
p.Contracts = append(p.Contracts, &Contract{
Address: "0x1f98431c8ad98523631ae4a59f267346ea31f984",
})
Expand All @@ -277,6 +280,7 @@ func TestDynamicContractAddressAlreadyExists(t *testing.T) {
f := &codegen.MsgWrapFactory{}
conv := NewWithSQL(f).(*Convo)
p := conv.state
p.currentContractIdx = 0
p.Contracts = append(p.Contracts, &Contract{
Address: "0x1f98431c8ad98523631ae4a59f267346ea31f984",
})
Expand Down

0 comments on commit 06f47e0

Please sign in to comment.