Skip to content

Commit

Permalink
fix: sf 606 fix voyager cors with official provider (#229)
Browse files Browse the repository at this point in the history
* fix: voyager cors issue

* chore: update voyager endpoint

* chore: update cicd

* chore: rollback file

* chore: update switch case
  • Loading branch information
stanleyyconsensys authored Apr 5, 2024
1 parent cc635ea commit 44ff4f1
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
REACT_APP_SNAP_VERSION=${VERSION}-staging yarn workspace wallet-ui build
npm --prefix ./packages/starknet-snap version --new-version ${VERSION}-staging --no-git-tag-version
SNAP_ENV=staging yarn workspace @consensys/starknet-snap build
SNAP_ENV=staging VOYAGER_API_KEY=${{ secrets.VOYAGER_API_KEY }} yarn workspace @consensys/starknet-snap build
npm publish ./packages/starknet-snap --tag staging --access public 2>&1 > /dev/null || :
aws s3 sync ./packages/wallet-ui/build s3://staging.snaps.consensys.io/starknet
Expand All @@ -62,7 +62,7 @@ jobs:
REACT_APP_SNAP_VERSION=${VERSION} REACT_APP_DEBUG_LEVEL=off yarn workspace wallet-ui build
npm --prefix ./packages/starknet-snap version --new-version ${VERSION} --no-git-tag-version --allow-same-version
SNAP_ENV=prod yarn workspace @consensys/starknet-snap build
SNAP_ENV=prod VOYAGER_API_KEY=${{ secrets.VOYAGER_API_KEY }} yarn workspace @consensys/starknet-snap build
npm publish ./packages/starknet-snap --tag latest --access public 2>&1 > /dev/null || :
aws s3 sync ./packages/wallet-ui/build s3://snaps.consensys.io/starknet
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-npm-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
HASH=$(git rev-parse --short HEAD)
DATE=$(date +%Y%m%d)
npm --prefix ./packages/starknet-snap version --new-version ${VERSION}-dev-${HASH}-${DATE} --no-git-tag-version
SNAP_ENV=dev yarn workspace @consensys/starknet-snap build
SNAP_ENV=dev VOYAGER_API_KEY=${{ secrets.VOYAGER_API_KEY }} yarn workspace @consensys/starknet-snap build
- uses: actions/setup-node@v3
with:
Expand Down
1 change: 1 addition & 0 deletions packages/starknet-snap/snap.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
bundler.transform(
envify({
SNAP_ENV: process.env.SNAP_ENV,
VOYAGER_API_KEY: process.env.VOYAGER_API_KEY,
}),
);
},
Expand Down
23 changes: 19 additions & 4 deletions packages/starknet-snap/src/utils/snapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DeclareSignerDetails,
Call,
DeployAccountSignerDetails,
constants,
} from 'starknet';
import { validateAndParseAddress } from './starknetUtils';
import { Component, text, copyable } from '@metamask/snaps-sdk';
Expand All @@ -28,8 +29,6 @@ import {
PRELOADED_NETWORKS,
PRELOADED_TOKENS,
STARKNET_SEPOLIA_TESTNET_NETWORK,
VOYAGER_API_TXNS_URL_SUFFIX,
VOYAGER_API_TXN_URL_SUFFIX,
} from './constants';
import convert from 'ethereum-unit-converter';
import { AddErc20TokenRequestParams, AddNetworkRequestParams } from '../types/snapApi';
Expand Down Expand Up @@ -527,12 +526,28 @@ export function getChainIdHex(network: Network) {
return `0x${num.toBigInt(network.chainId).toString(16)}`;
}

export function getVoyagerUrl(chainId: string) {
switch (chainId) {
case STARKNET_SEPOLIA_TESTNET_NETWORK.chainId:
return `https://sepolia-api.voyager.online/beta`;
case constants.StarknetChainId.SN_MAIN:
default:
return `https://api.voyager.online/beta`;
}
}

export function getVoyagerCredentials(): Record<string, string> {
return {
'X-API-Key': process.env.VOYAGER_API_KEY,
};
}

export function getTransactionFromVoyagerUrl(network: Network) {
return `${network.voyagerUrl}${VOYAGER_API_TXN_URL_SUFFIX}`;
return `${getVoyagerUrl(network.chainId)}/txn`;
}

export function getTransactionsFromVoyagerUrl(network: Network) {
return `${network.voyagerUrl}${VOYAGER_API_TXNS_URL_SUFFIX}`;
return `${getVoyagerUrl(network.chainId)}/txns`;
}

export function getTransaction(state: SnapState, txnHash: string, chainId: string) {
Expand Down
18 changes: 14 additions & 4 deletions packages/starknet-snap/src/utils/starknetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ import type { Hex } from '@noble/curves/abstract/utils';
import { Network, SnapState, Transaction, TransactionType } from '../types/snapState';
import { ACCOUNT_CLASS_HASH_V0, PROXY_CONTRACT_HASH, TRANSFER_SELECTOR_HEX } from './constants';
import { getAddressKey } from './keyPair';
import { getAccount, getAccounts, getTransactionFromVoyagerUrl, getTransactionsFromVoyagerUrl } from './snapUtils';
import {
getAccount,
getAccounts,
getTransactionFromVoyagerUrl,
getTransactionsFromVoyagerUrl,
getVoyagerCredentials,
} from './snapUtils';
import { logger } from './logger';
import { RpcV4GetTransactionReceiptResponse } from '../types/snapApi';

Expand Down Expand Up @@ -201,12 +207,15 @@ export const getTransactionsFromVoyager = async (
toQueryStr = `to=${num.toHex(num.toBigInt(toAddress))}&`;
}
// "ps" only effective on value: 10, 25, 50 as what's currently available in Voyager page
return getData(`${getTransactionsFromVoyagerUrl(network)}?${toQueryStr}ps=${pageSize}&p=${pageNum}`);
return getData(
`${getTransactionsFromVoyagerUrl(network)}?${toQueryStr}ps=${pageSize}&p=${pageNum}`,
getVoyagerCredentials(),
);
};

export const getTransactionFromVoyager = async (transactionHash: num.BigNumberish, network: Network) => {
const txHashHex = num.toHex(num.toBigInt(transactionHash));
return getData(`${getTransactionFromVoyagerUrl(network)}/${txHashHex}`);
return getData(`${getTransactionFromVoyagerUrl(network)}/${txHashHex}`, getVoyagerCredentials());
};

const getTransactionsFromVoyagerHelper = async (
Expand Down Expand Up @@ -335,11 +344,12 @@ export const getMassagedTransactions = async (
return massagedTxns;
};

export const getData = async (url = '') => {
export const getData = async (url = '', headers: Record<string, string> = {}) => {
// Default options are marked with *
const response = await fetch(url, {
method: 'GET', // *GET, POST, PUT, DELETE, etc.
redirect: 'follow', // manual, *follow, error
headers: headers,
});
return response.json(); // parses JSON response into native JavaScript objects
};
Expand Down
48 changes: 47 additions & 1 deletion packages/starknet-snap/test/utils/snapUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { expect } from 'chai';
import { Mutex } from 'async-mutex';

import { dappUrl, removeNetwork } from '../../src/utils/snapUtils';
import {
dappUrl,
removeNetwork,
getVoyagerUrl,
getTransactionFromVoyagerUrl,
getTransactionsFromVoyagerUrl,
getVoyagerCredentials,
} from '../../src/utils/snapUtils';
import { WalletMock } from '../wallet.mock.test';
import { Network, SnapState } from '../../src/types/snapState';
import {
STARKNET_SEPOLIA_TESTNET_NETWORK,
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 @@ -87,3 +95,41 @@ describe('removeNetwork', () => {
expect(state.currentNetwork).to.be.eql(STARKNET_MAINNET_NETWORK);
});
});

describe('getVoyagerUrl', () => {
it('returns Mainnet URL if chain id is Mainnet', () => {
expect(getVoyagerUrl(constants.StarknetChainId.SN_MAIN)).to.be.equal('https://api.voyager.online/beta');
});

it('returns Mainnet URL if chain id is not either Mainnet or Sepolia', () => {
expect(getVoyagerUrl('0x534e5f474f45524c49')).to.be.equal('https://api.voyager.online/beta');
});

it('returns Sepolia URL if chain id is Sepolia', () => {
expect(getVoyagerUrl(STARKNET_SEPOLIA_TESTNET_NETWORK.chainId)).to.be.equal(
'https://sepolia-api.voyager.online/beta',
);
});
});

describe('getTransactionFromVoyagerUrl', () => {
it('returns correct URL', () => {
expect(getTransactionFromVoyagerUrl({ chainId: constants.StarknetChainId.SN_MAIN } as Network)).to.be.equal(
'https://api.voyager.online/beta/txn',
);
});
});

describe('getTransactionsFromVoyagerUrl', () => {
it('returns correct URL', () => {
expect(getTransactionsFromVoyagerUrl({ chainId: constants.StarknetChainId.SN_MAIN } as Network)).to.be.equal(
'https://api.voyager.online/beta/txns',
);
});
});

describe('getVoyagerCredentials', () => {
it('returns correct credentials', () => {
expect(getVoyagerCredentials()).to.have.key('X-API-Key');
});
});

0 comments on commit 44ff4f1

Please sign in to comment.