@thru/thru-sdk 0.1.19 → 0.1.21

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 (88) hide show
  1. package/README.md +19 -0
  2. package/dist/{chunk-PH7P5EEU.js → chunk-SHMREHP5.js} +13 -9
  3. package/dist/chunk-SHMREHP5.js.map +1 -0
  4. package/dist/client.d.ts +2 -1
  5. package/dist/client.js +3 -2
  6. package/dist/client.js.map +1 -1
  7. package/dist/metafile-esm.json +1 -1
  8. package/dist/sdk.d.ts +3 -3
  9. package/dist/sdk.js +1 -1
  10. package/dist/{transactions-BzD9hYlc.d.ts → transactions-CLezIeXO.d.ts} +20 -18
  11. package/package.json +7 -3
  12. package/buf.gen.yaml +0 -12
  13. package/buf.lock +0 -9
  14. package/buf.yaml +0 -15
  15. package/dist/chunk-PH7P5EEU.js.map +0 -1
  16. package/proto/thru/common/v1/consensus.proto +0 -73
  17. package/proto/thru/common/v1/errors.proto +0 -65
  18. package/proto/thru/common/v1/filters.proto +0 -60
  19. package/proto/thru/common/v1/pagination.proto +0 -45
  20. package/proto/thru/core/v1/account.proto +0 -137
  21. package/proto/thru/core/v1/block.proto +0 -80
  22. package/proto/thru/core/v1/state.proto +0 -35
  23. package/proto/thru/core/v1/transaction.proto +0 -136
  24. package/proto/thru/core/v1/types.proto +0 -50
  25. package/proto/thru/services/v1/command_service.proto +0 -70
  26. package/proto/thru/services/v1/query_service.proto +0 -344
  27. package/proto/thru/services/v1/streaming_service.proto +0 -126
  28. package/thru-ts-client-sdk/__tests__/helpers/test-utils.ts +0 -228
  29. package/thru-ts-client-sdk/client.ts +0 -10
  30. package/thru-ts-client-sdk/core/__tests__/bound-client.test.ts +0 -354
  31. package/thru-ts-client-sdk/core/__tests__/client.test.ts +0 -53
  32. package/thru-ts-client-sdk/core/bound-client.ts +0 -156
  33. package/thru-ts-client-sdk/core/client.ts +0 -38
  34. package/thru-ts-client-sdk/defaults.ts +0 -26
  35. package/thru-ts-client-sdk/modules/__tests__/accounts.test.ts +0 -406
  36. package/thru-ts-client-sdk/modules/__tests__/blocks.test.ts +0 -199
  37. package/thru-ts-client-sdk/modules/__tests__/events.test.ts +0 -74
  38. package/thru-ts-client-sdk/modules/__tests__/height.test.ts +0 -39
  39. package/thru-ts-client-sdk/modules/__tests__/helpers.test.ts +0 -288
  40. package/thru-ts-client-sdk/modules/__tests__/keys.test.ts +0 -55
  41. package/thru-ts-client-sdk/modules/__tests__/proofs.test.ts +0 -119
  42. package/thru-ts-client-sdk/modules/__tests__/streaming.test.ts +0 -152
  43. package/thru-ts-client-sdk/modules/__tests__/transactions.test.ts +0 -730
  44. package/thru-ts-client-sdk/modules/__tests__/version.test.ts +0 -40
  45. package/thru-ts-client-sdk/modules/accounts.ts +0 -141
  46. package/thru-ts-client-sdk/modules/blocks.ts +0 -75
  47. package/thru-ts-client-sdk/modules/events.ts +0 -20
  48. package/thru-ts-client-sdk/modules/height.ts +0 -9
  49. package/thru-ts-client-sdk/modules/helpers.ts +0 -131
  50. package/thru-ts-client-sdk/modules/keys.ts +0 -29
  51. package/thru-ts-client-sdk/modules/proofs.ts +0 -20
  52. package/thru-ts-client-sdk/modules/streaming.ts +0 -133
  53. package/thru-ts-client-sdk/modules/transactions.ts +0 -374
  54. package/thru-ts-client-sdk/modules/version.ts +0 -10
  55. package/thru-ts-client-sdk/proto/buf/validate/validate_pb.ts +0 -4761
  56. package/thru-ts-client-sdk/proto/google/api/annotations_pb.ts +0 -39
  57. package/thru-ts-client-sdk/proto/google/api/client_pb.ts +0 -953
  58. package/thru-ts-client-sdk/proto/google/api/field_behavior_pb.ts +0 -157
  59. package/thru-ts-client-sdk/proto/google/api/http_pb.ts +0 -474
  60. package/thru-ts-client-sdk/proto/google/api/launch_stage_pb.ts +0 -118
  61. package/thru-ts-client-sdk/proto/thru/common/v1/consensus_pb.ts +0 -162
  62. package/thru-ts-client-sdk/proto/thru/common/v1/errors_pb.ts +0 -129
  63. package/thru-ts-client-sdk/proto/thru/common/v1/filters_pb.ts +0 -129
  64. package/thru-ts-client-sdk/proto/thru/common/v1/pagination_pb.ts +0 -79
  65. package/thru-ts-client-sdk/proto/thru/core/v1/account_pb.ts +0 -359
  66. package/thru-ts-client-sdk/proto/thru/core/v1/block_pb.ts +0 -259
  67. package/thru-ts-client-sdk/proto/thru/core/v1/state_pb.ts +0 -103
  68. package/thru-ts-client-sdk/proto/thru/core/v1/transaction_pb.ts +0 -528
  69. package/thru-ts-client-sdk/proto/thru/core/v1/types_pb.ts +0 -100
  70. package/thru-ts-client-sdk/proto/thru/services/v1/command_service_pb.ts +0 -146
  71. package/thru-ts-client-sdk/proto/thru/services/v1/query_service_pb.ts +0 -819
  72. package/thru-ts-client-sdk/proto/thru/services/v1/streaming_service_pb.ts +0 -390
  73. package/thru-ts-client-sdk/sdk.ts +0 -42
  74. package/thru-ts-client-sdk/test-scripts/counter.ts +0 -469
  75. package/thru-ts-client-sdk/test-scripts/create-account.ts +0 -74
  76. package/thru-ts-client-sdk/test-scripts/get-height.ts +0 -52
  77. package/thru-ts-client-sdk/transactions/Transaction.ts +0 -240
  78. package/thru-ts-client-sdk/transactions/TransactionBuilder.ts +0 -53
  79. package/thru-ts-client-sdk/transactions/__tests__/TransactionBuilder.test.ts +0 -411
  80. package/thru-ts-client-sdk/transactions/__tests__/utils.test.ts +0 -214
  81. package/thru-ts-client-sdk/transactions/index.ts +0 -3
  82. package/thru-ts-client-sdk/transactions/types.ts +0 -74
  83. package/thru-ts-client-sdk/transactions/utils.ts +0 -132
  84. package/thru-ts-client-sdk/types/types.ts +0 -8
  85. package/thru-ts-client-sdk/utils/utils.ts +0 -27
  86. package/tsconfig.json +0 -9
  87. package/tsup.config.ts +0 -14
  88. package/vitest.config.ts +0 -31
@@ -1,156 +0,0 @@
1
- import type { ThruClientContext } from "./client";
2
-
3
- import { decodeAddress, decodeSignature, encodeAddress, encodeSignature } from "@thru/helpers";
4
- import * as accountsModule from "../modules/accounts";
5
- import * as blocksModule from "../modules/blocks";
6
- import * as eventsModule from "../modules/events";
7
- import * as heightModule from "../modules/height";
8
- import {
9
- deriveProgramAddress,
10
- toBlockHash,
11
- toPubkey,
12
- toSignature,
13
- type DeriveProgramAddressOptions,
14
- type DeriveProgramAddressResult,
15
- } from "../modules/helpers";
16
- import * as keysModule from "../modules/keys";
17
- import * as proofsModule from "../modules/proofs";
18
- import * as streamingModule from "../modules/streaming";
19
- import * as transactionsModule from "../modules/transactions";
20
- import * as versionModule from "../modules/version";
21
- import { BlockHash, Pubkey, Signature } from "../proto/thru/core/v1/types_pb";
22
-
23
- type ContextualParameters<F> = F extends (ctx: ThruClientContext, ...args: infer P) => any ? P : never;
24
-
25
- type BoundFunction<F> = F extends (ctx: ThruClientContext, ...args: infer P) => infer R ? (...args: P) => R : never;
26
-
27
- function bind<F extends (ctx: ThruClientContext, ...args: any[]) => any>(
28
- ctx: ThruClientContext,
29
- fn: F,
30
- ): BoundFunction<F> {
31
- return ((...args: ContextualParameters<F>) => fn(ctx, ...args)) as BoundFunction<F>;
32
- }
33
-
34
- interface BoundBlocks {
35
- get: BoundFunction<typeof blocksModule.getBlock>;
36
- getRaw: BoundFunction<typeof blocksModule.getRawBlock>;
37
- list: BoundFunction<typeof blocksModule.listBlocks>;
38
- stream: BoundFunction<typeof streamingModule.streamBlocks>;
39
- getBlockHeight: BoundFunction<typeof heightModule.getBlockHeight>;
40
- }
41
-
42
- interface BoundAccounts {
43
- get: BoundFunction<typeof accountsModule.getAccount>;
44
- getRaw: BoundFunction<typeof accountsModule.getRawAccount>;
45
- list: BoundFunction<typeof accountsModule.listAccounts>;
46
- stream: BoundFunction<typeof streamingModule.streamAccountUpdates>;
47
- create: BoundFunction<typeof accountsModule.createAccount>;
48
- }
49
-
50
- interface BoundTransactions {
51
- get: BoundFunction<typeof transactionsModule.getTransaction>;
52
- getRaw: BoundFunction<typeof transactionsModule.getRawTransaction>;
53
- getStatus: BoundFunction<typeof transactionsModule.getTransactionStatus>;
54
- listForAccount: BoundFunction<typeof transactionsModule.listTransactionsForAccount>;
55
- stream: BoundFunction<typeof streamingModule.streamTransactions>;
56
- build: BoundFunction<typeof transactionsModule.buildTransaction>;
57
- buildAndSign: BoundFunction<typeof transactionsModule.buildAndSignTransaction>;
58
- send: BoundFunction<typeof transactionsModule.sendTransaction>;
59
- batchSend: BoundFunction<typeof transactionsModule.batchSendTransactions>;
60
- track: BoundFunction<typeof streamingModule.trackTransaction>;
61
- }
62
-
63
- interface BoundEvents {
64
- get: BoundFunction<typeof eventsModule.getEvent>;
65
- stream: BoundFunction<typeof streamingModule.streamEvents>;
66
- }
67
-
68
- interface BoundProofs {
69
- generate: BoundFunction<typeof proofsModule.generateStateProof>;
70
- }
71
-
72
- interface BoundKeys {
73
- generateKeyPair: typeof keysModule.generateKeyPair;
74
- }
75
-
76
- interface BoundVersion {
77
- get: BoundFunction<typeof versionModule.getVersion>;
78
- }
79
-
80
- interface Helpers {
81
- toSignature(value: Uint8Array | string): Signature;
82
- toPubkey(value: Uint8Array | string, field: string): Pubkey;
83
- toBlockHash(value: Uint8Array | string): BlockHash;
84
- encodeSignature(bytes: Uint8Array): string;
85
- decodeSignature(value: string): Uint8Array;
86
- encodeAddress(bytes: Uint8Array): string;
87
- decodeAddress(value: string): Uint8Array;
88
- deriveProgramAddress(options: DeriveProgramAddressOptions): DeriveProgramAddressResult;
89
- }
90
-
91
- export interface Thru {
92
- ctx: ThruClientContext;
93
- blocks: BoundBlocks;
94
- accounts: BoundAccounts;
95
- transactions: BoundTransactions;
96
- events: BoundEvents;
97
- proofs: BoundProofs;
98
- keys: BoundKeys;
99
- version: BoundVersion;
100
- helpers: Helpers;
101
- }
102
-
103
- export function createBoundThruClient(ctx: ThruClientContext): Thru {
104
- return {
105
- ctx,
106
- blocks: {
107
- get: bind(ctx, blocksModule.getBlock),
108
- getRaw: bind(ctx, blocksModule.getRawBlock),
109
- list: bind(ctx, blocksModule.listBlocks),
110
- stream: bind(ctx, streamingModule.streamBlocks),
111
- getBlockHeight: bind(ctx, heightModule.getBlockHeight),
112
- },
113
- accounts: {
114
- get: bind(ctx, accountsModule.getAccount),
115
- getRaw: bind(ctx, accountsModule.getRawAccount),
116
- list: bind(ctx, accountsModule.listAccounts),
117
- stream: bind(ctx, streamingModule.streamAccountUpdates),
118
- create: bind(ctx, accountsModule.createAccount),
119
- },
120
- transactions: {
121
- get: bind(ctx, transactionsModule.getTransaction),
122
- getRaw: bind(ctx, transactionsModule.getRawTransaction),
123
- getStatus: bind(ctx, transactionsModule.getTransactionStatus),
124
- listForAccount: bind(ctx, transactionsModule.listTransactionsForAccount),
125
- stream: bind(ctx, streamingModule.streamTransactions),
126
- build: bind(ctx, transactionsModule.buildTransaction),
127
- buildAndSign: bind(ctx, transactionsModule.buildAndSignTransaction),
128
- send: bind(ctx, transactionsModule.sendTransaction),
129
- batchSend: bind(ctx, transactionsModule.batchSendTransactions),
130
- track: bind(ctx, streamingModule.trackTransaction),
131
- },
132
- helpers: {
133
- toSignature,
134
- toPubkey,
135
- toBlockHash,
136
- encodeSignature,
137
- decodeSignature,
138
- encodeAddress,
139
- decodeAddress,
140
- deriveProgramAddress,
141
- },
142
- keys: {
143
- generateKeyPair: keysModule.generateKeyPair,
144
- },
145
- events: {
146
- get: bind(ctx, eventsModule.getEvent),
147
- stream: bind(ctx, streamingModule.streamEvents),
148
- },
149
- proofs: {
150
- generate: bind(ctx, proofsModule.generateStateProof),
151
- },
152
- version: {
153
- get: bind(ctx, versionModule.getVersion),
154
- },
155
- };
156
- }
@@ -1,38 +0,0 @@
1
- import { createClient } from "@connectrpc/connect";
2
- import { createGrpcWebTransport } from "@connectrpc/connect-web";
3
-
4
- import { DEFAULT_HOST } from "../defaults";
5
- import { CommandService } from "../proto/thru/services/v1/command_service_pb";
6
- import { QueryService } from "../proto/thru/services/v1/query_service_pb";
7
- import { StreamingService } from "../proto/thru/services/v1/streaming_service_pb";
8
-
9
- export interface ThruClientConfig {
10
- baseUrl?: string;
11
- }
12
-
13
- type QueryClient = ReturnType<typeof createClient<typeof QueryService>>;
14
- type CommandClient = ReturnType<typeof createClient<typeof CommandService>>;
15
- type StreamingClient = ReturnType<typeof createClient<typeof StreamingService>>;
16
-
17
- export interface ThruClientContext {
18
- baseUrl: string;
19
- transport: ReturnType<typeof createGrpcWebTransport>;
20
- query: QueryClient;
21
- command: CommandClient;
22
- streaming: StreamingClient;
23
- }
24
-
25
- export function createThruClientContext(config: ThruClientConfig = {}): ThruClientContext {
26
- const baseUrl = config.baseUrl ?? DEFAULT_HOST;
27
- const transport = createGrpcWebTransport({
28
- baseUrl,
29
- });
30
-
31
- return {
32
- baseUrl,
33
- transport,
34
- query: createClient(QueryService, transport),
35
- command: createClient(CommandService, transport),
36
- streaming: createClient(StreamingService, transport),
37
- };
38
- }
@@ -1,26 +0,0 @@
1
- import { create } from "@bufbuild/protobuf";
2
- import { ConsensusStatus, CurrentVersionSchema, VersionContextSchema } from "./proto/thru/common/v1/consensus_pb";
3
- import { AccountView } from "./proto/thru/core/v1/account_pb";
4
- import { BlockView } from "./proto/thru/core/v1/block_pb";
5
- import { TransactionView } from "./proto/thru/core/v1/transaction_pb";
6
-
7
- export const DEFAULT_HOST = "https://grpc-web.alphanet.thruput.org";
8
-
9
- export const DEFAULT_ACCOUNT_VIEW = AccountView.FULL;
10
- export const DEFAULT_BLOCK_VIEW = BlockView.FULL;
11
- export const DEFAULT_TRANSACTION_VIEW = TransactionView.FULL;
12
- export const DEFAULT_MIN_CONSENSUS = ConsensusStatus.UNSPECIFIED;
13
-
14
- // Default version context requests the latest version available at request time
15
- export const DEFAULT_VERSION_CONTEXT = create(VersionContextSchema, {
16
- version: {
17
- case: "current",
18
- value: create(CurrentVersionSchema, {}),
19
- },
20
- });
21
-
22
- export const DEFAULT_FEE = 1n;
23
- export const DEFAULT_COMPUTE_UNITS = 300_000_000;
24
- export const DEFAULT_STATE_UNITS = 10_000;
25
- export const DEFAULT_MEMORY_UNITS = 10_000;
26
- export const DEFAULT_EXPIRY_AFTER = 100;
@@ -1,406 +0,0 @@
1
- import { create } from "@bufbuild/protobuf";
2
- import { describe, expect, it, vi } from "vitest";
3
- import { createMockAccount, createMockContext, createMockHeightResponse, generateTestAddress, generateTestPubkey } from "../../__tests__/helpers/test-utils";
4
- import { ConsensusStatus, CurrentVersionSchema, VersionContextSchema } from "../../proto/thru/common/v1/consensus_pb";
5
- import { FilterParamValueSchema, FilterSchema } from "../../proto/thru/common/v1/filters_pb";
6
- import { AccountView } from "../../proto/thru/core/v1/account_pb";
7
- import { GenerateStateProofResponseSchema, ListAccountsResponseSchema } from "../../proto/thru/services/v1/query_service_pb";
8
- import { createAccount, getAccount, getRawAccount, listAccounts } from "../accounts";
9
- import { toPubkey } from "../helpers";
10
-
11
- describe("accounts", () => {
12
- describe("getAccount", () => {
13
- it("should return account with valid address", async () => {
14
- const ctx = createMockContext();
15
- const mockAccount = createMockAccount();
16
- vi.spyOn(ctx.query, "getAccount").mockResolvedValue(mockAccount);
17
-
18
- const address = generateTestPubkey(0x01);
19
- const result = await getAccount(ctx, address);
20
-
21
- expect(result).toBe(mockAccount);
22
- expect(ctx.query.getAccount).toHaveBeenCalledTimes(1);
23
- });
24
-
25
- it("should accept address as Uint8Array", async () => {
26
- const ctx = createMockContext();
27
- const mockAccount = createMockAccount();
28
- vi.spyOn(ctx.query, "getAccount").mockResolvedValue(mockAccount);
29
-
30
- const address = generateTestPubkey(0x01);
31
- await getAccount(ctx, address);
32
-
33
- const callArgs = (ctx.query.getAccount as any).mock.calls[0][0];
34
- expect(callArgs.address.value).toEqual(address);
35
- });
36
-
37
- it("should accept address as string", async () => {
38
- const ctx = createMockContext();
39
- const mockAccount = createMockAccount();
40
- vi.spyOn(ctx.query, "getAccount").mockResolvedValue(mockAccount);
41
-
42
- const address = generateTestAddress(0x01);
43
- await getAccount(ctx, address);
44
-
45
- const callArgs = (ctx.query.getAccount as any).mock.calls[0][0];
46
- expect(callArgs.address.value.length).toBe(32);
47
- });
48
-
49
- it("should use default view when not provided", async () => {
50
- const ctx = createMockContext();
51
- const mockAccount = createMockAccount();
52
- vi.spyOn(ctx.query, "getAccount").mockResolvedValue(mockAccount);
53
-
54
- await getAccount(ctx, generateTestPubkey(0x01));
55
-
56
- const callArgs = (ctx.query.getAccount as any).mock.calls[0][0];
57
- expect(callArgs.view).toBe(AccountView.FULL);
58
- });
59
-
60
- it("should use custom view when provided", async () => {
61
- const ctx = createMockContext();
62
- const mockAccount = createMockAccount();
63
- vi.spyOn(ctx.query, "getAccount").mockResolvedValue(mockAccount);
64
-
65
- await getAccount(ctx, generateTestPubkey(0x01), { view: AccountView.META_ONLY });
66
-
67
- const callArgs = (ctx.query.getAccount as any).mock.calls[0][0];
68
- expect(callArgs.view).toBe(AccountView.META_ONLY);
69
- });
70
-
71
- it("should use default minConsensus when not provided", async () => {
72
- const ctx = createMockContext();
73
- const mockAccount = createMockAccount();
74
- vi.spyOn(ctx.query, "getAccount").mockResolvedValue(mockAccount);
75
-
76
- await getAccount(ctx, generateTestPubkey(0x01));
77
-
78
- const callArgs = (ctx.query.getAccount as any).mock.calls[0][0];
79
- expect(callArgs.minConsensus).toBe(ConsensusStatus.UNSPECIFIED);
80
- });
81
-
82
- it("should use custom minConsensus when provided", async () => {
83
- const ctx = createMockContext();
84
- const mockAccount = createMockAccount();
85
- vi.spyOn(ctx.query, "getAccount").mockResolvedValue(mockAccount);
86
-
87
- await getAccount(ctx, generateTestPubkey(0x01), { minConsensus: ConsensusStatus.FINALIZED });
88
-
89
- const callArgs = (ctx.query.getAccount as any).mock.calls[0][0];
90
- expect(callArgs.minConsensus).toBe(ConsensusStatus.FINALIZED);
91
- });
92
-
93
- it("should include dataSlice when provided", async () => {
94
- const ctx = createMockContext();
95
- const mockAccount = createMockAccount();
96
- vi.spyOn(ctx.query, "getAccount").mockResolvedValue(mockAccount);
97
-
98
- const dataSlice = { offset: 0, length: 100 };
99
- await getAccount(ctx, generateTestPubkey(0x01), { dataSlice: dataSlice as any });
100
-
101
- const callArgs = (ctx.query.getAccount as any).mock.calls[0][0];
102
- expect(callArgs.dataSlice).toBeDefined();
103
- expect(callArgs.dataSlice.offset).toBe(0);
104
- expect(callArgs.dataSlice.length).toBe(100);
105
- });
106
-
107
- it("should include version context when provided", async () => {
108
- const ctx = createMockContext();
109
- const mockAccount = createMockAccount();
110
- vi.spyOn(ctx.query, "getAccount").mockResolvedValue(mockAccount);
111
-
112
- const versionContext = create(VersionContextSchema, {
113
- version: {
114
- case: "current",
115
- value: create(CurrentVersionSchema, {}),
116
- },
117
- });
118
-
119
- await getAccount(ctx, generateTestPubkey(0x01), { versionContext });
120
-
121
- const callArgs = (ctx.query.getAccount as any).mock.calls[0][0];
122
- expect(callArgs.versionContext).toBeDefined();
123
- expect(callArgs.versionContext.version?.case).toBe("current");
124
- });
125
- });
126
-
127
- describe("getRawAccount", () => {
128
- it("should return raw account", async () => {
129
- const ctx = createMockContext();
130
- const mockRawAccount = {
131
- address: { value: generateTestPubkey(0x01) },
132
- rawMeta: new Uint8Array([1, 2, 3]),
133
- rawData: new Uint8Array([4, 5, 6]),
134
- };
135
- vi.spyOn(ctx.query, "getRawAccount").mockResolvedValue(mockRawAccount as any);
136
-
137
- const address = generateTestPubkey(0x01);
138
- const result = await getRawAccount(ctx, address);
139
-
140
- expect(result).toBe(mockRawAccount);
141
- expect(ctx.query.getRawAccount).toHaveBeenCalledTimes(1);
142
- });
143
-
144
- it("should use default view and minConsensus", async () => {
145
- const ctx = createMockContext();
146
- const mockRawAccount = { address: { value: generateTestPubkey(0x01) } };
147
- vi.spyOn(ctx.query, "getRawAccount").mockResolvedValue(mockRawAccount as any);
148
-
149
- await getRawAccount(ctx, generateTestPubkey(0x01));
150
-
151
- const callArgs = (ctx.query.getRawAccount as any).mock.calls[0][0];
152
- expect(callArgs.view).toBe(AccountView.FULL);
153
- expect(callArgs.minConsensus).toBe(ConsensusStatus.UNSPECIFIED);
154
- });
155
- });
156
-
157
- describe("listAccounts", () => {
158
- it("should list owned accounts", async () => {
159
- const ctx = createMockContext();
160
- const mockResponse = create(ListAccountsResponseSchema, {
161
- accounts: [createMockAccount()],
162
- });
163
- vi.spyOn(ctx.query, "listAccounts").mockResolvedValue(mockResponse);
164
-
165
- const owner = generateTestPubkey(0x01);
166
- const ownerPubkey = toPubkey(owner, "owner");
167
- const ownerFilterParamValue = create(FilterParamValueSchema, {
168
- kind: {
169
- case: "bytesValue",
170
- value: ownerPubkey.value,
171
- },
172
- });
173
- const ownerFilter = create(FilterSchema, {
174
- expression: "meta.owner.value == params.owner_bytes",
175
- params: {
176
- owner_bytes: ownerFilterParamValue,
177
- },
178
- });
179
-
180
- const result = await listAccounts(ctx, { filter: ownerFilter });
181
-
182
- expect(result).toBe(mockResponse);
183
- expect(ctx.query.listAccounts).toHaveBeenCalledTimes(1);
184
- });
185
-
186
- it("should create owner filter", async () => {
187
- const ctx = createMockContext();
188
- const mockResponse = create(ListAccountsResponseSchema, { accounts: [] });
189
- vi.spyOn(ctx.query, "listAccounts").mockResolvedValue(mockResponse);
190
-
191
- const owner = generateTestPubkey(0x01);
192
- const ownerPubkey = toPubkey(owner, "owner");
193
- const ownerFilterParamValue = create(FilterParamValueSchema, {
194
- kind: {
195
- case: "bytesValue",
196
- value: ownerPubkey.value,
197
- },
198
- });
199
- const ownerFilter = create(FilterSchema, {
200
- expression: "meta.owner.value == params.owner_bytes",
201
- params: {
202
- owner_bytes: ownerFilterParamValue,
203
- },
204
- });
205
-
206
- await listAccounts(ctx, { filter: ownerFilter });
207
-
208
- const callArgs = (ctx.query.listAccounts as any).mock.calls[0][0];
209
- expect(callArgs.filter).toBeDefined();
210
- expect(callArgs.filter.expression).toBe("meta.owner.value == params.owner_bytes");
211
- expect(callArgs.filter.params.owner_bytes.kind.case).toBe("bytesValue");
212
- expect(callArgs.filter.params.owner_bytes.kind.value).toEqual(ownerPubkey.value);
213
- });
214
-
215
- it("should use custom filter when provided", async () => {
216
- const ctx = createMockContext();
217
- const mockResponse = create(ListAccountsResponseSchema, { accounts: [] });
218
- const customFilter = create(FilterSchema, {
219
- expression: "custom expression",
220
- });
221
- vi.spyOn(ctx.query, "listAccounts").mockResolvedValue(mockResponse);
222
-
223
- await listAccounts(ctx, { filter: customFilter });
224
-
225
- const callArgs = (ctx.query.listAccounts as any).mock.calls[0][0];
226
- expect(callArgs.filter.expression).toBe("custom expression");
227
- });
228
-
229
- it("should use default view and minConsensus", async () => {
230
- const ctx = createMockContext();
231
- const mockResponse = create(ListAccountsResponseSchema, { accounts: [] });
232
- vi.spyOn(ctx.query, "listAccounts").mockResolvedValue(mockResponse);
233
-
234
- const owner = generateTestPubkey(0x01);
235
- const ownerPubkey = toPubkey(owner, "owner");
236
- const ownerFilterParamValue = create(FilterParamValueSchema, {
237
- kind: {
238
- case: "bytesValue",
239
- value: ownerPubkey.value,
240
- },
241
- });
242
- const ownerFilter = create(FilterSchema, {
243
- expression: "meta.owner.value == params.owner_bytes",
244
- params: {
245
- owner_bytes: ownerFilterParamValue,
246
- },
247
- });
248
-
249
- await listAccounts(ctx, { filter: ownerFilter });
250
-
251
- const callArgs = (ctx.query.listAccounts as any).mock.calls[0][0];
252
- expect(callArgs.view).toBe(AccountView.FULL);
253
- expect(callArgs.minConsensus).toBe(ConsensusStatus.UNSPECIFIED);
254
- });
255
- });
256
-
257
- describe("createAccount", () => {
258
- it("should create account transaction", async () => {
259
- const ctx = createMockContext();
260
- const mockHeight = createMockHeightResponse({ finalized: 1000n });
261
- const mockProof = create(GenerateStateProofResponseSchema, {
262
- proof: {
263
- proof: new Uint8Array(64).fill(0x42),
264
- },
265
- });
266
-
267
- vi.spyOn(ctx.query, "getHeight").mockResolvedValue(mockHeight);
268
- vi.spyOn(ctx.query, "generateStateProof").mockResolvedValue(mockProof);
269
-
270
- const publicKey = generateTestPubkey(0x01);
271
- const transaction = await createAccount(ctx, { publicKey });
272
-
273
- expect(transaction).toBeDefined();
274
- expect(transaction.feePayer).toEqual(publicKey);
275
- expect(transaction.feePayerStateProof).toBeDefined();
276
- expect(ctx.query.getHeight).toHaveBeenCalledTimes(1);
277
- expect(ctx.query.generateStateProof).toHaveBeenCalledTimes(1);
278
- });
279
-
280
- it("should use finalized slot from height", async () => {
281
- const ctx = createMockContext();
282
- const mockHeight = createMockHeightResponse({ finalized: 2000n });
283
- const mockProof = create(GenerateStateProofResponseSchema, {
284
- proof: {
285
- proof: new Uint8Array(64).fill(0x42),
286
- },
287
- });
288
-
289
- vi.spyOn(ctx.query, "getHeight").mockResolvedValue(mockHeight);
290
- vi.spyOn(ctx.query, "generateStateProof").mockResolvedValue(mockProof);
291
-
292
- const publicKey = generateTestPubkey(0x01);
293
- const transaction = await createAccount(ctx, { publicKey });
294
-
295
- expect(transaction.startSlot).toBe(2000n);
296
- // Verify generateStateProof was called (we can't easily check nested properties)
297
- expect(ctx.query.generateStateProof).toHaveBeenCalledTimes(1);
298
- });
299
-
300
- it("should generate CREATING proof type", async () => {
301
- const ctx = createMockContext();
302
- const mockHeight = createMockHeightResponse();
303
- const mockProof = create(GenerateStateProofResponseSchema, {
304
- proof: {
305
- proof: new Uint8Array(64).fill(0x42),
306
- },
307
- });
308
-
309
- vi.spyOn(ctx.query, "getHeight").mockResolvedValue(mockHeight);
310
- vi.spyOn(ctx.query, "generateStateProof").mockResolvedValue(mockProof);
311
-
312
- const publicKey = generateTestPubkey(0x01);
313
- await createAccount(ctx, { publicKey });
314
-
315
- // Verify generateStateProof was called with correct address
316
- expect(ctx.query.generateStateProof).toHaveBeenCalledTimes(1);
317
- const proofCallArgs = (ctx.query.generateStateProof as any).mock.calls[0][0];
318
- expect(proofCallArgs.request).toBeDefined();
319
- });
320
-
321
- it("should throw error when proof is empty", async () => {
322
- const ctx = createMockContext();
323
- const mockHeight = createMockHeightResponse();
324
- const mockProof = create(GenerateStateProofResponseSchema, {
325
- proof: {
326
- proof: new Uint8Array(0), // Empty proof
327
- },
328
- });
329
-
330
- vi.spyOn(ctx.query, "getHeight").mockResolvedValue(mockHeight);
331
- vi.spyOn(ctx.query, "generateStateProof").mockResolvedValue(mockProof);
332
-
333
- const publicKey = generateTestPubkey(0x01);
334
-
335
- await expect(createAccount(ctx, { publicKey })).rejects.toThrow(
336
- "State proof generation returned empty proof"
337
- );
338
- });
339
-
340
- it("should throw error when proof is missing", async () => {
341
- const ctx = createMockContext();
342
- const mockHeight = createMockHeightResponse();
343
- const mockProof = create(GenerateStateProofResponseSchema, {
344
- // No proof field
345
- });
346
-
347
- vi.spyOn(ctx.query, "getHeight").mockResolvedValue(mockHeight);
348
- vi.spyOn(ctx.query, "generateStateProof").mockResolvedValue(mockProof);
349
-
350
- const publicKey = generateTestPubkey(0x01);
351
-
352
- await expect(createAccount(ctx, { publicKey })).rejects.toThrow(
353
- "State proof generation returned empty proof"
354
- );
355
- });
356
-
357
- it("should apply header overrides", async () => {
358
- const ctx = createMockContext();
359
- const mockHeight = createMockHeightResponse({ finalized: 1000n });
360
- const mockProof = create(GenerateStateProofResponseSchema, {
361
- proof: {
362
- proof: new Uint8Array(64).fill(0x42),
363
- },
364
- });
365
-
366
- vi.spyOn(ctx.query, "getHeight").mockResolvedValue(mockHeight);
367
- vi.spyOn(ctx.query, "generateStateProof").mockResolvedValue(mockProof);
368
-
369
- const publicKey = generateTestPubkey(0x01);
370
- const transaction = await createAccount(ctx, {
371
- publicKey,
372
- header: {
373
- fee: 100n,
374
- computeUnits: 20_000,
375
- },
376
- });
377
-
378
- expect(transaction.fee).toBe(100n);
379
- expect(transaction.requestedComputeUnits).toBe(20_000);
380
- });
381
-
382
- it("should use default header values", async () => {
383
- const ctx = createMockContext();
384
- const mockHeight = createMockHeightResponse({ finalized: 1000n });
385
- const mockProof = create(GenerateStateProofResponseSchema, {
386
- proof: {
387
- proof: new Uint8Array(64).fill(0x42),
388
- },
389
- });
390
-
391
- vi.spyOn(ctx.query, "getHeight").mockResolvedValue(mockHeight);
392
- vi.spyOn(ctx.query, "generateStateProof").mockResolvedValue(mockProof);
393
-
394
- const publicKey = generateTestPubkey(0x01);
395
- const transaction = await createAccount(ctx, { publicKey });
396
-
397
- expect(transaction.fee).toBe(0n);
398
- expect(transaction.nonce).toBe(0n);
399
- expect(transaction.expiryAfter).toBe(100);
400
- expect(transaction.requestedComputeUnits).toBe(10_000);
401
- expect(transaction.requestedStateUnits).toBe(10_000);
402
- expect(transaction.requestedMemoryUnits).toBe(10_000);
403
- });
404
- });
405
- });
406
-