feat: add plugins crypto (#1535)

This commit is contained in:
Thanh Tran
2023-10-07 09:44:19 -04:00
committed by GitHub
parent c430d64381
commit 9f08a38cc0
7 changed files with 308 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
<!-- Header -->
<table>
<tr><td colspan="2"><a href="/README.md#-plugins">← Back to plugins index</a></td></tr>
<tr><th colspan="2"><h3>🪙 Crypto</h3></th></tr>
<tr><td colspan="2" align="center">
<p>This plugin generates an SVG image containing crypto metrics from a given address. It uses the CoinGecko API to fetch crypto prices.</p>
<p>For more information, check the <a href="https://www.coingecko.com/vi/api/documentation">CoinGecko API documentation</a>.</p>
</td></tr>
<tr><th>Authors</th><td><a href="https://github.com/dajneem23">@dajneem23</a></td></tr>
<tr>
<th rowspan="3">Supported Features<br><sub><a href="metadata.yml">→ Full specification</a></sub></th>
<td>
<ul>
<li><a href="/source/templates/classic/README.md"><code>📗 Classic template</code></a></li>
<li><a href="/source/templates/repository/README.md"><code>📘 Repository template</code></a></li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li><code>👤 Users</code></li>
<li><code>👥 Organizations</code></li>
<li><code>📓 Repositories</code></li>
</ul>
</td>
</tr>
<tr>
<td><code>🗝️ plugin_crypto</code></td>
</tr>
<tr>
<td colspan="2" align="center">
<img src="https://via.placeholder.com/468x60?text=No%20preview%20available" alt=""></img>
<img width="900" height="1" alt="">
</td>
</tr>
</table>
<!-- /Header -->
## ➡️ Available Options
<!-- Options -->
<table>
<tr>
<td align="center" nowrap="nowrap">Option</td><td align="center" nowrap="nowrap">Description</td>
</tr>
<tr>
<td nowrap="nowrap"><h4><code>plugin_crypto</code></h4></td>
<td rowspan="2"><p>Enable crypto plugin</p><img width="900" height="1" alt=""></td>
</tr>
<tr>
<td nowrap="nowrap">Type: <code>boolean</code><br>Default: <code>no</code><br></td>
</tr>
<tr>
<td nowrap="nowrap"><h4><code>plugin_crypto_id</code></h4></td>
<td rowspan="2"><p>Crypto id (from Coingecko)</p><img width="900" height="1" alt=""></td>
</tr>
<tr>
<td nowrap="nowrap">Type: <code>string</code><br>Default: ""<br>Example: bitcoin<br></td>
</tr>
<tr>
<td nowrap="nowrap"><h4><code>plugin_crypto_vs_currency</code></h4></td>
<td rowspan="2"><p>The target currency of market data (usd, eur, jpy, etc.)</p><img width="900" height="1" alt=""></td>
</tr>
<tr>
<td nowrap="nowrap">Type: <code>string</code><br>Default: "usd"<br>Example: "usd"<br></td>
</tr>
<tr>
<td nowrap="nowrap"><h4><code>plugin_crypto_days</code></h4></td>
<td rowspan="2"><p>Data up to number of days ago (eg. 1,14,30,max)</p><img width="900" height="1" alt=""></td>
</tr>
<tr>
<td nowrap="nowrap">Type: <code>string</code><br>Default: "1"<br>Example: 1<br></td>
</tr>
<tr>
<td nowrap="nowrap"><h4><code>plugin_crypto_precision</code></h4></td>
<td rowspan="2"><p>The number of decimal places to use</p><img width="900" height="1" alt=""></td>
</tr>
<tr>
<td nowrap="nowrap">Type: <code>number</code><br>Default: 2<br>Example: 2<br></td>
</tr>
</table>
<!-- /Options -->
<!--examples-->
```yaml
name: Crypto Metrics
uses: lowlighter/metrics@latest
with:
filename: metrics.plugin.crypto.svg
token: NOT_NEEDED
base: ""
plugin_crypto: yes
plugin_crypto_id: bitcoin
plugin_crypto_vs_currency: usd
plugin_crypto_days: 1
plugin_crypto_precision: 2
```
<!--/examples-->

View File

@@ -0,0 +1,13 @@
- name: Crypto Metrics
uses: lowlighter/metrics@latest
with:
filename: metrics.plugin.crypto.svg
token: NOT_NEEDED
base: ""
plugin_crypto: yes
plugin_crypto_id: bitcoin
plugin_crypto_vs_currency: usd
plugin_crypto_days: 1
plugin_crypto_precision: 2
prod:
skip: true

View File

@@ -0,0 +1,73 @@
//Setup
export default async function({login, q, imports, data, account}, {enabled = false, extras = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!q.crypto) || (!imports.metadata.plugins.crypto.enabled(enabled, {extras})))
return null
//Load inputs
let {id, days, vs_currency, precision } = imports.metadata.plugins.crypto.inputs({data, account, q})
if (!id)
throw { error: { message: "Crypto currency id is not set" } }
console.debug(`metrics/compute/${login}/plugins > crypto > querying api for crypto`)
const {
data: coin
} = await imports.axios.get(`https://api.coingecko.com/api/v3/coins/${id}`,
{
params: {
market_data:true
}
})
if (!coin) {
throw {error: {message: "Crypto currency not found"}}
}
const {
data: { prices },
} = await imports.axios.get(`https://api.coingecko.com/api/v3/coins/${id}/market_chart`, {
params: {
vs_currency,
days,
precision
},
})
const chart = imports.Graph.timeline(
prices.map((y, _) => ({ x: new Date(y[0]), y: y[1] })),
{
low: Math.min(...prices.map((y, _) => y[1])),
high: Math.max(...prices.map((y, _) => y[1])),
points: false, text: false,
width: 480 * (1 + data.large),
height: 200
}
)
//Results
return {
chart,
id,
precision,
days:{"1":"Today", "14":"2 Weeks", "30":"1 Month", max :"All-time"}[days],
symbol: coin.symbol,
name: coin.name,
current_price: coin.market_data.current_price[vs_currency],
price_change_percentage_24h: coin.market_data.price_change_percentage_24h,
vs_currency,
logo:coin.image.small,
}
}
//Handle errors
catch (error) {
throw imports.format.error(error)
}
}

View File

@@ -0,0 +1,52 @@
name: 🪙 Crypto
category: community
description: |
Generates a SVG image containing crypto metrics from a given address.
This plugin using coingecko API to fetch crypto prices.
check https://www.coingecko.com/vi/api/documentation for more information.
examples:
default: https://via.placeholder.com/468x60?text=No%20preview%20available
authors:
- dajneem23
supports:
- user
- organization
- repository
scopes: []
inputs:
plugin_crypto:
description: |
Enable crypto plugin
type: boolean
default: no
plugin_crypto_id:
description: |
Crypto id
type: string
default: ""
example: bitcoin
plugin_crypto_vs_currency:
description: |
The target currency of market data (usd, eur, jpy, etc.)
type: string
default: "usd"
example: "usd"
plugin_crypto_days:
description: |
Data up to number of days ago (eg. 1,14,30,max)
type: string
default: "1"
example: 1
plugin_crypto_precision:
description: |
The number of decimal places to use
type: number
default: 2
example: 2