@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.
- package/README.md +279 -0
- package/dist/FiveSDK.d.ts +336 -0
- package/dist/FiveSDK.js +395 -0
- package/dist/accounts/index.d.ts +254 -0
- package/dist/accounts/index.js +543 -0
- package/dist/assets/vm/dummy.file +0 -0
- package/dist/assets/vm/five_vm_wasm.d.ts +762 -0
- package/dist/assets/vm/five_vm_wasm.js +3754 -0
- package/dist/assets/vm/five_vm_wasm_bg.js +3307 -0
- package/dist/assets/vm/five_vm_wasm_bg.wasm +0 -0
- package/dist/assets/vm/five_vm_wasm_bg.wasm.d.ts +247 -0
- package/dist/assets/vm/package.json +11 -0
- package/dist/bin/gen-types.d.ts +2 -0
- package/dist/bin/gen-types.js +35 -0
- package/dist/compiler/BytecodeCompiler.d.ts +83 -0
- package/dist/compiler/BytecodeCompiler.js +379 -0
- package/dist/config/ConfigManager.d.ts +13 -0
- package/dist/config/ConfigManager.js +27 -0
- package/dist/config/ProgramIdResolver.d.ts +62 -0
- package/dist/config/ProgramIdResolver.js +104 -0
- package/dist/crypto/index.d.ts +211 -0
- package/dist/crypto/index.js +451 -0
- package/dist/encoding/ParameterEncoder.d.ts +31 -0
- package/dist/encoding/ParameterEncoder.js +278 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +28 -0
- package/dist/lib/bytecode-encoder.d.ts +62 -0
- package/dist/lib/bytecode-encoder.js +281 -0
- package/dist/logging/index.d.ts +9 -0
- package/dist/logging/index.js +10 -0
- package/dist/metadata/index.d.ts +213 -0
- package/dist/metadata/index.js +296 -0
- package/dist/modules/accounts.d.ts +60 -0
- package/dist/modules/accounts.js +275 -0
- package/dist/modules/deploy.d.ts +90 -0
- package/dist/modules/deploy.js +1118 -0
- package/dist/modules/execute.d.ts +90 -0
- package/dist/modules/execute.js +649 -0
- package/dist/modules/fees.d.ts +14 -0
- package/dist/modules/fees.js +112 -0
- package/dist/modules/namespaces.d.ts +39 -0
- package/dist/modules/namespaces.js +190 -0
- package/dist/modules/state-diff.d.ts +35 -0
- package/dist/modules/state-diff.js +342 -0
- package/dist/modules/vm-state.d.ts +7 -0
- package/dist/modules/vm-state.js +44 -0
- package/dist/program/AccountResolver.d.ts +67 -0
- package/dist/program/AccountResolver.js +134 -0
- package/dist/program/BorshSchemaGenerator.d.ts +8 -0
- package/dist/program/BorshSchemaGenerator.js +57 -0
- package/dist/program/FiveProgram.d.ts +144 -0
- package/dist/program/FiveProgram.js +282 -0
- package/dist/program/FunctionBuilder.d.ts +114 -0
- package/dist/program/FunctionBuilder.js +347 -0
- package/dist/program/ProgramAccount.d.ts +38 -0
- package/dist/program/ProgramAccount.js +170 -0
- package/dist/program/TypeGenerator.d.ts +90 -0
- package/dist/program/TypeGenerator.js +195 -0
- package/dist/program/index.d.ts +24 -0
- package/dist/program/index.js +21 -0
- package/dist/project/config.d.ts +5 -0
- package/dist/project/config.js +33 -0
- package/dist/project/toml.d.ts +6 -0
- package/dist/project/toml.js +43 -0
- package/dist/project/workspace.d.ts +160 -0
- package/dist/project/workspace.js +73 -0
- package/dist/testing/AccountMetaGenerator.d.ts +121 -0
- package/dist/testing/AccountMetaGenerator.js +261 -0
- package/dist/testing/AccountTestFixture.d.ts +211 -0
- package/dist/testing/AccountTestFixture.js +530 -0
- package/dist/testing/OnChainAccountManager.d.ts +81 -0
- package/dist/testing/OnChainAccountManager.js +260 -0
- package/dist/testing/StateSerializer.d.ts +65 -0
- package/dist/testing/StateSerializer.js +330 -0
- package/dist/testing/TestDiscovery.d.ts +79 -0
- package/dist/testing/TestDiscovery.js +274 -0
- package/dist/testing/TestRunner.d.ts +117 -0
- package/dist/testing/TestRunner.js +346 -0
- package/dist/testing/index.d.ts +14 -0
- package/dist/testing/index.js +13 -0
- package/dist/types.d.ts +356 -0
- package/dist/types.js +32 -0
- package/dist/utils/abi.d.ts +31 -0
- package/dist/utils/abi.js +92 -0
- package/dist/utils/transaction.d.ts +5 -0
- package/dist/utils/transaction.js +48 -0
- package/dist/validation/InputValidator.d.ts +142 -0
- package/dist/validation/InputValidator.js +332 -0
- package/dist/validation/index.d.ts +4 -0
- package/dist/validation/index.js +4 -0
- package/dist/wasm/compiler/AbiLogic.d.ts +4 -0
- package/dist/wasm/compiler/AbiLogic.js +37 -0
- package/dist/wasm/compiler/AnalysisLogic.d.ts +6 -0
- package/dist/wasm/compiler/AnalysisLogic.js +61 -0
- package/dist/wasm/compiler/CompilationLogic.d.ts +10 -0
- package/dist/wasm/compiler/CompilationLogic.js +431 -0
- package/dist/wasm/compiler/FiveCompiler.d.ts +48 -0
- package/dist/wasm/compiler/FiveCompiler.js +183 -0
- package/dist/wasm/compiler/InfoLogic.d.ts +6 -0
- package/dist/wasm/compiler/InfoLogic.js +24 -0
- package/dist/wasm/compiler/OptimizationLogic.d.ts +2 -0
- package/dist/wasm/compiler/OptimizationLogic.js +13 -0
- package/dist/wasm/compiler/ValidationLogic.d.ts +7 -0
- package/dist/wasm/compiler/ValidationLogic.js +26 -0
- package/dist/wasm/compiler/index.d.ts +2 -0
- package/dist/wasm/compiler/index.js +2 -0
- package/dist/wasm/compiler/types.d.ts +8 -0
- package/dist/wasm/compiler/types.js +1 -0
- package/dist/wasm/compiler/utils.d.ts +8 -0
- package/dist/wasm/compiler/utils.js +75 -0
- package/dist/wasm/index.d.ts +9 -0
- package/dist/wasm/index.js +12 -0
- package/dist/wasm/instance.d.ts +1 -0
- package/dist/wasm/instance.js +26 -0
- package/dist/wasm/loader.d.ts +7 -0
- package/dist/wasm/loader.js +112 -0
- package/dist/wasm/vm.d.ts +33 -0
- package/dist/wasm/vm.js +250 -0
- package/package.json +59 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FiveProgram - High-level wrapper for Five VM scripts
|
|
3
|
+
*
|
|
4
|
+
* Provides Anchor-style ergonomics while maintaining zero-dependency design.
|
|
5
|
+
* FiveProgram simplifies interaction with Five scripts on-chain by:
|
|
6
|
+
* - Providing fluent API for function calls
|
|
7
|
+
* - Auto-injecting system accounts when needed
|
|
8
|
+
* - Generating TypeScript types from ABI
|
|
9
|
+
* - Handling parameter encoding internally
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const program = FiveProgram.fromABI(scriptAccount, abi);
|
|
14
|
+
* const ix = await program
|
|
15
|
+
* .function('increment')
|
|
16
|
+
* .accounts({ counter: counter1, owner: user1.publicKey })
|
|
17
|
+
* .instruction();
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import type { ScriptABI, FunctionDefinition, AccountFetcher } from '../metadata/index.js';
|
|
21
|
+
import type { Provider } from '../types.js';
|
|
22
|
+
import { FunctionBuilder } from './FunctionBuilder.js';
|
|
23
|
+
import { ProgramAccount } from './ProgramAccount.js';
|
|
24
|
+
export interface FiveProgramOptions {
|
|
25
|
+
/** Enable debug logging */
|
|
26
|
+
debug?: boolean;
|
|
27
|
+
/** Optional account fetcher for loading metadata from chain */
|
|
28
|
+
fetcher?: AccountFetcher;
|
|
29
|
+
/** Five VM Program ID (defaults to mainnet/devnet) */
|
|
30
|
+
fiveVMProgramId?: string;
|
|
31
|
+
/** Five VM State PDA account (if not provided, will be derived) */
|
|
32
|
+
vmStateAccount?: string;
|
|
33
|
+
/** Fee receiver account (admin account for transaction fees) */
|
|
34
|
+
feeReceiverAccount?: string;
|
|
35
|
+
/** Wallet/Network Provider for RPC calls */
|
|
36
|
+
provider?: Provider;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* FiveProgram represents a deployed Five script with its ABI
|
|
40
|
+
* Provides high-level API for building function calls
|
|
41
|
+
*/
|
|
42
|
+
export declare class FiveProgram {
|
|
43
|
+
private scriptAccount;
|
|
44
|
+
private abi;
|
|
45
|
+
private options;
|
|
46
|
+
private functionBuilderCache;
|
|
47
|
+
/**
|
|
48
|
+
* RPC Entry point for calling functions
|
|
49
|
+
* e.g. program.methods.myFunc(params).rpc()
|
|
50
|
+
*/
|
|
51
|
+
readonly methods: any;
|
|
52
|
+
constructor(scriptAccount: string, abi: ScriptABI, options?: FiveProgramOptions);
|
|
53
|
+
/**
|
|
54
|
+
* Factory method: Create FiveProgram from ABI
|
|
55
|
+
* Used when ABI is already available locally or compiled
|
|
56
|
+
*/
|
|
57
|
+
static fromABI(scriptAccount: string, abi: ScriptABI, options?: FiveProgramOptions): FiveProgram;
|
|
58
|
+
/**
|
|
59
|
+
* Factory method: Load FiveProgram from deployed script account
|
|
60
|
+
* Fetches script metadata from chain and extracts ABI
|
|
61
|
+
*
|
|
62
|
+
* @param scriptAddress - Script account address
|
|
63
|
+
* @param connection - web3.js Connection object or custom fetcher
|
|
64
|
+
* @param options - SDK options
|
|
65
|
+
* @returns Initialized FiveProgram instance
|
|
66
|
+
*/
|
|
67
|
+
static load(scriptAddress: string, connection: any, // Connection or custom fetcher
|
|
68
|
+
options?: FiveProgramOptions): Promise<FiveProgram>;
|
|
69
|
+
/**
|
|
70
|
+
* Get function builder for a specific function
|
|
71
|
+
* Creates a fluent interface for building function calls
|
|
72
|
+
*
|
|
73
|
+
* @param functionName - Name of the function to call
|
|
74
|
+
* @returns FunctionBuilder for chaining .accounts().args().instruction()
|
|
75
|
+
* @throws Error if function not found in ABI
|
|
76
|
+
*/
|
|
77
|
+
function(functionName: string): FunctionBuilder;
|
|
78
|
+
/**
|
|
79
|
+
* Get an account handler for fetching and decoding state
|
|
80
|
+
*
|
|
81
|
+
* @param structName - Name of the struct definition in ABI (e.g., "Counter")
|
|
82
|
+
* @returns ProgramAccount instance
|
|
83
|
+
*/
|
|
84
|
+
account(structName: string): ProgramAccount;
|
|
85
|
+
/**
|
|
86
|
+
* Get all available function names from ABI
|
|
87
|
+
*/
|
|
88
|
+
getFunctions(): string[];
|
|
89
|
+
/**
|
|
90
|
+
* Get function definition by name
|
|
91
|
+
*/
|
|
92
|
+
getFunction(name: string): FunctionDefinition | undefined;
|
|
93
|
+
/**
|
|
94
|
+
* Get all function definitions
|
|
95
|
+
*/
|
|
96
|
+
getAllFunctions(): FunctionDefinition[];
|
|
97
|
+
/**
|
|
98
|
+
* Generate TypeScript types from ABI
|
|
99
|
+
* Creates type-safe interfaces for function parameters
|
|
100
|
+
*
|
|
101
|
+
* @returns TypeScript interface as string
|
|
102
|
+
*/
|
|
103
|
+
generateTypes(): string;
|
|
104
|
+
/**
|
|
105
|
+
* Get script account address
|
|
106
|
+
*/
|
|
107
|
+
getScriptAccount(): string;
|
|
108
|
+
/**
|
|
109
|
+
* Get Five VM Program ID with consistent resolver precedence
|
|
110
|
+
*/
|
|
111
|
+
getFiveVMProgramId(): string;
|
|
112
|
+
/**
|
|
113
|
+
* Derive a Program Derived Address (PDA)
|
|
114
|
+
*
|
|
115
|
+
* @param seeds - Array of seeds (strings, public keys in base58, or buffers)
|
|
116
|
+
* @param programId - Optional program ID (defaults to Five VM)
|
|
117
|
+
* @returns [address, bump]
|
|
118
|
+
*/
|
|
119
|
+
findAddress(seeds: (string | Uint8Array | Buffer)[], programId?: string): Promise<[string, number]>;
|
|
120
|
+
/**
|
|
121
|
+
* Get the ABI
|
|
122
|
+
*/
|
|
123
|
+
getABI(): ScriptABI;
|
|
124
|
+
/**
|
|
125
|
+
* Get options
|
|
126
|
+
*/
|
|
127
|
+
getOptions(): FiveProgramOptions;
|
|
128
|
+
/**
|
|
129
|
+
* Get VM State Account
|
|
130
|
+
*/
|
|
131
|
+
getVMStateAccount(): string | undefined;
|
|
132
|
+
/**
|
|
133
|
+
* Set VM State Account
|
|
134
|
+
*/
|
|
135
|
+
setVMStateAccount(account: string): this;
|
|
136
|
+
/**
|
|
137
|
+
* Get Fee Receiver Account
|
|
138
|
+
*/
|
|
139
|
+
getFeeReceiverAccount(): string | undefined;
|
|
140
|
+
/**
|
|
141
|
+
* Set Fee Receiver Account
|
|
142
|
+
*/
|
|
143
|
+
setFeeReceiverAccount(account: string): this;
|
|
144
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FiveProgram - High-level wrapper for Five VM scripts
|
|
3
|
+
*
|
|
4
|
+
* Provides Anchor-style ergonomics while maintaining zero-dependency design.
|
|
5
|
+
* FiveProgram simplifies interaction with Five scripts on-chain by:
|
|
6
|
+
* - Providing fluent API for function calls
|
|
7
|
+
* - Auto-injecting system accounts when needed
|
|
8
|
+
* - Generating TypeScript types from ABI
|
|
9
|
+
* - Handling parameter encoding internally
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const program = FiveProgram.fromABI(scriptAccount, abi);
|
|
14
|
+
* const ix = await program
|
|
15
|
+
* .function('increment')
|
|
16
|
+
* .accounts({ counter: counter1, owner: user1.publicKey })
|
|
17
|
+
* .instruction();
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import { FunctionBuilder } from './FunctionBuilder.js';
|
|
21
|
+
import { TypeGenerator } from './TypeGenerator.js';
|
|
22
|
+
import { ProgramAccount } from './ProgramAccount.js';
|
|
23
|
+
import { ProgramIdResolver } from '../config/ProgramIdResolver.js';
|
|
24
|
+
/**
|
|
25
|
+
* FiveProgram represents a deployed Five script with its ABI
|
|
26
|
+
* Provides high-level API for building function calls
|
|
27
|
+
*/
|
|
28
|
+
export class FiveProgram {
|
|
29
|
+
constructor(scriptAccount, abi, options) {
|
|
30
|
+
this.functionBuilderCache = new Map();
|
|
31
|
+
this.scriptAccount = scriptAccount;
|
|
32
|
+
this.abi = abi;
|
|
33
|
+
this.options = {
|
|
34
|
+
debug: false,
|
|
35
|
+
...options,
|
|
36
|
+
// fiveVMProgramId will be resolved at call time via ProgramIdResolver
|
|
37
|
+
};
|
|
38
|
+
// Build cache for quick function lookup
|
|
39
|
+
this.abi.functions.forEach((func) => {
|
|
40
|
+
this.functionBuilderCache.set(func.name, func);
|
|
41
|
+
});
|
|
42
|
+
if (this.options.debug) {
|
|
43
|
+
console.log(`[FiveProgram] Initialized with ${this.abi.functions.length} functions`);
|
|
44
|
+
}
|
|
45
|
+
// Initialize the methods proxy
|
|
46
|
+
this.methods = new Proxy(this, {
|
|
47
|
+
get: (target, prop) => {
|
|
48
|
+
if (typeof prop === 'string') {
|
|
49
|
+
return (...args) => {
|
|
50
|
+
const builder = target.function(prop);
|
|
51
|
+
// TODO: Robust argument mapping based on ABI types
|
|
52
|
+
// Simple map: if args[0] is object, assume named params
|
|
53
|
+
if (args.length === 1 && typeof args[0] === 'object' && !Array.isArray(args[0])) {
|
|
54
|
+
builder.args(args[0]);
|
|
55
|
+
}
|
|
56
|
+
// Logic for positional args would go here
|
|
57
|
+
return builder;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Factory method: Create FiveProgram from ABI
|
|
66
|
+
* Used when ABI is already available locally or compiled
|
|
67
|
+
*/
|
|
68
|
+
static fromABI(scriptAccount, abi, options) {
|
|
69
|
+
return new FiveProgram(scriptAccount, abi, options);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Factory method: Load FiveProgram from deployed script account
|
|
73
|
+
* Fetches script metadata from chain and extracts ABI
|
|
74
|
+
*
|
|
75
|
+
* @param scriptAddress - Script account address
|
|
76
|
+
* @param connection - web3.js Connection object or custom fetcher
|
|
77
|
+
* @param options - SDK options
|
|
78
|
+
* @returns Initialized FiveProgram instance
|
|
79
|
+
*/
|
|
80
|
+
static async load(scriptAddress, connection, // Connection or custom fetcher
|
|
81
|
+
options = {}) {
|
|
82
|
+
const { ScriptMetadataParser } = await import('../metadata/index.js');
|
|
83
|
+
const { PublicKey } = await import('@solana/web3.js');
|
|
84
|
+
let fetcher;
|
|
85
|
+
// Check if connection is a web3.js Connection (has getAccountInfo)
|
|
86
|
+
if (connection && typeof connection.getAccountInfo === 'function') {
|
|
87
|
+
fetcher = {
|
|
88
|
+
getAccountData: async (address) => {
|
|
89
|
+
try {
|
|
90
|
+
const pubkey = new PublicKey(address);
|
|
91
|
+
const info = await connection.getAccountInfo(pubkey);
|
|
92
|
+
if (!info)
|
|
93
|
+
return null;
|
|
94
|
+
return {
|
|
95
|
+
address,
|
|
96
|
+
data: info.data,
|
|
97
|
+
owner: info.owner.toBase58(),
|
|
98
|
+
lamports: info.lamports
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
console.warn(`Error fetching account ${address}:`, error);
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
getMultipleAccountsData: async (addresses) => {
|
|
107
|
+
// Minimal implementation for getScriptMetadata requirements
|
|
108
|
+
const results = new Map();
|
|
109
|
+
// getScriptMetadata currently uses getAccountData, but if it used batch:
|
|
110
|
+
try {
|
|
111
|
+
const pubkeys = addresses.map(a => new PublicKey(a));
|
|
112
|
+
const infos = await connection.getMultipleAccountsInfo(pubkeys);
|
|
113
|
+
infos.forEach((info, i) => {
|
|
114
|
+
const address = addresses[i];
|
|
115
|
+
if (info) {
|
|
116
|
+
results.set(address, {
|
|
117
|
+
address,
|
|
118
|
+
data: info.data,
|
|
119
|
+
owner: info.owner.toBase58(),
|
|
120
|
+
lamports: info.lamports
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
results.set(address, null);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
catch (e) {
|
|
129
|
+
console.warn('Batch fetch failed', e);
|
|
130
|
+
}
|
|
131
|
+
return results;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
// Assume it matches AccountFetcher interface
|
|
137
|
+
fetcher = connection;
|
|
138
|
+
}
|
|
139
|
+
// Set fetcher in options so ProgramAccount can use it later
|
|
140
|
+
const newOptions = { ...options, fetcher: options.fetcher || fetcher };
|
|
141
|
+
const metadata = await ScriptMetadataParser.getScriptMetadata(fetcher, scriptAddress);
|
|
142
|
+
return new FiveProgram(scriptAddress, metadata.abi, newOptions);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Get function builder for a specific function
|
|
146
|
+
* Creates a fluent interface for building function calls
|
|
147
|
+
*
|
|
148
|
+
* @param functionName - Name of the function to call
|
|
149
|
+
* @returns FunctionBuilder for chaining .accounts().args().instruction()
|
|
150
|
+
* @throws Error if function not found in ABI
|
|
151
|
+
*/
|
|
152
|
+
function(functionName) {
|
|
153
|
+
const funcDef = this.functionBuilderCache.get(functionName);
|
|
154
|
+
if (!funcDef) {
|
|
155
|
+
const available = Array.from(this.functionBuilderCache.keys());
|
|
156
|
+
throw new Error(`Function '${functionName}' not found in ABI. Available: ${available.join(', ')}`);
|
|
157
|
+
}
|
|
158
|
+
return new FunctionBuilder(funcDef, this.scriptAccount, this.abi, this.options);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Get an account handler for fetching and decoding state
|
|
162
|
+
*
|
|
163
|
+
* @param structName - Name of the struct definition in ABI (e.g., "Counter")
|
|
164
|
+
* @returns ProgramAccount instance
|
|
165
|
+
*/
|
|
166
|
+
account(structName) {
|
|
167
|
+
return new ProgramAccount(structName, this.abi, this.options.fetcher);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Get all available function names from ABI
|
|
171
|
+
*/
|
|
172
|
+
getFunctions() {
|
|
173
|
+
return this.abi.functions.map((func) => func.name);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Get function definition by name
|
|
177
|
+
*/
|
|
178
|
+
getFunction(name) {
|
|
179
|
+
return this.functionBuilderCache.get(name);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Get all function definitions
|
|
183
|
+
*/
|
|
184
|
+
getAllFunctions() {
|
|
185
|
+
return this.abi.functions;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Generate TypeScript types from ABI
|
|
189
|
+
* Creates type-safe interfaces for function parameters
|
|
190
|
+
*
|
|
191
|
+
* @returns TypeScript interface as string
|
|
192
|
+
*/
|
|
193
|
+
generateTypes() {
|
|
194
|
+
const typeGenerator = new TypeGenerator(this.abi, {
|
|
195
|
+
scriptName: this.abi.name || 'Script',
|
|
196
|
+
debug: this.options.debug,
|
|
197
|
+
});
|
|
198
|
+
return typeGenerator.generate();
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Get script account address
|
|
202
|
+
*/
|
|
203
|
+
getScriptAccount() {
|
|
204
|
+
return this.scriptAccount;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Get Five VM Program ID with consistent resolver precedence
|
|
208
|
+
*/
|
|
209
|
+
getFiveVMProgramId() {
|
|
210
|
+
return ProgramIdResolver.resolve(this.options.fiveVMProgramId);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Derive a Program Derived Address (PDA)
|
|
214
|
+
*
|
|
215
|
+
* @param seeds - Array of seeds (strings, public keys in base58, or buffers)
|
|
216
|
+
* @param programId - Optional program ID (defaults to Five VM)
|
|
217
|
+
* @returns [address, bump]
|
|
218
|
+
*/
|
|
219
|
+
async findAddress(seeds, programId) {
|
|
220
|
+
const { PublicKey } = await import('@solana/web3.js');
|
|
221
|
+
// Convert seeds to Buffers
|
|
222
|
+
const bufferSeeds = seeds.map(seed => {
|
|
223
|
+
if (seed instanceof Uint8Array || (typeof Buffer !== 'undefined' && Buffer.isBuffer(seed))) {
|
|
224
|
+
return seed;
|
|
225
|
+
}
|
|
226
|
+
if (typeof seed === 'string') {
|
|
227
|
+
// Check if it looks like a Pubkey (Base58, 32 bytes approx)
|
|
228
|
+
// Heuristic: if it decodes to 32 bytes, treat as pubkey, else string literal
|
|
229
|
+
try {
|
|
230
|
+
const pk = new PublicKey(seed);
|
|
231
|
+
return pk.toBuffer();
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
return Buffer.from(seed);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
throw new Error(`Unsupported seed type: ${typeof seed}`);
|
|
238
|
+
});
|
|
239
|
+
const pid = new PublicKey(programId || this.getFiveVMProgramId());
|
|
240
|
+
// @ts-ignore - PublicKey.findProgramAddress return type mismatch in some versions
|
|
241
|
+
const [addr, bump] = PublicKey.findProgramAddressSync(bufferSeeds, pid);
|
|
242
|
+
return [addr.toBase58(), bump];
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Get the ABI
|
|
246
|
+
*/
|
|
247
|
+
getABI() {
|
|
248
|
+
return this.abi;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Get options
|
|
252
|
+
*/
|
|
253
|
+
getOptions() {
|
|
254
|
+
return this.options;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Get VM State Account
|
|
258
|
+
*/
|
|
259
|
+
getVMStateAccount() {
|
|
260
|
+
return this.options.vmStateAccount;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Set VM State Account
|
|
264
|
+
*/
|
|
265
|
+
setVMStateAccount(account) {
|
|
266
|
+
this.options.vmStateAccount = account;
|
|
267
|
+
return this;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Get Fee Receiver Account
|
|
271
|
+
*/
|
|
272
|
+
getFeeReceiverAccount() {
|
|
273
|
+
return this.options.feeReceiverAccount;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Set Fee Receiver Account
|
|
277
|
+
*/
|
|
278
|
+
setFeeReceiverAccount(account) {
|
|
279
|
+
this.options.feeReceiverAccount = account;
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FunctionBuilder - Fluent API for building Five VM function calls
|
|
3
|
+
*
|
|
4
|
+
* Provides chainable methods for specifying function parameters:
|
|
5
|
+
* - .accounts() - Map account names to their addresses
|
|
6
|
+
* - .args() - Map data parameters to their values
|
|
7
|
+
* - .instruction() - Generate serialized instruction data
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const ix = await program
|
|
12
|
+
* .function('add_amount')
|
|
13
|
+
* .accounts({ counter: counter1, owner: user1.publicKey })
|
|
14
|
+
* .args({ amount: 10 })
|
|
15
|
+
* .instruction();
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
import type { FunctionDefinition, ScriptABI } from '../metadata/index.js';
|
|
19
|
+
import type { SerializedInstruction } from '../types.js';
|
|
20
|
+
import type { FiveProgramOptions } from './FiveProgram.js';
|
|
21
|
+
/**
|
|
22
|
+
* FunctionBuilder implements fluent API for function calls
|
|
23
|
+
*/
|
|
24
|
+
export declare class FunctionBuilder {
|
|
25
|
+
private functionDef;
|
|
26
|
+
private scriptAccount;
|
|
27
|
+
private abi;
|
|
28
|
+
private options;
|
|
29
|
+
private accountsMap;
|
|
30
|
+
private argsMap;
|
|
31
|
+
private resolvedAccounts;
|
|
32
|
+
constructor(functionDef: FunctionDefinition, scriptAccount: string, abi: ScriptABI, options: FiveProgramOptions);
|
|
33
|
+
/**
|
|
34
|
+
* Specify accounts for this function call
|
|
35
|
+
* Accepts either base58 strings or PublicKey objects
|
|
36
|
+
*
|
|
37
|
+
* @param accounts - Map of account names to addresses
|
|
38
|
+
* @returns this for method chaining
|
|
39
|
+
*/
|
|
40
|
+
accounts(accounts: Record<string, string | {
|
|
41
|
+
toBase58(): string;
|
|
42
|
+
}>): this;
|
|
43
|
+
/**
|
|
44
|
+
* Specify data parameters for this function call
|
|
45
|
+
*
|
|
46
|
+
* @param args - Map of parameter names to values
|
|
47
|
+
* @returns this for method chaining
|
|
48
|
+
*/
|
|
49
|
+
args(args: Record<string, any>): this;
|
|
50
|
+
/**
|
|
51
|
+
* Build and return serialized instruction data
|
|
52
|
+
* This is the main method that orchestrates parameter resolution and instruction generation
|
|
53
|
+
*
|
|
54
|
+
* @returns SerializedInstruction ready for transaction building
|
|
55
|
+
*/
|
|
56
|
+
instruction(): Promise<SerializedInstruction>;
|
|
57
|
+
/**
|
|
58
|
+
* Build a Solana Transaction containing this instruction
|
|
59
|
+
*/
|
|
60
|
+
transaction(options?: {
|
|
61
|
+
computeUnits?: number;
|
|
62
|
+
}): Promise<any>;
|
|
63
|
+
/**
|
|
64
|
+
* Send transaction with this instruction (RPC)
|
|
65
|
+
* requires a provider to be set in options
|
|
66
|
+
*/
|
|
67
|
+
rpc(options?: {
|
|
68
|
+
signers?: any[];
|
|
69
|
+
skipPreflight?: boolean;
|
|
70
|
+
computeUnits?: number;
|
|
71
|
+
}): Promise<string>;
|
|
72
|
+
/**
|
|
73
|
+
* Validate that all required parameters are provided
|
|
74
|
+
* @throws Error if any required parameter is missing
|
|
75
|
+
*/
|
|
76
|
+
private validateParameters;
|
|
77
|
+
/**
|
|
78
|
+
* Merge account and data parameters in ABI order
|
|
79
|
+
* Returns both merged array and list of account pubkeys for instruction building
|
|
80
|
+
*
|
|
81
|
+
* @returns Object with mergedParams array and accountPubkeys list
|
|
82
|
+
*/
|
|
83
|
+
private mergeParameters;
|
|
84
|
+
/**
|
|
85
|
+
* Build account metadata (isSigner, isWritable) from ABI attributes
|
|
86
|
+
*
|
|
87
|
+
* @param accountPubkeys - List of account pubkeys in order
|
|
88
|
+
* @param systemAccounts - System accounts that were auto-injected
|
|
89
|
+
* @returns Map of pubkey to metadata
|
|
90
|
+
*/
|
|
91
|
+
private buildAccountMetadata;
|
|
92
|
+
/**
|
|
93
|
+
* Generate instruction data using serialization
|
|
94
|
+
* Integrates with FiveSDK.generateExecuteInstruction() for parameter encoding
|
|
95
|
+
*
|
|
96
|
+
* @param mergedParams - Parameters in ABI order
|
|
97
|
+
* @param accountList - List of all account pubkeys (function params + system accounts)
|
|
98
|
+
* @param accountMetadata - Account metadata (isSigner, isWritable)
|
|
99
|
+
* @returns SerializedInstruction
|
|
100
|
+
*/
|
|
101
|
+
private generateInstructionData;
|
|
102
|
+
/**
|
|
103
|
+
* Get the function definition
|
|
104
|
+
*/
|
|
105
|
+
getFunctionDef(): FunctionDefinition;
|
|
106
|
+
/**
|
|
107
|
+
* Get accounts that have been set
|
|
108
|
+
*/
|
|
109
|
+
getAccounts(): Record<string, string>;
|
|
110
|
+
/**
|
|
111
|
+
* Get arguments that have been set
|
|
112
|
+
*/
|
|
113
|
+
getArgs(): Record<string, any>;
|
|
114
|
+
}
|