diff --git a/packages/starknet-snap/snap.config.js b/packages/starknet-snap/snap.config.js index 43ca1d0c..90d809ef 100644 --- a/packages/starknet-snap/snap.config.js +++ b/packages/starknet-snap/snap.config.js @@ -6,6 +6,7 @@ module.exports = { environment: { SNAP_ENV: process.env.SNAP_ENV ?? "prod", VOYAGER_API_KEY: process.env.VOYAGER_API_KEY ?? "", + ALCHEMY_API_KEY: process.env.ALCHEMY_API_KEY ?? "", }, input: "./src/index.ts", server: { diff --git a/packages/starknet-snap/src/utils/snapUtils.ts b/packages/starknet-snap/src/utils/snapUtils.ts index 9294c5ea..8c477c17 100644 --- a/packages/starknet-snap/src/utils/snapUtils.ts +++ b/packages/starknet-snap/src/utils/snapUtils.ts @@ -526,6 +526,20 @@ export function getChainIdHex(network: Network) { return `0x${num.toBigInt(network.chainId).toString(16)}`; } +export function getRPCUrl(chainId: string) { + switch (chainId) { + case constants.StarknetChainId.SN_MAIN: + return `https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/${getRPCCredentials()}`; + default: + case STARKNET_SEPOLIA_TESTNET_NETWORK.chainId: + return `https://starknet-sepolia.g.alchemy.com/starknet/version/rpc/v0_7/${getRPCCredentials()}`; + } +} + +export function getRPCCredentials(): string { + return process.env.ALCHEMY_API_KEY ?? '' +} + export function getVoyagerUrl(chainId: string) { switch (chainId) { case STARKNET_SEPOLIA_TESTNET_NETWORK.chainId: diff --git a/packages/starknet-snap/src/utils/starknetUtils.ts b/packages/starknet-snap/src/utils/starknetUtils.ts index fe5a167e..e1df0492 100644 --- a/packages/starknet-snap/src/utils/starknetUtils.ts +++ b/packages/starknet-snap/src/utils/starknetUtils.ts @@ -40,6 +40,7 @@ import { getAddressKey } from './keyPair'; import { getAccount, getAccounts, + getRPCUrl, getTransactionFromVoyagerUrl, getTransactionsFromVoyagerUrl, getVoyagerCredentials, @@ -57,7 +58,7 @@ export const getCallDataArray = (callDataStr: string): string[] => { export const getProvider = (network: Network): ProviderInterface => { let providerParam: ProviderOptions = {}; providerParam = { - nodeUrl: network.nodeUrl, + nodeUrl: getRPCUrl(network.chainId), }; return new Provider(providerParam); }; diff --git a/packages/starknet-snap/test/utils/snapUtils.test.ts b/packages/starknet-snap/test/utils/snapUtils.test.ts index c0c6a5c6..e0a9b5a1 100644 --- a/packages/starknet-snap/test/utils/snapUtils.test.ts +++ b/packages/starknet-snap/test/utils/snapUtils.test.ts @@ -1,5 +1,6 @@ import { expect } from 'chai'; import { Mutex } from 'async-mutex'; +import { constants } from 'starknet'; import { dappUrl, @@ -8,6 +9,7 @@ import { getTransactionFromVoyagerUrl, getTransactionsFromVoyagerUrl, getVoyagerCredentials, + getRPCUrl, } from '../../src/utils/snapUtils'; import { WalletMock } from '../wallet.mock.test'; import { Network, SnapState } from '../../src/types/snapState'; @@ -16,7 +18,6 @@ import { STARKNET_TESTNET_NETWORK, STARKNET_MAINNET_NETWORK, } from '../../src/utils/constants'; -import { constants } from 'starknet'; describe('Snap Utils', () => { it('should return the proper dapp URL based on the environment', () => { @@ -133,3 +134,19 @@ describe('getVoyagerCredentials', () => { expect(getVoyagerCredentials()).to.have.key('X-API-Key'); }); }); + +describe('getRPCUrl', () => { + it('returns Mainnet RPC URL if chain id is Mainnet', () => { + expect(getRPCUrl(constants.StarknetChainId.SN_MAIN)).to.be.equal('https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/'); + }); + + it('returns Sepolia RPC URL if chain id is not either Mainnet or Sepolia', () => { + expect(getRPCUrl('0x534e5f474f45524c49')).to.be.equal('https://starknet-sepolia.g.alchemy.com/starknet/version/rpc/v0_7/'); + }); + + it('returns Sepolia RPC URL if chain id is Sepolia', () => { + expect(getRPCUrl(STARKNET_SEPOLIA_TESTNET_NETWORK.chainId)).to.be.equal( + 'https://starknet-sepolia.g.alchemy.com/starknet/version/rpc/v0_7/', + ); + }); +}); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 68a10d83..616abe29 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3164,7 +3164,7 @@ __metadata: ethers: ^5.5.1 starknet: 6.7.0 starknet_v4.22.0: "npm:starknet@4.22.0" - checksum: c19932a7c1a7887e2183ed8702394a595f24c1b193bbc109bd06b75b14479ad34655a060ebd5bb84dd8392b6fcbeff8cb39e1e1e70f37a4f059a3f8437a68b70 + checksum: 983f842b9dba2bec7cfcd8907f9dd8d420befc61e045b7138f1786f1662ff6c4f3279b8db49193cf2ae39dff0b9173a5475d1b072f918fda72d54d9d05d4bfd5 languageName: node linkType: hard