This repo is an example of how to build a QuickNode Marketplace add-on using Ruby on Rails and PostgreSQL.
It implements the 4 provisioning routes that a partner needs to integrate with Marketplace, as well as the required Healthcheck route.
It also has support for:
- RPC methods via a
POST /rpcroute - A dashboard view with Single Sign On using JSON Web Tokens (JWT).
To install and run the application locally:
- Clone this repo.
bundle- Update the this README.md file to have the name of your add-on, etc...
- Update config/database.yml, config/cable.yml, and config/envirotnments/production.rb file and replace
marketplace_starter_railswith the name of your application. cp .env.example .envand then update as you see fit.bin/rails db:createbin/rails db:migrate- Edit your credentials/secrets using
bin/rails credentials:edit. - Install foreman:
gem install foreman - Run the specs with
bin/ciand check spec coverage withopen coverage/index.html - Start the application in development mode with
bin/dev - Visit http://localhost:3009/
The application has 4 provisioning routes protected by HTTP Basic Auth:
POST /provisionPUT /updateDELETE /deactivate_endpointDELETE /deprovision
It has a public healthcheck route that returns 200 if the service and the database is up and running:
GET /healthcheck
It has a dashboard that can be accessed using Single Sign On with JSON Web Token (JWT):
GET /dash/:id?jwt=foobar
It has an JSON RPC route:
POST /rpc
To add a new RPC method, simply add a new file to the app/services/rpc_method_handlers directory following the naming convention where the filename should be the name of the method lowercased and using _ (for example: eth_send_raw_transaction or qn_get_token_balance) and the class should be inside the RPCMethodHandlers module and have a name in camel case (for example, EthSendRawTransaction or QnGetTokenBalance). These handlers must have an initialize class that accepts the params and have a call method which should return a ruby hash that can be converted to JSON using .to_json.
As an example, take a look at app/services/rpc_method_handlers/qn_hello_world.rb or app/services/rpc_method_handlers/eth_send_raw_transaction_faster.rb
This Rails app has a service object that makes it easy to make RPC calls to the customer's endpoint. You can use it like this:
endpoint = Endpoint.last service = EndpointService.new(endpoint) response = service.rpc_call('eth_blockNumber', []) puts responseYou can use the qn-marketplace-cli tool to quickly test your add-on while developing it.
For the commands below, the --basic-auth flag is the Base64 encoding of username:password. You need to make sure to replace that with your valid credentials (as defined in your bin/rails credentials:edit).
../qn-marketplace-cli/qn-marketplace-cli healthcheck --url http://localhost:3009/healthcheck../qn-marketplace-cli/qn-marketplace-cli pudd --base-url http://localhost:3009 --basic-auth dXNlcm5hbWU6cGFzc3dvcmQ=Below, make sure that the jwt-secret matches QN_SSO_SECRET in bin/rails credentials:edit
../qn-marketplace-cli/qn-marketplace-cli sso --url http://localhost:3009/provision --basic-auth dXNlcm5hbWU6cGFzc3dvcmQ= --jwt-secret jwt-secret --email nicolas@example.com --name "Nicolas Anelka" --org PSG ../qn-marketplace-cli/qn-marketplace-cli rpc --url http://localhost:3009/provision --rpc-url http://localhost:3009/rpc --rpc-method qnHelloWorld --rpc-params "[\"abc\"]" --basic-auth dXNlcm5hbWU6cGFzc3dvcmQ= --verbose../qn-marketplace-cli/qn-marketplace-cli rpc --url http://localhost:3009/provision --rpc-url http://localhost:3009/rpc --rpc-method eth_sendRawTransactionFaster --rpc-params "[\"abc\"]" --basic-auth dXNlcm5hbWU6cGFzc3dvcmQ= --verboseTo deploy to heroku:
- Ensure you have the Heroku CLI installed on your machine.
- Login to Heroku from CLI:
heroku login - Create the application
heroku apps:create <your app name>. Make sure you replace<your app name>with the name of your add-on. - Add Postgresql database:
heroku addons:create heroku-postgresql:basic. You can change to a bigger plan such asstandard-0orstandard-2instead ofbasicif you want a more robust database. - Set your master key on heroku config for rails credentials:
heroku config:set RAILS_MASTER_KEY=yourkey - Deploy it with
git push heroku main - Open the app:
heroku open - Verify that the healtheck endpoint works by adding
/healthcheckat the end of the URL from previous step. You should see a green page.
MIT