Skip to content

Commit 1dcd18d

Browse files
authored
Merge pull request #701 from bvotteler/chore-add-cjs-build
Refactor: Add separate ESM, CJS build
2 parents 4e85d3e + 87be8bc commit 1dcd18d

12 files changed

Lines changed: 170 additions & 104 deletions

File tree

.npmignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.cache
2+
.idea
3+
.chglog
4+
.github
5+
.husky
6+
.DS_Store
7+
*.log
8+
.nyc_output
9+
.vscode
10+
.yarn
11+
.eslintignore
12+
.gitignore
13+
.editorconfig
14+
.env.staging
15+
.eslintrc.json
16+
.nvmrc
17+
.prettierignore
18+
.prettierrc
19+
.yarnrc.yml
20+
node_modules
21+
local-setup
22+
test
23+
coverage
24+
src
25+
yarn.lock
26+
*.sh
27+
generate_docs
28+
typedoc.json

create-build-package-json.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
cat >build/cjs/package.json <<!EOF
3+
{
4+
"type": "commonjs"
5+
}
6+
!EOF
7+
8+
cat >build/esm/package.json <<!EOF
9+
{
10+
"type": "module"
11+
}
12+
!EOF

package.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "@interlay/interbtc-api",
3-
"version": "2.5.2",
3+
"version": "2.6.0-rc.0",
44
"description": "JavaScript library to interact with interBTC",
5-
"main": "build/src/index.js",
6-
"type": "module",
7-
"typings": "build/src/index.d.ts",
5+
"main": "build/cjs/src/index.js",
6+
"module": "build/esm/src/index.js",
7+
"types": "build/types/src/index.d.ts",
88
"repository": "https://github.com/interlay/interbtc-api",
99
"license": "Apache-2.0",
1010
"keywords": [
@@ -17,8 +17,12 @@
1717
"kBTC"
1818
],
1919
"scripts": {
20-
"build": "run-s generate:defs generate:meta build:tsc",
21-
"build:tsc": "tsc -p tsconfig.json",
20+
"build": "run-s generate:defs generate:meta build:clean build:types build:esm build:cjs build:create-package-json",
21+
"build:clean": "rm -fr build/*",
22+
"build:esm": "tsc -p tsconfig.json",
23+
"build:cjs": "tsc -p tsconfig-cjs.json",
24+
"build:types": "tsc -p tsconfig-types.json",
25+
"build:create-package-json": "sh ./create-build-package-json.sh",
2226
"fix": "run-s fix:*",
2327
"fix:prettier": "prettier \"src/**/*.ts\" --write",
2428
"fix:lint": "eslint --fix src --ext .ts",
@@ -92,10 +96,7 @@
9296
"bn.js": "4.12.0"
9397
},
9498
"files": [
95-
"build/main",
96-
"build/module",
97-
"!**/*.spec.*",
98-
"!**/*.json",
99+
"build",
99100
"CHANGELOG.md",
100101
"LICENSE",
101102
"README.md"

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import "./interfaces/augment-api";
2-
import "./interfaces/augment-types";
1+
import "./interfaces/augment-api.js";
2+
import "./interfaces/augment-types.js";
33

44
export * from "./factory";
55
export * from "./interbtc-api";

src/parachain/index.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
export { VaultsAPI, DefaultVaultsAPI } from "./vaults";
2-
export { IssueAPI, DefaultIssueAPI } from "./issue";
3-
export { RedeemAPI, DefaultRedeemAPI } from "./redeem";
4-
export { OracleAPI, DefaultOracleAPI } from "./oracle";
5-
export { BTCRelayAPI, DefaultBTCRelayAPI } from "./btc-relay";
6-
export { TokensAPI, DefaultTokensAPI } from "./tokens";
7-
export { SystemAPI, DefaultSystemAPI } from "./system";
8-
export { ConstantsAPI, DefaultConstantsAPI } from "./constants";
9-
export { ReplaceAPI, DefaultReplaceAPI } from "./replace";
10-
export { FeeAPI, DefaultFeeAPI } from "./fee";
11-
export { RewardsAPI, DefaultRewardsAPI } from "./rewards";
12-
export { NominationAPI, DefaultNominationAPI } from "./nomination";
13-
export { EscrowAPI, DefaultEscrowAPI } from "./escrow";
14-
export { AssetRegistryAPI, DefaultAssetRegistryAPI } from "./asset-registry";
15-
export { LoansAPI, DefaultLoansAPI } from "./loans";
16-
export { AMMAPI, DefaultAMMAPI } from "./amm";
1+
export * from "./vaults";
2+
export * from "./issue";
3+
export * from "./redeem";
4+
export * from "./oracle";
5+
export * from "./btc-relay";
6+
export * from "./tokens";
7+
export * from "./system";
8+
export * from "./constants";
9+
export * from "./replace";
10+
export * from "./fee";
11+
export * from "./rewards";
12+
export * from "./nomination";
13+
export * from "./escrow";
14+
export * from "./asset-registry";
15+
export * from "./loans";
16+
export * from "./amm";
1717
export * from "./transaction";
1818
export * from "./amm/";
1919

src/utils/issueRedeem.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import { Hash, EventRecord } from "@polkadot/types/interfaces";
2+
import { ApiTypes, AugmentedEvent } from "@polkadot/api/types";
3+
import { ISubmittableResult } from "@polkadot/types/types";
4+
import type { AnyTuple } from "@polkadot/types/types";
5+
import { ApiPromise } from "@polkadot/api";
16
import { MonetaryAmount } from "@interlay/monetary-js";
27
import { InterbtcPrimitivesVaultId } from "@polkadot/types/lookup";
38

4-
import { WrappedCurrency } from "../types";
9+
import { Issue, Redeem, WrappedCurrency } from "../types";
510
import { newMonetaryAmount } from "./currency";
11+
import { InterBtcApi } from "../interbtc-api";
612

713
/**
814
* Given a list of vaults with availabilities (e.g. collateral for issue, tokens
@@ -51,3 +57,45 @@ export function allocateAmountsToVaults(
5157
}
5258
return allocations;
5359
}
60+
61+
/**
62+
* @param events The EventRecord array returned after sending a transaction
63+
* @param methodToCheck The name of the event method whose existence to check
64+
* @returns The id associated with the transaction. If the EventRecord array does not
65+
* contain required events, the function throws an error.
66+
*/
67+
export function getRequestIdsFromEvents(
68+
events: EventRecord[],
69+
eventToFind: AugmentedEvent<ApiTypes, AnyTuple>,
70+
api: ApiPromise
71+
): Hash[] {
72+
const ids = new Array<Hash>();
73+
for (const { event } of events) {
74+
if (eventToFind.is(event)) {
75+
// the redeem id has type H256 and is the first item of the event data array
76+
const id = api.createType("Hash", event.data[0]);
77+
ids.push(id);
78+
}
79+
}
80+
81+
if (ids.length > 0) return ids;
82+
throw new Error("Transaction failed");
83+
}
84+
85+
export const getIssueRequestsFromExtrinsicResult = async (
86+
interBtcApi: InterBtcApi,
87+
result: ISubmittableResult
88+
): Promise<Array<Issue>> => {
89+
const ids = getRequestIdsFromEvents(result.events, interBtcApi.api.events.issue.RequestIssue, interBtcApi.api);
90+
const issueRequests = await interBtcApi.issue.getRequestsByIds(ids);
91+
return issueRequests;
92+
};
93+
94+
export const getRedeemRequestsFromExtrinsicResult = async (
95+
interBtcApi: InterBtcApi,
96+
result: ISubmittableResult
97+
): Promise<Array<Redeem>> => {
98+
const ids = getRequestIdsFromEvents(result.events, interBtcApi.api.events.redeem.RequestRedeem, interBtcApi.api);
99+
const redeemRequests = await interBtcApi.redeem.getRequestsByIds(ids);
100+
return redeemRequests;
101+
};

test/integration/parachain/staging/sequential/issue.partial.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
CollateralCurrencyExt,
66
currencyIdToMonetaryCurrency,
77
DefaultInterBtcApi,
8+
getIssueRequestsFromExtrinsicResult,
89
InterBtcApi,
910
InterbtcPrimitivesVaultId,
1011
IssueStatus,
@@ -25,7 +26,6 @@ import {
2526
PARACHAIN_ENDPOINT,
2627
ESPLORA_BASE_PATH,
2728
} from "../../../../config";
28-
import { getIssueRequestsFromExtrinsicResult } from "../../../../utils/issue-redeem";
2929
import { BitcoinCoreClient } from "../../../../utils/bitcoin-core-client";
3030
import { issueSingle } from "../../../../utils/issue-redeem";
3131
import { newVaultId, WrappedCurrency } from "../../../../../src";

test/utils/issue-redeem.ts

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import { Hash, EventRecord } from "@polkadot/types/interfaces";
2-
import { ApiTypes, AugmentedEvent } from "@polkadot/api/types";
3-
import type { AnyTuple } from "@polkadot/types/types";
4-
import { ApiPromise } from "@polkadot/api";
51
import { KeyringPair } from "@polkadot/keyring/types";
62
import { Bitcoin, BitcoinAmount, InterBtcAmount, MonetaryAmount } from "@interlay/monetary-js";
7-
import { InterbtcPrimitivesVaultId } from "@polkadot/types/lookup";
8-
import { ISubmittableResult } from "@polkadot/types/types";
3+
import { InterbtcPrimitivesVaultId } from "../../src/parachain";
94

10-
import { newAccountId } from "../../src/utils";
5+
6+
import { getIssueRequestsFromExtrinsicResult, getRedeemRequestsFromExtrinsicResult, newAccountId } from "../../src/utils";
117
import { BitcoinCoreClient } from "./bitcoin-core-client";
128
import { stripHexPrefix } from "../../src/utils/encoding";
139
import { Issue, IssueStatus, Redeem, RedeemStatus, WrappedCurrency } from "../../src/types";
@@ -28,48 +24,6 @@ export enum ExecuteRedeem {
2824
Auto,
2925
}
3026

31-
/**
32-
* @param events The EventRecord array returned after sending a transaction
33-
* @param methodToCheck The name of the event method whose existence to check
34-
* @returns The id associated with the transaction. If the EventRecord array does not
35-
* contain required events, the function throws an error.
36-
*/
37-
export function getRequestIdsFromEvents(
38-
events: EventRecord[],
39-
eventToFind: AugmentedEvent<ApiTypes, AnyTuple>,
40-
api: ApiPromise
41-
): Hash[] {
42-
const ids = new Array<Hash>();
43-
for (const { event } of events) {
44-
if (eventToFind.is(event)) {
45-
// the redeem id has type H256 and is the first item of the event data array
46-
const id = api.createType("Hash", event.data[0]);
47-
ids.push(id);
48-
}
49-
}
50-
51-
if (ids.length > 0) return ids;
52-
throw new Error("Transaction failed");
53-
}
54-
55-
export const getIssueRequestsFromExtrinsicResult = async (
56-
interBtcApi: InterBtcApi,
57-
result: ISubmittableResult
58-
): Promise<Array<Issue>> => {
59-
const ids = getRequestIdsFromEvents(result.events, interBtcApi.api.events.issue.RequestIssue, interBtcApi.api);
60-
const issueRequests = await interBtcApi.issue.getRequestsByIds(ids);
61-
return issueRequests;
62-
};
63-
64-
export const getRedeemRequestsFromExtrinsicResult = async (
65-
interBtcApi: InterBtcApi,
66-
result: ISubmittableResult
67-
): Promise<Array<Redeem>> => {
68-
const ids = getRequestIdsFromEvents(result.events, interBtcApi.api.events.redeem.RequestRedeem, interBtcApi.api);
69-
const redeemRequests = await interBtcApi.redeem.getRequestsByIds(ids);
70-
return redeemRequests;
71-
};
72-
7327
export async function issueSingle(
7428
interBtcApi: InterBtcApi,
7529
bitcoinCoreClient: BitcoinCoreClient,

tsconfig-base.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"ts-node": {
3+
"experimentalSpecifierResolution": "node",
4+
"moduleTypes": {
5+
"src/interfaces/**/*": "esm"
6+
}
7+
},
8+
"compilerOptions": {
9+
"declaration": true,
10+
"skipLibCheck": true,
11+
"strict": true,
12+
"noImplicitAny": true,
13+
"esModuleInterop": true,
14+
"baseUrl": "./",
15+
"resolveJsonModule": true,
16+
"sourceMap": true,
17+
"moduleResolution": "node",
18+
"lib": ["ES2019"],
19+
"paths": {
20+
"@polkadot/api/augment": ["src/interfaces/augment-api.ts"],
21+
"@polkadot/types/augment": ["src/interfaces/augment-types.ts"],
22+
"@polkadot/types/lookup": ["src/interfaces/types-lookup.ts"],
23+
"bitcoinjs-lib/src/bufferutils": ["node_modules/bitcoinjs-lib/types/bufferutils"],
24+
"@interlay/interbtc-api/*": ["src/*"]
25+
}
26+
},
27+
"include": ["src/**/*", "scripts/**/*.ts"],
28+
"exclude": ["./node_modules/*"]
29+
}

tsconfig-cjs.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "./tsconfig-base.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"outDir": "build/cjs",
6+
"target": "es2015"
7+
}
8+
}

0 commit comments

Comments
 (0)