@5ive-tech/sdk 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/README.md +279 -0
  2. package/dist/FiveSDK.d.ts +336 -0
  3. package/dist/FiveSDK.js +395 -0
  4. package/dist/accounts/index.d.ts +254 -0
  5. package/dist/accounts/index.js +543 -0
  6. package/dist/assets/vm/dummy.file +0 -0
  7. package/dist/assets/vm/five_vm_wasm.d.ts +762 -0
  8. package/dist/assets/vm/five_vm_wasm.js +3754 -0
  9. package/dist/assets/vm/five_vm_wasm_bg.js +3307 -0
  10. package/dist/assets/vm/five_vm_wasm_bg.wasm +0 -0
  11. package/dist/assets/vm/five_vm_wasm_bg.wasm.d.ts +247 -0
  12. package/dist/assets/vm/package.json +11 -0
  13. package/dist/bin/gen-types.d.ts +2 -0
  14. package/dist/bin/gen-types.js +35 -0
  15. package/dist/compiler/BytecodeCompiler.d.ts +83 -0
  16. package/dist/compiler/BytecodeCompiler.js +379 -0
  17. package/dist/config/ConfigManager.d.ts +13 -0
  18. package/dist/config/ConfigManager.js +27 -0
  19. package/dist/config/ProgramIdResolver.d.ts +62 -0
  20. package/dist/config/ProgramIdResolver.js +104 -0
  21. package/dist/crypto/index.d.ts +211 -0
  22. package/dist/crypto/index.js +451 -0
  23. package/dist/encoding/ParameterEncoder.d.ts +31 -0
  24. package/dist/encoding/ParameterEncoder.js +278 -0
  25. package/dist/index.d.ts +21 -0
  26. package/dist/index.js +28 -0
  27. package/dist/lib/bytecode-encoder.d.ts +62 -0
  28. package/dist/lib/bytecode-encoder.js +281 -0
  29. package/dist/logging/index.d.ts +9 -0
  30. package/dist/logging/index.js +10 -0
  31. package/dist/metadata/index.d.ts +213 -0
  32. package/dist/metadata/index.js +296 -0
  33. package/dist/modules/accounts.d.ts +60 -0
  34. package/dist/modules/accounts.js +275 -0
  35. package/dist/modules/deploy.d.ts +90 -0
  36. package/dist/modules/deploy.js +1118 -0
  37. package/dist/modules/execute.d.ts +90 -0
  38. package/dist/modules/execute.js +649 -0
  39. package/dist/modules/fees.d.ts +14 -0
  40. package/dist/modules/fees.js +112 -0
  41. package/dist/modules/namespaces.d.ts +39 -0
  42. package/dist/modules/namespaces.js +190 -0
  43. package/dist/modules/state-diff.d.ts +35 -0
  44. package/dist/modules/state-diff.js +342 -0
  45. package/dist/modules/vm-state.d.ts +7 -0
  46. package/dist/modules/vm-state.js +44 -0
  47. package/dist/program/AccountResolver.d.ts +67 -0
  48. package/dist/program/AccountResolver.js +134 -0
  49. package/dist/program/BorshSchemaGenerator.d.ts +8 -0
  50. package/dist/program/BorshSchemaGenerator.js +57 -0
  51. package/dist/program/FiveProgram.d.ts +144 -0
  52. package/dist/program/FiveProgram.js +282 -0
  53. package/dist/program/FunctionBuilder.d.ts +114 -0
  54. package/dist/program/FunctionBuilder.js +347 -0
  55. package/dist/program/ProgramAccount.d.ts +38 -0
  56. package/dist/program/ProgramAccount.js +170 -0
  57. package/dist/program/TypeGenerator.d.ts +90 -0
  58. package/dist/program/TypeGenerator.js +195 -0
  59. package/dist/program/index.d.ts +24 -0
  60. package/dist/program/index.js +21 -0
  61. package/dist/project/config.d.ts +5 -0
  62. package/dist/project/config.js +33 -0
  63. package/dist/project/toml.d.ts +6 -0
  64. package/dist/project/toml.js +43 -0
  65. package/dist/project/workspace.d.ts +160 -0
  66. package/dist/project/workspace.js +73 -0
  67. package/dist/testing/AccountMetaGenerator.d.ts +121 -0
  68. package/dist/testing/AccountMetaGenerator.js +261 -0
  69. package/dist/testing/AccountTestFixture.d.ts +211 -0
  70. package/dist/testing/AccountTestFixture.js +530 -0
  71. package/dist/testing/OnChainAccountManager.d.ts +81 -0
  72. package/dist/testing/OnChainAccountManager.js +260 -0
  73. package/dist/testing/StateSerializer.d.ts +65 -0
  74. package/dist/testing/StateSerializer.js +330 -0
  75. package/dist/testing/TestDiscovery.d.ts +79 -0
  76. package/dist/testing/TestDiscovery.js +274 -0
  77. package/dist/testing/TestRunner.d.ts +117 -0
  78. package/dist/testing/TestRunner.js +346 -0
  79. package/dist/testing/index.d.ts +14 -0
  80. package/dist/testing/index.js +13 -0
  81. package/dist/types.d.ts +356 -0
  82. package/dist/types.js +32 -0
  83. package/dist/utils/abi.d.ts +31 -0
  84. package/dist/utils/abi.js +92 -0
  85. package/dist/utils/transaction.d.ts +5 -0
  86. package/dist/utils/transaction.js +48 -0
  87. package/dist/validation/InputValidator.d.ts +142 -0
  88. package/dist/validation/InputValidator.js +332 -0
  89. package/dist/validation/index.d.ts +4 -0
  90. package/dist/validation/index.js +4 -0
  91. package/dist/wasm/compiler/AbiLogic.d.ts +4 -0
  92. package/dist/wasm/compiler/AbiLogic.js +37 -0
  93. package/dist/wasm/compiler/AnalysisLogic.d.ts +6 -0
  94. package/dist/wasm/compiler/AnalysisLogic.js +61 -0
  95. package/dist/wasm/compiler/CompilationLogic.d.ts +10 -0
  96. package/dist/wasm/compiler/CompilationLogic.js +431 -0
  97. package/dist/wasm/compiler/FiveCompiler.d.ts +48 -0
  98. package/dist/wasm/compiler/FiveCompiler.js +183 -0
  99. package/dist/wasm/compiler/InfoLogic.d.ts +6 -0
  100. package/dist/wasm/compiler/InfoLogic.js +24 -0
  101. package/dist/wasm/compiler/OptimizationLogic.d.ts +2 -0
  102. package/dist/wasm/compiler/OptimizationLogic.js +13 -0
  103. package/dist/wasm/compiler/ValidationLogic.d.ts +7 -0
  104. package/dist/wasm/compiler/ValidationLogic.js +26 -0
  105. package/dist/wasm/compiler/index.d.ts +2 -0
  106. package/dist/wasm/compiler/index.js +2 -0
  107. package/dist/wasm/compiler/types.d.ts +8 -0
  108. package/dist/wasm/compiler/types.js +1 -0
  109. package/dist/wasm/compiler/utils.d.ts +8 -0
  110. package/dist/wasm/compiler/utils.js +75 -0
  111. package/dist/wasm/index.d.ts +9 -0
  112. package/dist/wasm/index.js +12 -0
  113. package/dist/wasm/instance.d.ts +1 -0
  114. package/dist/wasm/instance.js +26 -0
  115. package/dist/wasm/loader.d.ts +7 -0
  116. package/dist/wasm/loader.js +112 -0
  117. package/dist/wasm/vm.d.ts +33 -0
  118. package/dist/wasm/vm.js +250 -0
  119. package/package.json +59 -0
@@ -0,0 +1,112 @@
1
+ import { RentCalculator } from "../crypto/index.js";
2
+ import { getVMState } from "./vm-state.js";
3
+ export async function getFees(connection, fiveVMProgramId) {
4
+ try {
5
+ const state = await getVMState(connection, fiveVMProgramId);
6
+ return {
7
+ deployFeeBps: state.deployFeeBps,
8
+ executeFeeBps: state.executeFeeBps,
9
+ adminAccount: state.authority
10
+ };
11
+ }
12
+ catch (error) {
13
+ return {
14
+ deployFeeBps: 0,
15
+ executeFeeBps: 0,
16
+ adminAccount: null
17
+ };
18
+ }
19
+ }
20
+ export async function calculateDeployFee(bytecodeSize, connection, fiveVMProgramId) {
21
+ try {
22
+ const accountSize = 64 + bytecodeSize;
23
+ const rentLamports = await RentCalculator.calculateRentExemption(accountSize);
24
+ const vmState = await getVMState(connection, fiveVMProgramId);
25
+ const deployFeeBps = vmState.deployFeeBps;
26
+ const feeLamports = Math.floor((rentLamports * deployFeeBps) / 10000);
27
+ return {
28
+ feeBps: deployFeeBps,
29
+ basisLamports: rentLamports,
30
+ feeLamports,
31
+ totalEstimatedCost: rentLamports + feeLamports,
32
+ costBreakdown: {
33
+ basis: RentCalculator.formatSOL(rentLamports),
34
+ fee: RentCalculator.formatSOL(feeLamports),
35
+ total: RentCalculator.formatSOL(rentLamports + feeLamports),
36
+ },
37
+ };
38
+ }
39
+ catch (error) {
40
+ const accountSize = 64 + bytecodeSize;
41
+ const rentLamports = await RentCalculator.calculateRentExemption(accountSize);
42
+ return {
43
+ feeBps: 0,
44
+ basisLamports: rentLamports,
45
+ feeLamports: 0,
46
+ totalEstimatedCost: rentLamports,
47
+ costBreakdown: {
48
+ basis: RentCalculator.formatSOL(rentLamports),
49
+ fee: "0 SOL",
50
+ total: RentCalculator.formatSOL(rentLamports),
51
+ },
52
+ };
53
+ }
54
+ }
55
+ export async function calculateExecuteFee(connection, fiveVMProgramId) {
56
+ const STANDARD_TX_FEE = 5000;
57
+ try {
58
+ const vmState = await getVMState(connection, fiveVMProgramId);
59
+ const executeFeeBps = vmState.executeFeeBps;
60
+ const feeLamports = Math.floor((STANDARD_TX_FEE * executeFeeBps) / 10000);
61
+ return {
62
+ feeBps: executeFeeBps,
63
+ basisLamports: STANDARD_TX_FEE,
64
+ feeLamports,
65
+ totalEstimatedCost: STANDARD_TX_FEE + feeLamports,
66
+ costBreakdown: {
67
+ basis: RentCalculator.formatSOL(STANDARD_TX_FEE),
68
+ fee: RentCalculator.formatSOL(feeLamports),
69
+ total: RentCalculator.formatSOL(STANDARD_TX_FEE + feeLamports),
70
+ },
71
+ };
72
+ }
73
+ catch (error) {
74
+ return {
75
+ feeBps: 0,
76
+ basisLamports: 5000,
77
+ feeLamports: 0,
78
+ totalEstimatedCost: 5000,
79
+ costBreakdown: {
80
+ basis: "0.000005 SOL",
81
+ fee: "0 SOL",
82
+ total: "0.000005 SOL",
83
+ },
84
+ };
85
+ }
86
+ }
87
+ export async function getFeeInformation(bytecodeSize, connection, fiveVMProgramId) {
88
+ try {
89
+ const [deployFee, executeFee, vmState] = await Promise.all([
90
+ calculateDeployFee(bytecodeSize, connection, fiveVMProgramId),
91
+ calculateExecuteFee(connection, fiveVMProgramId),
92
+ getVMState(connection, fiveVMProgramId),
93
+ ]);
94
+ const feesEnabled = vmState.deployFeeBps > 0 || vmState.executeFeeBps > 0;
95
+ return {
96
+ deploy: deployFee,
97
+ execute: executeFee,
98
+ adminAccount: vmState.authority,
99
+ feesEnabled,
100
+ };
101
+ }
102
+ catch (error) {
103
+ const deployFee = await calculateDeployFee(bytecodeSize, connection, fiveVMProgramId);
104
+ const executeFee = await calculateExecuteFee(connection, fiveVMProgramId);
105
+ return {
106
+ deploy: deployFee,
107
+ execute: executeFee,
108
+ adminAccount: null,
109
+ feesEnabled: false,
110
+ };
111
+ }
112
+ }
@@ -0,0 +1,39 @@
1
+ export interface ScopedNamespace {
2
+ symbol: "!" | "@" | "#" | "$" | "%";
3
+ domain: string;
4
+ subprogram?: string;
5
+ canonical: string;
6
+ }
7
+ export declare function canonicalizeScopedNamespace(input: string): ScopedNamespace;
8
+ export declare function namespaceSeedBytes(namespaceValue: string): Uint8Array;
9
+ export declare function resolveNamespaceFromLockfile(namespaceValue: string, lockfile: any): string | undefined;
10
+ export interface NamespaceDerivedAccounts {
11
+ config: string;
12
+ tld: string;
13
+ binding?: string;
14
+ }
15
+ export declare function deriveNamespaceAccounts(namespaceValue: string, fiveVMProgramId: string): Promise<NamespaceDerivedAccounts>;
16
+ interface NamespaceOnChainOptions {
17
+ managerScriptAccount: string;
18
+ connection: any;
19
+ signerKeypair: any;
20
+ fiveVMProgramId?: string;
21
+ debug?: boolean;
22
+ }
23
+ export declare function registerNamespaceTldOnChain(namespaceValue: string, options: NamespaceOnChainOptions): Promise<{
24
+ transactionId?: string;
25
+ tldAddress: string;
26
+ owner: string;
27
+ treasuryAccount: string;
28
+ }>;
29
+ export declare function bindNamespaceOnChain(namespaceValue: string, scriptAccount: string, options: NamespaceOnChainOptions): Promise<{
30
+ transactionId?: string;
31
+ bindingAddress: string;
32
+ owner: string;
33
+ }>;
34
+ export declare function resolveNamespaceOnChain(namespaceValue: string, options: NamespaceOnChainOptions): Promise<{
35
+ transactionId?: string;
36
+ resolvedScript?: string;
37
+ bindingAddress: string;
38
+ }>;
39
+ export {};
@@ -0,0 +1,190 @@
1
+ const SYMBOLS = new Set(["!", "@", "#", "$", "%"]);
2
+ const NAMESPACE_CONFIG_SEED = "5ns_config";
3
+ const NAMESPACE_TLD_SEED = "5ns_tld";
4
+ const NAMESPACE_BINDING_SEED = "5ns_binding";
5
+ export function canonicalizeScopedNamespace(input) {
6
+ const value = input.trim();
7
+ if (value.length < 2) {
8
+ throw new Error("namespace is too short");
9
+ }
10
+ const symbol = value[0];
11
+ if (!SYMBOLS.has(symbol)) {
12
+ throw new Error("namespace symbol must be one of ! @ # $ %");
13
+ }
14
+ const parts = value.slice(1).split("/");
15
+ if (parts.length === 0 || parts.length > 2) {
16
+ throw new Error("namespace must be !domain or !domain/subprogram");
17
+ }
18
+ const normalize = (seg) => seg.trim().toLowerCase();
19
+ const domain = normalize(parts[0]);
20
+ const subprogram = parts[1] ? normalize(parts[1]) : undefined;
21
+ const validSegment = (seg) => seg.length > 0 && /^[a-z0-9-]+$/.test(seg);
22
+ if (!validSegment(domain)) {
23
+ throw new Error("domain must be lowercase alphanumeric + hyphen");
24
+ }
25
+ if (subprogram && !validSegment(subprogram)) {
26
+ throw new Error("subprogram must be lowercase alphanumeric + hyphen");
27
+ }
28
+ const canonical = subprogram ? `${symbol}${domain}/${subprogram}` : `${symbol}${domain}`;
29
+ return {
30
+ symbol: symbol,
31
+ domain,
32
+ subprogram,
33
+ canonical,
34
+ };
35
+ }
36
+ export function namespaceSeedBytes(namespaceValue) {
37
+ const parsed = canonicalizeScopedNamespace(namespaceValue);
38
+ if (!parsed.subprogram) {
39
+ throw new Error("namespace seed requires !domain/subprogram");
40
+ }
41
+ const seed = `5NS/${parsed.canonical}`;
42
+ return new TextEncoder().encode(seed);
43
+ }
44
+ export function resolveNamespaceFromLockfile(namespaceValue, lockfile) {
45
+ const parsed = canonicalizeScopedNamespace(namespaceValue);
46
+ if (!parsed.subprogram)
47
+ return undefined;
48
+ const namespaces = Array.isArray(lockfile?.namespaces) ? lockfile.namespaces : [];
49
+ const match = namespaces.find((entry) => entry?.namespace === parsed.canonical);
50
+ return match?.address;
51
+ }
52
+ function asBuffer(value) {
53
+ return Buffer.from(value, "utf8");
54
+ }
55
+ export async function deriveNamespaceAccounts(namespaceValue, fiveVMProgramId) {
56
+ const { PDAUtils } = await import("../crypto/index.js");
57
+ const parsed = canonicalizeScopedNamespace(namespaceValue);
58
+ const cfg = await PDAUtils.findProgramAddress([asBuffer(NAMESPACE_CONFIG_SEED)], fiveVMProgramId);
59
+ const tld = await PDAUtils.findProgramAddress([asBuffer(NAMESPACE_TLD_SEED), asBuffer(parsed.symbol), asBuffer(parsed.domain)], fiveVMProgramId);
60
+ if (!parsed.subprogram) {
61
+ return {
62
+ config: cfg.address,
63
+ tld: tld.address,
64
+ };
65
+ }
66
+ const binding = await PDAUtils.findProgramAddress([
67
+ asBuffer(NAMESPACE_BINDING_SEED),
68
+ asBuffer(parsed.symbol),
69
+ asBuffer(parsed.domain),
70
+ asBuffer(parsed.subprogram),
71
+ ], fiveVMProgramId);
72
+ return {
73
+ config: cfg.address,
74
+ tld: tld.address,
75
+ binding: binding.address,
76
+ };
77
+ }
78
+ function nowUnix() {
79
+ return Math.floor(Date.now() / 1000);
80
+ }
81
+ export async function registerNamespaceTldOnChain(namespaceValue, options) {
82
+ const { ProgramIdResolver } = await import("../config/ProgramIdResolver.js");
83
+ const { executeOnSolana } = await import("./execute.js");
84
+ const { getVMState } = await import("./vm-state.js");
85
+ const parsed = canonicalizeScopedNamespace(namespaceValue);
86
+ if (parsed.subprogram) {
87
+ throw new Error("register expects top-level namespace like @domain");
88
+ }
89
+ const vmProgramId = ProgramIdResolver.resolve(options.fiveVMProgramId);
90
+ const addresses = await deriveNamespaceAccounts(parsed.canonical, vmProgramId);
91
+ const owner = options.signerKeypair.publicKey.toBase58();
92
+ const now = nowUnix();
93
+ const vmState = await getVMState(options.connection, vmProgramId);
94
+ const treasuryAccount = vmState.authority;
95
+ if (!treasuryAccount) {
96
+ throw new Error("VM state authority (treasury) is unavailable");
97
+ }
98
+ const result = await executeOnSolana(options.managerScriptAccount, options.connection, options.signerKeypair, "register_tld", [
99
+ addresses.config,
100
+ addresses.tld,
101
+ owner,
102
+ treasuryAccount,
103
+ parsed.symbol,
104
+ parsed.domain,
105
+ now,
106
+ ], [addresses.config, addresses.tld, owner, treasuryAccount], {
107
+ debug: options.debug,
108
+ fiveVMProgramId: vmProgramId,
109
+ computeUnitLimit: 500000,
110
+ });
111
+ if (!result.success) {
112
+ throw new Error(result.error || "register_tld failed");
113
+ }
114
+ return {
115
+ transactionId: result.transactionId,
116
+ tldAddress: addresses.tld,
117
+ owner,
118
+ treasuryAccount,
119
+ };
120
+ }
121
+ export async function bindNamespaceOnChain(namespaceValue, scriptAccount, options) {
122
+ const { ProgramIdResolver } = await import("../config/ProgramIdResolver.js");
123
+ const { executeOnSolana } = await import("./execute.js");
124
+ const parsed = canonicalizeScopedNamespace(namespaceValue);
125
+ if (!parsed.subprogram) {
126
+ throw new Error("bind expects namespace with subprogram like @domain/subprogram");
127
+ }
128
+ const vmProgramId = ProgramIdResolver.resolve(options.fiveVMProgramId);
129
+ const addresses = await deriveNamespaceAccounts(parsed.canonical, vmProgramId);
130
+ const owner = options.signerKeypair.publicKey.toBase58();
131
+ const now = nowUnix();
132
+ const result = await executeOnSolana(options.managerScriptAccount, options.connection, options.signerKeypair, "bind_subprogram", [
133
+ addresses.tld,
134
+ addresses.binding,
135
+ owner,
136
+ parsed.symbol,
137
+ parsed.domain,
138
+ parsed.subprogram,
139
+ scriptAccount,
140
+ now,
141
+ ], [addresses.tld, addresses.binding, owner], {
142
+ debug: options.debug,
143
+ fiveVMProgramId: vmProgramId,
144
+ computeUnitLimit: 650000,
145
+ });
146
+ if (!result.success) {
147
+ throw new Error(result.error || "bind_subprogram failed");
148
+ }
149
+ return {
150
+ transactionId: result.transactionId,
151
+ bindingAddress: addresses.binding,
152
+ owner,
153
+ };
154
+ }
155
+ export async function resolveNamespaceOnChain(namespaceValue, options) {
156
+ const { ProgramIdResolver } = await import("../config/ProgramIdResolver.js");
157
+ const { executeOnSolana } = await import("./execute.js");
158
+ const parsed = canonicalizeScopedNamespace(namespaceValue);
159
+ if (!parsed.subprogram) {
160
+ throw new Error("resolve expects namespace with subprogram like @domain/subprogram");
161
+ }
162
+ const vmProgramId = ProgramIdResolver.resolve(options.fiveVMProgramId);
163
+ const addresses = await deriveNamespaceAccounts(parsed.canonical, vmProgramId);
164
+ const result = await executeOnSolana(options.managerScriptAccount, options.connection, options.signerKeypair, "resolve", [addresses.binding], [addresses.binding], {
165
+ debug: options.debug,
166
+ fiveVMProgramId: vmProgramId,
167
+ computeUnitLimit: 300000,
168
+ });
169
+ if (!result.success) {
170
+ throw new Error(result.error || "resolve failed");
171
+ }
172
+ const raw = result.result;
173
+ let resolvedScript;
174
+ if (typeof raw === "string" && raw.length > 0) {
175
+ resolvedScript = raw;
176
+ }
177
+ else if (raw && typeof raw === "object") {
178
+ if (typeof raw.script_account === "string")
179
+ resolvedScript = raw.script_account;
180
+ if (typeof raw.scriptAccount === "string")
181
+ resolvedScript = raw.scriptAccount;
182
+ if (typeof raw.value === "string")
183
+ resolvedScript = raw.value;
184
+ }
185
+ return {
186
+ transactionId: result.transactionId,
187
+ resolvedScript,
188
+ bindingAddress: addresses.binding,
189
+ };
190
+ }
@@ -0,0 +1,35 @@
1
+ export declare function executeWithStateDiff(scriptAccount: string, connection: any, signerKeypair: any, functionName: string | number, parameters?: any[], options?: {
2
+ debug?: boolean;
3
+ network?: string;
4
+ computeUnitLimit?: number;
5
+ trackGlobalFields?: boolean;
6
+ additionalAccounts?: string[];
7
+ includeVMState?: boolean;
8
+ fiveVMProgramId?: string;
9
+ }): Promise<{
10
+ success: boolean;
11
+ execution?: {
12
+ transactionId?: string;
13
+ result?: any;
14
+ computeUnitsUsed?: number;
15
+ logs?: string[];
16
+ };
17
+ stateDiff?: {
18
+ beforeState: Map<string, any>;
19
+ afterState: Map<string, any>;
20
+ changes: Array<{
21
+ account: string;
22
+ fieldName?: string;
23
+ oldValue: any;
24
+ newValue: any;
25
+ changeType: "created" | "modified" | "deleted";
26
+ }>;
27
+ globalFieldChanges?: Array<{
28
+ fieldName: string;
29
+ oldValue: any;
30
+ newValue: any;
31
+ }>;
32
+ };
33
+ error?: string;
34
+ logs?: string[];
35
+ }>;