Skip to content

Commit

Permalink
add gha and linting (#72)
Browse files Browse the repository at this point in the history
* add type aliases

* make example use env vars

* finish tests

* add types

* cleanup

* add gha

* clean up all files

* add prettier lint

* sep12 stuff linting

* cleanup

* move to devdeps
  • Loading branch information
acharb authored Oct 17, 2023
1 parent f0523ec commit 5072808
Show file tree
Hide file tree
Showing 28 changed files with 699 additions and 170 deletions.
59 changes: 59 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports = {
ignorePatterns: ["lib/", "node_modules/", "docs/"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: ["tsconfig.json", "examples/tsconfig.json", "test/tsconfig.json"],
sourceType: "module",
},
plugins: ["@typescript-eslint", "jsdoc"],
extends: [
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:@typescript-eslint/recommended",
"prettier",
],
rules: {
// Off
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/no-floating-promises": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/prefer-regexp-exec": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-misused-promises": "off",
"@typescript-eslint/no-base-to-string": "off",

// Error
"@typescript-eslint/no-shadow": "error",
"@typescript-eslint/no-unused-expressions": "error",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "error",
"@typescript-eslint/prefer-function-type": "error",
"@typescript-eslint/prefer-namespace-keyword": "error",
"@typescript-eslint/triple-slash-reference": [
"error",
{
path: "always",
types: "prefer-import",
lib: "always",
},
],
"@typescript-eslint/unified-signatures": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-empty-function": "error",
"@typescript-eslint/no-empty-interface": "error",
"jsdoc/check-alignment": "error",
"jsdoc/check-indentation": "error",
},
overrides: [
{
files: ["test/**", "examples/**"],
rules: {
"@typescript-eslint/no-shadow": "off",
"@typescript-eslint/no-explicit-any": "off",
},
},
],
};
14 changes: 14 additions & 0 deletions .github/workflows/runTests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Run Tests
on: [pull_request]
jobs:
test-ci:
name: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 18
- run: yarn install
- run: yarn build
- run: yarn test:ci
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
. "$(dirname -- "$0")/_/husky.sh"

yarn pretty-quick --staged
yarn lint-staged
9 changes: 5 additions & 4 deletions examples/sep24/sep24.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const asset = new IssuedAssetId(assetCode, assetIssuer);
const runSep24 = async () => {
await createAccount();
await runDeposit(anchor, kp);
await runDepositWatcher(anchor);
runDepositWatcher(anchor);

while (!depositDone) {
await new Promise((resolve) => setTimeout(resolve, 1000));
Expand All @@ -61,7 +61,7 @@ const runSep24 = async () => {
}

await runWithdraw(anchor, kp);
await runWithdrawWatcher(anchor, kp);
runWithdrawWatcher(anchor, kp);
};

// Create Account
Expand Down Expand Up @@ -118,7 +118,7 @@ export let depositDone = false;
export const runDepositWatcher = (anchor: Anchor) => {
console.log("\nstarting watcher ...");

let stop: Types.WatcherStopFunction;
const stop: Types.WatcherStopFunction;
const onMessage = (m: Types.AnchorTransaction) => {
console.log({ m });
if (m.status === Types.TransactionStatus.completed) {
Expand Down Expand Up @@ -183,7 +183,7 @@ const sendWithdrawalTransaction = async (withdrawalTxn, kp) => {
export const runWithdrawWatcher = (anchor, kp) => {
console.log("\nstarting watcher ...");

let stop;
const stop;
const onMessage = (m) => {
console.log({ m });

Expand Down Expand Up @@ -219,6 +219,7 @@ const walletSigner = DefaultSigner;
walletSigner.signWithDomainAccount = async ({
transactionXDR,
networkPassphrase,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
accountKp,
}: Types.SignWithDomainAccountParams): Promise<Transaction> => {
if (!clientSecret) {
Expand Down
10 changes: 10 additions & 0 deletions examples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"esModuleInterop": true,
"baseUrl": "./",
"outDir": "lib",
"declaration": true,
"declarationDir": "lib"
},
"include": ["./"]
}
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@
"private": false,
"devDependencies": {
"@babel/preset-env": "^7.20.2",
"@stellar/eslint-config": "^2.1.2",
"@stellar/prettier-config": "^1.0.1",
"@stellar/tsconfig": "^1.0.2",
"@types/jest": "^29.4.0",
"@types/lodash": "^4.14.194",
"@types/sinon": "^10.0.15",
"@typescript-eslint/eslint-plugin": "^6.7.5",
"@typescript-eslint/parser": "^6.7.5",
"babel-jest": "^29.4.1",
"crypto-browserify": "^3.12.0",
"dotenv": "^16.3.1",
"eslint": "^8.33.0",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-jsdoc": "^46.8.2",
"husky": "^8.0.0",
"jest": "^29.4.1",
"lint-staged": "^14.0.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"pretty-quick": "^2.0.1",
Expand All @@ -49,11 +53,18 @@
"utility-types": "^3.10.0"
},
"scripts": {
"lint": "eslint . --ext .ts",
"prepare": "husky install",
"test": "jest --watchAll",
"test:ci": "jest --ci",
"build:web": "webpack --config webpack.config.js",
"build:node": "webpack --env NODE=true --config webpack.config.js",
"build": "run-p build:web build:node",
"example:sep24": "ts-node examples/sep24/sep24.ts"
},
"lint-staged": {
"**/*.ts": [
"eslint --fix --max-warnings 0"
]
}
}
3 changes: 1 addition & 2 deletions src/walletSdk/Anchor/Sep24.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
AnchorTransaction,
GetTransactionParams,
GetTransactionsParams,
TransactionStatus,
AnchorServiceInfo,
} from "../Types";
import { Watcher } from "../Watcher";
Expand Down Expand Up @@ -106,7 +105,7 @@ export class Sep24 {
if (!assets.includes(assetCode)) {
throw new AssetNotSupportedError(type, assetCode);
}
let memoMap = {};
const memoMap = {};
if (destinationMemo) {
memoMap["memo_type"] = destinationMemo.type;
memoMap["memo"] = destinationMemo.value;
Expand Down
8 changes: 4 additions & 4 deletions src/walletSdk/Asset/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import StellarSdk from "stellar-sdk";
import { Asset as StellarAsset } from "stellar-sdk";

const STELLAR_SCHEME = "stellar";

Expand All @@ -18,11 +18,11 @@ export class StellarAssetId extends AssetId {
issuer: string;
scheme = STELLAR_SCHEME;

toAsset() {
toAsset(): StellarAsset {
if (this.id === "native") {
return new StellarSdk.Asset("XLM");
return new StellarAsset("XLM");
}
return new StellarSdk.Asset(this.code, this.issuer);
return new StellarAsset(this.code, this.issuer);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/walletSdk/Auth/WalletSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const DefaultSigner: WalletSigner = {
transaction.sign(accountKp.keypair);
return transaction;
},
// eslint-disable-next-line @typescript-eslint/require-await
signWithDomainAccount: async () => {
throw new Error(
"The DefaultSigner can't sign transactions with domain account",
Expand Down Expand Up @@ -72,6 +73,7 @@ export class DomainSigner implements WalletSigner {
async signWithDomainAccount({
transactionXDR,
networkPassphrase,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
accountKp,
}: SignWithDomainAccountParams): Promise<Transaction> {
const response = await this.client.post(
Expand Down
11 changes: 5 additions & 6 deletions src/walletSdk/Customer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import queryString from "query-string";
import { Sep9InfoRequiredError, CustomerNotFoundError } from "../Exceptions";
import {
CustomerInfoMap,
Sep12Status,
Sep12Type,
Field,
ProvidedField,
GetCustomerParams,
GetCustomerResponse,
AddCustomerResponse,
Expand Down Expand Up @@ -75,9 +71,12 @@ export class Sep12 {
if (type) {
customerMap = { type, ...customerMap };
}
if (memo) {
customerMap["memo"] = memo;
}

// Check if binary data given so can adjust headers
let includesBinary = sep9BinaryInfo && Object.keys(sep9BinaryInfo).length;
const includesBinary = sep9BinaryInfo && Object.keys(sep9BinaryInfo).length;
const resp = await this.httpClient.put(
`${this.baseUrl}/customer`,
customerMap,
Expand Down Expand Up @@ -127,7 +126,7 @@ export class Sep12 {
customerMap = { ...customerMap, ...sep9Info, ...sep9BinaryInfo };

// Check if binary data given so can adjust headers
let includesBinary = sep9BinaryInfo && Object.keys(sep9BinaryInfo).length;
const includesBinary = sep9BinaryInfo && Object.keys(sep9BinaryInfo).length;
const resp = await this.httpClient.put(
`${this.baseUrl}/customer`,
customerMap,
Expand Down
2 changes: 1 addition & 1 deletion src/walletSdk/Exceptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class AccountDoesNotExistError extends Error {

export class TransactionSubmitFailedError extends Error {
constructor(response: Horizon.SubmitTransactionResponse) {
super(`Submit transaction failed ${response}`);
super(`Submit transaction failed ${JSON.stringify(response)}`);
Object.setPrototypeOf(this, TransactionSubmitFailedError.prototype);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/walletSdk/Horizon/Stellar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class Stellar {
}

try {
const success = await this.submitTransaction(transaction);
await this.submitTransaction(transaction);
return transaction;
} catch (e) {
const resultCode = getResultCode(e);
Expand Down
8 changes: 4 additions & 4 deletions src/walletSdk/Horizon/Transaction/CommonTransactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export abstract class CommonTransactionBuilder<T> {
source: this.sourceAddress,
}),
);
return this as any as T;
return this as unknown as T;
}

removeAssetSupport(asset: IssuedAssetId): T {
Expand All @@ -36,7 +36,7 @@ export abstract class CommonTransactionBuilder<T> {
},
}),
);
return this as any as T;
return this as unknown as T;
}

removeAccountSigner(signerAddress: AccountKeypair): T {
Expand All @@ -50,7 +50,7 @@ export abstract class CommonTransactionBuilder<T> {
masterWeight: 0,
}),
);
return this as any as T;
return this as unknown as T;
}

setThreshold({
Expand All @@ -70,6 +70,6 @@ export abstract class CommonTransactionBuilder<T> {
highThreshold: high,
}),
);
return this as any as T;
return this as unknown as T;
}
}
7 changes: 1 addition & 6 deletions src/walletSdk/Horizon/Transaction/SponsoringBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import StellarSdk, {
TransactionBuilder as StellarTransactionBuilder,
Transaction,
xdr,
} from "stellar-sdk";
import { IssuedAssetId } from "../../Asset";
import StellarSdk, { xdr } from "stellar-sdk";

import { CommonTransactionBuilder } from "./CommonTransactionBuilder";
import { AccountKeypair } from "../Account";
Expand Down
2 changes: 1 addition & 1 deletion src/walletSdk/Recovery/AccountRecover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export abstract class AccountRecover {
* from the account will be recognized as the lost device key:
* 1. Only signer that's not in [serverAuth]
* 2. All signers in [serverAuth] have the same weight, and potential signer is
* the only one with a different weight.
* the only one with a different weight.
*
* @param stellarAccount stellar account to lookup existing signers on account
* @param serverAuth sap of recovery servers to use
Expand Down
10 changes: 5 additions & 5 deletions src/walletSdk/Recovery/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,20 @@ export class Recovery extends AccountRecover {

if (sponsorAddress) {
if (accountInfo) {
const buildingFunction = (builder: SponsoringBuilder) =>
const buildingFunction = (_builder: SponsoringBuilder) =>
this.register(
builder,
_builder,
accountSigners,
accountThreshold,
builderExtra,
) as SponsoringBuilder;

builder.sponsoring(sponsorAddress, buildingFunction);
} else {
const buildingFunction = (builder: SponsoringBuilder) => {
builder.createAccount(account);
const buildingFunction = (_builder: SponsoringBuilder) => {
_builder.createAccount(account);
return this.register(
builder,
_builder,
accountSigners,
accountThreshold,
builderExtra,
Expand Down
1 change: 1 addition & 0 deletions src/walletSdk/Types/anchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface WithdrawTransaction extends ProcessingAnchorTransaction {
withdraw_anchor_account: string;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface ErrorTransaction
extends Optional<DepositTransaction & WithdrawTransaction> {}

Expand Down
2 changes: 1 addition & 1 deletion src/walletSdk/Types/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Keypair, Transaction } from "stellar-sdk";
import { Transaction } from "stellar-sdk";
import { WalletSigner } from "../Auth/WalletSigner";
import { AccountKeypair } from "../Horizon/Account";

Expand Down
1 change: 1 addition & 0 deletions src/walletSdk/Utils/camelToSnakeCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const camelToSnakeCaseKey = (key: string): string =>
(upperCaseLetter) => `_${upperCaseLetter.toLowerCase()}`,
);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const camelToSnakeCaseObject = (obj: any): any => {
const snakeCasedObj = {};
for (const key in obj) {
Expand Down
Loading

0 comments on commit 5072808

Please sign in to comment.