iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
♦️

Display Ethereum and Polygon Gas Fees in the macOS Menu Bar

に公開

This is what the final display will look like

Ethereum

Polygon

Step 1: Install xBar or SwitBar

Note: Originally, there was a software called BitBar, but it became less maintained and SwitBar was born. Later, BitBar was reborn as xBar. I had been using SwitBar, but since xBar seems to be more popular now, I will describe how to set up xBar. All of these tools are utilities that display information by running small programs on the menu bar.

If you are a developer or have Homebrew set up, you can install it using brew install --cask xbar. (Homebrew: https://formulae.brew.sh/cask/xbar)

Otherwise, go to https://xbarapp.com/ and click "Download xbar for macOS" in the top right corner. You should find a file such as xbar.v2.1.7-beta.dmg to install. If you are asked for permission to run the application, please grant it.

Step 2: Prepare the Ruby execution environment

This plugin is written in Ruby. Therefore, a Ruby execution environment is required. Since Ruby is not bundled with macOS 10.15 or later, you will need to install it.

ref. https://developer.apple.com/documentation/macos-release-notes/macos-catalina-10_15-release-notes

I recommend using rbenv or similar tools. For details on the installation method, please refer to the installation manual or various articles available online.
https://github.com/rbenv/rbenv#installation

Step 3: Install the plugin

Add via Plugin browser (In preparation)

When you launch xBar, it will appear in the menu bar. I have submitted a request to make it possible to install the plugin via this Plugin browser, but it has not been merged yet (as I just created the PR today).

Pull Request: https://github.com/matryer/xbar-plugins/pull/1770

Add manually

You can also add the plugin manually. I have made the code available at the link below, so please download it.

https://github.com/geeknees/xbar-plugins

You can download it from "Download ZIP". You can open the folder where the plugins should be placed as shown below. You can add the plugins by placing eth_gas_widget.10m.rb and polygon_gas_widget.10m.rb there.

Step 4: Add API keys

It will work as is, but since it will likely be rate-limited quickly without an API key, you should set one up. You need to obtain API keys from https://etherscan.io/ for Ethereum and https://polygonscan.com/ for Polygon. You can issue API keys from the following pages after registering as a member:

Open the files you placed in Step 3 and add your API keys to each of them.

Ethereum

require 'open-uri'
require 'json'

ENDPOINT = "https://api.polygonscan.com/api?module=gastracker&action=gasoracle&apikey="
APIKEY = "" # <- Add your API key here

charset = nil
html = URI.open(ENDPOINT+APIKEY) do |f|
  charset = f.charset
  f.read
end

response = JSON.parse(html)

puts "♾️ #{response['result']['ProposeGasPrice']}"
puts '---'

response['result'].each do |k, v|
  puts "#{k}: #{v.to_f.floor(5)}"
end

Polygon

require 'open-uri'
require 'json'

ENDPOINT = "https://api.polygonscan.com/api?module=gastracker&action=gasoracle&apikey="
APIKEY = "" # <- Add your API key here

charset = nil
html = URI.open(ENDPOINT+APIKEY) do |f|
  charset = f.charset
  f.read
end

response = JSON.parse(html)

puts "♾️ #{response['result']['ProposeGasPrice']}"
puts '---'

response['result'].each do |k, v|
  puts "#{k}: #{v.to_f.floor(5)}"
end

That's it! The setup is complete. Now you should be able to see the display shown at the beginning. Note that the file name is likely polygon_gas_widget.10m.rb; the 10m part indicates the update interval. You can change this as needed if you want to reduce the API load or increase the update frequency.

Discussion