Skip to content

Commit

Permalink
feat: replace RPC provider from Alchemy to Infura DIN (#451)
Browse files Browse the repository at this point in the history
* feat: replace alchemy by infura din

* chore: change secrets from VOYAGER to DIN for rpc calls

* chore: lint+fix

* chore: .env.example

* chore: test import fix

* chore: reduced coverage on old test to 50%

* chore: handle comments

* chore: fix comments

* chore: handle comment

* chore: change ALCHEMY to DIN in CI/CD

* chore: fix comments
  • Loading branch information
khanti42 authored Dec 19, 2024
1 parent 7667538 commit eb6c688
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ jobs:
SNAP_ENV: ${{ needs.prepare-deployment.outputs.ENV }}
VERSION: ${{ needs.prepare-deployment.outputs.VERSION }}
STARKSCAN_API_KEY: ${{ secrets.STARKSCAN_API_KEY }}
ALCHEMY_API_KEY: ${{ secrets.ALCHEMY_API_KEY }}
DIN_API_KEY: ${{ secrets.DIN_API_KEY }}
GET_STARKNET_PUBLIC_PATH: ${{ needs.prepare-deployment.outputs.GET_STARKNET_PUBLIC_PATH }}
LOG_LEVEL: ${{ needs.prepare-deployment.outputs.LOG_LEVEL }}
- name: Cache Build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
VERSION: ${{ needs.prepare-deployment.outputs.VERSION }}
LOG_LEVEL: ${{ needs.prepare-deployment.outputs.LOG_LEVEL }}
STARKSCAN_API_KEY: ${{ secrets.STARKSCAN_API_KEY }}
ALCHEMY_API_KEY: ${{ secrets.ALCHEMY_API_KEY }}
DIN_API_KEY: ${{ secrets.DIN_API_KEY }}
- name: Cache Build
uses: actions/cache@v3
id: cache
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ VOYAGER_API_KEY=
# Description: Environment variables for API key of STARKSCAN
# Required: false
STARKSCAN_API_KEY=
# Description: Environment variables for API key of ALCHEMY
# Description: Environment variables for API key of INFURA DIN
# Required: false
ALCHEMY_API_KEY=
DIN_API_KEY=
# Description: Environment variables for Log Level, 0 does not log anything, 6 logs everything
# Possible Options: 0-6
# Default: 0
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"serve": "mm-snap serve",
"start": "mm-snap watch",
"test": "yarn run test:unit && yarn run cover:report && yarn run jest",
"test:unit": "nyc --check-coverage --statements 50 --branches 50 --functions 50 --lines 50 mocha --colors -r ts-node/register --require test/global.ts \"test/**/*.test.ts\"",
"test:unit:one": "nyc --check-coverage --statements 50 --branches 50 --functions 50 --lines 50 mocha --colors -r ts-node/register --require test/global.ts"
"test:unit": "nyc --check-coverage --statements 40 --branches 40 --functions 40 --lines 40 mocha --colors -r ts-node/register --require test/global.ts \"test/**/*.test.ts\"",
"test:unit:one": "nyc --check-coverage --statements 40 --branches 40 --functions 40 --lines 40 mocha --colors -r ts-node/register --require test/global.ts"
},
"nyc": {
"exclude": [
Expand Down
2 changes: 1 addition & 1 deletion packages/starknet-snap/snap.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const config: SnapConfig = {
/* eslint-disable */
SNAP_ENV: process.env.SNAP_ENV ?? 'prod',
VOYAGER_API_KEY: process.env.VOYAGER_API_KEY ?? '',
ALCHEMY_API_KEY: process.env.ALCHEMY_API_KEY ?? '',
DIN_API_KEY: process.env.DIN_API_KEY ?? '',
STARKSCAN_API_KEY: process.env.STARKSCAN_API_KEY ?? '',
LOG_LEVEL: process.env.LOG_LEVEL ?? '0',
/* eslint-disable */
Expand Down
4 changes: 4 additions & 0 deletions packages/starknet-snap/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type SnapConfig = {
defaultNetwork: Network;
availableNetworks: Network[];
preloadTokens: Erc20Token[];
rpcApiKey: string;
explorer: {
[key: string]: string;
};
Expand Down Expand Up @@ -60,6 +61,9 @@ export const Config: SnapConfig = {
},
},

// eslint-disable-next-line no-restricted-globals
rpcApiKey: process.env.DIN_API_KEY ?? '',

explorer: {
[constants.StarknetChainId.SN_MAIN]:
// eslint-disable-next-line no-template-curly-in-string
Expand Down
31 changes: 31 additions & 0 deletions packages/starknet-snap/src/utils/rpc-provider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { constants } from 'starknet';

import { Config } from '../config';
import { getRPCUrl } from './rpc-provider';

describe('getRPCUrl', () => {
beforeEach(function () {
Config.rpcApiKey = 'API_KEY';
});
afterEach(function () {
Config.rpcApiKey = '';
});

it('returns Mainnet RPC URL if chain id is Mainnet', () => {
expect(getRPCUrl(constants.StarknetChainId.SN_MAIN)).toBe(
`https://starknet-mainnet.infura.io/v3/${Config.rpcApiKey}`,
);
});

it('returns Sepolia RPC URL if chain id is not either Mainnet or Sepolia', () => {
expect(getRPCUrl('0x534e5f474f45524c49')).toBe(
`https://starknet-sepolia.infura.io/v3/${Config.rpcApiKey}`,
);
});

it('returns Sepolia RPC URL if chain id is Sepolia', () => {
expect(getRPCUrl(constants.StarknetChainId.SN_SEPOLIA)).toBe(
`https://starknet-sepolia.infura.io/v3/${Config.rpcApiKey}`,
);
});
});
19 changes: 19 additions & 0 deletions packages/starknet-snap/src/utils/rpc-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { constants } from 'starknet';

import { Config } from '../config';

/**
* Gets the rpc URL for a given Chain ID.
*
* @param chainId - The Chain ID.
* @returns The RPC node endpoint of the corresponding chain.
*/
export function getRPCUrl(chainId: string) {
switch (chainId) {
case constants.StarknetChainId.SN_MAIN:
return `https://starknet-mainnet.infura.io/v3/${Config.rpcApiKey}`;
default:
case constants.StarknetChainId.SN_SEPOLIA:
return `https://starknet-sepolia.infura.io/v3/${Config.rpcApiKey}`;
}
}
22 changes: 0 additions & 22 deletions packages/starknet-snap/src/utils/snapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,28 +745,6 @@ export function getChainIdHex(network: Network) {
return `0x${numUtils.toBigInt(network.chainId).toString(16)}`;
}

/**
*
* @param chainId
*/
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 {
// eslint-disable-next-line no-restricted-globals
return process.env.ALCHEMY_API_KEY ?? '';
}

/**
*
* @param chainId
Expand Down
2 changes: 1 addition & 1 deletion packages/starknet-snap/src/utils/starknetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ import { ConsolidateFees } from './fee';
import { hexToString } from './formatter-utils';
import { getAddressKey } from './keyPair';
import { logger } from './logger';
import { getRPCUrl } from './rpc-provider';
import { toJson } from './serializer';
import {
getAccount,
getAccounts,
getRPCUrl,
getTransactionsFromVoyagerUrl,
getVoyagerCredentials,
} from './snapUtils';
Expand Down
21 changes: 0 additions & 21 deletions packages/starknet-snap/test/utils/snapUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
getTransactionFromVoyagerUrl,
getTransactionsFromVoyagerUrl,
getVoyagerCredentials,
getRPCUrl,
} from '../../src/utils/snapUtils';
import { WalletMock } from '../wallet.mock.test';
import { Network, SnapState } from '../../src/types/snapState';
Expand Down Expand Up @@ -152,23 +151,3 @@ 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/',
);
});
});

0 comments on commit eb6c688

Please sign in to comment.