-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: create noauth client for blockscout on etherum mainnet (#568)
* create etherscan tests * test: get transactions * test: get transaction by hash * feat: blockscout client * create blockscout auth client * fix linting * fix linting * turn off whynotlint * fix linting * add test
- Loading branch information
Showing
43 changed files
with
1,234 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package settings //nolint: dupl | ||
|
||
import ( | ||
"github.com/mailchain/mailchain/cmd/internal/settings/output" | ||
"github.com/mailchain/mailchain/cmd/internal/settings/values" | ||
"github.com/mailchain/mailchain/cmd/mailchain/internal/settings/defaults" | ||
"github.com/mailchain/mailchain/internal/clients/blockscout" | ||
"github.com/mailchain/mailchain/internal/mailbox" | ||
"github.com/mailchain/mailchain/internal/protocols/ethereum" | ||
) | ||
|
||
// BlockscoutReceiver configuration element. | ||
type BlockscoutReceiver struct { | ||
kind string | ||
EnabledProtocolNetworks values.StringSlice | ||
APIKey values.String | ||
} | ||
|
||
func blockscoutReceiverNoAuth(s values.Store) *BlockscoutReceiver { | ||
return blockscoutReceiverAny(s, defaults.ClientBlockscoutNoAuth) | ||
} | ||
|
||
func blockscoutReceiverAny(s values.Store, kind string) *BlockscoutReceiver { | ||
return &BlockscoutReceiver{ | ||
kind: kind, | ||
EnabledProtocolNetworks: values.NewDefaultStringSlice( | ||
[]string{"ethereum/" + ethereum.Mainnet}, | ||
s, | ||
"receivers."+kind+".enabled-networks", | ||
), | ||
APIKey: values.NewDefaultString("", s, "receivers."+kind+".api-key"), | ||
} | ||
} | ||
|
||
// Supports a map of what protocol and network combinations are supported. | ||
func (r BlockscoutReceiver) Supports() map[string]bool { | ||
m := map[string]bool{} | ||
for _, np := range r.EnabledProtocolNetworks.Get() { | ||
m[np] = true | ||
} | ||
|
||
return m | ||
} | ||
|
||
// Produce `mailbox.Receiver` based on configuration settings. | ||
func (r BlockscoutReceiver) Produce() (mailbox.Receiver, error) { | ||
return blockscout.NewAPIClient(r.APIKey.Get()) | ||
} | ||
|
||
// Output configuration as an `output.Element` for use in exporting configuration. | ||
func (r BlockscoutReceiver) Output() output.Element { | ||
return output.Element{ | ||
FullName: r.kind, | ||
Attributes: []output.Attribute{ | ||
r.EnabledProtocolNetworks.Attribute(), | ||
r.APIKey.Attribute(), | ||
}, | ||
} | ||
} |
174 changes: 174 additions & 0 deletions
174
cmd/mailchain/internal/settings/blockscout_receiver_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
package settings | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/mailchain/mailchain/cmd/internal/settings/values" | ||
"github.com/mailchain/mailchain/cmd/internal/settings/values/valuestest" | ||
"github.com/mailchain/mailchain/internal/clients/blockscout" | ||
"github.com/mailchain/mailchain/internal/mailbox" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_blockscoutReceiverAny(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
type args struct { | ||
s values.Store | ||
kind string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantEnabledProtocolNetworks []string | ||
wantAPIKey string | ||
}{ | ||
{ | ||
"success", | ||
args{ | ||
func() values.Store { | ||
m := valuestest.NewMockStore(mockCtrl) | ||
m.EXPECT().IsSet("receivers.type.enabled-networks").Return(false) | ||
m.EXPECT().IsSet("receivers.type.api-key").Return(false) | ||
return m | ||
}(), | ||
"type", | ||
}, | ||
[]string{"ethereum/mainnet"}, | ||
"", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := blockscoutReceiverAny(tt.args.s, tt.args.kind) | ||
assert.Equal(t, tt.wantEnabledProtocolNetworks, got.EnabledProtocolNetworks.Get()) | ||
assert.Equal(t, tt.wantAPIKey, got.APIKey.Get()) | ||
}) | ||
} | ||
} | ||
|
||
func Test_blockscoutReceiverNoAuth(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
type args struct { | ||
s values.Store | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantEnabledProtocolNetworks []string | ||
wantAPIKey string | ||
}{ | ||
{ | ||
"success", | ||
args{ | ||
func() values.Store { | ||
m := valuestest.NewMockStore(mockCtrl) | ||
m.EXPECT().IsSet("receivers.blockscout-no-auth.enabled-networks").Return(false) | ||
m.EXPECT().IsSet("receivers.blockscout-no-auth.api-key").Return(false) | ||
return m | ||
}(), | ||
}, | ||
[]string{"ethereum/mainnet"}, | ||
"", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := blockscoutReceiverNoAuth(tt.args.s) | ||
assert.Equal(t, tt.wantEnabledProtocolNetworks, got.EnabledProtocolNetworks.Get()) | ||
assert.Equal(t, tt.wantAPIKey, got.APIKey.Get()) | ||
}) | ||
} | ||
} | ||
|
||
func TestBlockscoutReceiver_Supports(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
type fields struct { | ||
EnabledProtocolNetworks values.StringSlice | ||
APIKey values.String | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want map[string]bool | ||
}{ | ||
{ | ||
"success", | ||
fields{ | ||
func() values.StringSlice { | ||
m := valuestest.NewMockStringSlice(mockCtrl) | ||
m.EXPECT().Get().Return([]string{"ethereum/mainnet", "ethereum/ropsten"}) | ||
return m | ||
}(), | ||
nil, | ||
}, | ||
map[string]bool{"ethereum/mainnet": true, "ethereum/ropsten": true}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := BlockscoutReceiver{ | ||
EnabledProtocolNetworks: tt.fields.EnabledProtocolNetworks, | ||
APIKey: tt.fields.APIKey, | ||
} | ||
if got := r.Supports(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("blockscoutReceiver.Supports() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestBlockscoutReceiver_Produce(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
type fields struct { | ||
kind string | ||
EnabledProtocolNetworks values.StringSlice | ||
APIKey values.String | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want mailbox.Receiver | ||
wantErr bool | ||
}{ | ||
{ | ||
"success", | ||
fields{ | ||
"test", | ||
func() values.StringSlice { | ||
m := valuestest.NewMockStringSlice(mockCtrl) | ||
return m | ||
}(), | ||
func() values.String { | ||
m := valuestest.NewMockString(mockCtrl) | ||
m.EXPECT().Get().Return("apikey") | ||
return m | ||
}(), | ||
}, | ||
&blockscout.APIClient{}, | ||
false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := BlockscoutReceiver{ | ||
kind: tt.fields.kind, | ||
EnabledProtocolNetworks: tt.fields.EnabledProtocolNetworks, | ||
APIKey: tt.fields.APIKey, | ||
} | ||
got, err := r.Produce() | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("BlockscoutReceiver.Produce() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !assert.IsType(t, tt.want, got) { | ||
t.Errorf("BlockscoutReceiver.Produce() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.