-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (79 loc) · 3.02 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
document.getElementById('check-balance').addEventListener('click', async function () {
let input = document.getElementById('stx-address').value.trim();
if (!input) {
alert('Please enter a valid STX address or BNS name.');
return;
}
// Clear previous results
document.getElementById('balance').innerText = 'Loading...';
document.getElementById('balance-usd').innerText = '';
let address;
if (input.endsWith('.btc')) {
// If input is a BNS name, convert to lowercase and resolve to Stacks address
address = await getStacksAddressFromBNS(input.toLowerCase()); // Convert BNS name to lowercase
} else {
// Assume input is a direct Stacks address (do not modify case)
address = input;
}
if (!address) {
document.getElementById('balance').innerText = 'Invalid BNS name or address';
return;
}
// Get the balance in STX
const balance = await getBalance(address);
if (balance !== null) {
// Get the price of STX in USD
const priceUSD = await getSTXPriceUSD();
// Calculate the value in USD
const balanceUSD = priceUSD ? (balance * priceUSD).toFixed(2) : 'N/A';
// Display results on the page
document.getElementById('balance').innerText = `${balance} STX`;
document.getElementById('balance-usd').innerText = `≈ ${balanceUSD} USD`;
} else {
document.getElementById('balance').innerText = 'Unable to retrieve the balance.';
}
});
// Allow the search to be triggered when pressing Enter
document.getElementById('stx-address').addEventListener('keypress', function (event) {
if (event.key === 'Enter') {
document.getElementById('check-balance').click();
}
});
// Get the balance of the STX address
async function getBalance(address) {
const url = `https://stacks-node-api.mainnet.stacks.co/v2/accounts/${address}`;
try {
const response = await fetch(url);
const data = await response.json();
if (data.balance) {
return parseInt(data.balance, 16) / 1000000; // Convert hex balance to decimal STX
}
} catch (error) {
console.error('Error getting balance:', error);
}
return null;
}
// Get the price of STX in USD
async function getSTXPriceUSD() {
const url = 'https://api.coingecko.com/api/v3/simple/price?ids=blockstack&vs_currencies=usd';
try {
const response = await fetch(url);
const data = await response.json();
return data.blockstack.usd;
} catch (error) {
console.error('Error getting STX price:', error);
return null;
}
}
// Get Stacks address from BNS name
async function getStacksAddressFromBNS(bnsName) {
const url = `https://api.hiro.so/v1/names/${bnsName}`;
try {
const response = await fetch(url);
const data = await response.json();
return data.address || null;
} catch (error) {
console.error('Error resolving BNS name:', error);
return null;
}
}