Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Mainnet - graphql #1

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ end

gem 'annotate', '~> 3.2'
gem 'eth', github: 'wuminzhe/eth.rb'
gem 'pug', github: 'wuminzhe/pug', branch: 'support-tron'
gem 'pug', github: 'wuminzhe/pug', branch: 'tron-models'
# gem 'pug', path: '/workspaces/pug'

gem 'dotenv', '~> 2.8'
gem 'graphiql-rails', group: :development
gem 'graphql'
gem 'kaminari'
23 changes: 15 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ GIT

GIT
remote: https://github.com/wuminzhe/pug.git
revision: f3e79b34c793b03c9e1c725b847548623686de4e
branch: support-tron
revision: 85bc024cb014800ed83b3cd4762da73540b6e59b
branch: tron-models
specs:
pug (0.1.0)
abi_coder_rb (~> 0.2.2)
abi_coder_rb (~> 0.2.4)
etherscan (~> 0.3.0)
rails (>= 7.1.1)

GEM
remote: https://rubygems.org/
specs:
abi_coder_rb (0.2.2)
abi_coder_rb (0.2.4)
activesupport
actioncable (7.1.2)
actionpack (= 7.1.2)
Expand Down Expand Up @@ -141,10 +141,15 @@ GEM
forwardable (1.3.3)
globalid (1.2.1)
activesupport (>= 6.1)
graphiql-rails (1.9.0)
railties
sprockets-rails
graphql (2.2.1)
racc (~> 1.4)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
io-console (0.7.0)
irb (1.10.1)
io-console (0.7.1)
irb (1.11.0)
rdoc
reline (>= 0.3.8)
jbuilder (2.11.5)
Expand Down Expand Up @@ -198,7 +203,7 @@ GEM
openssl (3.2.0)
pg (1.5.4)
pkg-config (1.5.5)
psych (5.1.1.1)
psych (5.1.2)
stringio
public_suffix (5.0.3)
puma (6.4.0)
Expand Down Expand Up @@ -246,7 +251,7 @@ GEM
mini_portile2 (~> 2.8)
pkg-config (~> 1.5)
rubyzip (~> 2.3)
rdoc (6.6.1)
rdoc (6.6.2)
psych (>= 4.0.0)
redis (5.0.8)
redis-client (>= 0.17.0)
Expand Down Expand Up @@ -308,6 +313,8 @@ DEPENDENCIES
debug
dotenv (~> 2.8)
eth!
graphiql-rails
graphql
jbuilder
jsbundling-rails
kaminari
Expand Down
46 changes: 27 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,31 @@
/messages/0x830962b211927e61720770bad65a8be0d56263fe33dc77b04229834a462b2f83
```

# Add new contracts
## add new netowrks and their contracts
1. (optional)update networks rpcs in rails console.
`Pug::Network.find_by(chain_id: 1).update rpc: 'https://ethereum.publicnode.com'`
## Add new network and contract
* Add a network.
```ruby
chain_id = 46
name = 'darwinia'
display_name = 'Darwinia Network'
rpc = 'https://rpc.darwinia.network'
explorer = 'https://darwinia.subscan.io'
Pug::Network.create(chain_id:, name:, display_name:, rpc:, explorer:, scan_span: 2000)
```

* Add a contract.
```bash
ETHERSCAN_API_KEY=#{api_key} bin/rails 'pug:add_contract[#{network.chain_id},#{contract_address}]' --trace
```
NOTE: `pug:add_contract` depends on etherscan(-like) api to get contract creational info.

* Update `Procfile.pug` if a new network was added.
```bash
bin/rails pug:procfile
```

## Start tracing

2. update `config/ormpscan2.yml`.

3. add contracts of the new networks.
`ETHERSCAN_API_KEY=#{api_key} rails 'pug:add_contract[#{network.chain_id},#{contract_address}]' --trace`
NOTE1: you can manually add the contract. or, you can use helper task `rails contracts:add` to add all msgport and ormp contracts.
NOTE2: `pug:add_contract` depends on etherscan(-like) api to get contract creational info.
NOTE3: `ETHERSCAN_API_KEY` is optional, but without it, the api is limited to a small requests per second.

4. update Procfile.pug, then rerun `bin/pug`.
`bin/rails pug:procfile`

5. rerun `bin/rails messages:trace`

## add new contract to an existing network
1. add the contract to the network.
```bash
bin/pug
bin/rails messages:trace
```
52 changes: 52 additions & 0 deletions app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

class GraphqlController < ApplicationController
# If accessing from outside this domain, nullify the session
# This allows for outside API access while preventing CSRF attacks,
# but you'll have to authenticate your user separately
# protect_from_forgery with: :null_session

def execute
variables = prepare_variables(params[:variables])
query = params[:query]
operation_name = params[:operationName]
context = {
# Query context goes here, for example:
# current_user: current_user,
}
result = Ormpscan2Schema.execute(query, variables: variables, context: context, operation_name: operation_name)
render json: result
rescue StandardError => e
raise e unless Rails.env.development?
handle_error_in_development(e)
end

private

# Handle variables in form data, JSON body, or a blank value
def prepare_variables(variables_param)
case variables_param
when String
if variables_param.present?
JSON.parse(variables_param) || {}
else
{}
end
when Hash
variables_param
when ActionController::Parameters
variables_param.to_unsafe_hash # GraphQL-Ruby will validate name and type of incoming variables.
when nil
{}
else
raise ArgumentError, "Unexpected parameter: #{variables_param}"
end
end

def handle_error_in_development(e)
logger.error e.message
logger.error e.backtrace.join("\n")

render json: { errors: [{ message: e.message, backtrace: e.backtrace }], data: {} }, status: 500
end
end
Empty file added app/graphql/mutations/.keep
Empty file.
10 changes: 10 additions & 0 deletions app/graphql/mutations/base_mutation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Mutations
class BaseMutation < GraphQL::Schema::RelayClassicMutation
argument_class Types::BaseArgument
field_class Types::BaseField
input_object_class Types::BaseInputObject
object_class Types::BaseObject
end
end
42 changes: 42 additions & 0 deletions app/graphql/ormpscan2_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

class Ormpscan2Schema < GraphQL::Schema
mutation(Types::MutationType)
query(Types::QueryType)

# For batch-loading (see https://graphql-ruby.org/dataloader/overview.html)
use GraphQL::Dataloader

# GraphQL-Ruby calls this when something goes wrong while running a query:
def self.type_error(err, context)
# if err.is_a?(GraphQL::InvalidNullError)
# # report to your bug tracker here
# return nil
# end
super
end

# Union and Interface Resolution
def self.resolve_type(abstract_type, obj, ctx)
# TODO: Implement this method
# to return the correct GraphQL object type for `obj`
raise(GraphQL::RequiredImplementationMissingError)
end

# Stop validating when it encounters this many errors:
validate_max_errors(100)

# Relay-style Object Identification:

# Return a string UUID for `object`
def self.id_from_object(object, type_definition, query_ctx)
# For example, use Rails' GlobalID library (https://github.com/rails/globalid):
object.to_gid_param
end

# Given a string UUID, find the object
def self.object_from_id(global_id, query_ctx)
# For example, use Rails' GlobalID library (https://github.com/rails/globalid):
GlobalID.find(global_id)
end
end
4 changes: 4 additions & 0 deletions app/graphql/resolvers/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Types
class BaseResolver < GraphQL::Schema::Resolver
end
end
Empty file added app/graphql/types/.keep
Empty file.
6 changes: 6 additions & 0 deletions app/graphql/types/base_argument.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Types
class BaseArgument < GraphQL::Schema::Argument
end
end
8 changes: 8 additions & 0 deletions app/graphql/types/base_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Types
class BaseConnection < Types::BaseObject
# add `nodes` and `pageInfo` fields, as well as `edge_type(...)` and `node_nullable(...)` overrides
include GraphQL::Types::Relay::ConnectionBehaviors
end
end
8 changes: 8 additions & 0 deletions app/graphql/types/base_edge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Types
class BaseEdge < Types::BaseObject
# add `node` and `cursor` fields, as well as `node_type(...)` override
include GraphQL::Types::Relay::EdgeBehaviors
end
end
6 changes: 6 additions & 0 deletions app/graphql/types/base_enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Types
class BaseEnum < GraphQL::Schema::Enum
end
end
7 changes: 7 additions & 0 deletions app/graphql/types/base_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Types
class BaseField < GraphQL::Schema::Field
argument_class Types::BaseArgument
end
end
7 changes: 7 additions & 0 deletions app/graphql/types/base_input_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Types
class BaseInputObject < GraphQL::Schema::InputObject
argument_class Types::BaseArgument
end
end
11 changes: 11 additions & 0 deletions app/graphql/types/base_interface.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Types
module BaseInterface
include GraphQL::Schema::Interface
edge_type_class(Types::BaseEdge)
connection_type_class(Types::BaseConnection)

field_class Types::BaseField
end
end
9 changes: 9 additions & 0 deletions app/graphql/types/base_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Types
class BaseObject < GraphQL::Schema::Object
edge_type_class(Types::BaseEdge)
connection_type_class(Types::BaseConnection)
field_class Types::BaseField
end
end
6 changes: 6 additions & 0 deletions app/graphql/types/base_scalar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Types
class BaseScalar < GraphQL::Schema::Scalar
end
end
8 changes: 8 additions & 0 deletions app/graphql/types/base_union.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Types
class BaseUnion < GraphQL::Schema::Union
edge_type_class(Types::BaseEdge)
connection_type_class(Types::BaseConnection)
end
end
35 changes: 35 additions & 0 deletions app/graphql/types/message_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Types
class MessageType < Types::BaseObject
field :id, ID, null: false
field :index, Integer
field :msg_hash, String
field :root, String
field :channel, String
field :from_chain_id, Integer
field :from, String
field :to_chain_id, Integer
field :to, String
field :block_number, Integer
field :block_timestamp, Integer
field :transaction_hash, String
field :status, Integer
field :encoded, String
field :from_network_id, Integer
field :to_network_id, Integer
field :dispatch_transaction_hash, String
field :dispatch_block_number, Integer
field :dispatch_block_timestamp, Integer
field :clear_transaction_hash, String
field :clear_block_number, Integer
field :clear_block_timestamp, Integer
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
field :proof, GraphQL::Types::JSON
field :msgport_payload, String
field :msgport_from, String
field :msgport_to, String
field :gas_limit, Integer
end
end
12 changes: 12 additions & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Types
class MutationType < Types::BaseObject
# TODO: remove me
field :test_field, String, null: false,
description: "An example field added by the generator"
def test_field
"Hello World"
end
end
end
9 changes: 9 additions & 0 deletions app/graphql/types/node_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Types
module NodeType
include Types::BaseInterface
# Add the `id` field
include GraphQL::Types::Relay::NodeBehaviors
end
end
Loading