Skip to content

Commit

Permalink
Merge pull request #10 from blockchain-certificates/chore/publish
Browse files Browse the repository at this point in the history
refactor(Types): reorganize explorer model
  • Loading branch information
Julien Fraichot authored Jan 27, 2021
2 parents 532bb36 + 56a3f48 commit 399b08a
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 37 deletions.
7 changes: 5 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ export default {
{
file: 'dist/MerkleProof2019.js',
format: 'cjs',
name: 'MerkleProof2019'
name: 'MerkleProof2019',
exports: 'named',
sourcemap: true
},
{
file: 'dist/MerkleProof2019-es.js',
format: 'es',
name: 'MerkleProof2019'
name: 'MerkleProof2019',
sourcemap: true
}
],
plugins: [
Expand Down
3 changes: 1 addition & 2 deletions src/explorers/bitcoin/bitpay.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { stripHashPrefix } from '../../utils/stripHashPrefix';
import { timestampToDateObject } from '../../utils/date';
import { ExplorerAPI, ExplorerURLs } from '../../models/Explorers';
import { ExplorerAPI, ExplorerURLs, IParsingFunctionAPI } from '../../models/Explorers';
import { TransactionData } from '../../models/TransactionData';
import { TRANSACTION_APIS, TRANSACTION_ID_PLACEHOLDER } from '../../constants/api';
import CONFIG from '../../constants/config';
import { BLOCKCHAINS } from '../../constants/blockchains';
import { IParsingFunctionAPI } from '../explorer';

// TODO: use tests/explorers/mocks/mockBitpayResponse as type
function parsingFunction ({ jsonResponse }: IParsingFunctionAPI): TransactionData {
Expand Down
3 changes: 1 addition & 2 deletions src/explorers/bitcoin/blockcypher.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { stripHashPrefix } from '../../utils/stripHashPrefix';
import { dateToUnixTimestamp } from '../../utils/date';
import { ExplorerAPI, ExplorerURLs } from '../../models/Explorers';
import { ExplorerAPI, ExplorerURLs, IParsingFunctionAPI } from '../../models/Explorers';
import { TransactionData } from '../../models/TransactionData';
import { TRANSACTION_APIS, TRANSACTION_ID_PLACEHOLDER } from '../../constants/api';
import CONFIG from '../../constants/config';
import { BLOCKCHAINS } from '../../constants/blockchains';
import { IParsingFunctionAPI } from '../explorer';

// TODO: use tests/explorers/mocks/mockBlockcypher as type
function parsingFunction ({ jsonResponse }: IParsingFunctionAPI): TransactionData {
Expand Down
3 changes: 1 addition & 2 deletions src/explorers/bitcoin/blockexplorer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { stripHashPrefix } from '../../utils/stripHashPrefix';
import { timestampToDateObject } from '../../utils/date';
import { ExplorerAPI, ExplorerURLs } from '../../models/Explorers';
import { ExplorerAPI, ExplorerURLs, IParsingFunctionAPI } from '../../models/Explorers';
import { TransactionData } from '../../models/TransactionData';
import { TRANSACTION_APIS, TRANSACTION_ID_PLACEHOLDER } from '../../constants/api';
import { BLOCKCHAINS } from '../../constants/blockchains';
import CONFIG from '../../constants/config';
import { IParsingFunctionAPI } from '../explorer';

// TODO: use tests/explorers/mocks/mockBlockexplorer as type
function parsingFunction ({ jsonResponse }: IParsingFunctionAPI): TransactionData {
Expand Down
3 changes: 1 addition & 2 deletions src/explorers/bitcoin/blockstream.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { stripHashPrefix } from '../../utils/stripHashPrefix';
import { timestampToDateObject } from '../../utils/date';
import { ExplorerAPI, ExplorerURLs } from '../../models/Explorers';
import { ExplorerAPI, ExplorerURLs, IParsingFunctionAPI } from '../../models/Explorers';
import { TransactionData } from '../../models/TransactionData';
import { TRANSACTION_APIS, TRANSACTION_ID_PLACEHOLDER } from '../../constants/api';
import { BLOCKCHAINS } from '../../constants/blockchains';
import { IParsingFunctionAPI } from '../explorer';

// TODO: use tests/explorers/mocks/mockBlockstreamResponse as type
function parsingFunction ({ jsonResponse }: IParsingFunctionAPI): TransactionData {
Expand Down
3 changes: 1 addition & 2 deletions src/explorers/ethereum/blockcypher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { stripHashPrefix } from '../../utils/stripHashPrefix';
import { BLOCKCHAINS } from '../../constants/blockchains';
import { TransactionData } from '../../models/TransactionData';
import { TRANSACTION_APIS, TRANSACTION_ID_PLACEHOLDER } from '../../constants/api';
import { ExplorerAPI, ExplorerURLs } from '../../models/Explorers';
import { ExplorerAPI, ExplorerURLs, IParsingFunctionAPI } from '../../models/Explorers';
import CONFIG from '../../constants/config';
import { dateToUnixTimestamp } from '../../utils/date';
import { prependHashPrefix } from '../../utils/prependHashPrefix';
import { IParsingFunctionAPI } from '../explorer';

const serviceURL: ExplorerURLs = {
main: `https://api.blockcypher.com/v1/eth/main/txs/${TRANSACTION_ID_PLACEHOLDER}?limit=500`,
Expand Down
3 changes: 1 addition & 2 deletions src/explorers/ethereum/etherscan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { buildTransactionServiceUrl } from '../../services/transaction-apis';
import { BLOCKCHAINS, isTestChain, SupportedChains } from '../../constants/blockchains';
import { TransactionData } from '../../models/TransactionData';
import { TRANSACTION_APIS, TRANSACTION_ID_PLACEHOLDER } from '../../constants/api';
import { ExplorerAPI, ExplorerURLs } from '../../models/Explorers';
import { ExplorerAPI, ExplorerURLs, IParsingFunctionAPI } from '../../models/Explorers';
import CONFIG from '../../constants/config';
import { IParsingFunctionAPI } from '../explorer';

const MAIN_API_BASE_URL = 'https://api.etherscan.io/api?module=proxy';
const TEST_API_BASE_URL = 'https://api-ropsten.etherscan.io/api?module=proxy';
Expand Down
17 changes: 1 addition & 16 deletions src/explorers/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,14 @@ import { buildTransactionServiceUrl } from '../services/transaction-apis';
import { request } from '../services/request';
import { isTestChain, SupportedChains } from '../constants/blockchains';
import { TransactionData } from '../models/TransactionData';
import { ExplorerAPI } from '../models/Explorers';
import { ExplorerAPI, TExplorerFunctionsArray } from '../models/Explorers';
import { explorerApi as EtherscanApi } from './ethereum/etherscan';
import { explorerApi as BlockCypherETHApi } from './ethereum/blockcypher';
import { explorerApi as BlockExplorerApi } from './bitcoin/blockexplorer';
import { explorerApi as BlockstreamApi } from './bitcoin/blockstream';
import { explorerApi as BlockCypherBTCApi } from './bitcoin/blockcypher';
import { explorerApi as BitPayApi } from './bitcoin/bitpay';

export type TExplorerFunctionsArray = Array<{
getTxData: (transactionId: string, chain?: SupportedChains) => Promise<TransactionData>;
priority?: number;
}>;
export interface IParsingFunctionAPI {
jsonResponse?: any; // the response from the service when called as rest
chain?: SupportedChains; // TODO: look at how to deprecate this. Only used in etherscan
key?: string; // identification key to pass to the service -> TODO: can this be merged into the serviceUrl? Only used in etherscan
keyPropertyName?: string; // the key property to associate with the identification key -> TODO: can this be merged into the serviceUrl? Only used in etherscan
transactionId?: string; // when using in RPCs we pass the tx id to look up since these functions are responsible for service lookup
serviceUrl?: string; // the distant service url
}
export type TExplorerParsingFunction = ((data: IParsingFunctionAPI) => TransactionData) |
((data: IParsingFunctionAPI) => Promise<TransactionData>);

export function explorerFactory (TransactionAPIArray: ExplorerAPI[]): TExplorerFunctionsArray {
return TransactionAPIArray
.map(explorerAPI => (
Expand Down
5 changes: 2 additions & 3 deletions src/explorers/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {
BitcoinTransactionAPIArray as BitcoinExplorers,
EthereumTransactionAPIArray as EthereumExplorers,
explorerFactory,
TExplorerFunctionsArray
explorerFactory
} from './explorer';
import { TRANSACTION_APIS } from '../constants/api';
import { ExplorerAPI } from '../models/Explorers';
import { ExplorerAPI, TExplorerFunctionsArray } from '../models/Explorers';
import { ethereumRPCParsingFunction } from './rpc/ethereum';
import { bitcoinRPCParsingFunction } from './rpc/bitcoin';

Expand Down
2 changes: 1 addition & 1 deletion src/explorers/rpc/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TransactionData } from '../../models/TransactionData';
import { stripHashPrefix } from '../../utils/stripHashPrefix';
import { timestampToDateObject } from '../../utils/date';
import { request } from '../../services/request';
import { IParsingFunctionAPI } from '../explorer';
import { IParsingFunctionAPI } from '../../models/Explorers';

export async function bitcoinRPCParsingFunction ({ serviceUrl, transactionId }: IParsingFunctionAPI): Promise<TransactionData> {
const getRawTransactionParams = {
Expand Down
2 changes: 1 addition & 1 deletion src/explorers/rpc/ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TransactionData } from '../../models/TransactionData';
import { stripHashPrefix } from '../../utils/stripHashPrefix';
import { request } from '../../services/request';
import { IParsingFunctionAPI } from '../explorer';
import { IParsingFunctionAPI } from '../../models/Explorers';

export async function ethereumRPCParsingFunction ({ serviceUrl, transactionId }: IParsingFunctionAPI): Promise<TransactionData> {
const transactionByHashParams = {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/lookForTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { SupportedChains, BLOCKCHAINS } from '../constants/blockchains';
import CONFIG from '../constants/config';
import PromiseProperRace from './promiseProperRace';
import { TransactionData } from '../models/TransactionData';
import { TExplorerFunctionsArray } from '../explorers/explorer';
import { TExplorerAPIs } from '../explorers';
import { TExplorerFunctionsArray } from '../models/Explorers';

export function getExplorersByChain (chain: SupportedChains, explorerAPIs: TExplorerAPIs): TExplorerFunctionsArray {
switch (chain) {
Expand Down
18 changes: 17 additions & 1 deletion src/models/Explorers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { TRANSACTION_APIS } from '../constants/api';
import { TExplorerParsingFunction } from '../explorers/explorer';
import { SupportedChains } from '../constants/blockchains';
import { TransactionData } from './TransactionData';

export interface ExplorerURLs {
main: string;
test: string;
}

export type TExplorerFunctionsArray = Array<{
getTxData: (transactionId: string, chain?: SupportedChains) => Promise<TransactionData>;
priority?: number;
}>;
export interface IParsingFunctionAPI {
jsonResponse?: any; // the response from the service when called as rest
chain?: SupportedChains; // TODO: look at how to deprecate this. Only used in etherscan
key?: string; // identification key to pass to the service -> TODO: can this be merged into the serviceUrl? Only used in etherscan
keyPropertyName?: string; // the key property to associate with the identification key -> TODO: can this be merged into the serviceUrl? Only used in etherscan
transactionId?: string; // when using in RPCs we pass the tx id to look up since these functions are responsible for service lookup
serviceUrl?: string; // the distant service url
}
export type TExplorerParsingFunction = ((data: IParsingFunctionAPI) => TransactionData) |
((data: IParsingFunctionAPI) => Promise<TransactionData>);

export interface ExplorerAPI {
serviceURL?: string | ExplorerURLs;
priority?: 0 | 1 | -1; // 0: custom APIs will run before the default APIs, 1: after, -1: reserved to default APIs
Expand Down

0 comments on commit 399b08a

Please sign in to comment.