EOSIO

Available on:

  • EOS Mainnet
  • Worbli
  • WAX
  • CryptoKylin Testnet
  • Jungle 2.0 Testnet
  • Your own network

Ethereum (Alpha)

Available on:

  • Ethereum Mainnet
  • Ropsten

Sample Code to Get Started Developing on EOSIO in Just Minutes

Sep 3, 2019 12:52:54 PM / by dfuse

Getting started with developing for blockchain can seem like a difficult task. Thankfully, there are many resources out there to help guide you along your development journey. As the most popular data access provider for developers, dfuse wanted to ensure that we offered many examples to help you get started developing on EOSIO as quickly as possible.

Basic Examples of Using dfuse

Once you have already signed up for your free API key and you’re ready to get started, we recommend starting out with our Basic Examples. These examples are all written in TypeScript. There are four examples in this section:

  1. REST - Retrieve the EOS token balance of an account for a given block height as well as returning the current balance
  2. REST - View the most recent 10 transactions authorized by an account
  3. WebSocket - Open up a WebSocket connection and listen for changes to the global table state of `eosio` for 5 seconds before closing the connection
  4. WebSocket - Open up a WebSocket connection and listen for any EOS token transfers for 5 seconds before closing the connection

Advanced Examples of Using dfuse

Now that you’ve run through the basic examples above, you should take a look through our more advanced examples. Again, these are all written in TypeScript.

  1. WebSocket - Set up a listener for specific connection events, such as errors, closing, reconnection, or invalid message returned.
  2. WebSocket - Set up a stream and reconnect if the stream ever disconnects. This will not take into account the responses you missed during the time the stream was closed.
  3. REST - Verify if an account exists on the blockchain.
  4. WebSocket - Experiment with the difference between opening up two independent streams, each listening for independent events, versus opening a single stream listening for multiple events.
  5. WebSocket - Microforks can happen at any moment, and your code needs to be resilient. Learn how to follow `undo and `redo notifications.
  6. WebSocket - Never miss a beat by ensuring you mark the progress of your stream.
  7. WebSocket - Avoid polluting the JavaScript runtime global scope.
  8. WebSocket - Ensure that the response is only sent for irreversible actions.
  9. WebSocket - Utilize the cursor to paginate your responses and filter for just the actions that match your query, not all actions within a transaction.

Reference Code Samples

There is also a reference folder, which has nearly 20 working samples of simple calls. These include actions like generating a new JWT token, fetching a transaction by ID, and streaming the head info from the chain, to name a few. These are great pieces of code to drop into your application to save you time when dealing with some of the more straightforward tasks. These examples will all return JSON payloads, allowing you to easily and quickly view what an unformatted response looks like. From there, you can format the response to meet your specific needs.

Feel the Power of GraphQL

Now that you’ve mastered the above basic, advanced, and reference examples, it’s time to take the next step into GraphQL. While many developers are familiar with REST and WebSocket APIs, we have been blown away by the power of GraphQL (and know that you will be too). You can utilize it for either queries (similar to REST) or subscriptions (similar to WebSocket), and use them both in combination to start from any point in the chain, catch up to the head block and continue listening from there. You will also be able to tailor the payload that is returned to ensure that you aren’t bombarded by lots of information that isn’t pertinent to your needs.

  1. Connect over the Apollo transport layer - There is a basic example that simply streams in the next 10 transfer actions, and an advanced example that uses a `cursor to mark your progress.
  2. Fetch different chain statistics - token.ts provides token transaction, action, and notification counts for a given block range, while account-creation.ts provides account creation statistics for a given block range.
  3. Connect over gRPC - This is the same as the basic example of the Apollo transport layer example, however it is all done over gRPC and uses Golang.
  4. Example in Python - Connect over gRPC to stream the next 10 EOS token transfers. If Python is your preferred language, this is a great place to get started.
  5. Use dfuse GraphQL in a React Application - Stream all the latest actions of the chain and filter them by smart contract.
  6. EOS/REX price feed - Consume real-time data about EOS mainnet’s REX.

Utilizing dfuse in Your Language of Choice

While we have given you examples to get started in multiple languages (such as JavaScript, Golang and Python), dfuse will work within whichever language you are most comfortable. If you’ve created any sample code that you feel would be helpful for other developers, please reach out to us and we’d love to add your example to the repository.

Continue Diving Into dfuse Examples

The above examples are a non-exhaustive list from the dfuse GitHub repository. Once you’ve gone through these, you can now consider yourself a dfuse Pro! Let us know which examples have helped you the most, or if there are any others that you’d like to see! You can always reach us through our dfuse Telegram Channel. We love to see what you’re working on, so please let us know what you’re building!

topics EOSIO, developer, Use case, Client-JS