Skip to content

Commit

Permalink
Tim/enforce prettier and eslint on precommit (#230)
Browse files Browse the repository at this point in the history
* chore: add husky precommit hooks to prettier and eslint staged files

* chore: run prettier --write .
  • Loading branch information
teebszet authored Sep 4, 2023
1 parent 5cab1f1 commit d8691a4
Show file tree
Hide file tree
Showing 37 changed files with 835 additions and 297 deletions.
6 changes: 1 addition & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
"root": true,
"plugins": [
"@typescript-eslint",
"import",
"prettier"
],
"plugins": ["@typescript-eslint", "import", "prettier"],
"extends": [
"airbnb-typescript/base",
"prettier",
Expand Down
7 changes: 6 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 🔘 PR Type

What kind of change does this PR introduce?

<!-- Please check the one that applies to this PR using "x". -->
Expand All @@ -12,32 +13,36 @@ What kind of change does this PR introduce?
- [ ] Documentation content changes
- [ ] Other... Please describe:


# 📜 Background

Provide a brief explanation of why this pull request is needed. Include the problem you are solving or the functionality you are adding. Reference any related issues.

Issue Link: #[issue_number]
Context Link (if applicable):

# 🔄 Changes

Does this PR introduce a breaking change?

- [ ] Yes, Incompatible API changes
- [ ] No, Adds functionality (backwards compatible)
- [ ] No, Bug fixes (backwards compatible)

Changes:

- What has been modified, added, or removed
- Which are breaking changes

Impact:

- How it improves performance, fixes bugs, adds functionality, etc.
- Impact on downstream apps, link to issues or PRs for migration
- What to test

# 🖼 Screenshot / 📹 Video

# ✅ Review checklist

Please ensure the following are true before merging:

- [ ] Code Style is consistent with the project guidelines.
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ jobs:
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
- run: npm publish
if: "!github.event.release.prerelease"
if: '!github.event.release.prerelease'
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Xverse Core

- Folders are separated according to functionaility e.g. wallet works with getting assets and balances, transaction works with creating transaction objects.
- Files under `/api` contains functions that are making external API calls, the file name should indicate which service it is calling.
- All types are under the `types` directory, some are interfaces that maps to API responses, some are data types for business logic.
- All export should be group and exported using `index.ts`.
- All export should be group and exported using `index.ts`.
20 changes: 4 additions & 16 deletions api/btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ export async function fetchBtcOrdinalTransactions(ordinalsAddress: string, netwo
return transactions.filter((tx) => tx.incoming);
}

export async function fetchBtcPaymentTransactions(
btcAddress: string,
ordinalsAddress: string,
network: NetworkType
) {
export async function fetchBtcPaymentTransactions(btcAddress: string, ordinalsAddress: string, network: NetworkType) {
const btcClient = new BitcoinEsploraApiProvider({
network,
});
Expand All @@ -36,22 +32,14 @@ export async function fetchBtcTransactionsData(
btcAddress: string,
ordinalsAddress: string,
network: NetworkType,
withOrdinals: boolean
withOrdinals: boolean,
): Promise<BtcTransactionData[]> {
if (withOrdinals) {
const ordinalsTransactions = await fetchBtcOrdinalTransactions(ordinalsAddress, network);
const paymentTransactions = await fetchBtcPaymentTransactions(
btcAddress,
ordinalsAddress,
network
);
const paymentTransactions = await fetchBtcPaymentTransactions(btcAddress, ordinalsAddress, network);
return [...new Set([...paymentTransactions, ...ordinalsTransactions])];
}
const paymentTransactions = await fetchBtcPaymentTransactions(
btcAddress,
ordinalsAddress,
network
);
const paymentTransactions = await fetchBtcPaymentTransactions(btcAddress, ordinalsAddress, network);
return paymentTransactions;
}

Expand Down
8 changes: 3 additions & 5 deletions api/esplora/esploraAPiProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class BitcoinEsploraApiProvider extends ApiInstance implements Bi
}

private async getUtxosEsplora(
address: string
address: string,
): Promise<(esplora.UTXO & { address: string; blockHeight?: number })[]> {
const data = await this.httpGet<esplora.UTXO[]>(`/address/${address}/utxo`);

Expand All @@ -71,7 +71,7 @@ export default class BitcoinEsploraApiProvider extends ApiInstance implements Bi
}

private async getUtxosBlockcypher(
address: string
address: string,
): Promise<(esplora.UTXO & { address: string; blockHeight?: number })[]> {
// blockcypher has a limit of 2000 UTXOs per request and allows pagination
// so we will try get all unspent UTXOs by making multiple requests if there are more than 2000
Expand Down Expand Up @@ -115,9 +115,7 @@ export default class BitcoinEsploraApiProvider extends ApiInstance implements Bi
return utxoSets;
}

async getUnspentUtxos(
address: string
): Promise<(esplora.UTXO & { address: string; blockHeight?: number })[]> {
async getUnspentUtxos(address: string): Promise<(esplora.UTXO & { address: string; blockHeight?: number })[]> {
try {
// we try using the base esplora api first, however, it has a limit to the number of UTXOs it can return
const utxoSets = await this.getUtxosEsplora(address);
Expand Down
4 changes: 2 additions & 2 deletions api/esplora/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BtcAddressData, BtcTransactionBroadcastResponse } from "../../types";
import { BtcAddressData, BtcTransactionBroadcastResponse } from '../../types';
import { BtcAddressMempool, Transaction, UTXO } from '../../types/api/esplora';
export interface BitcoinApiProvider {
/**
Expand Down Expand Up @@ -42,4 +42,4 @@ export interface BitcoinApiProvider {
* @returns {Promise<number>}
*/
getLatestBlockHeight(address: string): Promise<number>;
}
}
2 changes: 1 addition & 1 deletion api/gamma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NFT_BASE_URI } from '../constant';
export async function getNftDetail(
tokenId: string,
contractAddress: string,
contractName: string
contractName: string,
): Promise<NftDetailResponse> {
const apiUrl = `${NFT_BASE_URI}/${contractAddress}.${contractName}/${tokenId}`;

Expand Down
2 changes: 1 addition & 1 deletion api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export * from './stacks';
export * from './gamma';
export * from './ordinals';
export * from './esplora/esploraAPiProvider';
export * from './ordinals/provider';
export * from './ordinals/provider';
1 change: 0 additions & 1 deletion api/ordinals/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Inscription, InscriptionsList } from '../../types/api/ordinals';


export interface OrdinalsApiProvider {
/**
* Get the inscriptions of an account given its addresses.
Expand Down
1 change: 0 additions & 1 deletion api/stacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ export async function fetchAddressOfBnsName(
}

export async function fetchStxPendingTxData(stxAddress: string, network: StacksNetwork): Promise<StxPendingTxData> {

const [confirmedTransactions, mempoolTransactions] = await Promise.all([
getConfirmedTransactions({
stxAddress,
Expand Down
9 changes: 2 additions & 7 deletions coins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { fetchCoinMetaData } from '../api';
import { supportedCoins } from '../constant';
import { Coin, CoinMetaData, CoinsResponse } from '../types/index';

export async function getCoinMetaData(
contractIds: string[],
network: StacksNetwork
): Promise<CoinsResponse> {
export async function getCoinMetaData(contractIds: string[], network: StacksNetwork): Promise<CoinsResponse> {
const coinMetaDataList = contractIds.map(async (contract) => {
const response: CoinMetaData | undefined = await fetchCoinMetaData(contract, network);
let coin: Coin;
Expand All @@ -28,9 +25,7 @@ export async function getCoinMetaData(
};
if (coin.name === '') coin.name = contract.substring(coin.contract.indexOf('.') + 1);
}
const isSupported = supportedCoins.find(
(supportedCoin) => supportedCoin.contract === coin.contract
);
const isSupported = supportedCoins.find((supportedCoin) => supportedCoin.contract === coin.contract);
if (isSupported) {
coin.supported = true;
coin.name = isSupported.name;
Expand Down
14 changes: 4 additions & 10 deletions currency/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@ const microstacksToStx = (microstacks: BigNumber): BigNumber => microstacks.mult

const stxToMicrostacks = (stacks: BigNumber): BigNumber => stacks.multipliedBy(1000000);

const getStxFiatEquivalent = (
stxAmount: BigNumber,
stxBtcRate: BigNumber,
btcFiatRate: BigNumber
): BigNumber => microstacksToStx(stxAmount).multipliedBy(stxBtcRate).multipliedBy(btcFiatRate);
const getStxFiatEquivalent = (stxAmount: BigNumber, stxBtcRate: BigNumber, btcFiatRate: BigNumber): BigNumber =>
microstacksToStx(stxAmount).multipliedBy(stxBtcRate).multipliedBy(btcFiatRate);

const getBtcFiatEquivalent = (btcAmount: BigNumber, btcFiatRate: BigNumber): BigNumber =>
satsToBtc(btcAmount).multipliedBy(btcFiatRate);

const getStxTokenEquivalent = (
fiatAmount: BigNumber,
stxBtcRate: BigNumber,
btcFiatRate: BigNumber
): BigNumber => fiatAmount.dividedBy(stxBtcRate).dividedBy(btcFiatRate);
const getStxTokenEquivalent = (fiatAmount: BigNumber, stxBtcRate: BigNumber, btcFiatRate: BigNumber): BigNumber =>
fiatAmount.dividedBy(stxBtcRate).dividedBy(btcFiatRate);

const getBtcEquivalent = (fiatAmount: BigNumber, btcFiatRate: BigNumber): BigNumber =>
fiatAmount.dividedBy(btcFiatRate);
Expand Down
10 changes: 3 additions & 7 deletions gaia/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { getPublicKeyFromPrivate, publicKeyToBtcAddress, randomBytes } from "@stacks/encryption";
import { createFetchFn } from "@stacks/network";
import { GaiaHubConfig } from "@stacks/storage";
import { getPublicKeyFromPrivate, publicKeyToBtcAddress, randomBytes } from '@stacks/encryption';
import { createFetchFn } from '@stacks/network';
import { GaiaHubConfig } from '@stacks/storage';
import { Json, TokenSigner } from 'jsontokens';

interface HubInfo {
challenge_text?: string;
read_url_prefix: string;
}


export type FetchFn = (url: string, init?: RequestInit) => Promise<Response>;

export const getHubInfo = async (gaiaHubUrl: string, fetchFn: FetchFn = createFetchFn()) => {
Expand All @@ -17,7 +16,6 @@ export const getHubInfo = async (gaiaHubUrl: string, fetchFn: FetchFn = createFe
return data;
};


interface GaiaAuthPayload {
gaiaHubUrl: string;
iss: string;
Expand Down Expand Up @@ -50,7 +48,6 @@ const makeGaiaAuthToken = ({
return `v1:${token}`;
};


interface ConnectToGaiaOptions {
hubInfo: HubInfo;
privateKey: string;
Expand All @@ -74,5 +71,4 @@ export const connectToGaiaHubWithConfig = ({
};
};


export * from './walletConfig';
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['dist']
testPathIgnorePatterns: ['dist'],
};
Loading

0 comments on commit d8691a4

Please sign in to comment.