Arbitrum SDK Examples
Real-world examples on how to query information from Arbitrum using the most powerful Web3 SDK.
If you’re looking to start using the Alchemy SDK, but want some plug-and-play examples on how to get started with common use cases on the Arbitrum blockchain, here’s the place to start! We’ll walk you through setting up the Alchemy SDK and a variety of basic tasks you can do.
Setting up the Alchemy SDK
This guide assumes you already have an Alchemy account and access to our Dashboard.
1. Create an Alchemy Key
To access Alchemy’s free node infrastructure, you need an API key to authenticate your requests.
You can create API keys from the dashboard. Check out this video on how to create an app, replacing the chain “Ethereum” with “Arbitrum”.
Or follow the written steps below:
First, navigate to the “create app” button in the “Apps” tab.
Fill in the details under “Create App” to get your new key. You can also see apps you previously made and those made by your team here. Make sure to select the Arbitrum chain.
Pull existing keys by clicking on “View Key” for any app.
You can also pull existing API keys by hovering over “Apps” and selecting one. You can “View Key” here, as well as “Edit App” to whitelist specific domains, see several developer tools, and view analytics.
2. Install and set up the Alchemy SDK
To install the Alchemy SDK, you want to create a project, and then navigate to your project directory to run the installation. Let’s go ahead and do that! Once we’re in our home directory, let’s execute the following:
With NPM:
Next, create a file named index.js
and add the following contents:
You should ultimately replace demo
with your Alchemy HTTP API key.
Unfamiliar with the async stuff? Check out this Medium post.
3. Run your dApp using Node
You should now see the latest block number output in your console!
Below, we’ll list a number of examples on how to make common requests on Arbitrum or EVM blockchains.
How to Get the Latest Block Number on Arbitrum
If you’re looking for the latest block on Arbitrum, you can plug in the following:
How to Get a Block By Its Block Hash on Arbitrum
Every block on Arbitrum correspond to a specific hash. If you’d like to look up a block by its hash, you can plug in the following code:
How to Get a Block By Its Block Number on Arbitrum
Every block on Arbitrum correspond to a specific number. If you’d like to look up a block by its number, you can plug in the following code:
How to Get Logs for an Arbitrum Transaction
Logs are essentially a published list of user-defined events that have happened on the blockchain during an Arbitrum transaction. You can learn more about them in Understanding Logs: Deep Dive into eth_getLogs.
Here’s an example on how to write a getLogs query on Arbitrum:
How to Make an Eth_Call on Arbitrum
An eth_call in Arbitrum is essentially a way to execute a message call immediately without creating a transaction on the block chain. It can be used to query internal contract state, to execute validations coded into a contract or even to test what the effect of a transaction would be without running it live.
Here’s an example on how to write a call query on Arbitrum:
How to Get a Transaction by Its Hash on Arbitrum
Every transaction on Arbitrum correspond to a specific hash. If you’d like to look up a transaction by its hash, you can plug in the following code:
How to Get a Transaction Receipt on Arbitrum
Every transaction on Arbitrum has an associated receipt with metadata about the transaction, such as the gas used and logs printed during the transaction. If you’d like to look up a transaction’s receipt, you can plug in the following code:
How to get a User’s Transaction Count on Arbitrum
The number of transactions a user has sent is particularly important when it comes to calculating the nonce for sending new transactions on Arbitrum. Without it, you’ll be unable to send new transactions because your nonce won’t be set correctly.
How to Fetch Historical Transactions on Arbitrum
Oftentimes, you’ll want to look up a set of transactions on Arbitrum between a set of blocks, corresponding to a set of token types such as ERC20 or ERC721, or with certain attributes. Alchemy’s proprietary Transfers API allows you to do so in milliseconds, rather than searching every block on the blockchain.
How to Subscribe to New Blocks on Arbitrum
If you’d like to open a WebSocket subscription to send you a JSON object whenever a new block is published to the blockchain, you can set one up using the following syntax.
How to Estimate the Gas of a Transaction on Arbitrum
Oftentimes, you’ll need to calculate how much gas a particular transaction will use on the blockchain to understand the maximum amount that you’ll pay on that transaction. This example will return the gas used by the specified transaction:
How to Get the Current Gas Price in Arbitrum
You can retrieve the current gas price in wei using this method: