@thru/programs 0.3.3 → 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.
- package/README.md +123 -37
- package/dist/abi-manager/index.cjs +9568 -0
- package/dist/abi-manager/index.cjs.map +1 -0
- package/dist/abi-manager/index.d.cts +2169 -0
- package/dist/abi-manager/index.d.ts +2169 -0
- package/dist/abi-manager/index.js +4 -0
- package/dist/abi-manager/index.js.map +1 -0
- package/dist/amm/index.cjs +1 -3
- package/dist/amm/index.cjs.map +1 -1
- package/dist/amm/index.d.cts +1 -1
- package/dist/amm/index.d.ts +1 -1
- package/dist/amm/index.js +2 -2
- package/dist/amm/index.js.map +1 -1
- package/dist/chunk-6IC6CO5S.js +9443 -0
- package/dist/chunk-6IC6CO5S.js.map +1 -0
- package/dist/chunk-7DRJVAYI.js +8587 -0
- package/dist/chunk-7DRJVAYI.js.map +1 -0
- package/dist/chunk-7FBCBZIY.js +41 -0
- package/dist/chunk-7FBCBZIY.js.map +1 -0
- package/dist/chunk-BNYDH4ZP.js +4351 -0
- package/dist/chunk-BNYDH4ZP.js.map +1 -0
- package/dist/chunk-OODN6LJA.js +6567 -0
- package/dist/chunk-OODN6LJA.js.map +1 -0
- package/dist/chunk-YX7BLFQZ.js +81 -0
- package/dist/chunk-YX7BLFQZ.js.map +1 -0
- package/dist/clob/index.cjs +19361 -6851
- package/dist/clob/index.cjs.map +1 -1
- package/dist/clob/index.d.cts +1243 -63
- package/dist/clob/index.d.ts +1243 -63
- package/dist/clob/index.js +6826 -2818
- package/dist/clob/index.js.map +1 -1
- package/dist/deploy/index.cjs +24734 -0
- package/dist/deploy/index.cjs.map +1 -0
- package/dist/deploy/index.d.cts +180 -0
- package/dist/deploy/index.d.ts +180 -0
- package/dist/deploy/index.js +1846 -0
- package/dist/deploy/index.js.map +1 -0
- package/dist/helpers-DD7q61nr.d.cts +7 -0
- package/dist/helpers-DD7q61nr.d.ts +7 -0
- package/dist/manager/index.cjs +6680 -0
- package/dist/manager/index.cjs.map +1 -0
- package/dist/manager/index.d.cts +1279 -0
- package/dist/manager/index.d.ts +1279 -0
- package/dist/manager/index.js +4 -0
- package/dist/manager/index.js.map +1 -0
- package/dist/passkey-manager/index.cjs +4 -5
- package/dist/passkey-manager/index.cjs.map +1 -1
- package/dist/passkey-manager/index.js +2 -2
- package/dist/token/index.js +1 -8585
- package/dist/token/index.js.map +1 -1
- package/dist/uploader/index.cjs +4442 -0
- package/dist/uploader/index.cjs.map +1 -0
- package/dist/uploader/index.d.cts +761 -0
- package/dist/uploader/index.d.ts +761 -0
- package/dist/uploader/index.js +4 -0
- package/dist/uploader/index.js.map +1 -0
- package/package.json +22 -2
- package/src/abi-manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
- package/src/abi-manager/abi/thru/common/primitives/types.ts +2428 -0
- package/src/abi-manager/abi/thru/program/abi_manager/types.ts +7037 -0
- package/src/abi-manager/accounts.ts +91 -0
- package/src/abi-manager/constants.ts +19 -0
- package/src/abi-manager/derivation.ts +86 -0
- package/src/abi-manager/errors.ts +48 -0
- package/src/abi-manager/index.test.ts +136 -0
- package/src/abi-manager/index.ts +56 -0
- package/src/abi-manager/instructions.ts +573 -0
- package/src/abi-manager/types.ts +150 -0
- package/src/amm/index.test.ts +8 -2
- package/src/amm/index.ts +1 -1
- package/src/clob/abi/thru/program/clob/types.ts +6981 -3070
- package/src/clob/exchange-client.test.ts +592 -0
- package/src/clob/exchange-client.ts +953 -0
- package/src/clob/exchange.test.ts +18 -16
- package/src/clob/exchange.ts +4 -0
- package/src/clob/index.test.ts +134 -7
- package/src/clob/index.ts +303 -10
- package/src/clob/price.test.ts +32 -0
- package/src/clob/price.ts +77 -0
- package/src/deploy/accounts.ts +208 -0
- package/src/deploy/chain.ts +163 -0
- package/src/deploy/constants.ts +25 -0
- package/src/deploy/deploy.test.ts +349 -0
- package/src/deploy/errors.ts +72 -0
- package/src/deploy/index.ts +44 -0
- package/src/deploy/inspection.ts +146 -0
- package/src/deploy/seeds.ts +29 -0
- package/src/deploy/types.ts +178 -0
- package/src/deploy/uploader.test.ts +326 -0
- package/src/deploy/uploader.ts +558 -0
- package/src/deploy/validation-imports.test.ts +55 -0
- package/src/deploy/validation.ts +183 -0
- package/src/deploy/workflows.test.ts +422 -0
- package/src/deploy/workflows.ts +1088 -0
- package/src/helpers/bytes.test.ts +58 -0
- package/src/helpers/bytes.ts +28 -3
- package/src/manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
- package/src/manager/abi/thru/common/primitives/types.ts +2428 -0
- package/src/manager/abi/thru/program/manager/types.ts +3814 -0
- package/src/manager/accounts.ts +44 -0
- package/src/manager/constants.ts +16 -0
- package/src/manager/derivation.ts +32 -0
- package/src/manager/errors.ts +47 -0
- package/src/manager/index.test.ts +108 -0
- package/src/manager/index.ts +68 -0
- package/src/manager/instructions.ts +281 -0
- package/src/manager/types.ts +95 -0
- package/src/manager/validation.ts +35 -0
- package/src/uploader/abi/thru/common/primitives/types.ts +2428 -0
- package/src/uploader/abi/thru/program/uploader/types.ts +3022 -0
- package/src/uploader/accounts.ts +38 -0
- package/src/uploader/constants.ts +14 -0
- package/src/uploader/derivation.ts +34 -0
- package/src/uploader/errors.ts +25 -0
- package/src/uploader/index.test.ts +107 -0
- package/src/uploader/index.ts +34 -0
- package/src/uploader/instructions.ts +163 -0
- package/src/uploader/types.ts +67 -0
- package/src/utils/helpers.ts +121 -0
- package/tsup.config.ts +4 -0
- package/dist/chunk-P5OABVJI.js +0 -28
- package/dist/chunk-P5OABVJI.js.map +0 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Account } from '@thru/sdk';
|
|
2
|
+
import { UploaderProgramMeta } from './abi/thru/program/uploader/types';
|
|
3
|
+
import {
|
|
4
|
+
accountData,
|
|
5
|
+
pubkeyAddress,
|
|
6
|
+
} from '../utils/helpers';
|
|
7
|
+
import {
|
|
8
|
+
UPLOADER_META_SIZE,
|
|
9
|
+
UPLOADER_STATE_FINALIZED,
|
|
10
|
+
UPLOADER_STATE_OPEN,
|
|
11
|
+
} from './constants';
|
|
12
|
+
import type { UploaderProgramMetaInfo } from './types';
|
|
13
|
+
|
|
14
|
+
export function parseUploaderProgramMeta(
|
|
15
|
+
account: Account | Uint8Array
|
|
16
|
+
): UploaderProgramMetaInfo {
|
|
17
|
+
const data = accountData(account, 'uploader metadata account');
|
|
18
|
+
if (data.length !== UPLOADER_META_SIZE) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
`uploader metadata account must be ${UPLOADER_META_SIZE} bytes, got ${data.length}`
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
const parsed = UploaderProgramMeta.from_array(data);
|
|
24
|
+
if (!parsed) throw new Error('uploader metadata account is malformed');
|
|
25
|
+
const state = parsed.get_state();
|
|
26
|
+
if (state !== UPLOADER_STATE_OPEN && state !== UPLOADER_STATE_FINALIZED) {
|
|
27
|
+
throw new Error(`uploader metadata account has invalid state ${state}`);
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
authorityAddress: pubkeyAddress(
|
|
31
|
+
Uint8Array.from(parsed.get_authority().get_bytes())
|
|
32
|
+
),
|
|
33
|
+
expectedHash: Uint8Array.from(
|
|
34
|
+
parsed.get_expected_account_hash().get_bytes()
|
|
35
|
+
),
|
|
36
|
+
state,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { encodeAddress } from "@thru/sdk/helpers";
|
|
2
|
+
import { systemProgramPubkey } from "../utils/helpers";
|
|
3
|
+
|
|
4
|
+
export const UPLOADER_PROGRAM_PUBKEY = systemProgramPubkey(0x02);
|
|
5
|
+
export const UPLOADER_PROGRAM_ADDRESS = encodeAddress(UPLOADER_PROGRAM_PUBKEY);
|
|
6
|
+
|
|
7
|
+
export const UPLOADER_META_SIZE = 65;
|
|
8
|
+
export const UPLOADER_STATE_OPEN = 0x01;
|
|
9
|
+
export const UPLOADER_STATE_FINALIZED = 0x02;
|
|
10
|
+
|
|
11
|
+
/* Safe uploader write sizes matching the CLI policy under TN_TXN_MTU. */
|
|
12
|
+
export const UPLOADER_DEFAULT_CHUNK_SIZE = 30_720;
|
|
13
|
+
export const UPLOADER_MIN_CHUNK_SIZE = 1_024;
|
|
14
|
+
export const UPLOADER_MAX_CHUNK_SIZE = 31_000;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { deriveProgramAddress } from '@thru/sdk';
|
|
2
|
+
import {
|
|
3
|
+
padSeed,
|
|
4
|
+
pubkeyBytes,
|
|
5
|
+
type ProgramSeed,
|
|
6
|
+
} from '../utils/helpers';
|
|
7
|
+
import { UPLOADER_PROGRAM_ADDRESS } from './constants';
|
|
8
|
+
import type { UploadAddresses } from './types';
|
|
9
|
+
|
|
10
|
+
export function deriveUploadAddresses(
|
|
11
|
+
seed: ProgramSeed,
|
|
12
|
+
uploaderProgramAddress: string | Uint8Array = UPLOADER_PROGRAM_ADDRESS
|
|
13
|
+
): UploadAddresses {
|
|
14
|
+
const uploader = pubkeyBytes(
|
|
15
|
+
uploaderProgramAddress,
|
|
16
|
+
'uploader program address'
|
|
17
|
+
);
|
|
18
|
+
const meta = deriveProgramAddress({
|
|
19
|
+
programAddress: uploader,
|
|
20
|
+
seed: padSeed(seed),
|
|
21
|
+
ephemeral: true,
|
|
22
|
+
});
|
|
23
|
+
const buffer = deriveProgramAddress({
|
|
24
|
+
programAddress: uploader,
|
|
25
|
+
seed: meta.bytes,
|
|
26
|
+
ephemeral: true,
|
|
27
|
+
});
|
|
28
|
+
return {
|
|
29
|
+
metaAccountAddress: meta.address,
|
|
30
|
+
metaAccountBytes: meta.bytes,
|
|
31
|
+
bufferAccountAddress: buffer.address,
|
|
32
|
+
bufferAccountBytes: buffer.bytes,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const UPLOADER_ERRORS = {
|
|
2
|
+
1: 'NOT_AUTHORIZED',
|
|
3
|
+
2: 'INVALID_ARGS',
|
|
4
|
+
3: 'META_ACCOUNT_MISSING',
|
|
5
|
+
4: 'BUFFER_ACCOUNT_MISSING',
|
|
6
|
+
5: 'ACCOUNT_NOT_OPEN',
|
|
7
|
+
6: 'HASH_MISMATCH',
|
|
8
|
+
7: 'WRITE_OVERFLOW',
|
|
9
|
+
8: 'WRITE_OUT_OF_BOUNDS',
|
|
10
|
+
9: 'INVALID_ACCOUNT_INDEX',
|
|
11
|
+
10: 'INVALID_META_SIZE',
|
|
12
|
+
11: 'ACCOUNT_RELATIONSHIP',
|
|
13
|
+
12: 'INVALID_INSTRUCTION',
|
|
14
|
+
} as const;
|
|
15
|
+
|
|
16
|
+
export type UploaderErrorName =
|
|
17
|
+
(typeof UPLOADER_ERRORS)[keyof typeof UPLOADER_ERRORS];
|
|
18
|
+
|
|
19
|
+
export function uploaderErrorName(code: number | bigint): UploaderErrorName | undefined {
|
|
20
|
+
const normalized = typeof code === 'bigint' ? Number(code) : code;
|
|
21
|
+
if (!Number.isSafeInteger(normalized) || normalized < 0) {
|
|
22
|
+
throw new Error('uploader error code must be a non-negative integer');
|
|
23
|
+
}
|
|
24
|
+
return UPLOADER_ERRORS[normalized as keyof typeof UPLOADER_ERRORS];
|
|
25
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
UPLOADER_DEFAULT_CHUNK_SIZE,
|
|
4
|
+
UPLOADER_MAX_CHUNK_SIZE,
|
|
5
|
+
UPLOADER_MIN_CHUNK_SIZE,
|
|
6
|
+
UPLOADER_STATE_FINALIZED,
|
|
7
|
+
buildUploaderInstructionBytes,
|
|
8
|
+
deriveUploadAddresses,
|
|
9
|
+
parseUploaderProgramMeta,
|
|
10
|
+
uploaderErrorName,
|
|
11
|
+
} from "./index";
|
|
12
|
+
|
|
13
|
+
describe("uploader SDK", () => {
|
|
14
|
+
it("exports the shared CLI-compatible chunk limits", () => {
|
|
15
|
+
expect(UPLOADER_DEFAULT_CHUNK_SIZE).toBe(30_720);
|
|
16
|
+
expect(UPLOADER_MIN_CHUNK_SIZE).toBe(1_024);
|
|
17
|
+
expect(UPLOADER_MAX_CHUNK_SIZE).toBe(31_000);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("matches the Rust derivation vector", () => {
|
|
21
|
+
expect(deriveUploadAddresses("nft_program")).toMatchObject({
|
|
22
|
+
metaAccountAddress: "taQEPklQRpE6rVlJ-XvUglKbjGIka1MTU9NDebMAWK4HBr",
|
|
23
|
+
bufferAccountAddress: "tavDEeyUOTr1GZKoYzACMI9omCnevh01E7XkXZMIv9pxcM",
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("encodes all uploader instructions exactly", () => {
|
|
28
|
+
const hash = new Uint8Array(32).fill(5);
|
|
29
|
+
expect(
|
|
30
|
+
Array.from(
|
|
31
|
+
buildUploaderInstructionBytes({
|
|
32
|
+
kind: "create",
|
|
33
|
+
bufferAccountIdx: 1,
|
|
34
|
+
metaAccountIdx: 2,
|
|
35
|
+
authorityAccountIdx: 3,
|
|
36
|
+
bufferSize: 4,
|
|
37
|
+
expectedHash: hash,
|
|
38
|
+
seed: Uint8Array.of(6, 7),
|
|
39
|
+
}),
|
|
40
|
+
),
|
|
41
|
+
).toEqual([
|
|
42
|
+
0,
|
|
43
|
+
0,
|
|
44
|
+
0,
|
|
45
|
+
0,
|
|
46
|
+
1,
|
|
47
|
+
0,
|
|
48
|
+
2,
|
|
49
|
+
0,
|
|
50
|
+
3,
|
|
51
|
+
0,
|
|
52
|
+
4,
|
|
53
|
+
0,
|
|
54
|
+
0,
|
|
55
|
+
0,
|
|
56
|
+
...hash,
|
|
57
|
+
2,
|
|
58
|
+
0,
|
|
59
|
+
0,
|
|
60
|
+
0,
|
|
61
|
+
6,
|
|
62
|
+
7,
|
|
63
|
+
]);
|
|
64
|
+
expect(
|
|
65
|
+
Array.from(
|
|
66
|
+
buildUploaderInstructionBytes({
|
|
67
|
+
kind: "write",
|
|
68
|
+
bufferAccountIdx: 1,
|
|
69
|
+
metaAccountIdx: 2,
|
|
70
|
+
offset: 8,
|
|
71
|
+
data: Uint8Array.of(9, 10),
|
|
72
|
+
}),
|
|
73
|
+
),
|
|
74
|
+
).toEqual([1, 0, 0, 0, 1, 0, 2, 0, 2, 0, 0, 0, 8, 0, 0, 0, 9, 10]);
|
|
75
|
+
expect(
|
|
76
|
+
Array.from(
|
|
77
|
+
buildUploaderInstructionBytes({
|
|
78
|
+
kind: "destroy",
|
|
79
|
+
bufferAccountIdx: 1,
|
|
80
|
+
metaAccountIdx: 2,
|
|
81
|
+
}),
|
|
82
|
+
),
|
|
83
|
+
).toEqual([2, 0, 0, 0, 1, 0, 2, 0]);
|
|
84
|
+
expect(
|
|
85
|
+
Array.from(
|
|
86
|
+
buildUploaderInstructionBytes({
|
|
87
|
+
kind: "finalize",
|
|
88
|
+
bufferAccountIdx: 1,
|
|
89
|
+
metaAccountIdx: 2,
|
|
90
|
+
expectedHash: hash,
|
|
91
|
+
}),
|
|
92
|
+
),
|
|
93
|
+
).toEqual([3, 0, 0, 0, 1, 0, 2, 0, ...hash]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("parses metadata and maps errors", () => {
|
|
97
|
+
const data = new Uint8Array(65);
|
|
98
|
+
data.fill(1, 0, 32);
|
|
99
|
+
data.fill(2, 32, 64);
|
|
100
|
+
data[64] = UPLOADER_STATE_FINALIZED;
|
|
101
|
+
expect(parseUploaderProgramMeta(data)).toMatchObject({
|
|
102
|
+
expectedHash: new Uint8Array(32).fill(2),
|
|
103
|
+
state: UPLOADER_STATE_FINALIZED,
|
|
104
|
+
});
|
|
105
|
+
expect(uploaderErrorName(6n)).toBe("HASH_MISMATCH");
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export {
|
|
2
|
+
UPLOADER_DEFAULT_CHUNK_SIZE,
|
|
3
|
+
UPLOADER_MAX_CHUNK_SIZE,
|
|
4
|
+
UPLOADER_META_SIZE,
|
|
5
|
+
UPLOADER_MIN_CHUNK_SIZE,
|
|
6
|
+
UPLOADER_PROGRAM_ADDRESS,
|
|
7
|
+
UPLOADER_PROGRAM_PUBKEY,
|
|
8
|
+
UPLOADER_STATE_FINALIZED,
|
|
9
|
+
UPLOADER_STATE_OPEN,
|
|
10
|
+
} from "./constants";
|
|
11
|
+
export { deriveUploadAddresses } from "./derivation";
|
|
12
|
+
export { parseUploaderProgramMeta } from "./accounts";
|
|
13
|
+
export {
|
|
14
|
+
buildUploaderInstructionBytes,
|
|
15
|
+
createDestroyUploadInstruction,
|
|
16
|
+
createFinalizeUploadInstruction,
|
|
17
|
+
createUploadBufferInstruction,
|
|
18
|
+
createWriteUploadInstruction,
|
|
19
|
+
} from "./instructions";
|
|
20
|
+
export { UPLOADER_ERRORS, uploaderErrorName } from "./errors";
|
|
21
|
+
export type { UploaderErrorName } from "./errors";
|
|
22
|
+
export type {
|
|
23
|
+
AccountLookupContext,
|
|
24
|
+
CreateUploadBufferInstructionArgs,
|
|
25
|
+
DestroyUploadInstructionArgs,
|
|
26
|
+
FinalizeUploadInstructionArgs,
|
|
27
|
+
InstructionData,
|
|
28
|
+
ProgramSeed,
|
|
29
|
+
UploadAddresses,
|
|
30
|
+
UploaderInstructionArgs,
|
|
31
|
+
UploaderProgramMetaInfo,
|
|
32
|
+
WriteUploadInstructionArgs,
|
|
33
|
+
} from "./types";
|
|
34
|
+
export * from "./abi/thru/program/uploader/types";
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CreateArgsBuilder,
|
|
3
|
+
DestroyArgsBuilder,
|
|
4
|
+
FinalizeArgsBuilder,
|
|
5
|
+
UploaderInstruction,
|
|
6
|
+
UploaderInstructionBuilder,
|
|
7
|
+
WriteArgsBuilder,
|
|
8
|
+
} from './abi/thru/program/uploader/types';
|
|
9
|
+
import {
|
|
10
|
+
accountIndex,
|
|
11
|
+
assertU16,
|
|
12
|
+
assertU32,
|
|
13
|
+
exactBytes,
|
|
14
|
+
normalizeSeed,
|
|
15
|
+
type AccountLookupContext,
|
|
16
|
+
type InstructionData,
|
|
17
|
+
} from '../utils/helpers';
|
|
18
|
+
import type {
|
|
19
|
+
CreateUploadBufferInstructionArgs,
|
|
20
|
+
DestroyUploadInstructionArgs,
|
|
21
|
+
FinalizeUploadInstructionArgs,
|
|
22
|
+
UploaderInstructionArgs,
|
|
23
|
+
WriteUploadInstructionArgs,
|
|
24
|
+
} from './types';
|
|
25
|
+
|
|
26
|
+
type UploaderVariant =
|
|
27
|
+
(typeof UploaderInstruction.payloadVariantDescriptors)[number]['name'];
|
|
28
|
+
|
|
29
|
+
function envelope(
|
|
30
|
+
variant: UploaderVariant,
|
|
31
|
+
payload: Uint8Array
|
|
32
|
+
): Uint8Array {
|
|
33
|
+
return new UploaderInstructionBuilder()
|
|
34
|
+
.payload()
|
|
35
|
+
.select(variant)
|
|
36
|
+
.writePayload(payload)
|
|
37
|
+
.finish()
|
|
38
|
+
.build();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function validateIndices(args: {
|
|
42
|
+
bufferAccountIdx: number;
|
|
43
|
+
metaAccountIdx: number;
|
|
44
|
+
}): void {
|
|
45
|
+
assertU16(args.bufferAccountIdx, 'bufferAccountIdx');
|
|
46
|
+
assertU16(args.metaAccountIdx, 'metaAccountIdx');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function buildUploaderInstructionBytes(
|
|
50
|
+
args: UploaderInstructionArgs
|
|
51
|
+
): Uint8Array {
|
|
52
|
+
validateIndices(args);
|
|
53
|
+
switch (args.kind) {
|
|
54
|
+
case 'create': {
|
|
55
|
+
assertU16(args.authorityAccountIdx, 'authorityAccountIdx');
|
|
56
|
+
assertU32(args.bufferSize, 'bufferSize');
|
|
57
|
+
const builder = new CreateArgsBuilder()
|
|
58
|
+
.set_buffer_account_idx(args.bufferAccountIdx)
|
|
59
|
+
.set_meta_account_idx(args.metaAccountIdx)
|
|
60
|
+
.set_authority_account_idx(args.authorityAccountIdx)
|
|
61
|
+
.set_buffer_account_sz(args.bufferSize)
|
|
62
|
+
.set_expected_account_hash(
|
|
63
|
+
exactBytes(args.expectedHash, 32, 'expectedHash')
|
|
64
|
+
);
|
|
65
|
+
builder.seed().write(normalizeSeed(args.seed)).finish();
|
|
66
|
+
return envelope('create', builder.build());
|
|
67
|
+
}
|
|
68
|
+
case 'write': {
|
|
69
|
+
assertU32(args.offset, 'offset');
|
|
70
|
+
assertU32(args.data.length, 'data length');
|
|
71
|
+
const builder = new WriteArgsBuilder()
|
|
72
|
+
.set_buffer_account_idx(args.bufferAccountIdx)
|
|
73
|
+
.set_meta_account_idx(args.metaAccountIdx)
|
|
74
|
+
.set_data_offset(args.offset);
|
|
75
|
+
builder.data().write(args.data).finish();
|
|
76
|
+
return envelope('write', builder.build());
|
|
77
|
+
}
|
|
78
|
+
case 'destroy':
|
|
79
|
+
return envelope(
|
|
80
|
+
'destroy',
|
|
81
|
+
new DestroyArgsBuilder()
|
|
82
|
+
.set_buffer_account_idx(args.bufferAccountIdx)
|
|
83
|
+
.set_meta_account_idx(args.metaAccountIdx)
|
|
84
|
+
.build()
|
|
85
|
+
);
|
|
86
|
+
case 'finalize':
|
|
87
|
+
return envelope(
|
|
88
|
+
'finalize',
|
|
89
|
+
new FinalizeArgsBuilder()
|
|
90
|
+
.set_buffer_account_idx(args.bufferAccountIdx)
|
|
91
|
+
.set_meta_account_idx(args.metaAccountIdx)
|
|
92
|
+
.set_expected_account_hash(
|
|
93
|
+
exactBytes(args.expectedHash, 32, 'expectedHash')
|
|
94
|
+
)
|
|
95
|
+
.build()
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function indices(
|
|
101
|
+
context: AccountLookupContext,
|
|
102
|
+
args: { bufferAccount: Uint8Array; metaAccount: Uint8Array }
|
|
103
|
+
): { bufferAccountIdx: number; metaAccountIdx: number } {
|
|
104
|
+
return {
|
|
105
|
+
bufferAccountIdx: accountIndex(
|
|
106
|
+
context,
|
|
107
|
+
args.bufferAccount,
|
|
108
|
+
'bufferAccount'
|
|
109
|
+
),
|
|
110
|
+
metaAccountIdx: accountIndex(context, args.metaAccount, 'metaAccount'),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function createUploadBufferInstruction(
|
|
115
|
+
args: CreateUploadBufferInstructionArgs
|
|
116
|
+
): InstructionData {
|
|
117
|
+
return async (context) =>
|
|
118
|
+
buildUploaderInstructionBytes({
|
|
119
|
+
kind: 'create',
|
|
120
|
+
...indices(context, args),
|
|
121
|
+
authorityAccountIdx: accountIndex(
|
|
122
|
+
context,
|
|
123
|
+
args.authorityAccount,
|
|
124
|
+
'authorityAccount'
|
|
125
|
+
),
|
|
126
|
+
bufferSize: args.bufferSize,
|
|
127
|
+
expectedHash: args.expectedHash,
|
|
128
|
+
seed: args.seed,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function createWriteUploadInstruction(
|
|
133
|
+
args: WriteUploadInstructionArgs
|
|
134
|
+
): InstructionData {
|
|
135
|
+
return async (context) =>
|
|
136
|
+
buildUploaderInstructionBytes({
|
|
137
|
+
kind: 'write',
|
|
138
|
+
...indices(context, args),
|
|
139
|
+
data: args.data,
|
|
140
|
+
offset: args.offset,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function createDestroyUploadInstruction(
|
|
145
|
+
args: DestroyUploadInstructionArgs
|
|
146
|
+
): InstructionData {
|
|
147
|
+
return async (context) =>
|
|
148
|
+
buildUploaderInstructionBytes({
|
|
149
|
+
kind: 'destroy',
|
|
150
|
+
...indices(context, args),
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function createFinalizeUploadInstruction(
|
|
155
|
+
args: FinalizeUploadInstructionArgs
|
|
156
|
+
): InstructionData {
|
|
157
|
+
return async (context) =>
|
|
158
|
+
buildUploaderInstructionBytes({
|
|
159
|
+
kind: 'finalize',
|
|
160
|
+
...indices(context, args),
|
|
161
|
+
expectedHash: args.expectedHash,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AccountLookupContext,
|
|
3
|
+
InstructionData,
|
|
4
|
+
ProgramSeed,
|
|
5
|
+
} from '../utils/helpers';
|
|
6
|
+
|
|
7
|
+
export type { AccountLookupContext, InstructionData, ProgramSeed };
|
|
8
|
+
|
|
9
|
+
export interface UploadAddresses {
|
|
10
|
+
metaAccountAddress: string;
|
|
11
|
+
metaAccountBytes: Uint8Array;
|
|
12
|
+
bufferAccountAddress: string;
|
|
13
|
+
bufferAccountBytes: Uint8Array;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface UploaderProgramMetaInfo {
|
|
17
|
+
authorityAddress: string;
|
|
18
|
+
expectedHash: Uint8Array;
|
|
19
|
+
state: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface UploadIndices {
|
|
23
|
+
bufferAccountIdx: number;
|
|
24
|
+
metaAccountIdx: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type UploaderInstructionArgs =
|
|
28
|
+
| (UploadIndices & {
|
|
29
|
+
kind: 'create';
|
|
30
|
+
authorityAccountIdx: number;
|
|
31
|
+
bufferSize: number;
|
|
32
|
+
expectedHash: Uint8Array;
|
|
33
|
+
seed: ProgramSeed;
|
|
34
|
+
})
|
|
35
|
+
| (UploadIndices & {
|
|
36
|
+
kind: 'write';
|
|
37
|
+
data: Uint8Array;
|
|
38
|
+
offset: number;
|
|
39
|
+
})
|
|
40
|
+
| (UploadIndices & { kind: 'destroy' })
|
|
41
|
+
| (UploadIndices & {
|
|
42
|
+
kind: 'finalize';
|
|
43
|
+
expectedHash: Uint8Array;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
interface UploadAccounts {
|
|
47
|
+
bufferAccount: Uint8Array;
|
|
48
|
+
metaAccount: Uint8Array;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface CreateUploadBufferInstructionArgs extends UploadAccounts {
|
|
52
|
+
authorityAccount: Uint8Array;
|
|
53
|
+
bufferSize: number;
|
|
54
|
+
expectedHash: Uint8Array;
|
|
55
|
+
seed: ProgramSeed;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface WriteUploadInstructionArgs extends UploadAccounts {
|
|
59
|
+
data: Uint8Array;
|
|
60
|
+
offset: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type DestroyUploadInstructionArgs = UploadAccounts;
|
|
64
|
+
|
|
65
|
+
export interface FinalizeUploadInstructionArgs extends UploadAccounts {
|
|
66
|
+
expectedHash: Uint8Array;
|
|
67
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { Pubkey, deriveAddress, type Account } from '@thru/sdk';
|
|
2
|
+
import { encodeAddress } from '@thru/sdk/helpers';
|
|
3
|
+
|
|
4
|
+
export const PUBKEY_LENGTH = 32;
|
|
5
|
+
export const HASH_LENGTH = 32;
|
|
6
|
+
|
|
7
|
+
const TEXT_ENCODER = new TextEncoder();
|
|
8
|
+
|
|
9
|
+
export type ProgramSeed = string | Uint8Array;
|
|
10
|
+
|
|
11
|
+
export interface AccountLookupContext {
|
|
12
|
+
getAccountIndex: (pubkey: Uint8Array) => number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type InstructionData = (
|
|
16
|
+
context: AccountLookupContext
|
|
17
|
+
) => Promise<Uint8Array>;
|
|
18
|
+
|
|
19
|
+
export function systemProgramPubkey(lastByte: number): Uint8Array {
|
|
20
|
+
assertInteger(lastByte, 0, 0xff, 'lastByte');
|
|
21
|
+
const bytes = new Uint8Array(PUBKEY_LENGTH);
|
|
22
|
+
bytes[PUBKEY_LENGTH - 1] = lastByte;
|
|
23
|
+
return bytes;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function normalizeSeed(seed: ProgramSeed): Uint8Array {
|
|
27
|
+
const bytes =
|
|
28
|
+
typeof seed === 'string' ? TEXT_ENCODER.encode(seed) : new Uint8Array(seed);
|
|
29
|
+
if (bytes.length === 0 || bytes.length > PUBKEY_LENGTH) {
|
|
30
|
+
throw new Error(`seed must be 1-${PUBKEY_LENGTH} bytes, got ${bytes.length}`);
|
|
31
|
+
}
|
|
32
|
+
return bytes;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function padSeed(seed: ProgramSeed): Uint8Array {
|
|
36
|
+
const normalized = normalizeSeed(seed);
|
|
37
|
+
const padded = new Uint8Array(PUBKEY_LENGTH);
|
|
38
|
+
padded.set(normalized);
|
|
39
|
+
return padded;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function exactBytes(
|
|
43
|
+
value: Uint8Array,
|
|
44
|
+
length: number,
|
|
45
|
+
label: string
|
|
46
|
+
): Uint8Array {
|
|
47
|
+
if (!(value instanceof Uint8Array) || value.length !== length) {
|
|
48
|
+
throw new Error(`${label} must be ${length} bytes`);
|
|
49
|
+
}
|
|
50
|
+
return new Uint8Array(value);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function pubkeyBytes(
|
|
54
|
+
value: string | Uint8Array,
|
|
55
|
+
label = 'public key'
|
|
56
|
+
): Uint8Array {
|
|
57
|
+
try {
|
|
58
|
+
return Pubkey.from(value).toBytes();
|
|
59
|
+
} catch {
|
|
60
|
+
throw new Error(`${label} is invalid`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function pubkeyAddress(value: Uint8Array): string {
|
|
65
|
+
return encodeAddress(exactBytes(value, PUBKEY_LENGTH, 'public key'));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function accountData(
|
|
69
|
+
account: Account | Uint8Array,
|
|
70
|
+
label: string
|
|
71
|
+
): Uint8Array {
|
|
72
|
+
if (account instanceof Uint8Array) return account;
|
|
73
|
+
const data = account.data?.data;
|
|
74
|
+
if (!data) throw new Error(`${label} data is missing`);
|
|
75
|
+
return data;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function accountIndex(
|
|
79
|
+
context: AccountLookupContext,
|
|
80
|
+
account: Uint8Array,
|
|
81
|
+
label: string
|
|
82
|
+
): number {
|
|
83
|
+
const index = context.getAccountIndex(exactBytes(account, PUBKEY_LENGTH, label));
|
|
84
|
+
assertU16(index, `${label} index`);
|
|
85
|
+
return index;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function assertU16(value: number, label: string): void {
|
|
89
|
+
assertInteger(value, 0, 0xffff, label);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function assertU32(value: number, label: string): void {
|
|
93
|
+
assertInteger(value, 0, 0xffffffff, label);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function concatBytes(...parts: Uint8Array[]): Uint8Array {
|
|
97
|
+
const output = new Uint8Array(
|
|
98
|
+
parts.reduce((total, part) => total + part.length, 0)
|
|
99
|
+
);
|
|
100
|
+
let offset = 0;
|
|
101
|
+
for (const part of parts) {
|
|
102
|
+
output.set(part, offset);
|
|
103
|
+
offset += part.length;
|
|
104
|
+
}
|
|
105
|
+
return output;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function hashBytes(...parts: Uint8Array[]): Uint8Array {
|
|
109
|
+
return deriveAddress([concatBytes(...parts)]).bytes;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function assertInteger(
|
|
113
|
+
value: number,
|
|
114
|
+
minimum: number,
|
|
115
|
+
maximum: number,
|
|
116
|
+
label: string
|
|
117
|
+
): void {
|
|
118
|
+
if (!Number.isInteger(value) || value < minimum || value > maximum) {
|
|
119
|
+
throw new Error(`${label} must be an integer from ${minimum} to ${maximum}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
package/tsup.config.ts
CHANGED
|
@@ -8,6 +8,10 @@ export default defineConfig({
|
|
|
8
8
|
"amm/index": "src/amm/index.ts",
|
|
9
9
|
"clob/index": "src/clob/index.ts",
|
|
10
10
|
"oracle/index": "src/oracle/index.ts",
|
|
11
|
+
"manager/index": "src/manager/index.ts",
|
|
12
|
+
"abi-manager/index": "src/abi-manager/index.ts",
|
|
13
|
+
"uploader/index": "src/uploader/index.ts",
|
|
14
|
+
"deploy/index": "src/deploy/index.ts",
|
|
11
15
|
},
|
|
12
16
|
format: ["cjs", "esm"],
|
|
13
17
|
dts: true,
|
package/dist/chunk-P5OABVJI.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
// src/helpers/bytes.ts
|
|
2
|
-
function bytesEqual(a, b) {
|
|
3
|
-
if (a.length !== b.length) return false;
|
|
4
|
-
for (let i = 0; i < a.length; i++) {
|
|
5
|
-
if (a[i] !== b[i]) return false;
|
|
6
|
-
}
|
|
7
|
-
return true;
|
|
8
|
-
}
|
|
9
|
-
function compareBytes(a, b) {
|
|
10
|
-
const len = Math.min(a.length, b.length);
|
|
11
|
-
for (let i = 0; i < len; i++) {
|
|
12
|
-
if (a[i] !== b[i]) return a[i] - b[i];
|
|
13
|
-
}
|
|
14
|
-
return a.length - b.length;
|
|
15
|
-
}
|
|
16
|
-
function uniqueAccounts(accounts) {
|
|
17
|
-
const unique = [];
|
|
18
|
-
for (const account of accounts) {
|
|
19
|
-
if (!unique.some((candidate) => bytesEqual(candidate, account))) {
|
|
20
|
-
unique.push(account);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return unique;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export { bytesEqual, compareBytes, uniqueAccounts };
|
|
27
|
-
//# sourceMappingURL=chunk-P5OABVJI.js.map
|
|
28
|
-
//# sourceMappingURL=chunk-P5OABVJI.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/helpers/bytes.ts"],"names":[],"mappings":";AAAO,SAAS,UAAA,CAAW,GAAe,CAAA,EAAwB;AAChE,EAAA,IAAI,CAAA,CAAE,MAAA,KAAW,CAAA,CAAE,MAAA,EAAQ,OAAO,KAAA;AAClC,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,CAAA,CAAE,QAAQ,CAAA,EAAA,EAAK;AACjC,IAAA,IAAI,EAAE,CAAC,CAAA,KAAM,CAAA,CAAE,CAAC,GAAG,OAAO,KAAA;AAAA,EAC5B;AACA,EAAA,OAAO,IAAA;AACT;AAEO,SAAS,YAAA,CAAa,GAAe,CAAA,EAAuB;AACjE,EAAA,MAAM,MAAM,IAAA,CAAK,GAAA,CAAI,CAAA,CAAE,MAAA,EAAQ,EAAE,MAAM,CAAA;AACvC,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,GAAA,EAAK,CAAA,EAAA,EAAK;AAC5B,IAAA,IAAI,CAAA,CAAE,CAAC,CAAA,KAAM,CAAA,CAAE,CAAC,CAAA,EAAG,OAAO,CAAA,CAAE,CAAC,CAAA,GAAI,CAAA,CAAE,CAAC,CAAA;AAAA,EACtC;AACA,EAAA,OAAO,CAAA,CAAE,SAAS,CAAA,CAAE,MAAA;AACtB;AAEO,SAAS,eAAe,QAAA,EAAsC;AACnE,EAAA,MAAM,SAAuB,EAAC;AAC9B,EAAA,KAAA,MAAW,WAAW,QAAA,EAAU;AAC9B,IAAA,IAAI,CAAC,OAAO,IAAA,CAAK,CAAC,cAAc,UAAA,CAAW,SAAA,EAAW,OAAO,CAAC,CAAA,EAAG;AAC/D,MAAA,MAAA,CAAO,KAAK,OAAO,CAAA;AAAA,IACrB;AAAA,EACF;AACA,EAAA,OAAO,MAAA;AACT","file":"chunk-P5OABVJI.js","sourcesContent":["export function bytesEqual(a: Uint8Array, b: Uint8Array): boolean {\n if (a.length !== b.length) return false;\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false;\n }\n return true;\n}\n\nexport function compareBytes(a: Uint8Array, b: Uint8Array): number {\n const len = Math.min(a.length, b.length);\n for (let i = 0; i < len; i++) {\n if (a[i] !== b[i]) return a[i] - b[i];\n }\n return a.length - b.length;\n}\n\nexport function uniqueAccounts(accounts: Uint8Array[]): Uint8Array[] {\n const unique: Uint8Array[] = [];\n for (const account of accounts) {\n if (!unique.some((candidate) => bytesEqual(candidate, account))) {\n unique.push(account);\n }\n }\n return unique;\n}\n"]}
|