Skip to content

Commit

Permalink
fix solana rpc (#761)
Browse files Browse the repository at this point in the history
  • Loading branch information
IDIDOS authored Nov 20, 2024
2 parents e4d4b36 + cb8dc48 commit 7dd5458
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rubic-sdk",
"version": "5.45.2",
"version": "5.45.3",
"description": "Simplify dApp creation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export const rpcErrors = [
'your account has been suspended',
'too many requests, we recommend you to use free api key',
'origin not allowed',
'api key disabled'
'api key disabled',
'access forbidden'
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface SolanaToken {
name: string;
symbol: string;
logoURI: string | null;
verified?: boolean;
address: string;
decimals: number | null;
holders?: number | null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injector } from 'src/core/injector/injector';

import { SolanaToken } from '../models/solana-token';

export class SolanaTokensApiService {
private static readonly apiEndpoint = 'https://x-api.rubic.exchange/sol_token_list';

private static readonly xApiKey = 'sndfje3u4b3fnNSDNFUSDNVSunw345842hrnfd3b4nt4';

public static getTokensList(tokenAddresses: string[]): Promise<{ content: SolanaToken[] }> {
return Injector.httpClient.post(
`${this.apiEndpoint}/v1/mints?chainId=101`,
{ addresses: tokenAddresses },
{
headers: {
apiKey: this.xApiKey
}
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
TOKEN_PROGRAM_ID
} from '@solana/spl-token';
import { BlockhashWithExpiryBlockHeight, Connection, PublicKey } from '@solana/web3.js';
import { Client as TokenSdk } from '@solflare-wallet/utl-sdk';
import { Client as TokenSdk, UtlConfig } from '@solflare-wallet/utl-sdk';
import BigNumber from 'bignumber.js';
import { catchError, firstValueFrom, from, map, of, timeout } from 'rxjs';
import { nativeTokensList } from 'src/common/tokens/constants/native-tokens';
import { compareAddresses } from 'src/common/utils/blockchain';
import { Cache } from 'src/common/utils/decorators';
import { NATIVE_SOLANA_MINT_ADDRESS } from 'src/core/blockchain/constants/solana/native-solana-mint-address';
import { BLOCKCHAIN_NAME } from 'src/core/blockchain/models/blockchain-name';
Expand All @@ -24,6 +25,8 @@ import { Web3Public } from 'src/core/blockchain/web3-public-service/web3-public/
import { SolanaWeb3Pure } from 'src/core/blockchain/web3-pure/typed-web3-pure/solana-web3-pure/solana-web3-pure';
import { AbiItem } from 'web3-utils';

import { SolanaToken } from './models/solana-token';
import { SolanaTokensApiService } from './services/solana-tokens-api-service';
/**
* Class containing methods for calling contracts in order to obtain information from the blockchain.
* To send transaction or execute contract method use {@link Web3Private}.
Expand Down Expand Up @@ -77,8 +80,8 @@ export class SolanaWeb3Public extends Web3Public {
);

const mints = filteredTokenAddresses.map(address => new PublicKey(address));
const tokenSdk = new TokenSdk();
const tokensMint = await tokenSdk.fetchMints(mints);

const tokensMint = await this.fetchMints(mints);

const tokens = tokensMint.map(token => {
const entries = tokenFields.map(field => [field, token?.[field]]);
Expand Down Expand Up @@ -131,6 +134,32 @@ export class SolanaWeb3Public extends Web3Public {
throw new Error('Method call is not supported');
}

private async fetchMints(mints: PublicKey[]): Promise<SolanaToken[]> {
const tokensAddresses = mints.map(mint => mint.toString());

let tokensList: SolanaToken[] = [];

try {
const { content } = await SolanaTokensApiService.getTokensList(tokensAddresses);
tokensList = content;
} catch {}

const tokensNotFetched = mints.filter(mint =>
tokensList.some(token => !compareAddresses(token.address, mint.toString()))
);

const config = new UtlConfig({
connection: this.connection,
timeout: 5000
});

const tokenSDK = new TokenSdk(config);

const metaplexTokens = await tokenSDK.getFromMetaplex(tokensNotFetched);

return [...tokensList, ...metaplexTokens];
}

public healthCheck(timeoutMs: number = 4000): Promise<boolean> {
const request = this.connection.getBalanceAndContext(
new PublicKey('DVLwQbEaw5txuduQwvfbNP3sXvjawHqaoMuGMKZx15bQ'),
Expand Down

0 comments on commit 7dd5458

Please sign in to comment.