From dcbbc2773efe2119b678b3c395f775d447c0dec5 Mon Sep 17 00:00:00 2001 From: cfal Date: Wed, 18 Dec 2024 15:30:58 +0700 Subject: [PATCH] add to graphql --- core/config/docs/chains-tron.toml | 8 +- core/web/resolver/resolver_test.go | 2 + core/web/resolver/tron_key.go | 43 +++++++++++ core/web/resolver/tron_key_test.go | 74 +++++++++++++++++++ core/web/schema/schema.graphql | 1 + core/web/schema/type/tron_key.graphql | 7 ++ .../nodeclient/chainlink_models.go | 4 +- 7 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 core/web/resolver/tron_key.go create mode 100644 core/web/resolver/tron_key_test.go create mode 100644 core/web/schema/type/tron_key.graphql diff --git a/core/config/docs/chains-tron.toml b/core/config/docs/chains-tron.toml index 226c26366e1..55a44bacd7a 100644 --- a/core/config/docs/chains-tron.toml +++ b/core/config/docs/chains-tron.toml @@ -1,13 +1,13 @@ [[Tron]] # ChainID is the Tron chain ID. ChainID = 'foobar' # Example -# FeederURL is required to get tx metadata (that the RPC can't) -FeederURL = 'http://feeder.url' # Example # Enabled enables this chain. Enabled = true # Default [[Tron.Nodes]] # Name is a unique (per-chain) identifier for this node. Name = 'primary' # Example -# URL is the base HTTP(S) endpoint for this node. -URL = 'http://api.trongrid.io' # Example \ No newline at end of file +# URL is the full node HTTP endpoint for this node. +URL = 'https://api.trongrid.io/wallet' # Example +# SolidityURL is the solidity node HTTP endpoint for this node. +SolidityURL = 'http://api.trongrid.io/wallet' # Example diff --git a/core/web/resolver/resolver_test.go b/core/web/resolver/resolver_test.go index 0d365b0891e..6ff9d954b37 100644 --- a/core/web/resolver/resolver_test.go +++ b/core/web/resolver/resolver_test.go @@ -54,6 +54,7 @@ type mocks struct { aptos *keystoreMocks.Aptos cosmos *keystoreMocks.Cosmos starknet *keystoreMocks.StarkNet + tron *keystoreMocks.Tron chain *legacyEvmORMMocks.Chain legacyEVMChains *legacyEvmORMMocks.LegacyChainContainer relayerChainInterops *chainlinkMocks.FakeRelayerChainInteroperators @@ -112,6 +113,7 @@ func setupFramework(t *testing.T) *gqlTestFramework { aptos: keystoreMocks.NewAptos(t), cosmos: keystoreMocks.NewCosmos(t), starknet: keystoreMocks.NewStarkNet(t), + tron: keystoreMocks.NewTron(t), chain: legacyEvmORMMocks.NewChain(t), legacyEVMChains: legacyEvmORMMocks.NewLegacyChainContainer(t), relayerChainInterops: &chainlinkMocks.FakeRelayerChainInteroperators{}, diff --git a/core/web/resolver/tron_key.go b/core/web/resolver/tron_key.go new file mode 100644 index 00000000000..595749e2f9a --- /dev/null +++ b/core/web/resolver/tron_key.go @@ -0,0 +1,43 @@ +package resolver + +import ( + "github.com/graph-gophers/graphql-go" + + "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/tronkey" +) + +type TronKeyResolver struct { + key tronkey.Key +} + +func NewTronKey(key tronkey.Key) *TronKeyResolver { + return &TronKeyResolver{key: key} +} + +func NewTronKeys(keys []tronkey.Key) []*TronKeyResolver { + var resolvers []*TronKeyResolver + + for _, k := range keys { + resolvers = append(resolvers, NewTronKey(k)) + } + + return resolvers +} + +func (r *TronKeyResolver) ID() graphql.ID { + return graphql.ID(r.key.ID()) +} + +// -- GetTronKeys Query -- + +type TronKeysPayloadResolver struct { + keys []tronkey.Key +} + +func NewTronKeysPayload(keys []tronkey.Key) *TronKeysPayloadResolver { + return &TronKeysPayloadResolver{keys: keys} +} + +func (r *TronKeysPayloadResolver) Results() []*TronKeyResolver { + return NewTronKeys(r.keys) +} diff --git a/core/web/resolver/tron_key_test.go b/core/web/resolver/tron_key_test.go new file mode 100644 index 00000000000..6ccbeb1072d --- /dev/null +++ b/core/web/resolver/tron_key_test.go @@ -0,0 +1,74 @@ +package resolver + +import ( + "context" + "errors" + "fmt" + "testing" + + gqlerrors "github.com/graph-gophers/graphql-go/errors" + + "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/keystest" + "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/tronkey" +) + +func TestResolver_TronKeys(t *testing.T) { + t.Parallel() + + query := ` + query GetTronKeys { + tronKeys { + results { + id + } + } + }` + k := tronkey.MustNewInsecure(keystest.NewRandReaderFromSeed(1)) + result := fmt.Sprintf(` + { + "tronKeys": { + "results": [ + { + "id": "%s" + } + ] + } + }`, k.ID()) + gError := errors.New("error") + + testCases := []GQLTestCase{ + unauthorizedTestCase(GQLTestCase{query: query}, "tronKeys"), + { + name: "success", + authenticated: true, + before: func(ctx context.Context, f *gqlTestFramework) { + f.Mocks.tron.On("GetAll").Return([]tronkey.Key{k}, nil) + f.Mocks.keystore.On("Tron").Return(f.Mocks.tron) + f.App.On("GetKeyStore").Return(f.Mocks.keystore) + }, + query: query, + result: result, + }, + { + name: "no keys returned by GetAll", + authenticated: true, + before: func(ctx context.Context, f *gqlTestFramework) { + f.Mocks.tron.On("GetAll").Return([]tronkey.Key{}, gError) + f.Mocks.keystore.On("Tron").Return(f.Mocks.tron) + f.App.On("GetKeyStore").Return(f.Mocks.keystore) + }, + query: query, + result: `null`, + errors: []*gqlerrors.QueryError{ + { + Extensions: nil, + ResolverError: gError, + Path: []interface{}{"tronKeys"}, + Message: gError.Error(), + }, + }, + }, + } + + RunGQLTests(t, testCases) +} diff --git a/core/web/schema/schema.graphql b/core/web/schema/schema.graphql index 568716f7b76..6f9e51b79eb 100644 --- a/core/web/schema/schema.graphql +++ b/core/web/schema/schema.graphql @@ -36,6 +36,7 @@ type Query { aptosKeys: AptosKeysPayload! cosmosKeys: CosmosKeysPayload! starknetKeys: StarkNetKeysPayload! + tronKeys: TronKeysPayload! sqlLogging: GetSQLLoggingPayload! vrfKey(id: ID!): VRFKeyPayload! vrfKeys: VRFKeysPayload! diff --git a/core/web/schema/type/tron_key.graphql b/core/web/schema/type/tron_key.graphql new file mode 100644 index 00000000000..e7319f08d6b --- /dev/null +++ b/core/web/schema/type/tron_key.graphql @@ -0,0 +1,7 @@ +type TronKey { + id: ID! +} + +type TronKeysPayload { + results: [TronKey!]! +} diff --git a/deployment/environment/nodeclient/chainlink_models.go b/deployment/environment/nodeclient/chainlink_models.go index a4430fc02bd..1f4bbe4ebd0 100644 --- a/deployment/environment/nodeclient/chainlink_models.go +++ b/deployment/environment/nodeclient/chainlink_models.go @@ -531,7 +531,7 @@ type TronChainConfig struct { // TronChainAttributes is the model that represents the tron chain type TronChainAttributes struct { - ChainID string `json:"chainID"` + ChainID string `json:"chainID"` Config TronChainConfig `json:"config"` } @@ -549,7 +549,7 @@ type TronChainCreate struct { type TronNodeAttributes struct { Name string `json:"name"` ChainID string `json:"chainId"` - Url string `json:"url"` + URL string `json:"url"` } // TronNode is the model that represents the tron node when read