Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: RPC response time stats #1067

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -29,6 +29,7 @@
"@astar-network/astar-ui": "^0.0.136",
"@astar-network/metamask-astar-adapter": "^0.5.4",
"@astar-network/metamask-astar-types": "^0.6.1",
"@aws-sdk/client-secrets-manager": "^3.462.0",
"@ethersproject/bignumber": "^5.5.0",
"@polkadot/api": "^10.9.1",
"@polkadot/api-contract": "^10.9.1",
Expand Down Expand Up @@ -60,6 +61,7 @@
"ethereumjs-util": "^7.1.3",
"ethers": "^5.5.4",
"express": "^4.17.1",
"firebase": "^10.7.0",
"highcharts": "^10.0.0",
"highcharts-vue": "^1.4.0",
"inversify": "^6.0.1",
Expand Down
59 changes: 59 additions & 0 deletions src/config/api/polkadot/connectApi.ts
Expand Up @@ -8,6 +8,10 @@ import jsonParachainSpecShiden from './chain-specs/shiden.json';
import jsonParachainSpecShibuya from './chain-specs/shibuya.json';
import jsonParachainSpecTokyo from './chain-specs/tokyo.json';
import { WellKnownChain } from '@substrate/connect';
import { storedEvmAddressMapping } from 'src/hooks/helper/addressUtils';
import { initializeApp } from 'firebase/app';
import { getFirestore, collection, addDoc, Timestamp } from 'firebase/firestore';
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';

const RES_INVALID_CONNECTION = 'invalid connection';
const RES_CONNECTED_API = 'connected';
Expand Down Expand Up @@ -123,6 +127,7 @@ export async function connectApi(

store.commit('general/setCurrentNetworkStatus', 'connecting');
store.commit('general/setLoading', true);
const startTime = performance.now();

api.on('error', (error: Error) => console.error(error.message));
try {
Expand Down Expand Up @@ -169,6 +174,60 @@ export async function connectApi(
try {
await api.isReady;

interface FirebaseConfig {
apiKey: string;
authDomain: string;
databaseURL: string;
projectId: string;
storageBucket: string;
messagingSenderId: string;
appId: string;
}

const secret_name = 'gcp-sa';
const client = new SecretsManagerClient({
region: 'ap-northeast-1',
});

let secrets;
try {
secrets = await client.send(
new GetSecretValueCommand({
SecretId: secret_name,
VersionStage: 'AWSCURRENT',
})
);
} catch (error) {
console.info('SecretsManagerClient.GetSecretValueCommand', error);
}

let firebaseConfig: FirebaseConfig;
if (secrets) {
firebaseConfig = JSON.parse(secrets?.SecretString || '{}');

const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);

const endTime = performance.now();
const responseTime = parseFloat(((endTime - startTime) / 1000).toFixed(3));
const timestamp = Timestamp.now();
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const address =
localStorage.getItem(LOCAL_STORAGE.SELECTED_ADDRESS) === 'Ethereum Extension'
? storedEvmAddressMapping()[0]?.evm
: localStorage.getItem(LOCAL_STORAGE.SELECTED_ADDRESS);

const colRef = collection(db, 'response_times');
const response = await addDoc(colRef, {
endpoint,
responseTime,
timestamp,
timezone,
address,
});
console.log('response.id', response.id);
}

store.commit('general/setCurrentNetworkStatus', 'connected');
} catch (err) {
console.error(err);
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/helper/addressUtils.ts
Expand Up @@ -7,7 +7,7 @@ interface EvmMappedAddress {
ss58: string;
}

const storedEvmAddressMapping = (): EvmMappedAddress[] | [] => {
export const storedEvmAddressMapping = (): EvmMappedAddress[] | [] => {
const data = localStorage.getItem(LOCAL_STORAGE.EVM_ADDRESS_MAPPING);
const addressMapping = data ? JSON.parse(data) : [];
return addressMapping;
Expand Down