Publish daily BTC price in public remoteStorage

This commit is contained in:
Râu Cao 2025-01-17 09:53:52 -05:00
parent 72ac8c6a84
commit 89f1790afc
Signed by: raucao
GPG Key ID: 37036C356E56CC51
6 changed files with 138 additions and 13 deletions

View File

@ -0,0 +1,10 @@
{
"id": "kosmos-rs",
"auth_tokens": {
"encrypted_data": "fiznpRw7VKlm232+U6XV1rqkAf2Z8CpoD8KyvuvOH2JniaymlcTHgazGWQ8s\nGeqK4RU9l4d29e9i+Mh0k4vnhO4q\n",
"iv": "SvurcL2oNSNWjlxp\n",
"auth_tag": "JLQ7vGXAuYYJpLEpL6C+Rw==\n",
"version": 3,
"cipher": "aes-256-gcm"
}
}

View File

@ -1,30 +1,30 @@
{ {
"id": "lndhub-go", "id": "lndhub-go",
"jwt_secret": { "jwt_secret": {
"encrypted_data": "3T4JYnoISKXCnatCBeLCXyE8wVjzphw5/JU5A0vHfQ2xSDZreIRQ\n", "encrypted_data": "lJsKBTCRzI83xmRHXzpnuRH/4cuMOR+Rd+SBU50G9HdibadIEDhS\n",
"iv": "bGQZjCk6FtD/hqVj\n", "iv": "f/SvsWtZIYOVc54X\n",
"auth_tag": "CS87+UK1ZIFMiNcNaoyO6w==\n", "auth_tag": "YlJ78EuJbcPfjCPc2eH+ug==\n",
"version": 3, "version": 3,
"cipher": "aes-256-gcm" "cipher": "aes-256-gcm"
}, },
"postgresql_password": { "postgresql_password": {
"encrypted_data": "u8kf/6WdSTzyIz2kF+24JgOPLndWH2WmTFZ3CToJsnay\n", "encrypted_data": "aT0yNlWjvk/0S4z2kZB4Ye1u/ngk5J6fGPbwZSfdq6cy\n",
"iv": "KqLtV2UuaAzJx7C8\n", "iv": "OgUttF4LlSrL/7gH\n",
"auth_tag": "3aqx45+epb2NFkNfOfG89A==\n", "auth_tag": "pcbbGqbQ2RjU+i9dt8c3OQ==\n",
"version": 3, "version": 3,
"cipher": "aes-256-gcm" "cipher": "aes-256-gcm"
}, },
"admin_token": { "admin_token": {
"encrypted_data": "Z737fXqRE9JHfunRhc2GG281dFFN1bvBvTzTDzl/Vb8O\n", "encrypted_data": "I9EsqCCxMIw+fX6sfu6KX8B5fJj9DX5Y4tbX30jdnmxr\n",
"iv": "oKLQJbD67tiz2235\n", "iv": "vnERvIWYInO6+Y8q\n",
"auth_tag": "SlVIqC9d9SRoO78M7cBjTw==\n", "auth_tag": "gO+MprZUQgPEWJQUmSF1sA==\n",
"version": 3, "version": 3,
"cipher": "aes-256-gcm" "cipher": "aes-256-gcm"
}, },
"sentry_dsn": { "sentry_dsn": {
"encrypted_data": "gmDHGDWkTIvaXjcWMs1dnKnbqtsADPJ2mLmWw8Idj6RVevU5CabjvviAxEo1\n3hs2LWuObumRSCQt2QKap191uMq3CL2+da53hbsv+JUkxl4=\n", "encrypted_data": "+sUXWgl6dXpA1/0FqjKC3Jnl54aor6gtM+19EM/NsHwg4qu672YnSgxV+c9x\nHM3JZBYxBYvJ+HYGAvMmhlGvaOOEIvLmFUpCCJeVUXR32S8=\n",
"iv": "Yt0fSsxL4SNicwUY\n", "iv": "82+DzAnHiptaX7sO\n",
"auth_tag": "j7BWbcNnymHHMNTADWmCNw==\n", "auth_tag": "CDx44iRBVhSIF8DOxb2c+w==\n",
"version": 3, "version": 3,
"cipher": "aes-256-gcm" "cipher": "aes-256-gcm"
} }

View File

@ -38,6 +38,7 @@
"kosmos-bitcoin::dotnet", "kosmos-bitcoin::dotnet",
"kosmos-bitcoin::nbxplorer", "kosmos-bitcoin::nbxplorer",
"kosmos-bitcoin::btcpay", "kosmos-bitcoin::btcpay",
"kosmos-bitcoin::price_tracking",
"apt::default", "apt::default",
"timezone_iii::default", "timezone_iii::default",
"timezone_iii::debian", "timezone_iii::debian",
@ -102,6 +103,7 @@
"role[bitcoind]", "role[bitcoind]",
"role[lnd]", "role[lnd]",
"role[lndhub]", "role[lndhub]",
"role[btcpay]" "role[btcpay]",
"recipe[kosmos-bitcoin::price_tracking]"
] ]
} }

View File

@ -111,3 +111,5 @@ node.default['btcpay']['postgres']['user'] = 'satoshi'
node.default['peerswap']['repo'] = 'https://github.com/ElementsProject/peerswap.git' node.default['peerswap']['repo'] = 'https://github.com/ElementsProject/peerswap.git'
node.default['peerswap']['revision'] = 'master' node.default['peerswap']['revision'] = 'master'
node.default['peerswap-lnd']['source_dir'] = '/opt/peerswap' node.default['peerswap-lnd']['source_dir'] = '/opt/peerswap'
node.default['price_tracking']['rs_base_url'] = "https://storage.kosmos.org/kosmos/public/btc-price"

View File

@ -0,0 +1,59 @@
#
# Cookbook:: kosmos-bitcoin
# Recipe:: price_tracking
#
# Track BTC rates and publish them via remoteStorage
#
%w[curl jq].each do |pkg|
apt_package pkg
end
daily_tracker_path = "/usr/local/bin/btc-price-tracker-daily"
credentials = Chef::EncryptedDataBagItem.load('credentials', 'kosmos-rs')
template daily_tracker_path do
source "btc-price-tracker-daily.sh.erb"
mode '0740'
variables rs_base_url: node['price_tracking']['rs_base_url']
notifies :restart, "systemd_unit[lnd-channel-backup.service]", :delayed
end
systemd_unit 'btc-price-tracker-daily.service' do
content({
Unit: {
Description: 'BTC price tracker (daily rates)',
After: 'network-online.target',
Wants: 'network-online.target'
},
Service: {
Type: 'oneshot',
ExecStart: daily_tracker_path,
Environment: "RS_AUTH=#{credentials["auth_tokens"]["/btc-price"]}"
},
Install: {
WantedBy: 'multi-user.target'
}
})
sensitive true
triggers_reload true
action [:create]
end
systemd_unit 'btc-price-tracker-daily.timer' do
content({
Unit: {
Description: 'Run BTC price tracker daily'
},
Timer: {
OnCalendar: '*-*-* 00:00:00',
Persistent: 'true'
},
Install: {
WantedBy: 'timers.target'
}
})
triggers_reload true
action [:create, :enable, :start]
end

View File

@ -0,0 +1,52 @@
#!/bin/bash
# Calculate yesterday's date in YYYY-MM-DD format
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
echo "Starting price tracking for $YESTERDAY" >&2
# Get USD data and extract values
usd_data=$(curl -s "https://www.bitstamp.net/api/v2/ticker/btcusd/")
if [ $? -eq 0 ] && [ ! -z "$usd_data" ]; then
echo "Successfully retrieved USD price data" >&2
usd_open24=$(echo $usd_data | jq -r '.open_24')
usd_last=$(echo $usd_data | jq -r '.last')
else
echo "ERROR: Failed to retrieve USD price data" >&2
exit 1
fi
# Get EUR data and extract values
eur_data=$(curl -s "https://www.bitstamp.net/api/v2/ticker/btceur/")
if [ $? -eq 0 ] && [ ! -z "$eur_data" ]; then
echo "Successfully retrieved EUR price data" >&2
eur_open24=$(echo $eur_data | jq -r '.open_24')
eur_last=$(echo $eur_data | jq -r '.last')
else
echo "ERROR: Failed to retrieve EUR price data" >&2
exit 1
fi
# Calculate averages
usd_avg=$(( (${usd_open24%.*} + ${usd_last%.*}) / 2 ))
eur_avg=$(( (${eur_open24%.*} + ${eur_last%.*}) / 2 ))
echo "Calculated averages - USD: $usd_avg, EUR: $eur_avg" >&2
# Create JSON
json="{\"EUR\":$eur_avg,\"USD\":$usd_avg}"
# PUT in remote storage
response=$(curl -X PUT \
-H "Authorization: Bearer $RS_AUTH" \
-H "Content-Type: application/json" \
-d "$json" \
-w "%{http_code}" \
-s \
-o /dev/null \
"<%= @rs_base_url %>/$YESTERDAY")
if [ "$response" -eq 200 ] || [ "$response" -eq 201 ]; then
echo "Successfully uploaded price data" >&2
else
echo "ERROR: Failed to upload price data. HTTP status: $response" >&2
exit 1
fi