Skip to content

Commit

Permalink
Universal Bundle (#66)
Browse files Browse the repository at this point in the history
Some projects have been struggling to import from this package because of some node target stuff I do not understand.

We now support both ESModules and CommonJS.
Additionally suggest users run the example scripts with tsx.
WINNING!
  • Loading branch information
bh2smith authored Jul 1, 2024
1 parent 52045b7 commit d837525
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 123 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ yarn
cp .env.example .env <---- paste your Near credentials
# Send Eth. You'll need to fund your account first.
# More details in the 'Fund your account' part of this document
npx ts-node examples/send-eth.ts
npx tsx examples/send-eth.ts
```

### NEAR Credentials
Expand All @@ -50,7 +50,7 @@ For example, you can use the [Mintbase Wallet](https://testnet.wallet.mintbase.x
Get your address

```sh
npx ts-node examples/getEthAddress.ts
npx tsx examples/getEthAddress.ts
```

After getting your address fund it from one of your own wallets.
Expand Down Expand Up @@ -108,7 +108,7 @@ await adapter.signAndSendTransaction({
Each of the following scripts can be run with

```bash
npx ts-node examples/*.ts
npx tsx examples/*.ts
```

1. [(Basic) Send ETH](./examples/send-eth.ts)
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
You can run any of the following example scripts using the command:

```bash
npx ts-node examples/*.ts
npx tsx examples/*.ts
```

Here are some of the available examples:
Expand All @@ -30,7 +30,7 @@ yarn
cp .env.example .env # Paste your NEAR credentials into the .env file

# Run the OpenSea example script
npx ts-node examples/opensea.ts
npx tsx examples/opensea.ts
```

You will be prompted to provide a `collectionSlug`.
Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"name": "near-ca",
"module": "index.ts",
"version": "0.0.0-alpha.1",
"version": "0.0.0",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
"types": "dist/esm/index.d.ts",
"files": [
"dist/**/*"
],
"scripts": {
"build": "rm -rf ./dist && tsc",
"build": "rm -fr dist/* && yarn build:esm && yarn build:cjs",
"build:esm": "tsc -p tsconfig.esm.json",
"build:cjs": "tsc -p tsconfig.cjs.json",
"lint": "eslint . --ignore-pattern dist/",
"test": "jest --testTimeout 30000",
"coverage": "yarn test --coverage",
Expand All @@ -32,7 +34,7 @@
"opensea-js": "^7.1.9",
"prettier": "^3.3.2",
"ts-jest": "^29.1.5",
"ts-node": "^10.9.2",
"tsx": "^4.16.0",
"typescript": "^5.5.2"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/chains/near.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const nearAccountFromEnv = async (
return nearAccountFromKeyPair({
keyPair,
accountId: process.env.NEAR_ACCOUNT_ID!,
network,
network: network || TESTNET_CONFIG,
});
};

Expand Down
6 changes: 3 additions & 3 deletions src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Network implements NetworkFields {
client: PublicClient;

constructor({ name, rpcUrl, chainId, scanUrl }: NetworkFields) {
const network = SUPPORTED_NETWORKS[chainId];
const network = SUPPORTED_NETWORKS[chainId]!;

this.name = name;
this.rpcUrl = rpcUrl;
Expand All @@ -53,7 +53,7 @@ export class Network implements NetworkFields {

/// Returns Network by ChainId
static fromChainId(chainId: number): Network {
const networkFields = SUPPORTED_NETWORKS[chainId];
const networkFields = SUPPORTED_NETWORKS[chainId]!;
return new Network(networkFields);
}
}
Expand All @@ -66,7 +66,7 @@ function createNetworkMap(supportedNetworks: Chain[]): NetworkMap {
supportedNetworks.forEach((network) => {
networkMap[network.id] = {
name: network.name,
rpcUrl: network.rpcUrls.default.http[0],
rpcUrl: network.rpcUrls.default.http[0]!,
chainId: network.id,
scanUrl: network.blockExplorers?.default.url || "",
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/kdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Address, keccak256 } from "viem";
export function najPublicKeyStrToUncompressedHexPoint(
najPublicKeyStr: string
): string {
const decodedKey = base_decode(najPublicKeyStr.split(":")[1]);
const decodedKey = base_decode(najPublicKeyStr.split(":")[1]!);
return "04" + Buffer.from(decodedKey).toString("hex");
}

Expand Down
4 changes: 2 additions & 2 deletions src/wallet-connect/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ export async function wcRouter(
to: tx.to,
chainId: parseInt(stripEip155Prefix(chainId)),
value: fromHex(tx.value || "0x0", "bigint"),
data: tx.data,
gas: tx.gas ? fromHex(tx.gas, "bigint") : undefined,
data: tx.data || "0x",
...(tx.gas ? { gas: fromHex(tx.gas, "bigint") } : {}),
},
tx.from
);
Expand Down
7 changes: 7 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist/cjs",
}
}
8 changes: 8 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"outDir": "dist/esm",
}
}
35 changes: 19 additions & 16 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext"],
"target": "ESNext",
"jsx": "react-jsx",
"allowJs": true,
"target": "ES2020",
"moduleResolution": "Node",
"resolveJsonModule": true,

"declaration": true,
"outDir": "dist",
"esModuleInterop": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": true,
"noUnusedParameters": true,
"noPropertyAccessFromIndexSignature": false
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
"strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
"strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
"strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
"strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
"noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
"useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
"alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
"noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
"noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
"exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
"noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
"noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
"noUncheckedIndexedAccess": true,
"noPropertyAccessFromIndexSignature": false,
"skipLibCheck": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "dist", "examples", "tests", "eslint.config.js"]
}
Loading

0 comments on commit d837525

Please sign in to comment.