@claritydao/midnight-agora-sdk 0.1.0 → 1.2.0

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.
Files changed (47) hide show
  1. package/README.md +58 -55
  2. package/dist/browser-providers.d.ts +24 -19
  3. package/dist/browser-providers.d.ts.map +1 -1
  4. package/dist/browser-providers.js +654 -136
  5. package/dist/browser-providers.js.map +1 -1
  6. package/dist/common-types.d.ts +13 -24
  7. package/dist/common-types.d.ts.map +1 -1
  8. package/dist/common-types.js +1 -1
  9. package/dist/common-types.js.map +1 -1
  10. package/dist/governor-api.d.ts +42 -17
  11. package/dist/governor-api.d.ts.map +1 -1
  12. package/dist/governor-api.js +233 -248
  13. package/dist/governor-api.js.map +1 -1
  14. package/dist/index.d.ts +11 -10
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +10 -9
  17. package/dist/index.js.map +1 -1
  18. package/dist/manager.d.ts +11 -9
  19. package/dist/manager.d.ts.map +1 -1
  20. package/dist/manager.js +17 -17
  21. package/dist/manager.js.map +1 -1
  22. package/dist/types.d.ts +9 -17
  23. package/dist/types.d.ts.map +1 -1
  24. package/dist/types.js.map +1 -1
  25. package/package.json +26 -26
  26. package/.prettierrc +0 -1
  27. package/LICENSE +0 -0
  28. package/eslint.config.mjs +0 -48
  29. package/src/browser-providers.ts +0 -274
  30. package/src/common-types.ts +0 -51
  31. package/src/errors.ts +0 -124
  32. package/src/governor-api.ts +0 -948
  33. package/src/index.ts +0 -51
  34. package/src/manager.ts +0 -203
  35. package/src/types.ts +0 -403
  36. package/src/utils/index.ts +0 -60
  37. package/src/validators.ts +0 -245
  38. package/test/README.md +0 -409
  39. package/test/errors.test.ts +0 -179
  40. package/test/integration/governor-api.test.ts +0 -370
  41. package/test/mocks/providers.ts +0 -130
  42. package/test/types.test.ts +0 -112
  43. package/test/utils.test.ts +0 -111
  44. package/test/validators.test.ts +0 -368
  45. package/test/wasm-init.test.ts +0 -132
  46. package/tsconfig.json +0 -18
  47. package/vitest.config.ts +0 -9
package/README.md CHANGED
@@ -18,7 +18,7 @@ The Agora DAO Midnight SDK provides a type-safe, professional-grade interface fo
18
18
  ## Installation
19
19
 
20
20
  ```bash
21
- pnpm add @agora-dao-midnight/sdk
21
+ pnpm add @claritydao/midnight-agora-sdk
22
22
  ```
23
23
 
24
24
  ## Quick Start
@@ -26,7 +26,7 @@ pnpm add @agora-dao-midnight/sdk
26
26
  ### 1. Deploy a New Governor Contract
27
27
 
28
28
  ```typescript
29
- import { GovernorAPI, initializeProviders, ConfigPresets } from '@agora-dao-midnight/sdk';
29
+ import { GovernorAPI, initializeProviders, ConfigPresets } from '@claritydao/midnight-agora-sdk';
30
30
  import { randomBytes } from '@midnight-ntwrk/midnight-js-utils';
31
31
  import pino from 'pino';
32
32
 
@@ -62,7 +62,7 @@ console.log('Contract deployed at:', api.deployedContractAddress);
62
62
  ### 2. Join an Existing Contract
63
63
 
64
64
  ```typescript
65
- import { GovernorAPI, initializeProviders } from '@agora-dao-midnight/sdk';
65
+ import { GovernorAPI, initializeProviders } from '@claritydao/midnight-agora-sdk';
66
66
  import pino from 'pino';
67
67
 
68
68
  const logger = pino({ level: 'info' });
@@ -76,27 +76,25 @@ const api = await GovernorAPI.join(providers, contractAddress, logger);
76
76
  ### 3. Create a Proposal
77
77
 
78
78
  ```typescript
79
- import { VoteChoice } from '@agora-dao-midnight/sdk';
80
- import type { ProposalAction } from '@agora-dao-midnight/sdk';
81
-
82
- // Define proposal actions (what happens if proposal passes)
83
- const actions: ProposalAction[] = [
84
- {
85
- typ: 0, // Transfer type
86
- token: tokenAddress,
87
- amount: BigInt(10000),
88
- to: recipientAddress,
89
- configKey: new Uint8Array(32),
90
- configValue: new Uint8Array(32),
91
- },
92
- ];
79
+ import { VoteChoice } from '@claritydao/midnight-agora-sdk';
80
+ import type { EffectAction } from '@claritydao/midnight-agora-sdk';
81
+
82
+ // Define the proposal effect (what happens if proposal passes)
83
+ const action: EffectAction = {
84
+ typ: 2, // Transfer type
85
+ token: tokenAddress,
86
+ amount: BigInt(10000),
87
+ to: recipientAddress,
88
+ configKey: new Uint8Array(32),
89
+ configValue: new Uint8Array(32),
90
+ };
93
91
 
94
92
  // Create proposal (requires sufficient stake)
95
93
  const { txData, proposalId } = await api.createProposal(
96
94
  'Fund Community Initiative',
97
95
  'Transfer 10,000 tokens to community wallet for Q1 initiatives',
98
96
  creatorAddress,
99
- actions
97
+ action
100
98
  );
101
99
 
102
100
  console.log('Proposal created with ID:', proposalId);
@@ -105,7 +103,7 @@ console.log('Proposal created with ID:', proposalId);
105
103
  ### 4. Cast a Vote
106
104
 
107
105
  ```typescript
108
- import { VoteChoice } from '@agora-dao-midnight/sdk';
106
+ import { VoteChoice } from '@claritydao/midnight-agora-sdk';
109
107
 
110
108
  // Vote on proposal (type-safe!)
111
109
  await api.castVote(
@@ -126,7 +124,7 @@ The SDK provides two configuration presets:
126
124
  For development and testnet:
127
125
 
128
126
  ```typescript
129
- import { ConfigPresets } from '@agora-dao-midnight/sdk';
127
+ import { ConfigPresets } from '@claritydao/midnight-agora-sdk';
130
128
 
131
129
  const governanceConfig = ConfigPresets.testing();
132
130
  // Returns:
@@ -147,7 +145,7 @@ const governanceConfig = ConfigPresets.testing();
147
145
  For mainnet:
148
146
 
149
147
  ```typescript
150
- import { ConfigPresets } from '@agora-dao-midnight/sdk';
148
+ import { ConfigPresets } from '@claritydao/midnight-agora-sdk';
151
149
 
152
150
  const governanceConfig = ConfigPresets.production();
153
151
  // Returns:
@@ -224,13 +222,13 @@ interface TokenConfig {
224
222
  }
225
223
  ```
226
224
 
227
- #### ProposalAction
225
+ #### EffectAction
228
226
 
229
- Actions to execute when proposal passes:
227
+ The single action to execute when a proposal passes:
230
228
 
231
229
  ```typescript
232
- interface ProposalAction {
233
- typ: number; // Action type (0 = transfer)
230
+ interface EffectAction {
231
+ typ: number; // Action type (1 = mint, 2 = transfer, 3 = config update)
234
232
  token: Uint8Array; // Token address (32 bytes)
235
233
  amount: bigint; // Amount to transfer
236
234
  to: Uint8Array; // Recipient address (32 bytes)
@@ -316,7 +314,7 @@ async createProposal(
316
314
  title: string,
317
315
  description: string,
318
316
  creator: Uint8Array,
319
- actions: ProposalAction[]
317
+ action: EffectAction
320
318
  ): Promise<{ txData: FinalizedTxData; proposalId: bigint }>
321
319
  ```
322
320
 
@@ -326,7 +324,7 @@ Creates a new governance proposal.
326
324
  - `title`: Proposal title (max 256 chars)
327
325
  - `description`: Proposal description (max 10,000 chars)
328
326
  - `creator`: Creator address (32 bytes, must have sufficient stake)
329
- - `actions`: Array of actions to execute if proposal passes
327
+ - `action`: Single effect action to execute if proposal passes
330
328
 
331
329
  **Returns:** Object with transaction data and proposal ID
332
330
 
@@ -540,14 +538,14 @@ import {
540
538
  DeploymentError,
541
539
  InvalidConfigurationError,
542
540
  NetworkNotConfiguredError,
543
- } from '@agora-dao-midnight/sdk';
541
+ } from '@claritydao/midnight-agora-sdk';
544
542
  ```
545
543
 
546
544
  ### Error Handling Pattern
547
545
 
548
546
  ```typescript
549
547
  try {
550
- await api.createProposal(title, description, creator, actions);
548
+ await api.createProposal(title, description, creator, action);
551
549
  } catch (error) {
552
550
  if (error instanceof InsufficientStakeError) {
553
551
  console.error('Need more stake:', error.message);
@@ -566,7 +564,7 @@ try {
566
564
  The SDK includes comprehensive input validation:
567
565
 
568
566
  ```typescript
569
- import { validators } from '@agora-dao-midnight/sdk';
567
+ import { validators } from '@claritydao/midnight-agora-sdk';
570
568
 
571
569
  // Validate proposal parameters
572
570
  validators.validateProposal(title, description, creator);
@@ -606,6 +604,9 @@ import { defineConfig } from 'vite';
606
604
  import react from '@vitejs/plugin-react';
607
605
  import wasm from 'vite-plugin-wasm';
608
606
  import topLevelAwait from 'vite-plugin-top-level-await';
607
+ import path from 'node:path';
608
+
609
+ const onchainRuntimePackage = '@midnight-ntwrk/onchain-runtime-v3';
609
610
 
610
611
  export default defineConfig({
611
612
  cacheDir: './.vite',
@@ -616,7 +617,7 @@ export default defineConfig({
616
617
  output: {
617
618
  manualChunks: {
618
619
  // CRITICAL: Separate chunk for WASM modules
619
- wasm: ['@midnight-ntwrk/onchain-runtime'],
620
+ wasm: [onchainRuntimePackage],
620
621
  },
621
622
  },
622
623
  },
@@ -639,7 +640,7 @@ export default defineConfig({
639
640
  name: 'wasm-module-resolver',
640
641
  resolveId(source, importer) {
641
642
  if (
642
- source === '@midnight-ntwrk/onchain-runtime' &&
643
+ (source === '@midnight-ntwrk/onchain-runtime' || source === onchainRuntimePackage) &&
643
644
  importer &&
644
645
  importer.includes('@midnight-ntwrk/compact-runtime')
645
646
  ) {
@@ -665,14 +666,18 @@ export default defineConfig({
665
666
  include: ['@midnight-ntwrk/compact-runtime'],
666
667
  // CRITICAL: Exclude WASM from optimization
667
668
  exclude: [
668
- '@midnight-ntwrk/onchain-runtime',
669
- '@midnight-ntwrk/onchain-runtime/midnight_onchain_runtime_wasm_bg.wasm',
670
- '@midnight-ntwrk/onchain-runtime/midnight_onchain_runtime_wasm.js',
669
+ onchainRuntimePackage,
670
+ `${onchainRuntimePackage}/midnight_onchain_runtime_wasm_bg.wasm`,
671
+ `${onchainRuntimePackage}/midnight_onchain_runtime_wasm.js`,
671
672
  ],
672
673
  },
673
674
  resolve: {
674
675
  extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.wasm'],
675
676
  mainFields: ['browser', 'module', 'main'],
677
+ alias: {
678
+ '@midnight-ntwrk/onchain-runtime': onchainRuntimePackage,
679
+ 'isomorphic-ws': path.resolve(__dirname, './src/shims/isomorphic-ws.ts'),
680
+ },
676
681
  },
677
682
  });
678
683
  ```
@@ -681,7 +686,7 @@ export default defineConfig({
681
686
 
682
687
  The SDK depends on `@midnight-ntwrk/compact-runtime`, which:
683
688
  1. Uses CommonJS (`require()`)
684
- 2. Imports `@midnight-ntwrk/onchain-runtime` (WASM module with top-level await)
689
+ 2. Imports `@midnight-ntwrk/onchain-runtime-v3` (WASM module with top-level await)
685
690
  3. Executes `ocrt.maxField()` at the top level
686
691
 
687
692
  This creates a CJS/ESM/WASM incompatibility that requires the custom resolver plugin and specific optimization settings.
@@ -705,7 +710,7 @@ Even with the `--webpack` flag, the CJS/ESM/WASM interaction is too complex for
705
710
  ### React Example
706
711
 
707
712
  ```tsx
708
- import { GovernorManager, VoteChoice, ConfigPresets } from '@agora-dao-midnight/sdk';
713
+ import { GovernorManager, VoteChoice, ConfigPresets } from '@claritydao/midnight-agora-sdk';
709
714
  import { randomBytes } from '@midnight-ntwrk/midnight-js-utils';
710
715
  import { useEffect, useState } from 'react';
711
716
  import pino from 'pino';
@@ -752,22 +757,20 @@ function GovernanceApp() {
752
757
 
753
758
  const handleCreateProposal = async () => {
754
759
  try {
755
- const actions = [
756
- {
757
- typ: 0,
758
- token: tokenAddress,
759
- amount: BigInt(1000),
760
- to: recipientAddress,
761
- configKey: new Uint8Array(32),
762
- configValue: new Uint8Array(32),
763
- },
764
- ];
760
+ const action = {
761
+ typ: 2,
762
+ token: tokenAddress,
763
+ amount: BigInt(1000),
764
+ to: recipientAddress,
765
+ configKey: new Uint8Array(32),
766
+ configValue: new Uint8Array(32),
767
+ };
765
768
 
766
769
  const { proposalId } = await api.createProposal(
767
770
  'My Proposal',
768
771
  'Description here',
769
772
  creatorAddress,
770
- actions
773
+ action
771
774
  );
772
775
 
773
776
  alert(`Proposal created: ${proposalId}`);
@@ -827,14 +830,14 @@ const config = {
827
830
  ### 3. Validate User Inputs
828
831
 
829
832
  ```typescript
830
- import { validators } from '@agora-dao-midnight/sdk';
833
+ import { validators } from '@claritydao/midnight-agora-sdk';
831
834
 
832
835
  // ✅ Good: Validate before submission
833
836
  validators.validateProposal(title, description, creator);
834
- await api.createProposal(title, description, creator, actions);
837
+ await api.createProposal(title, description, creator, action);
835
838
 
836
839
  // ❌ Bad: No validation
837
- await api.createProposal(title, description, creator, actions);
840
+ await api.createProposal(title, description, creator, action);
838
841
  ```
839
842
 
840
843
  ### 4. Handle Errors Gracefully
@@ -865,7 +868,7 @@ try {
865
868
 
866
869
  **Problem:** `TypeError: ocrt.maxField is not a function`
867
870
 
868
- **Root Cause:** CJS/ESM/WASM module incompatibility. The SDK depends on `@midnight-ntwrk/compact-runtime` (CommonJS) which requires `@midnight-ntwrk/onchain-runtime` (WASM with top-level await).
871
+ **Root Cause:** CJS/ESM/WASM module incompatibility. The SDK depends on `@midnight-ntwrk/compact-runtime` (CommonJS) which requires `@midnight-ntwrk/onchain-runtime-v3` (WASM with top-level await).
869
872
 
870
873
  **Solutions:**
871
874
 
@@ -945,7 +948,7 @@ commonjsOptions: {
945
948
  rollupOptions: {
946
949
  output: {
947
950
  manualChunks: {
948
- wasm: ['@midnight-ntwrk/onchain-runtime'],
951
+ wasm: ['@midnight-ntwrk/onchain-runtime-v3'],
949
952
  },
950
953
  },
951
954
  }
@@ -961,8 +964,8 @@ rollupOptions: {
961
964
  optimizeDeps: {
962
965
  include: ['@midnight-ntwrk/compact-runtime'], // Pre-bundle for CJS→ESM
963
966
  exclude: [
964
- '@midnight-ntwrk/onchain-runtime', // Don't optimize WASM
965
- '@agora-dao-midnight/contract', // Don't optimize contract
967
+ '@midnight-ntwrk/onchain-runtime-v3', // Don't optimize WASM
968
+ '@claritydao/midnight-agora-contracts', // Don't optimize contract
966
969
  ],
967
970
  }
968
971
  ```
@@ -1,21 +1,26 @@
1
- import { type GovernorProviders } from "./common-types";
2
- import { type Logger } from "pino";
3
- import { type InitialAPI } from "@midnight-ntwrk/dapp-connector-api";
4
- /**
5
- * Initializes and returns the providers required for Governor contract interactions in a browser environment.
6
- *
7
- * @param logger The logger instance for logging.
8
- * @param networkId The network ID to connect to (e.g., 'testnet', 'mainnet').
9
- * @returns A promise that resolves to configured GovernorProviders.
10
- *
11
- * @remarks
12
- * This function connects to the Midnight Lace wallet extension and retrieves network configuration
13
- * from the wallet. The network ID is specified by the caller.
14
- */
15
- export declare const initializeProviders: (logger: Logger, networkId?: string) => Promise<GovernorProviders>;
16
- declare global {
17
- interface Window {
18
- midnight?: Record<string, InitialAPI>;
19
- }
1
+ import { type GovernorProviders } from './common-types';
2
+ import type { Configuration, ConnectedAPI, InitialAPI } from '@midnight-ntwrk/dapp-connector-api';
3
+ import { type Logger } from 'pino';
4
+ export interface InitializeGovernorProvidersOptions {
5
+ accountId?: string;
6
+ contractAddress?: string;
7
+ proofServerUri?: string;
8
+ zkConfigBaseUrl?: string;
20
9
  }
10
+ export interface InitializeProvidersOptions extends InitializeGovernorProvidersOptions {
11
+ initialAPI?: InitialAPI;
12
+ networkId?: string;
13
+ walletName?: string;
14
+ }
15
+ export declare function parseMidnightRecipientAddress(value: string, networkId?: string): Uint8Array;
16
+ export declare function connectMidnightWallet(initialAPI: InitialAPI, networkId?: string): Promise<ConnectedAPI>;
17
+ export declare function encodeWalletUserId(walletOrCoinPublicKey: ConnectedAPI | string, networkId?: string): Promise<Uint8Array>;
18
+ export declare function initializeGovernorProviders(wallet: ConnectedAPI, options?: InitializeGovernorProvidersOptions): Promise<{
19
+ providers: GovernorProviders;
20
+ config: Configuration;
21
+ walletPublicKey: Uint8Array;
22
+ accountId: string;
23
+ }>;
24
+ export declare const initializeProviders: (logger: Logger, options?: InitializeProvidersOptions) => Promise<GovernorProviders>;
25
+ export declare function stripHexPrefix(value: string): string;
21
26
  //# sourceMappingURL=browser-providers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"browser-providers.d.ts","sourceRoot":"","sources":["../src/browser-providers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,iBAAiB,EAAyB,MAAM,gBAAgB,CAAC;AAe/E,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,MAAM,CAAC;AACnC,OAAO,EACL,KAAK,UAAU,EAGhB,MAAM,oCAAoC,CAAC;AAgC5C;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB,GAC9B,QAAQ,MAAM,EACd,YAAW,MAAkB,KAC5B,OAAO,CAAC,iBAAiB,CAoC3B,CAAC;AAuKF,OAAO,CAAC,MAAM,C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