Skip to content

Commit

Permalink
Refactor fetchTxCount()
Browse files Browse the repository at this point in the history
  • Loading branch information
froch committed Mar 20, 2024
1 parent 28a2016 commit 8f69ab3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
21 changes: 3 additions & 18 deletions src/modules/[chain]/indexStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export function colorMap(color: string) {
export const useIndexModule = defineStore('module-index', {
state: () => {
return {
transactionCount: 0 as number,
days: 14,
tickerIndex: 0,
coinInfo: {
Expand Down Expand Up @@ -78,7 +77,8 @@ export const useIndexModule = defineStore('module-index', {
},
getters: {
txCount(): number {
return this.transactionCount;
const chain = useBlockchain();
return chain.txCount;
},
blockchain() {
const chain = useBlockchain();
Expand Down Expand Up @@ -214,6 +214,7 @@ export const useIndexModule = defineStore('module-index', {
this.$reset();
this.initCoingecko();
useMintStore().fetchInflation();
useBlockchain().fetchTxCount();
useDistributionStore()
.fetchCommunityPool()
.then((x) => {
Expand All @@ -224,22 +225,6 @@ export const useIndexModule = defineStore('module-index', {
denom: t.denom,
}));
});
// const gov = useGovStore();
// gov.fetchProposals('2').then((x) => {
// this.proposals = x;
// });
// Fetch the transaction count
try {
const response = await fetch('https://chain-data.xion-testnet-1.burnt.com/api/txs/count');
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
this.transactionCount = data.count;
} catch (error) {
console.error('Fetching transaction count failed', error);
this.transactionCount = 0;
}
},
tickerColor(color: string) {
return colorMap(color);
Expand Down
31 changes: 29 additions & 2 deletions src/stores/useBlockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const useBlockchain = defineStore('blockchain', {
address: string;
provider: string;
},
tx_count: 0 as number,
connErr: '',
};
},
Expand All @@ -49,6 +50,9 @@ export const useBlockchain = defineStore('blockchain', {
}
return chain;
},
txCount(): number {
return this.tx_count
},
logo(): string {
return this.current?.logo || '';
},
Expand Down Expand Up @@ -167,6 +171,29 @@ export const useBlockchain = defineStore('blockchain', {
}
},

async fetchTxCount() {
try {
let response;
console.log('fetching tx count', this.chainName);
if (this.chainName === 'xion-testnet-1') {
response = await fetch('https://chain-data.xion-testnet-1.burnt.com/api/txs/count');
} else {
response = await fetch('https://chain-data.xion-mainnet-1.burnt.com/api/txs/count');
}

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

const data = await response.json();
this.tx_count = data.count;

} catch (error) {
console.error('Fetching transaction count failed', error);
this.tx_count = 0;
}
},

async randomSetupEndpoint() {
const endpoint = this.randomEndpoint(this.chainName)
if(endpoint) await this.setRestEndpoint(endpoint);
Expand All @@ -188,8 +215,8 @@ export const useBlockchain = defineStore('blockchain', {
}

// Find the case-sensitive name for the chainName, else simply use the parameter-value.
const caseSensitiveName =
Object.keys(this.dashboard.chains).find((x) => x.toLowerCase() === name.toLowerCase())
const caseSensitiveName =
Object.keys(this.dashboard.chains).find((x) => x.toLowerCase() === name.toLowerCase())
|| name;

// Update chainName if needed
Expand Down

0 comments on commit 8f69ab3

Please sign in to comment.