Skip to content

Commit

Permalink
fix(blockchain-link): solana fetch isTestnet only once
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Nov 26, 2024
1 parent ae131c8 commit fb0a013
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/blockchain-link/src/workers/solana/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,11 @@ const getAccountInfo = async (request: Request<MessageTypes.GetAccountInfo>) =>
} as const;
};

const getInfo = async (request: Request<MessageTypes.GetInfo>) => {
const getInfo = async (request: Request<MessageTypes.GetInfo>, isTestnet: boolean) => {
const api = await request.connect();
const { blockhash: blockHash, lastValidBlockHeight: blockHeight } =
await api.getLatestBlockhash('finalized');
const isTestnet =
(await api.getGenesisHash()) !== '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d';
const serverInfo = {
// genesisHash is reliable identifier of the network, for mainnet the genesis hash is 5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d
testnet: isTestnet,
blockHeight,
blockHash,
Expand Down Expand Up @@ -517,12 +514,12 @@ const unsubscribe = (request: Request<MessageTypes.Unsubscribe>) => {
} as const;
};

const onRequest = (request: Request<MessageTypes.Message>) => {
const onRequest = (request: Request<MessageTypes.Message>, isTestnet: boolean) => {
switch (request.type) {
case MESSAGES.GET_ACCOUNT_INFO:
return getAccountInfo(request);
case MESSAGES.GET_INFO:
return getInfo(request);
return getInfo(request, isTestnet);
case MESSAGES.PUSH_TRANSACTION:
return pushTransaction(request);
case MESSAGES.ESTIMATE_FEE:
Expand All @@ -542,6 +539,7 @@ class SolanaWorker extends BaseWorker<SolanaAPI> {
}

private lazyTokens = createLazy(() => solanaUtils.getTokenMetadata());
private isTestnet = false;

async tryConnect(url: string): Promise<SolanaAPI> {
const api = new Connection(url, {
Expand All @@ -551,7 +549,11 @@ class SolanaWorker extends BaseWorker<SolanaAPI> {
'User-Agent': `Trezor Suite ${getSuiteVersion()}`,
},
});
await api.getLatestBlockhash('finalized');

// genesisHash is reliable identifier of the network, for mainnet the genesis hash is 5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d
this.isTestnet =
(await api.getGenesisHash()) !== '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d';

this.post({ id: -1, type: RESPONSES.CONNECTED });

return Promise.resolve(api);
Expand All @@ -570,7 +572,7 @@ class SolanaWorker extends BaseWorker<SolanaAPI> {
getTokenMetadata: this.lazyTokens.getOrInit,
};

const response = await onRequest(request);
const response = await onRequest(request, this.isTestnet);
this.post({ id: event.data.id, ...response });
} catch (error) {
this.errorResponse(event.data.id, error);
Expand Down

0 comments on commit fb0a013

Please sign in to comment.