Skip to content

Commit

Permalink
Merge pull request #124 from input-output-hk/feat/eslint-sorting-rules
Browse files Browse the repository at this point in the history
Feat/eslint sorting rules
  • Loading branch information
rhyslbw authored Oct 27, 2021
2 parents da4a965 + 2c14a16 commit 3659ec6
Show file tree
Hide file tree
Showing 85 changed files with 716 additions and 662 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
"plugin:@typescript-eslint/recommended",
"plugin:jsdoc/recommended"
],
"plugins": ["jsdoc"],
"plugins": ["jsdoc", "sort-keys-fix", "sort-imports-es6-autofix"],
"settings": {
"import/resolver": {
"typescript": {}
Expand All @@ -27,6 +27,9 @@ module.exports = {
"no-unused-expressions": 0,
"no-useless-constructor": 0,
"quotes": ["error", "single", { "avoidEscape": true }],
"sort-keys-fix/sort-keys-fix": ["warn", "asc", {"natural": true}],
"sort-imports-es6-autofix/sort-imports-es6": "warn",
"sort-imports": ["warn", {ignoreDeclarationSort: true}],
"unicorn/filename-case": 0,
"unicorn/prevent-abbreviations": 0,
"unicorn/no-null": 0,
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"docs": "typedoc --packages . && yarn workspaces run coverage && node collectCoverage.mjs",
"cleanup": "yarn workspaces run cleanup && shx rm -rf node_modules",
"lint": "yarn workspaces run lint",
"lint:fix": "yarn workspaces run lint:fix",
"tscNoEmit": "yarn workspaces run tscNoEmit",
"prepare": "husky install",
"pre-commit": "lint-staged",
Expand Down Expand Up @@ -65,6 +66,8 @@
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-sonarjs": "^0.9.1",
"eslint-plugin-sort-imports-es6-autofix": "^0.6.0",
"eslint-plugin-sort-keys-fix": "^1.1.2",
"eslint-plugin-unicorn": "^35.0.0",
"eslint-watch": "^7.0.0",
"fs-extra": "^10.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/blockfrost/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"tscNoEmit": "shx echo typescript --noEmit command not implemented yet",
"cleanup": "shx rm -rf dist node_modules",
"lint": "eslint --ignore-path ../../.eslintignore \"**/*.ts\"",
"lint:fix": "eslint --fix --ignore-path ../../.eslintignore \"**/*.ts\"",
"test": "jest -c ./jest.config.js",
"coverage": "shx echo No coverage report for this package",
"prepack": "yarn build"
Expand Down
63 changes: 32 additions & 31 deletions packages/blockfrost/src/BlockfrostToCore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Responses } from '@blockfrost/blockfrost-js';
import { Cardano, ProtocolParametersRequiredByWallet } from '@cardano-sdk/core';
import { Responses } from '@blockfrost/blockfrost-js';

type Unpacked<T> = T extends (infer U)[] ? U : T;
type BlockfrostAddressUtxoContent = Responses['address_utxo_content'];
Expand All @@ -15,21 +15,28 @@ export const BlockfrostToCore = {
BlockfrostToCore.txIn(BlockfrostToCore.inputFromUtxo(address, utxo)),
BlockfrostToCore.txOut(BlockfrostToCore.outputFromUtxo(address, utxo))
]) as Cardano.Utxo[],
// without `as OgmiosSchema.Utxo` above TS thinks the return value is (OgmiosSchema.TxIn | OgmiosSchema.TxOut)[][]

transactionUtxos: (utxoResponse: Responses['tx_content_utxo']) => ({
inputs: utxoResponse.inputs.map((input) => ({
...BlockfrostToCore.txIn(input),
address: input.address
})),
outputs: utxoResponse.outputs.map(BlockfrostToCore.txOut)
}),
blockToTip: (block: Responses['block_content']): Cardano.Tip => ({
blockNo: block.height!,
hash: block.hash,
slot: block.slot!
}),

currentWalletProtocolParameters: (
blockfrost: Responses['epoch_param_content']
): ProtocolParametersRequiredByWallet => ({
coinsPerUtxoWord: Number(blockfrost.coins_per_utxo_word),
maxCollateralInputs: blockfrost.max_collateral_inputs || undefined,
maxTxSize: Number(blockfrost.max_tx_size),
maxValueSize: Number(blockfrost.max_val_size),
minFeeCoefficient: blockfrost.min_fee_a,
minFeeConstant: blockfrost.min_fee_b,
minPoolCost: Number(blockfrost.min_pool_cost),
poolDeposit: Number(blockfrost.pool_deposit),
protocolVersion: { major: blockfrost.protocol_major_ver, minor: blockfrost.protocol_minor_ver },
stakeKeyDeposit: Number(blockfrost.key_deposit)
}),

inputFromUtxo: (address: string, utxo: BlockfrostUtxo): BlockfrostInput => ({
address,
amount: utxo.amount,
Expand All @@ -46,16 +53,25 @@ export const BlockfrostToCore = {

outputs: (outputs: BlockfrostOutputs): Cardano.TxOut[] => outputs.map((output) => BlockfrostToCore.txOut(output)),

// without `as OgmiosSchema.Utxo` above TS thinks the return value is (OgmiosSchema.TxIn | OgmiosSchema.TxOut)[][]
transactionUtxos: (utxoResponse: Responses['tx_content_utxo']) => ({
inputs: utxoResponse.inputs.map((input) => ({
...BlockfrostToCore.txIn(input),
address: input.address
})),
outputs: utxoResponse.outputs.map(BlockfrostToCore.txOut)
}),

txContentUtxo: (blockfrost: Responses['tx_content_utxo']) => ({
hash: blockfrost.hash,
inputs: BlockfrostToCore.inputs(blockfrost.inputs),
outputs: BlockfrostToCore.outputs(blockfrost.outputs),
hash: blockfrost.hash
outputs: BlockfrostToCore.outputs(blockfrost.outputs)
}),

txIn: (blockfrost: BlockfrostInput): Cardano.TxIn => ({
txId: blockfrost.tx_hash,
address: blockfrost.address,
index: blockfrost.output_index,
address: blockfrost.address
txId: blockfrost.tx_hash
}),

txOut: (blockfrost: BlockfrostOutput): Cardano.TxOut => {
Expand All @@ -67,24 +83,9 @@ export const BlockfrostToCore = {
return {
address: blockfrost.address,
value: {
coins: BigInt(blockfrost.amount.find(({ unit }) => unit === 'lovelace')!.quantity),
assets
assets,
coins: BigInt(blockfrost.amount.find(({ unit }) => unit === 'lovelace')!.quantity)
}
};
},

currentWalletProtocolParameters: (
blockfrost: Responses['epoch_param_content']
): ProtocolParametersRequiredByWallet => ({
maxTxSize: Number(blockfrost.max_tx_size),
minFeeCoefficient: blockfrost.min_fee_a,
minFeeConstant: blockfrost.min_fee_b,
stakeKeyDeposit: Number(blockfrost.key_deposit),
poolDeposit: Number(blockfrost.pool_deposit),
minPoolCost: Number(blockfrost.min_pool_cost),
coinsPerUtxoWord: Number(blockfrost.coins_per_utxo_word),
maxValueSize: Number(blockfrost.max_val_size),
maxCollateralInputs: blockfrost.max_collateral_inputs || undefined,
protocolVersion: { major: blockfrost.protocol_major_ver, minor: blockfrost.protocol_minor_ver }
})
}
};
64 changes: 32 additions & 32 deletions packages/blockfrost/src/blockfrostProvider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { WalletProvider, ProviderError, ProviderFailure, Cardano } from '@cardano-sdk/core';
import { BlockFrostAPI, Error as BlockfrostError, Responses } from '@blockfrost/blockfrost-js';
import { Options } from '@blockfrost/blockfrost-js/lib/types';
import { BlockfrostToCore } from './BlockfrostToCore';
import { Cardano, ProviderError, ProviderFailure, WalletProvider } from '@cardano-sdk/core';
import { Options } from '@blockfrost/blockfrost-js/lib/types';
import { dummyLogger } from 'ts-log';

const formatBlockfrostError = (error: unknown) => {
Expand Down Expand Up @@ -32,9 +32,9 @@ const formatBlockfrostError = (error: unknown) => {
throw new ProviderError(ProviderFailure.Unknown, error, 'failed to parse error (status code)');
}
return {
status_code,
error: errorAsType2.errno.toString(),
message: errorAsType1.message,
error: errorAsType2.errno.toString()
status_code
};
}
throw new ProviderError(ProviderFailure.Unknown, error, 'failed to parse error (response json)');
Expand Down Expand Up @@ -127,7 +127,7 @@ export const blockfrostProvider = (options: Options, logger = dummyLogger): Wall
rewards: BigInt(accountResponse.withdrawable_amount)
};

return { utxo, delegationAndRewards };
return { delegationAndRewards, utxo };
};

const fetchRedeemers = async ({
Expand All @@ -138,11 +138,11 @@ export const blockfrostProvider = (options: Options, logger = dummyLogger): Wall
const response = await blockfrost.txsRedeemers(hash);
return response.map(
({ purpose, script_hash, unit_mem, unit_steps, tx_index }): Cardano.Redeemer => ({
index: tx_index,
executionUnits: {
memory: Number.parseInt(unit_mem),
steps: Number.parseInt(unit_steps)
},
index: tx_index,
purpose: ((): Cardano.Redeemer['purpose'] => {
switch (purpose) {
case 'cert':
Expand Down Expand Up @@ -184,8 +184,8 @@ export const blockfrostProvider = (options: Options, logger = dummyLogger): Wall
const fetchPoolRetireCerts = async (hash: string): Promise<Cardano.PoolCertificate[]> => {
const response = await blockfrost.txsPoolRetires(hash);
return response.map(({ cert_index, pool_id, retiring_epoch }) => ({
epoch: retiring_epoch,
certIndex: cert_index,
epoch: retiring_epoch,
poolId: pool_id,
type: Cardano.CertificateType.PoolRetirement
}));
Expand All @@ -194,8 +194,8 @@ export const blockfrostProvider = (options: Options, logger = dummyLogger): Wall
const fetchPoolUpdateCerts = async (hash: string): Promise<Cardano.PoolCertificate[]> => {
const response = await blockfrost.txsPoolUpdates(hash);
return response.map(({ cert_index, pool_id, active_epoch }) => ({
epoch: active_epoch,
certIndex: cert_index,
epoch: active_epoch,
poolId: pool_id,
type: Cardano.CertificateType.PoolRegistration
}));
Expand All @@ -204,32 +204,32 @@ export const blockfrostProvider = (options: Options, logger = dummyLogger): Wall
const fetchMirCerts = async (hash: string): Promise<Cardano.MirCertificate[]> => {
const response = await blockfrost.txsMirs(hash);
return response.map(({ address, amount, cert_index, pot }) => ({
type: Cardano.CertificateType.MIR,
address,
quantity: BigInt(amount),
certIndex: cert_index,
pot
pot,
quantity: BigInt(amount),
type: Cardano.CertificateType.MIR
}));
};

const fetchStakeCerts = async (hash: string): Promise<Cardano.StakeAddressCertificate[]> => {
const response = await blockfrost.txsStakes(hash);
return response.map(({ address, cert_index, registration }) => ({
type: registration ? Cardano.CertificateType.StakeRegistration : Cardano.CertificateType.StakeDeregistration,
address,
certIndex: cert_index
certIndex: cert_index,
type: registration ? Cardano.CertificateType.StakeRegistration : Cardano.CertificateType.StakeDeregistration
}));
};

const fetchDelegationCerts = async (hash: string): Promise<Cardano.StakeDelegationCertificate[]> => {
const response = await blockfrost.txsDelegations(hash);
return response.map(({ cert_index, index, address, active_epoch, pool_id }) => ({
type: Cardano.CertificateType.StakeDelegation,
address,
certIndex: cert_index,
delegationIndex: index,
address,
epoch: active_epoch,
poolId: pool_id
poolId: pool_id,
type: Cardano.CertificateType.StakeDelegation
}));
};

Expand Down Expand Up @@ -257,30 +257,30 @@ export const blockfrostProvider = (options: Options, logger = dummyLogger): Wall
const { inputs, outputs } = BlockfrostToCore.transactionUtxos(await blockfrost.txsUtxos(hash));
const response = await blockfrost.txs(hash);
return {
id: hash,
blockHeader: {
slot: response.slot,
blockHash: response.block,
blockHeight: response.block_height,
blockHash: response.block
},
txSize: response.size,
implicitCoin: {
deposit: BigInt(response.deposit)
// TODO: use computeImplicitCoin to compute implicit input
slot: response.slot
},
body: {
certificates: await fetchCertificates(response),
fee: BigInt(response.fees),
index: response.index,
inputs,
mint: await fetchMint(response),
outputs,
fee: BigInt(response.fees),
validityInterval: {
invalidBefore: parseValidityInterval(response.invalid_before),
invalidHereafter: parseValidityInterval(response.invalid_hereafter)
},
withdrawals: await fetchWithdrawals(response),
mint: await fetchMint(response),
certificates: await fetchCertificates(response)
withdrawals: await fetchWithdrawals(response)
},
id: hash,
implicitCoin: {
deposit: BigInt(response.deposit)
// TODO: use computeImplicitCoin to compute implicit input
},
txSize: response.size,
witness: {
redeemers: await fetchRedeemers(response)
}
Expand Down Expand Up @@ -314,14 +314,14 @@ export const blockfrostProvider = (options: Options, logger = dummyLogger): Wall
};

const providerFunctions: WalletProvider = {
currentWalletProtocolParameters,
ledgerTip,
networkInfo,
stakePoolStats,
submitTx,
utxoDelegationAndRewards,
queryTransactionsByAddresses,
queryTransactionsByHashes,
currentWalletProtocolParameters
stakePoolStats,
submitTx,
utxoDelegationAndRewards
};

return Object.keys(providerFunctions).reduce((provider, key) => {
Expand Down
Loading

0 comments on commit 3659ec6

Please sign in to comment.