Skip to content

Commit

Permalink
feat: sf 612 add alchemy provider (#236)
Browse files Browse the repository at this point in the history
* feat: bump starknetjs to 6.6.0

* chore: remove unuse script

* chore: add default config

* chore: remove patching

* chore: update library version

* chore: update getStarkName

* feat: add alchemy provider

* fix: update mainnet-url
  • Loading branch information
stanleyyconsensys authored May 1, 2024
1 parent a07c3f1 commit 499f59d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/starknet-snap/snap.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
14 changes: 14 additions & 0 deletions packages/starknet-snap/src/utils/snapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion packages/starknet-snap/src/utils/starknetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { getAddressKey } from './keyPair';
import {
getAccount,
getAccounts,
getRPCUrl,
getTransactionFromVoyagerUrl,
getTransactionsFromVoyagerUrl,
getVoyagerCredentials,
Expand All @@ -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);
};
Expand Down
19 changes: 18 additions & 1 deletion packages/starknet-snap/test/utils/snapUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import { Mutex } from 'async-mutex';
import { constants } from 'starknet';

import {
dappUrl,
Expand All @@ -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';
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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/',
);
});
});
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3164,7 +3164,7 @@ __metadata:
ethers: ^5.5.1
starknet: 6.7.0
starknet_v4.22.0: "npm:[email protected]"
checksum: c19932a7c1a7887e2183ed8702394a595f24c1b193bbc109bd06b75b14479ad34655a060ebd5bb84dd8392b6fcbeff8cb39e1e1e70f37a4f059a3f8437a68b70
checksum: 983f842b9dba2bec7cfcd8907f9dd8d420befc61e045b7138f1786f1662ff6c4f3279b8db49193cf2ae39dff0b9173a5475d1b072f918fda72d54d9d05d4bfd5
languageName: node
linkType: hard

Expand Down

0 comments on commit 499f59d

Please sign in to comment.