@thru/programs 0.3.4 → 0.3.5

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 (118) hide show
  1. package/README.md +123 -37
  2. package/dist/abi-manager/index.cjs +9568 -0
  3. package/dist/abi-manager/index.cjs.map +1 -0
  4. package/dist/abi-manager/index.d.cts +2169 -0
  5. package/dist/abi-manager/index.d.ts +2169 -0
  6. package/dist/abi-manager/index.js +4 -0
  7. package/dist/abi-manager/index.js.map +1 -0
  8. package/dist/amm/index.cjs +1 -3
  9. package/dist/amm/index.cjs.map +1 -1
  10. package/dist/amm/index.d.cts +1 -1
  11. package/dist/amm/index.d.ts +1 -1
  12. package/dist/amm/index.js +2 -2
  13. package/dist/amm/index.js.map +1 -1
  14. package/dist/chunk-6IC6CO5S.js +9443 -0
  15. package/dist/chunk-6IC6CO5S.js.map +1 -0
  16. package/dist/chunk-7FBCBZIY.js +41 -0
  17. package/dist/chunk-7FBCBZIY.js.map +1 -0
  18. package/dist/chunk-BNYDH4ZP.js +4351 -0
  19. package/dist/chunk-BNYDH4ZP.js.map +1 -0
  20. package/dist/chunk-OODN6LJA.js +6567 -0
  21. package/dist/chunk-OODN6LJA.js.map +1 -0
  22. package/dist/chunk-YX7BLFQZ.js +81 -0
  23. package/dist/chunk-YX7BLFQZ.js.map +1 -0
  24. package/dist/clob/index.cjs +6674 -3209
  25. package/dist/clob/index.cjs.map +1 -1
  26. package/dist/clob/index.d.cts +1114 -52
  27. package/dist/clob/index.d.ts +1114 -52
  28. package/dist/clob/index.js +6812 -3393
  29. package/dist/clob/index.js.map +1 -1
  30. package/dist/deploy/index.cjs +24734 -0
  31. package/dist/deploy/index.cjs.map +1 -0
  32. package/dist/deploy/index.d.cts +180 -0
  33. package/dist/deploy/index.d.ts +180 -0
  34. package/dist/deploy/index.js +1846 -0
  35. package/dist/deploy/index.js.map +1 -0
  36. package/dist/helpers-DD7q61nr.d.cts +7 -0
  37. package/dist/helpers-DD7q61nr.d.ts +7 -0
  38. package/dist/manager/index.cjs +6680 -0
  39. package/dist/manager/index.cjs.map +1 -0
  40. package/dist/manager/index.d.cts +1279 -0
  41. package/dist/manager/index.d.ts +1279 -0
  42. package/dist/manager/index.js +4 -0
  43. package/dist/manager/index.js.map +1 -0
  44. package/dist/passkey-manager/index.cjs +4 -5
  45. package/dist/passkey-manager/index.cjs.map +1 -1
  46. package/dist/passkey-manager/index.js +2 -2
  47. package/dist/uploader/index.cjs +4442 -0
  48. package/dist/uploader/index.cjs.map +1 -0
  49. package/dist/uploader/index.d.cts +761 -0
  50. package/dist/uploader/index.d.ts +761 -0
  51. package/dist/uploader/index.js +4 -0
  52. package/dist/uploader/index.js.map +1 -0
  53. package/package.json +22 -2
  54. package/src/abi-manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
  55. package/src/abi-manager/abi/thru/common/primitives/types.ts +2428 -0
  56. package/src/abi-manager/abi/thru/program/abi_manager/types.ts +7037 -0
  57. package/src/abi-manager/accounts.ts +91 -0
  58. package/src/abi-manager/constants.ts +19 -0
  59. package/src/abi-manager/derivation.ts +86 -0
  60. package/src/abi-manager/errors.ts +48 -0
  61. package/src/abi-manager/index.test.ts +136 -0
  62. package/src/abi-manager/index.ts +56 -0
  63. package/src/abi-manager/instructions.ts +573 -0
  64. package/src/abi-manager/types.ts +150 -0
  65. package/src/amm/index.test.ts +8 -2
  66. package/src/amm/index.ts +1 -1
  67. package/src/clob/abi/thru/program/clob/types.ts +6981 -3070
  68. package/src/clob/exchange-client.test.ts +115 -2
  69. package/src/clob/exchange-client.ts +52 -13
  70. package/src/clob/exchange.test.ts +18 -16
  71. package/src/clob/exchange.ts +4 -0
  72. package/src/clob/index.test.ts +134 -7
  73. package/src/clob/index.ts +302 -10
  74. package/src/clob/price.test.ts +32 -0
  75. package/src/clob/price.ts +77 -0
  76. package/src/deploy/accounts.ts +208 -0
  77. package/src/deploy/chain.ts +163 -0
  78. package/src/deploy/constants.ts +25 -0
  79. package/src/deploy/deploy.test.ts +349 -0
  80. package/src/deploy/errors.ts +72 -0
  81. package/src/deploy/index.ts +44 -0
  82. package/src/deploy/inspection.ts +146 -0
  83. package/src/deploy/seeds.ts +29 -0
  84. package/src/deploy/types.ts +178 -0
  85. package/src/deploy/uploader.test.ts +326 -0
  86. package/src/deploy/uploader.ts +558 -0
  87. package/src/deploy/validation-imports.test.ts +55 -0
  88. package/src/deploy/validation.ts +183 -0
  89. package/src/deploy/workflows.test.ts +422 -0
  90. package/src/deploy/workflows.ts +1088 -0
  91. package/src/helpers/bytes.test.ts +58 -0
  92. package/src/helpers/bytes.ts +28 -3
  93. package/src/manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
  94. package/src/manager/abi/thru/common/primitives/types.ts +2428 -0
  95. package/src/manager/abi/thru/program/manager/types.ts +3814 -0
  96. package/src/manager/accounts.ts +44 -0
  97. package/src/manager/constants.ts +16 -0
  98. package/src/manager/derivation.ts +32 -0
  99. package/src/manager/errors.ts +47 -0
  100. package/src/manager/index.test.ts +108 -0
  101. package/src/manager/index.ts +68 -0
  102. package/src/manager/instructions.ts +281 -0
  103. package/src/manager/types.ts +95 -0
  104. package/src/manager/validation.ts +35 -0
  105. package/src/uploader/abi/thru/common/primitives/types.ts +2428 -0
  106. package/src/uploader/abi/thru/program/uploader/types.ts +3022 -0
  107. package/src/uploader/accounts.ts +38 -0
  108. package/src/uploader/constants.ts +14 -0
  109. package/src/uploader/derivation.ts +34 -0
  110. package/src/uploader/errors.ts +25 -0
  111. package/src/uploader/index.test.ts +107 -0
  112. package/src/uploader/index.ts +34 -0
  113. package/src/uploader/instructions.ts +163 -0
  114. package/src/uploader/types.ts +67 -0
  115. package/src/utils/helpers.ts +121 -0
  116. package/tsup.config.ts +4 -0
  117. package/dist/chunk-P5OABVJI.js +0 -28
  118. package/dist/chunk-P5OABVJI.js.map +0 -1
@@ -0,0 +1,44 @@
1
+ import type { Account } from '@thru/sdk';
2
+ import { ManagerProgramMeta } from './abi/thru/program/manager/types';
3
+ import {
4
+ MANAGER_META_SIZE,
5
+ MANAGER_STATE_FINALIZED,
6
+ MANAGER_STATE_OPEN,
7
+ MANAGER_STATE_PAUSED,
8
+ } from './constants';
9
+ import {
10
+ accountData,
11
+ pubkeyAddress,
12
+ } from '../utils/helpers';
13
+ import type { ManagerProgramMetaInfo } from './types';
14
+
15
+ export function parseManagerProgramMeta(
16
+ account: Account | Uint8Array
17
+ ): ManagerProgramMetaInfo {
18
+ const data = accountData(account, 'manager metadata account');
19
+ if (data.length !== MANAGER_META_SIZE) {
20
+ throw new Error(
21
+ `manager metadata account must be ${MANAGER_META_SIZE} bytes, got ${data.length}`
22
+ );
23
+ }
24
+ const parsed = ManagerProgramMeta.from_array(data);
25
+ if (!parsed) throw new Error('manager metadata account is malformed');
26
+ const state = parsed.get_state();
27
+ if (
28
+ state !== MANAGER_STATE_OPEN &&
29
+ state !== MANAGER_STATE_PAUSED &&
30
+ state !== MANAGER_STATE_FINALIZED
31
+ ) {
32
+ throw new Error(`manager metadata account has invalid state ${state}`);
33
+ }
34
+ return {
35
+ authorityAddress: pubkeyAddress(
36
+ Uint8Array.from(parsed.get_authority().get_bytes())
37
+ ),
38
+ authorityCandidateAddress: pubkeyAddress(
39
+ Uint8Array.from(parsed.get_authority_candidate().get_bytes())
40
+ ),
41
+ version: parsed.get_version(),
42
+ state,
43
+ };
44
+ }
@@ -0,0 +1,16 @@
1
+ import { encodeAddress } from '@thru/sdk/helpers';
2
+ import { systemProgramPubkey } from '../utils/helpers';
3
+
4
+ export const MANAGER_PROGRAM_PUBKEY = systemProgramPubkey(0x04);
5
+ export const MANAGER_PROGRAM_ADDRESS = encodeAddress(MANAGER_PROGRAM_PUBKEY);
6
+
7
+ export const MANAGER_META_SIZE = 73;
8
+
9
+ export const MANAGER_STATE_OPEN = 0x00;
10
+ export const MANAGER_STATE_PAUSED = 0x01;
11
+ export const MANAGER_STATE_FINALIZED = 0x02;
12
+
13
+ export const MANAGER_PROGRAM_IMAGE_VERSION = 0x01;
14
+ export const MANAGER_PROGRAM_IMAGE_HEADER_SIZE = 8;
15
+ export const MANAGER_PROGRAM_IMAGE_TRAILER_SIZE = 8;
16
+ export const MANAGER_PROGRAM_IMAGE_MIN_TEXT_SIZE = 4;
@@ -0,0 +1,32 @@
1
+ import { deriveProgramAddress } from '@thru/sdk';
2
+ import { MANAGER_PROGRAM_ADDRESS } from './constants';
3
+ import {
4
+ padSeed,
5
+ pubkeyBytes,
6
+ type ProgramSeed,
7
+ } from '../utils/helpers';
8
+ import type { ManagedProgramAddresses } from './types';
9
+
10
+ export function deriveManagedProgramAddresses(
11
+ seed: ProgramSeed,
12
+ ephemeral = false,
13
+ managerProgramAddress: string | Uint8Array = MANAGER_PROGRAM_ADDRESS
14
+ ): ManagedProgramAddresses {
15
+ const manager = pubkeyBytes(managerProgramAddress, 'manager program address');
16
+ const meta = deriveProgramAddress({
17
+ programAddress: manager,
18
+ seed: padSeed(seed),
19
+ ephemeral,
20
+ });
21
+ const program = deriveProgramAddress({
22
+ programAddress: manager,
23
+ seed: meta.bytes,
24
+ ephemeral,
25
+ });
26
+ return {
27
+ programMetaAccountAddress: meta.address,
28
+ programMetaAccountBytes: meta.bytes,
29
+ programAccountAddress: program.address,
30
+ programAccountBytes: program.bytes,
31
+ };
32
+ }
@@ -0,0 +1,47 @@
1
+ export const MANAGER_ERROR_TYPES = {
2
+ 0x0100: 'SIZE_ERROR',
3
+ 0x0200: 'VALUE_ERROR',
4
+ 0x0300: 'AUTHORIZATION_ERROR',
5
+ 0x0400: 'INDEX_ERROR',
6
+ 0x0500: 'NOT_AVAILABLE_ERROR',
7
+ 0x0600: 'RELATIONSHIP_ERROR',
8
+ 0x0700: 'STATE_ERROR',
9
+ 0x0800: 'NOT_WRITABLE_ERROR',
10
+ 0x0900: 'INVALID_PROGRAM',
11
+ } as const;
12
+
13
+ export const MANAGER_ERROR_OBJECTS = {
14
+ 0x01: 'INSTRUCTION',
15
+ 0x02: 'DISCRIMINANT',
16
+ 0x03: 'SOURCE_BUFFER_ACCOUNT',
17
+ 0x04: 'META_ACCOUNT',
18
+ 0x05: 'PROGRAM_ACCOUNT',
19
+ 0x06: 'AUTHORITY_ACCOUNT',
20
+ 0x07: 'AUTHORITY_CANDIDATE_ACCOUNT',
21
+ } as const;
22
+
23
+ export interface DecodedManagerError {
24
+ code: number;
25
+ typeCode: number;
26
+ type: string | undefined;
27
+ objectCode: number;
28
+ object: string | undefined;
29
+ }
30
+
31
+ export function decodeManagerError(code: number): DecodedManagerError {
32
+ if (!Number.isInteger(code) || code < 0 || code > 0xffff) {
33
+ throw new Error('manager error code must be a u16');
34
+ }
35
+ const typeCode = code & 0xff00;
36
+ const objectCode = code & 0xff;
37
+ return {
38
+ code,
39
+ typeCode,
40
+ type: MANAGER_ERROR_TYPES[typeCode as keyof typeof MANAGER_ERROR_TYPES],
41
+ objectCode,
42
+ object:
43
+ MANAGER_ERROR_OBJECTS[
44
+ objectCode as keyof typeof MANAGER_ERROR_OBJECTS
45
+ ],
46
+ };
47
+ }
@@ -0,0 +1,108 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ MANAGER_PROGRAM_ADDRESS,
4
+ MANAGER_STATE_OPEN,
5
+ buildManagerInstructionBytes,
6
+ decodeManagerError,
7
+ deriveManagedProgramAddresses,
8
+ parseManagerProgramMeta,
9
+ validateManagerProgramImage,
10
+ } from './index';
11
+ import { StateProof } from './abi/thru/blockchain/state_proof/types';
12
+
13
+ function creationProof(siblingCount = 0): Uint8Array {
14
+ const proof = new Uint8Array(40 + 64 + siblingCount * 32);
15
+ new DataView(proof.buffer).setBigUint64(0, 2n << 62n, true);
16
+ for (let index = 0; index < siblingCount; index++) {
17
+ proof[8 + Math.floor(index / 8)] |= 1 << (index % 8);
18
+ }
19
+ const validation = StateProof.validate(proof);
20
+ expect(validation.ok).toBe(true);
21
+ expect(validation.consumed).toBe(proof.length);
22
+ return proof;
23
+ }
24
+
25
+ describe('manager SDK', () => {
26
+ it('exports the canonical manager ID and Rust-compatible derivation vectors', () => {
27
+ expect(MANAGER_PROGRAM_ADDRESS).toBe(
28
+ 'taAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQE'
29
+ );
30
+ expect(deriveManagedProgramAddresses('nft')).toMatchObject({
31
+ programMetaAccountAddress:
32
+ 'ta00Efqv-BVcX3MsYbqO9JN2arQVJEMg3xqQF2iy0H1TGV',
33
+ programAccountAddress:
34
+ 'taAFaJ4ctkbuhYBl2FX6tmXGJZQgShIXt6TPrMw4-GOsv4',
35
+ });
36
+ });
37
+
38
+ it('encodes ephemeral create exactly', () => {
39
+ const encoded = buildManagerInstructionBytes({
40
+ kind: 'createEphemeral',
41
+ metaAccountIdx: 1,
42
+ programAccountIdx: 2,
43
+ sourceBufferAccountIdx: 3,
44
+ sourceOffset: 4,
45
+ sourceSize: 5,
46
+ authorityAccountIdx: 6,
47
+ seed: Uint8Array.of(7, 8, 9),
48
+ });
49
+ expect(Array.from(encoded)).toEqual([
50
+ 1,
51
+ 1, 0,
52
+ 2, 0,
53
+ 3, 0,
54
+ 4, 0, 0, 0,
55
+ 5, 0, 0, 0,
56
+ 6, 0,
57
+ 3, 0, 0, 0,
58
+ 7, 8, 9,
59
+ ]);
60
+ });
61
+
62
+ it('supports independently sized permanent-create proofs', () => {
63
+ const metaProof = creationProof();
64
+ const programProof = creationProof(1);
65
+ const encoded = buildManagerInstructionBytes({
66
+ kind: 'createPermanent',
67
+ metaAccountIdx: 1,
68
+ programAccountIdx: 2,
69
+ sourceBufferAccountIdx: 3,
70
+ sourceSize: 10,
71
+ authorityAccountIdx: 4,
72
+ seed: 'x',
73
+ metaStateProof: metaProof,
74
+ programStateProof: programProof,
75
+ });
76
+ expect(encoded[0]).toBe(0);
77
+ expect(encoded.length).toBe(1 + 20 + 1 + metaProof.length + programProof.length);
78
+ });
79
+
80
+ it('parses metadata and rejects unknown states', () => {
81
+ const data = new Uint8Array(73);
82
+ data.fill(1, 0, 32);
83
+ data.fill(2, 32, 64);
84
+ new DataView(data.buffer).setBigUint64(64, 9n, true);
85
+ data[72] = MANAGER_STATE_OPEN;
86
+ expect(parseManagerProgramMeta(data)).toMatchObject({
87
+ version: 9n,
88
+ state: MANAGER_STATE_OPEN,
89
+ });
90
+ data[72] = 9;
91
+ expect(() => parseManagerProgramMeta(data)).toThrow('invalid state');
92
+ });
93
+
94
+ it('validates managed program images', () => {
95
+ const valid = new Uint8Array(20);
96
+ valid[0] = 1;
97
+ expect(() => validateManagerProgramImage(valid)).not.toThrow();
98
+ valid[19] = 1;
99
+ expect(() => validateManagerProgramImage(valid)).toThrow('zero trailer');
100
+ });
101
+
102
+ it('decodes composite manager errors', () => {
103
+ expect(decodeManagerError(0x0306)).toMatchObject({
104
+ type: 'AUTHORIZATION_ERROR',
105
+ object: 'AUTHORITY_ACCOUNT',
106
+ });
107
+ });
108
+ });
@@ -0,0 +1,68 @@
1
+ export {
2
+ MANAGER_META_SIZE,
3
+ MANAGER_PROGRAM_ADDRESS,
4
+ MANAGER_PROGRAM_IMAGE_HEADER_SIZE,
5
+ MANAGER_PROGRAM_IMAGE_MIN_TEXT_SIZE,
6
+ MANAGER_PROGRAM_IMAGE_TRAILER_SIZE,
7
+ MANAGER_PROGRAM_IMAGE_VERSION,
8
+ MANAGER_PROGRAM_PUBKEY,
9
+ MANAGER_STATE_FINALIZED,
10
+ MANAGER_STATE_OPEN,
11
+ MANAGER_STATE_PAUSED,
12
+ } from './constants';
13
+ export { deriveManagedProgramAddresses } from './derivation';
14
+ export { parseManagerProgramMeta } from './accounts';
15
+ export { validateManagerProgramImage } from './validation';
16
+ export {
17
+ MANAGER_ERROR_OBJECTS,
18
+ MANAGER_ERROR_TYPES,
19
+ decodeManagerError,
20
+ } from './errors';
21
+ export type { DecodedManagerError } from './errors';
22
+ export {
23
+ buildManagerInstructionBytes,
24
+ createClaimAuthorityInstruction,
25
+ createDestroyProgramInstruction,
26
+ createEphemeralProgramInstruction,
27
+ createFinalizeProgramInstruction,
28
+ createPermanentProgramInstruction,
29
+ createSetAuthorityInstruction,
30
+ createSetPauseInstruction,
31
+ createUpgradeProgramInstruction,
32
+ } from './instructions';
33
+ export type {
34
+ AccountLookupContext,
35
+ ClaimAuthorityInstructionArgs,
36
+ CreateEphemeralProgramInstructionArgs,
37
+ CreatePermanentProgramInstructionArgs,
38
+ DestroyProgramInstructionArgs,
39
+ FinalizeProgramInstructionArgs,
40
+ InstructionData,
41
+ ManagedProgramAddresses,
42
+ ManagerInstructionArgs,
43
+ ManagerProgramMetaInfo,
44
+ ProgramSeed,
45
+ SetAuthorityInstructionArgs,
46
+ SetPauseInstructionArgs,
47
+ UpgradeProgramInstructionArgs,
48
+ } from './types';
49
+ export {
50
+ CreateEphemeralArgs,
51
+ CreateEphemeralArgsBuilder,
52
+ CreatePermanentArgs,
53
+ CreatePermanentArgsBuilder,
54
+ HeaderOnlyArgs,
55
+ HeaderOnlyArgsBuilder,
56
+ ManagerError,
57
+ ManagerErrorBuilder,
58
+ ManagerInstruction,
59
+ ManagerInstructionBuilder,
60
+ ManagerProgramMeta,
61
+ ManagerProgramMetaBuilder,
62
+ SetAuthorityArgs,
63
+ SetAuthorityArgsBuilder,
64
+ SetPauseArgs,
65
+ SetPauseArgsBuilder,
66
+ UpgradeArgs,
67
+ UpgradeArgsBuilder,
68
+ } from './abi/thru/program/manager/types';
@@ -0,0 +1,281 @@
1
+ import {
2
+ CreateEphemeralArgsBuilder,
3
+ HeaderOnlyArgsBuilder,
4
+ ManagerInstructionBuilder,
5
+ SetAuthorityArgsBuilder,
6
+ SetPauseArgsBuilder,
7
+ UpgradeArgsBuilder,
8
+ } from './abi/thru/program/manager/types';
9
+ import { StateProof } from './abi/thru/blockchain/state_proof/types';
10
+ import {
11
+ accountIndex,
12
+ assertU16,
13
+ assertU32,
14
+ concatBytes,
15
+ exactBytes,
16
+ normalizeSeed,
17
+ type InstructionData,
18
+ } from '../utils/helpers';
19
+ import type {
20
+ ClaimAuthorityInstructionArgs,
21
+ CreateEphemeralProgramInstructionArgs,
22
+ CreatePermanentProgramInstructionArgs,
23
+ DestroyProgramInstructionArgs,
24
+ FinalizeProgramInstructionArgs,
25
+ ManagerInstructionArgs,
26
+ SetAuthorityInstructionArgs,
27
+ SetPauseInstructionArgs,
28
+ UpgradeProgramInstructionArgs,
29
+ } from './types';
30
+
31
+ type ManagerVariant =
32
+ (typeof import('./abi/thru/program/manager/types').ManagerInstruction.payloadVariantDescriptors)[number]['name'];
33
+
34
+ function envelope(variant: ManagerVariant, payload: Uint8Array): Uint8Array {
35
+ return new ManagerInstructionBuilder()
36
+ .payload()
37
+ .select(variant)
38
+ .writePayload(payload)
39
+ .finish()
40
+ .build();
41
+ }
42
+
43
+ function validateIndices(args: {
44
+ metaAccountIdx: number;
45
+ programAccountIdx: number;
46
+ }): void {
47
+ assertU16(args.metaAccountIdx, 'metaAccountIdx');
48
+ assertU16(args.programAccountIdx, 'programAccountIdx');
49
+ }
50
+
51
+ function validateProof(proof: Uint8Array, label: string): Uint8Array {
52
+ const result = StateProof.validate(proof);
53
+ if (!result.ok || result.consumed !== proof.length) {
54
+ throw new Error(`${label} is not a complete state proof`);
55
+ }
56
+ return proof;
57
+ }
58
+
59
+ function buildCreateBase(args: {
60
+ metaAccountIdx: number;
61
+ programAccountIdx: number;
62
+ sourceBufferAccountIdx: number;
63
+ sourceOffset?: number;
64
+ sourceSize: number;
65
+ authorityAccountIdx: number;
66
+ seed: string | Uint8Array;
67
+ }): Uint8Array {
68
+ validateIndices(args);
69
+ assertU16(args.sourceBufferAccountIdx, 'sourceBufferAccountIdx');
70
+ assertU16(args.authorityAccountIdx, 'authorityAccountIdx');
71
+ assertU32(args.sourceOffset ?? 0, 'sourceOffset');
72
+ assertU32(args.sourceSize, 'sourceSize');
73
+ const builder = new CreateEphemeralArgsBuilder()
74
+ .set_meta_account_idx(args.metaAccountIdx)
75
+ .set_program_account_idx(args.programAccountIdx)
76
+ .set_srcbuf_account_idx(args.sourceBufferAccountIdx)
77
+ .set_srcbuf_offset(args.sourceOffset ?? 0)
78
+ .set_srcbuf_size(args.sourceSize)
79
+ .set_authority_account_idx(args.authorityAccountIdx);
80
+ builder.seed().write(normalizeSeed(args.seed)).finish();
81
+ return builder.build();
82
+ }
83
+
84
+ export function buildManagerInstructionBytes(
85
+ args: ManagerInstructionArgs
86
+ ): Uint8Array {
87
+ switch (args.kind) {
88
+ case 'createPermanent': {
89
+ const payload = concatBytes(
90
+ buildCreateBase(args),
91
+ validateProof(args.metaStateProof, 'metaStateProof'),
92
+ validateProof(args.programStateProof, 'programStateProof')
93
+ );
94
+ return envelope('create_permanent', payload);
95
+ }
96
+ case 'createEphemeral':
97
+ return envelope('create_ephemeral', buildCreateBase(args));
98
+ case 'upgrade': {
99
+ validateIndices(args);
100
+ assertU16(args.sourceBufferAccountIdx, 'sourceBufferAccountIdx');
101
+ assertU32(args.sourceOffset ?? 0, 'sourceOffset');
102
+ assertU32(args.sourceSize, 'sourceSize');
103
+ return envelope(
104
+ 'upgrade',
105
+ new UpgradeArgsBuilder()
106
+ .set_meta_account_idx(args.metaAccountIdx)
107
+ .set_program_account_idx(args.programAccountIdx)
108
+ .set_srcbuf_account_idx(args.sourceBufferAccountIdx)
109
+ .set_srcbuf_offset(args.sourceOffset ?? 0)
110
+ .set_srcbuf_size(args.sourceSize)
111
+ .build()
112
+ );
113
+ }
114
+ case 'setPause':
115
+ validateIndices(args);
116
+ return envelope(
117
+ 'set_pause',
118
+ new SetPauseArgsBuilder()
119
+ .set_meta_account_idx(args.metaAccountIdx)
120
+ .set_program_account_idx(args.programAccountIdx)
121
+ .set_is_paused(args.paused ? 1 : 0)
122
+ .build()
123
+ );
124
+ case 'destroy':
125
+ case 'finalize':
126
+ case 'claimAuthority':
127
+ validateIndices(args);
128
+ return envelope(
129
+ args.kind === 'claimAuthority' ? 'claim_authority' : args.kind,
130
+ new HeaderOnlyArgsBuilder()
131
+ .set_meta_account_idx(args.metaAccountIdx)
132
+ .set_program_account_idx(args.programAccountIdx)
133
+ .build()
134
+ );
135
+ case 'setAuthority':
136
+ validateIndices(args);
137
+ return envelope(
138
+ 'set_authority',
139
+ new SetAuthorityArgsBuilder()
140
+ .set_meta_account_idx(args.metaAccountIdx)
141
+ .set_program_account_idx(args.programAccountIdx)
142
+ .set_authority_candidate(
143
+ exactBytes(args.authorityCandidate, 32, 'authorityCandidate')
144
+ )
145
+ .build()
146
+ );
147
+ }
148
+ }
149
+
150
+ function commonIndices(
151
+ context: Parameters<InstructionData>[0],
152
+ args: { metaAccount: Uint8Array; programAccount: Uint8Array }
153
+ ): { metaAccountIdx: number; programAccountIdx: number } {
154
+ return {
155
+ metaAccountIdx: accountIndex(context, args.metaAccount, 'metaAccount'),
156
+ programAccountIdx: accountIndex(
157
+ context,
158
+ args.programAccount,
159
+ 'programAccount'
160
+ ),
161
+ };
162
+ }
163
+
164
+ export function createPermanentProgramInstruction(
165
+ args: CreatePermanentProgramInstructionArgs
166
+ ): InstructionData {
167
+ return async (context) =>
168
+ buildManagerInstructionBytes({
169
+ kind: 'createPermanent',
170
+ ...commonIndices(context, args),
171
+ sourceBufferAccountIdx: accountIndex(
172
+ context,
173
+ args.sourceBufferAccount,
174
+ 'sourceBufferAccount'
175
+ ),
176
+ authorityAccountIdx: accountIndex(
177
+ context,
178
+ args.authorityAccount,
179
+ 'authorityAccount'
180
+ ),
181
+ sourceOffset: args.sourceOffset,
182
+ sourceSize: args.sourceSize,
183
+ seed: args.seed,
184
+ metaStateProof: args.metaStateProof,
185
+ programStateProof: args.programStateProof,
186
+ });
187
+ }
188
+
189
+ export function createEphemeralProgramInstruction(
190
+ args: CreateEphemeralProgramInstructionArgs
191
+ ): InstructionData {
192
+ return async (context) =>
193
+ buildManagerInstructionBytes({
194
+ kind: 'createEphemeral',
195
+ ...commonIndices(context, args),
196
+ sourceBufferAccountIdx: accountIndex(
197
+ context,
198
+ args.sourceBufferAccount,
199
+ 'sourceBufferAccount'
200
+ ),
201
+ authorityAccountIdx: accountIndex(
202
+ context,
203
+ args.authorityAccount,
204
+ 'authorityAccount'
205
+ ),
206
+ sourceOffset: args.sourceOffset,
207
+ sourceSize: args.sourceSize,
208
+ seed: args.seed,
209
+ });
210
+ }
211
+
212
+ export function createUpgradeProgramInstruction(
213
+ args: UpgradeProgramInstructionArgs
214
+ ): InstructionData {
215
+ return async (context) =>
216
+ buildManagerInstructionBytes({
217
+ kind: 'upgrade',
218
+ ...commonIndices(context, args),
219
+ sourceBufferAccountIdx: accountIndex(
220
+ context,
221
+ args.sourceBufferAccount,
222
+ 'sourceBufferAccount'
223
+ ),
224
+ sourceOffset: args.sourceOffset,
225
+ sourceSize: args.sourceSize,
226
+ });
227
+ }
228
+
229
+ export function createSetPauseInstruction(
230
+ args: SetPauseInstructionArgs
231
+ ): InstructionData {
232
+ return async (context) =>
233
+ buildManagerInstructionBytes({
234
+ kind: 'setPause',
235
+ ...commonIndices(context, args),
236
+ paused: args.paused,
237
+ });
238
+ }
239
+
240
+ function headerInstruction(
241
+ kind: 'destroy' | 'finalize' | 'claimAuthority',
242
+ args:
243
+ | DestroyProgramInstructionArgs
244
+ | FinalizeProgramInstructionArgs
245
+ | ClaimAuthorityInstructionArgs
246
+ ): InstructionData {
247
+ return async (context) =>
248
+ buildManagerInstructionBytes({
249
+ kind,
250
+ ...commonIndices(context, args),
251
+ });
252
+ }
253
+
254
+ export function createDestroyProgramInstruction(
255
+ args: DestroyProgramInstructionArgs
256
+ ): InstructionData {
257
+ return headerInstruction('destroy', args);
258
+ }
259
+
260
+ export function createFinalizeProgramInstruction(
261
+ args: FinalizeProgramInstructionArgs
262
+ ): InstructionData {
263
+ return headerInstruction('finalize', args);
264
+ }
265
+
266
+ export function createClaimAuthorityInstruction(
267
+ args: ClaimAuthorityInstructionArgs
268
+ ): InstructionData {
269
+ return headerInstruction('claimAuthority', args);
270
+ }
271
+
272
+ export function createSetAuthorityInstruction(
273
+ args: SetAuthorityInstructionArgs
274
+ ): InstructionData {
275
+ return async (context) =>
276
+ buildManagerInstructionBytes({
277
+ kind: 'setAuthority',
278
+ ...commonIndices(context, args),
279
+ authorityCandidate: args.authorityCandidate,
280
+ });
281
+ }
@@ -0,0 +1,95 @@
1
+ import type {
2
+ AccountLookupContext,
3
+ InstructionData,
4
+ ProgramSeed,
5
+ } from '../utils/helpers';
6
+
7
+ export type { AccountLookupContext, InstructionData, ProgramSeed };
8
+
9
+ export interface ManagedProgramAddresses {
10
+ programMetaAccountAddress: string;
11
+ programMetaAccountBytes: Uint8Array;
12
+ programAccountAddress: string;
13
+ programAccountBytes: Uint8Array;
14
+ }
15
+
16
+ export interface ManagerProgramMetaInfo {
17
+ authorityAddress: string;
18
+ authorityCandidateAddress: string;
19
+ version: bigint;
20
+ state: number;
21
+ }
22
+
23
+ interface ManagerAccountIndices {
24
+ metaAccountIdx: number;
25
+ programAccountIdx: number;
26
+ }
27
+
28
+ interface ManagerCreateIndices extends ManagerAccountIndices {
29
+ sourceBufferAccountIdx: number;
30
+ sourceOffset?: number;
31
+ sourceSize: number;
32
+ authorityAccountIdx: number;
33
+ seed: ProgramSeed;
34
+ }
35
+
36
+ export type ManagerInstructionArgs =
37
+ | (ManagerCreateIndices & {
38
+ kind: 'createPermanent';
39
+ metaStateProof: Uint8Array;
40
+ programStateProof: Uint8Array;
41
+ })
42
+ | (ManagerCreateIndices & { kind: 'createEphemeral' })
43
+ | (ManagerAccountIndices & {
44
+ kind: 'upgrade';
45
+ sourceBufferAccountIdx: number;
46
+ sourceOffset?: number;
47
+ sourceSize: number;
48
+ })
49
+ | (ManagerAccountIndices & { kind: 'setPause'; paused: boolean })
50
+ | (ManagerAccountIndices & { kind: 'destroy' })
51
+ | (ManagerAccountIndices & { kind: 'finalize' })
52
+ | (ManagerAccountIndices & {
53
+ kind: 'setAuthority';
54
+ authorityCandidate: Uint8Array;
55
+ })
56
+ | (ManagerAccountIndices & { kind: 'claimAuthority' });
57
+
58
+ interface ManagerAccounts {
59
+ metaAccount: Uint8Array;
60
+ programAccount: Uint8Array;
61
+ }
62
+
63
+ interface ManagerCreateAccounts extends ManagerAccounts {
64
+ sourceBufferAccount: Uint8Array;
65
+ sourceOffset?: number;
66
+ sourceSize: number;
67
+ authorityAccount: Uint8Array;
68
+ seed: ProgramSeed;
69
+ }
70
+
71
+ export interface CreatePermanentProgramInstructionArgs
72
+ extends ManagerCreateAccounts {
73
+ metaStateProof: Uint8Array;
74
+ programStateProof: Uint8Array;
75
+ }
76
+
77
+ export type CreateEphemeralProgramInstructionArgs = ManagerCreateAccounts;
78
+
79
+ export interface UpgradeProgramInstructionArgs extends ManagerAccounts {
80
+ sourceBufferAccount: Uint8Array;
81
+ sourceOffset?: number;
82
+ sourceSize: number;
83
+ }
84
+
85
+ export interface SetPauseInstructionArgs extends ManagerAccounts {
86
+ paused: boolean;
87
+ }
88
+
89
+ export type DestroyProgramInstructionArgs = ManagerAccounts;
90
+ export type FinalizeProgramInstructionArgs = ManagerAccounts;
91
+ export type ClaimAuthorityInstructionArgs = ManagerAccounts;
92
+
93
+ export interface SetAuthorityInstructionArgs extends ManagerAccounts {
94
+ authorityCandidate: Uint8Array;
95
+ }