Skip to content

Commit

Permalink
Merge pull request #701 from bvotteler/chore-add-cjs-build
Browse files Browse the repository at this point in the history
Refactor: Add separate ESM, CJS build
  • Loading branch information
bvotteler authored Oct 26, 2023
2 parents 4e85d3e + 87be8bc commit 1dcd18d
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 104 deletions.
28 changes: 28 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.cache
.idea
.chglog
.github
.husky
.DS_Store
*.log
.nyc_output
.vscode
.yarn
.eslintignore
.gitignore
.editorconfig
.env.staging
.eslintrc.json
.nvmrc
.prettierignore
.prettierrc
.yarnrc.yml
node_modules
local-setup
test
coverage
src
yarn.lock
*.sh
generate_docs
typedoc.json
12 changes: 12 additions & 0 deletions create-build-package-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
cat >build/cjs/package.json <<!EOF
{
"type": "commonjs"
}
!EOF

cat >build/esm/package.json <<!EOF
{
"type": "module"
}
!EOF
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@interlay/interbtc-api",
"version": "2.5.2",
"version": "2.6.0-rc.0",
"description": "JavaScript library to interact with interBTC",
"main": "build/src/index.js",
"type": "module",
"typings": "build/src/index.d.ts",
"main": "build/cjs/src/index.js",
"module": "build/esm/src/index.js",
"types": "build/types/src/index.d.ts",
"repository": "https://github.com/interlay/interbtc-api",
"license": "Apache-2.0",
"keywords": [
Expand All @@ -17,8 +17,12 @@
"kBTC"
],
"scripts": {
"build": "run-s generate:defs generate:meta build:tsc",
"build:tsc": "tsc -p tsconfig.json",
"build": "run-s generate:defs generate:meta build:clean build:types build:esm build:cjs build:create-package-json",
"build:clean": "rm -fr build/*",
"build:esm": "tsc -p tsconfig.json",
"build:cjs": "tsc -p tsconfig-cjs.json",
"build:types": "tsc -p tsconfig-types.json",
"build:create-package-json": "sh ./create-build-package-json.sh",
"fix": "run-s fix:*",
"fix:prettier": "prettier \"src/**/*.ts\" --write",
"fix:lint": "eslint --fix src --ext .ts",
Expand Down Expand Up @@ -92,10 +96,7 @@
"bn.js": "4.12.0"
},
"files": [
"build/main",
"build/module",
"!**/*.spec.*",
"!**/*.json",
"build",
"CHANGELOG.md",
"LICENSE",
"README.md"
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "./interfaces/augment-api";
import "./interfaces/augment-types";
import "./interfaces/augment-api.js";
import "./interfaces/augment-types.js";

export * from "./factory";
export * from "./interbtc-api";
Expand Down
32 changes: 16 additions & 16 deletions src/parachain/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
export { VaultsAPI, DefaultVaultsAPI } from "./vaults";
export { IssueAPI, DefaultIssueAPI } from "./issue";
export { RedeemAPI, DefaultRedeemAPI } from "./redeem";
export { OracleAPI, DefaultOracleAPI } from "./oracle";
export { BTCRelayAPI, DefaultBTCRelayAPI } from "./btc-relay";
export { TokensAPI, DefaultTokensAPI } from "./tokens";
export { SystemAPI, DefaultSystemAPI } from "./system";
export { ConstantsAPI, DefaultConstantsAPI } from "./constants";
export { ReplaceAPI, DefaultReplaceAPI } from "./replace";
export { FeeAPI, DefaultFeeAPI } from "./fee";
export { RewardsAPI, DefaultRewardsAPI } from "./rewards";
export { NominationAPI, DefaultNominationAPI } from "./nomination";
export { EscrowAPI, DefaultEscrowAPI } from "./escrow";
export { AssetRegistryAPI, DefaultAssetRegistryAPI } from "./asset-registry";
export { LoansAPI, DefaultLoansAPI } from "./loans";
export { AMMAPI, DefaultAMMAPI } from "./amm";
export * from "./vaults";
export * from "./issue";
export * from "./redeem";
export * from "./oracle";
export * from "./btc-relay";
export * from "./tokens";
export * from "./system";
export * from "./constants";
export * from "./replace";
export * from "./fee";
export * from "./rewards";
export * from "./nomination";
export * from "./escrow";
export * from "./asset-registry";
export * from "./loans";
export * from "./amm";
export * from "./transaction";
export * from "./amm/";

Expand Down
50 changes: 49 additions & 1 deletion src/utils/issueRedeem.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Hash, EventRecord } from "@polkadot/types/interfaces";
import { ApiTypes, AugmentedEvent } from "@polkadot/api/types";
import { ISubmittableResult } from "@polkadot/types/types";
import type { AnyTuple } from "@polkadot/types/types";
import { ApiPromise } from "@polkadot/api";
import { MonetaryAmount } from "@interlay/monetary-js";
import { InterbtcPrimitivesVaultId } from "@polkadot/types/lookup";

import { WrappedCurrency } from "../types";
import { Issue, Redeem, WrappedCurrency } from "../types";
import { newMonetaryAmount } from "./currency";
import { InterBtcApi } from "../interbtc-api";

/**
* Given a list of vaults with availabilities (e.g. collateral for issue, tokens
Expand Down Expand Up @@ -51,3 +57,45 @@ export function allocateAmountsToVaults(
}
return allocations;
}

/**
* @param events The EventRecord array returned after sending a transaction
* @param methodToCheck The name of the event method whose existence to check
* @returns The id associated with the transaction. If the EventRecord array does not
* contain required events, the function throws an error.
*/
export function getRequestIdsFromEvents(
events: EventRecord[],
eventToFind: AugmentedEvent<ApiTypes, AnyTuple>,
api: ApiPromise
): Hash[] {
const ids = new Array<Hash>();
for (const { event } of events) {
if (eventToFind.is(event)) {
// the redeem id has type H256 and is the first item of the event data array
const id = api.createType("Hash", event.data[0]);
ids.push(id);
}
}

if (ids.length > 0) return ids;
throw new Error("Transaction failed");
}

export const getIssueRequestsFromExtrinsicResult = async (
interBtcApi: InterBtcApi,
result: ISubmittableResult
): Promise<Array<Issue>> => {
const ids = getRequestIdsFromEvents(result.events, interBtcApi.api.events.issue.RequestIssue, interBtcApi.api);
const issueRequests = await interBtcApi.issue.getRequestsByIds(ids);
return issueRequests;
};

export const getRedeemRequestsFromExtrinsicResult = async (
interBtcApi: InterBtcApi,
result: ISubmittableResult
): Promise<Array<Redeem>> => {
const ids = getRequestIdsFromEvents(result.events, interBtcApi.api.events.redeem.RequestRedeem, interBtcApi.api);
const redeemRequests = await interBtcApi.redeem.getRequestsByIds(ids);
return redeemRequests;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CollateralCurrencyExt,
currencyIdToMonetaryCurrency,
DefaultInterBtcApi,
getIssueRequestsFromExtrinsicResult,
InterBtcApi,
InterbtcPrimitivesVaultId,
IssueStatus,
Expand All @@ -25,7 +26,6 @@ import {
PARACHAIN_ENDPOINT,
ESPLORA_BASE_PATH,
} from "../../../../config";
import { getIssueRequestsFromExtrinsicResult } from "../../../../utils/issue-redeem";
import { BitcoinCoreClient } from "../../../../utils/bitcoin-core-client";
import { issueSingle } from "../../../../utils/issue-redeem";
import { newVaultId, WrappedCurrency } from "../../../../../src";
Expand Down
52 changes: 3 additions & 49 deletions test/utils/issue-redeem.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { Hash, EventRecord } from "@polkadot/types/interfaces";
import { ApiTypes, AugmentedEvent } from "@polkadot/api/types";
import type { AnyTuple } from "@polkadot/types/types";
import { ApiPromise } from "@polkadot/api";
import { KeyringPair } from "@polkadot/keyring/types";
import { Bitcoin, BitcoinAmount, InterBtcAmount, MonetaryAmount } from "@interlay/monetary-js";
import { InterbtcPrimitivesVaultId } from "@polkadot/types/lookup";
import { ISubmittableResult } from "@polkadot/types/types";
import { InterbtcPrimitivesVaultId } from "../../src/parachain";

import { newAccountId } from "../../src/utils";

import { getIssueRequestsFromExtrinsicResult, getRedeemRequestsFromExtrinsicResult, newAccountId } from "../../src/utils";
import { BitcoinCoreClient } from "./bitcoin-core-client";
import { stripHexPrefix } from "../../src/utils/encoding";
import { Issue, IssueStatus, Redeem, RedeemStatus, WrappedCurrency } from "../../src/types";
Expand All @@ -28,48 +24,6 @@ export enum ExecuteRedeem {
Auto,
}

/**
* @param events The EventRecord array returned after sending a transaction
* @param methodToCheck The name of the event method whose existence to check
* @returns The id associated with the transaction. If the EventRecord array does not
* contain required events, the function throws an error.
*/
export function getRequestIdsFromEvents(
events: EventRecord[],
eventToFind: AugmentedEvent<ApiTypes, AnyTuple>,
api: ApiPromise
): Hash[] {
const ids = new Array<Hash>();
for (const { event } of events) {
if (eventToFind.is(event)) {
// the redeem id has type H256 and is the first item of the event data array
const id = api.createType("Hash", event.data[0]);
ids.push(id);
}
}

if (ids.length > 0) return ids;
throw new Error("Transaction failed");
}

export const getIssueRequestsFromExtrinsicResult = async (
interBtcApi: InterBtcApi,
result: ISubmittableResult
): Promise<Array<Issue>> => {
const ids = getRequestIdsFromEvents(result.events, interBtcApi.api.events.issue.RequestIssue, interBtcApi.api);
const issueRequests = await interBtcApi.issue.getRequestsByIds(ids);
return issueRequests;
};

export const getRedeemRequestsFromExtrinsicResult = async (
interBtcApi: InterBtcApi,
result: ISubmittableResult
): Promise<Array<Redeem>> => {
const ids = getRequestIdsFromEvents(result.events, interBtcApi.api.events.redeem.RequestRedeem, interBtcApi.api);
const redeemRequests = await interBtcApi.redeem.getRequestsByIds(ids);
return redeemRequests;
};

export async function issueSingle(
interBtcApi: InterBtcApi,
bitcoinCoreClient: BitcoinCoreClient,
Expand Down
29 changes: 29 additions & 0 deletions tsconfig-base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"ts-node": {
"experimentalSpecifierResolution": "node",
"moduleTypes": {
"src/interfaces/**/*": "esm"
}
},
"compilerOptions": {
"declaration": true,
"skipLibCheck": true,
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"baseUrl": "./",
"resolveJsonModule": true,
"sourceMap": true,
"moduleResolution": "node",
"lib": ["ES2019"],
"paths": {
"@polkadot/api/augment": ["src/interfaces/augment-api.ts"],
"@polkadot/types/augment": ["src/interfaces/augment-types.ts"],
"@polkadot/types/lookup": ["src/interfaces/types-lookup.ts"],
"bitcoinjs-lib/src/bufferutils": ["node_modules/bitcoinjs-lib/types/bufferutils"],
"@interlay/interbtc-api/*": ["src/*"]
}
},
"include": ["src/**/*", "scripts/**/*.ts"],
"exclude": ["./node_modules/*"]
}
8 changes: 8 additions & 0 deletions tsconfig-cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig-base.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "build/cjs",
"target": "es2015"
}
}
8 changes: 8 additions & 0 deletions tsconfig-types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "build/types",
"declaration": true,
"emitDeclarationOnly": true
}
}
28 changes: 3 additions & 25 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
{
"ts-node": {
"experimentalSpecifierResolution": "node"
},
"extends": "./tsconfig-base.json",
"compilerOptions": {
"skipLibCheck": true,
"target": "ES6",
"module": "ES6",
"outDir": "build",
"declaration": true,
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"baseUrl": "./",
"resolveJsonModule": true,
// Generate files for debugging
"sourceMap": true,
"moduleResolution": "node",
"lib": ["ES2019"],
"paths": {
"@polkadot/api/augment": ["src/interfaces/augment-api.ts"],
"@polkadot/types/augment": ["src/interfaces/augment-types.ts"],
"@polkadot/types/lookup": ["src/interfaces/types-lookup.ts"],
"bitcoinjs-lib/src/bufferutils": ["node_modules/bitcoinjs-lib/types/bufferutils"],
"@interlay/interbtc-api/*": ["src/*"]
}
},
"include": ["src/**/*", "scripts/**/*.ts"],
"exclude": ["./node_modules/*"]
"outDir": "build/esm",
}
}

0 comments on commit 1dcd18d

Please sign in to comment.