@polymarbot/shared 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/crypto.cjs CHANGED
@@ -38,7 +38,11 @@ var import_node_crypto = __toESM(require("crypto"), 1);
38
38
  var ALGORITHM = "aes-256-gcm";
39
39
  var IV_LENGTH = 12;
40
40
  var AUTH_TAG_LENGTH = 16;
41
- var ENCRYPTION_KEY = (() => {
41
+ var cachedKey = null;
42
+ function getEncryptionKey() {
43
+ if (cachedKey) {
44
+ return cachedKey;
45
+ }
42
46
  const key = process.env.ENCRYPTION_KEY;
43
47
  if (!key) {
44
48
  throw new Error("ENCRYPTION_KEY environment variable is required");
@@ -47,11 +51,12 @@ var ENCRYPTION_KEY = (() => {
47
51
  if (keyBuffer.length !== 32) {
48
52
  throw new Error("ENCRYPTION_KEY must be a 32-byte hex string (64 characters)");
49
53
  }
50
- return keyBuffer;
51
- })();
54
+ cachedKey = keyBuffer;
55
+ return cachedKey;
56
+ }
52
57
  function encrypt(plaintext) {
53
58
  const iv = import_node_crypto.default.randomBytes(IV_LENGTH);
54
- const cipher = import_node_crypto.default.createCipheriv(ALGORITHM, ENCRYPTION_KEY, iv, {
59
+ const cipher = import_node_crypto.default.createCipheriv(ALGORITHM, getEncryptionKey(), iv, {
55
60
  authTagLength: AUTH_TAG_LENGTH
56
61
  });
57
62
  const encrypted = Buffer.concat([
@@ -75,7 +80,7 @@ function decrypt(encryptedData) {
75
80
  if (authTag.length !== AUTH_TAG_LENGTH) {
76
81
  throw new Error("Invalid auth tag length");
77
82
  }
78
- const decipher = import_node_crypto.default.createDecipheriv(ALGORITHM, ENCRYPTION_KEY, iv, {
83
+ const decipher = import_node_crypto.default.createDecipheriv(ALGORITHM, getEncryptionKey(), iv, {
79
84
  authTagLength: AUTH_TAG_LENGTH
80
85
  });
81
86
  decipher.setAuthTag(authTag);
package/crypto.js CHANGED
@@ -3,7 +3,11 @@ import crypto from "crypto";
3
3
  var ALGORITHM = "aes-256-gcm";
4
4
  var IV_LENGTH = 12;
5
5
  var AUTH_TAG_LENGTH = 16;
6
- var ENCRYPTION_KEY = (() => {
6
+ var cachedKey = null;
7
+ function getEncryptionKey() {
8
+ if (cachedKey) {
9
+ return cachedKey;
10
+ }
7
11
  const key = process.env.ENCRYPTION_KEY;
8
12
  if (!key) {
9
13
  throw new Error("ENCRYPTION_KEY environment variable is required");
@@ -12,11 +16,12 @@ var ENCRYPTION_KEY = (() => {
12
16
  if (keyBuffer.length !== 32) {
13
17
  throw new Error("ENCRYPTION_KEY must be a 32-byte hex string (64 characters)");
14
18
  }
15
- return keyBuffer;
16
- })();
19
+ cachedKey = keyBuffer;
20
+ return cachedKey;
21
+ }
17
22
  function encrypt(plaintext) {
18
23
  const iv = crypto.randomBytes(IV_LENGTH);
19
- const cipher = crypto.createCipheriv(ALGORITHM, ENCRYPTION_KEY, iv, {
24
+ const cipher = crypto.createCipheriv(ALGORITHM, getEncryptionKey(), iv, {
20
25
  authTagLength: AUTH_TAG_LENGTH
21
26
  });
22
27
  const encrypted = Buffer.concat([
@@ -40,7 +45,7 @@ function decrypt(encryptedData) {
40
45
  if (authTag.length !== AUTH_TAG_LENGTH) {
41
46
  throw new Error("Invalid auth tag length");
42
47
  }
43
- const decipher = crypto.createDecipheriv(ALGORITHM, ENCRYPTION_KEY, iv, {
48
+ const decipher = crypto.createDecipheriv(ALGORITHM, getEncryptionKey(), iv, {
44
49
  authTagLength: AUTH_TAG_LENGTH
45
50
  });
46
51
  decipher.setAuthTag(authTag);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polymarbot/shared",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
package/wallet.cjs CHANGED
@@ -26,6 +26,7 @@ __export(wallet_exports, {
26
26
  formatBalance: () => formatBalance,
27
27
  getPublicClient: () => getPublicClient,
28
28
  getUSDCBalance: () => getUSDCBalance,
29
+ getUSDCBalancesBatch: () => getUSDCBalancesBatch,
29
30
  isAddressEqual: () => isAddressEqual
30
31
  });
31
32
  module.exports = __toCommonJS(wallet_exports);
@@ -59,6 +60,24 @@ async function getUSDCBalance(address) {
59
60
  args: [address]
60
61
  });
61
62
  }
63
+ async function getUSDCBalancesBatch(addresses) {
64
+ if (addresses.length === 0) {
65
+ return [];
66
+ }
67
+ const publicClient = getPublicClient();
68
+ const multicallResults = await publicClient.multicall({
69
+ contracts: addresses.map((address) => ({
70
+ address: USDC_ADDRESS,
71
+ abi: USDC_ABI,
72
+ functionName: "balanceOf",
73
+ args: [address]
74
+ })),
75
+ allowFailure: true
76
+ });
77
+ return multicallResults.map(
78
+ (result) => result.status === "success" ? result.result : 0n
79
+ );
80
+ }
62
81
  function isAddressEqual(a, b) {
63
82
  if (!a) return false;
64
83
  if (!b) return false;
@@ -72,5 +91,6 @@ function isAddressEqual(a, b) {
72
91
  formatBalance,
73
92
  getPublicClient,
74
93
  getUSDCBalance,
94
+ getUSDCBalancesBatch,
75
95
  isAddressEqual
76
96
  });
package/wallet.d.cts CHANGED
@@ -20,6 +20,8 @@ declare function formatBalance(rawBalance: string | bigint, decimals?: number):
20
20
 
21
21
  declare function getUSDCBalance(address: string): Promise<bigint>;
22
22
 
23
+ declare function getUSDCBalancesBatch(addresses: string[]): Promise<bigint[]>;
24
+
23
25
  declare function isAddressEqual(a?: Address | string | null, b?: Address | string | null): boolean;
24
26
 
25
- export { USDC_ABI, USDC_ADDRESS, USDC_DECIMALS, formatBalance, getPublicClient, getUSDCBalance, isAddressEqual };
27
+ export { USDC_ABI, USDC_ADDRESS, USDC_DECIMALS, formatBalance, getPublicClient, getUSDCBalance, getUSDCBalancesBatch, isAddressEqual };
package/wallet.d.ts CHANGED
@@ -20,6 +20,8 @@ declare function formatBalance(rawBalance: string | bigint, decimals?: number):
20
20
 
21
21
  declare function getUSDCBalance(address: string): Promise<bigint>;
22
22
 
23
+ declare function getUSDCBalancesBatch(addresses: string[]): Promise<bigint[]>;
24
+
23
25
  declare function isAddressEqual(a?: Address | string | null, b?: Address | string | null): boolean;
24
26
 
25
- export { USDC_ABI, USDC_ADDRESS, USDC_DECIMALS, formatBalance, getPublicClient, getUSDCBalance, isAddressEqual };
27
+ export { USDC_ABI, USDC_ADDRESS, USDC_DECIMALS, formatBalance, getPublicClient, getUSDCBalance, getUSDCBalancesBatch, isAddressEqual };
package/wallet.js CHANGED
@@ -29,6 +29,24 @@ async function getUSDCBalance(address) {
29
29
  args: [address]
30
30
  });
31
31
  }
32
+ async function getUSDCBalancesBatch(addresses) {
33
+ if (addresses.length === 0) {
34
+ return [];
35
+ }
36
+ const publicClient = getPublicClient();
37
+ const multicallResults = await publicClient.multicall({
38
+ contracts: addresses.map((address) => ({
39
+ address: USDC_ADDRESS,
40
+ abi: USDC_ABI,
41
+ functionName: "balanceOf",
42
+ args: [address]
43
+ })),
44
+ allowFailure: true
45
+ });
46
+ return multicallResults.map(
47
+ (result) => result.status === "success" ? result.result : 0n
48
+ );
49
+ }
32
50
  function isAddressEqual(a, b) {
33
51
  if (!a) return false;
34
52
  if (!b) return false;
@@ -41,5 +59,6 @@ export {
41
59
  formatBalance,
42
60
  getPublicClient,
43
61
  getUSDCBalance,
62
+ getUSDCBalancesBatch,
44
63
  isAddressEqual
45
64
  };