@sentinelysolana/sdk 1.0.0
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 +432 -0
- package/dist/__tests__/integration.test.d.ts +32 -0
- package/dist/__tests__/integration.test.d.ts.map +1 -0
- package/dist/__tests__/integration.test.js +225 -0
- package/dist/__tests__/integration.test.js.map +1 -0
- package/dist/clients/compliance.d.ts +126 -0
- package/dist/clients/compliance.d.ts.map +1 -0
- package/dist/clients/compliance.js +269 -0
- package/dist/clients/compliance.js.map +1 -0
- package/dist/clients/oracle.d.ts +113 -0
- package/dist/clients/oracle.d.ts.map +1 -0
- package/dist/clients/oracle.js +285 -0
- package/dist/clients/oracle.js.map +1 -0
- package/dist/clients/vault.d.ts +173 -0
- package/dist/clients/vault.d.ts.map +1 -0
- package/dist/clients/vault.js +529 -0
- package/dist/clients/vault.js.map +1 -0
- package/dist/index.d.ts +86 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +106 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +234 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +86 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ============ ORACLECLIENT: MPP PAYMENT ABSTRACTION ============
|
|
4
|
+
*
|
|
5
|
+
* The masterpiece: Hides the x402 Protocol handshake behind
|
|
6
|
+
* a single beautiful method call.
|
|
7
|
+
*
|
|
8
|
+
* Developer calls: oracle.queryKalshiSignal()
|
|
9
|
+
* SDK silently handles:
|
|
10
|
+
* 1. Initial request (receives 402 Payment Required)
|
|
11
|
+
* 2. Micro-payment transaction (0.05 USDC)
|
|
12
|
+
* 3. Awaits confirmation
|
|
13
|
+
* 4. Fetches final prediction
|
|
14
|
+
*
|
|
15
|
+
* Developer experiences: 2-second await → AI prediction JSON
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.OracleClient = void 0;
|
|
19
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
20
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
21
|
+
/**
|
|
22
|
+
* OracleClient: Machine-to-Machine prediction oracle with invisible MPP payments
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const oracle = new OracleClient(config);
|
|
27
|
+
* const signal = await oracle.queryKalshiSignal({
|
|
28
|
+
* marketTicker: 'FED_RATES_MAR26',
|
|
29
|
+
* });
|
|
30
|
+
* // ^ 2-second await, automatic 0.05 USDC payment, full prediction
|
|
31
|
+
* console.log(signal.reasoning);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
class OracleClient {
|
|
35
|
+
/**
|
|
36
|
+
* Initialize OracleClient with Solana connection and config
|
|
37
|
+
*
|
|
38
|
+
* Supports both backend (keypair) and frontend (wallet adapter) modes
|
|
39
|
+
*
|
|
40
|
+
* @param config - SDK configuration
|
|
41
|
+
*/
|
|
42
|
+
constructor(config) {
|
|
43
|
+
/** Treasury wallet (receives 0.05 USDC payments) */
|
|
44
|
+
this.TREASURY_WALLET = new web3_js_1.PublicKey('A7FnyNVtkcRMEkhaBjgtKZ1Z7Mh4N9XLBN8AGneXNK2F');
|
|
45
|
+
/** USDC Devnet mint */
|
|
46
|
+
this.USDC_MINT = new web3_js_1.PublicKey('EPjFWdd5Au17y2edWjNx5mKnvUcghLgMLKXZKL7xS1d');
|
|
47
|
+
/** x402 Protocol payment amount (0.05 USDC in cents) */
|
|
48
|
+
this.PAYMENT_AMOUNT_CENTS = 5;
|
|
49
|
+
this.connection = new web3_js_1.Connection(config.rpcUrl, config.commitment || 'confirmed');
|
|
50
|
+
// Determine signing mode (same as VaultClient)
|
|
51
|
+
if (config.keypair) {
|
|
52
|
+
this.keypair = web3_js_1.Keypair.fromSecretKey(config.keypair.secretKey);
|
|
53
|
+
this.userPublicKey = this.keypair.publicKey;
|
|
54
|
+
}
|
|
55
|
+
else if (config.signTransaction && config.publicKey) {
|
|
56
|
+
this.signTransaction = config.signTransaction;
|
|
57
|
+
this.userPublicKey = new web3_js_1.PublicKey(config.publicKey);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
throw new Error('OracleClient requires either (keypair) or (signTransaction + publicKey)');
|
|
61
|
+
}
|
|
62
|
+
this.mcpGatewayUrl =
|
|
63
|
+
config.mcpGatewayUrl || 'https://api.sentinely.io/v1';
|
|
64
|
+
this.irysGatewayUrl = config.irysGatewayUrl || 'https://gateway.irys.xyz';
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Query Kalshi signal with automatic x402 micro-payment
|
|
68
|
+
*
|
|
69
|
+
* This is the core abstraction: developer calls once, SDK handles:
|
|
70
|
+
* 1. Initial request to MCP Gateway
|
|
71
|
+
* 2. Receives 402 Payment Required header
|
|
72
|
+
* 3. Executes 0.05 USDC Solana transaction
|
|
73
|
+
* 4. Awaits confirmation
|
|
74
|
+
* 5. Re-queries with payment headers
|
|
75
|
+
* 6. Returns final AI prediction
|
|
76
|
+
*
|
|
77
|
+
* @param query - Market query parameters
|
|
78
|
+
* @returns AI prediction with reasoning and context
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const signal = await oracle.queryKalshiSignal({
|
|
83
|
+
* marketTicker: 'FED_RATES_MAR26',
|
|
84
|
+
* maxPriceBps: 9500,
|
|
85
|
+
* });
|
|
86
|
+
*
|
|
87
|
+
* console.log('Predicted side:', signal.side);
|
|
88
|
+
* console.log('Confidence:', signal.confidence, '%');
|
|
89
|
+
* console.log('Reasoning:', signal.reasoning);
|
|
90
|
+
* console.log('Kalshi market YES bid:', signal.kalshiContext.marketSnapshot.yesBidBps / 100, '%');
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
async queryKalshiSignal(query) {
|
|
94
|
+
try {
|
|
95
|
+
// ========= STEP 1: INITIAL REQUEST (EXPECTS 402) =========
|
|
96
|
+
this.log('Querying MCP Gateway...');
|
|
97
|
+
const initialResponse = await fetch(`${this.mcpGatewayUrl}/api/predict`, {
|
|
98
|
+
method: 'POST',
|
|
99
|
+
headers: { 'Content-Type': 'application/json' },
|
|
100
|
+
body: JSON.stringify({
|
|
101
|
+
market_ticker: query.marketTicker,
|
|
102
|
+
max_price_bps: query.maxPriceBps || 10000,
|
|
103
|
+
}),
|
|
104
|
+
});
|
|
105
|
+
// Check if payment is required
|
|
106
|
+
if (initialResponse.status === 402) {
|
|
107
|
+
this.log('Payment required. Executing x402 micro-transaction...');
|
|
108
|
+
// ========= STEP 2: EXTRACT PAYMENT HEADERS =========
|
|
109
|
+
const paymentRequired = initialResponse.headers.get('x-mpp-version');
|
|
110
|
+
const paymentAmount = initialResponse.headers.get('x-payment-required-amount');
|
|
111
|
+
if (!paymentRequired || !paymentAmount) {
|
|
112
|
+
throw new Error('Invalid 402 response: missing payment headers');
|
|
113
|
+
}
|
|
114
|
+
// ========= STEP 3: EXECUTE MICRO-PAYMENT =========
|
|
115
|
+
const paymentStatus = await this.executeMicroPayment(query.timeoutMs);
|
|
116
|
+
if (!paymentStatus.success || !paymentStatus.txHash) {
|
|
117
|
+
throw new Error(`Payment failed: ${paymentStatus.error}`);
|
|
118
|
+
}
|
|
119
|
+
this.log(`Payment confirmed: ${paymentStatus.txHash}`);
|
|
120
|
+
// ========= STEP 4: RETRY REQUEST WITH PAYMENT PROOF =========
|
|
121
|
+
this.log('Retrying request with payment proof...');
|
|
122
|
+
const paidResponse = await fetch(`${this.mcpGatewayUrl}/api/predict`, {
|
|
123
|
+
method: 'POST',
|
|
124
|
+
headers: {
|
|
125
|
+
'Content-Type': 'application/json',
|
|
126
|
+
'x-solana-payment-tx': paymentStatus.txHash,
|
|
127
|
+
'x-solana-payment-signature': this.userPublicKey.toString(),
|
|
128
|
+
'x-machine-public-key': this.userPublicKey.toString(),
|
|
129
|
+
},
|
|
130
|
+
body: JSON.stringify({
|
|
131
|
+
market_ticker: query.marketTicker,
|
|
132
|
+
max_price_bps: query.maxPriceBps || 10000,
|
|
133
|
+
}),
|
|
134
|
+
});
|
|
135
|
+
if (!paidResponse.ok) {
|
|
136
|
+
throw new Error(`Request failed after payment: ${paidResponse.statusText}`);
|
|
137
|
+
}
|
|
138
|
+
// ========= STEP 5: PARSE AND RETURN PREDICTION =========
|
|
139
|
+
const responseData = (await paidResponse.json());
|
|
140
|
+
return this.parseKalshiSignal(responseData, query.marketTicker);
|
|
141
|
+
}
|
|
142
|
+
else if (initialResponse.ok) {
|
|
143
|
+
// Payment already made (e.g., subscription)
|
|
144
|
+
const responseData = (await initialResponse.json());
|
|
145
|
+
return this.parseKalshiSignal(responseData, query.marketTicker);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
throw new Error(`Request failed: ${initialResponse.status} ${initialResponse.statusText}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
throw this.formatError('QUERY_FAILED', error);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Execute atomic 0.05 USDC micro-payment to treasury
|
|
157
|
+
*
|
|
158
|
+
* @private
|
|
159
|
+
* @param timeoutMs - Optional timeout
|
|
160
|
+
* @returns Payment status with transaction hash
|
|
161
|
+
*/
|
|
162
|
+
async executeMicroPayment(timeoutMs) {
|
|
163
|
+
try {
|
|
164
|
+
// Get user's USDC token account
|
|
165
|
+
const userTokenAccount = await (0, spl_token_1.getAssociatedTokenAddress)(this.USDC_MINT, this.userPublicKey);
|
|
166
|
+
// Get treasury's USDC token account
|
|
167
|
+
const treasuryTokenAccount = await (0, spl_token_1.getAssociatedTokenAddress)(this.USDC_MINT, this.TREASURY_WALLET);
|
|
168
|
+
// Build transaction
|
|
169
|
+
const transaction = new web3_js_1.Transaction();
|
|
170
|
+
// Add SPL token transfer instruction
|
|
171
|
+
transaction.add((0, spl_token_1.createTransferInstruction)(userTokenAccount, treasuryTokenAccount, this.userPublicKey, this.PAYMENT_AMOUNT_CENTS, // 0.05 USDC in cents
|
|
172
|
+
[], spl_token_1.TOKEN_PROGRAM_ID));
|
|
173
|
+
// Sign and send with timeout
|
|
174
|
+
const startTime = Date.now();
|
|
175
|
+
const timeout = timeoutMs || 30000;
|
|
176
|
+
const txHash = await Promise.race([
|
|
177
|
+
this.sendAndConfirmTx(transaction),
|
|
178
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error('Payment transaction timeout')), timeout)),
|
|
179
|
+
]);
|
|
180
|
+
const elapsedTime = Date.now() - startTime;
|
|
181
|
+
this.log(`Payment executed in ${elapsedTime}ms`);
|
|
182
|
+
return {
|
|
183
|
+
success: true,
|
|
184
|
+
txHash,
|
|
185
|
+
amountPaidCents: this.PAYMENT_AMOUNT_CENTS,
|
|
186
|
+
timestamp: Date.now(),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
return {
|
|
191
|
+
success: false,
|
|
192
|
+
error: error instanceof Error ? error.message : String(error),
|
|
193
|
+
timestamp: Date.now(),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Parse MCP Gateway response into KalshiSignal
|
|
199
|
+
*
|
|
200
|
+
* @private
|
|
201
|
+
*/
|
|
202
|
+
parseKalshiSignal(data, marketTicker) {
|
|
203
|
+
const prediction = data.prediction || {};
|
|
204
|
+
return {
|
|
205
|
+
marketTicker,
|
|
206
|
+
side: prediction.predicted_outcome === 'YES' ? 'YES' : 'NO',
|
|
207
|
+
amount: prediction.amount_usdc || 0,
|
|
208
|
+
confidence: prediction.confidence || 0,
|
|
209
|
+
riskScore: prediction.risk_score || 0,
|
|
210
|
+
reasoning: prediction.reasoning || '',
|
|
211
|
+
timestamp: Date.now(),
|
|
212
|
+
predictionId: `pred_${Date.now()}`,
|
|
213
|
+
kalshiContext: {
|
|
214
|
+
marketSnapshot: {
|
|
215
|
+
yesBidBps: prediction.kalshi_yes_bid_bps || 5000,
|
|
216
|
+
noBidBps: prediction.kalshi_no_bid_bps || 5000,
|
|
217
|
+
isLive: prediction.kalshi_is_live || false,
|
|
218
|
+
},
|
|
219
|
+
topTradersConsensus: {
|
|
220
|
+
consensusSide: prediction.top_traders_side || 'YES',
|
|
221
|
+
consensusBps: prediction.top_traders_bps || 5000,
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Send and confirm transaction (supports both keypair and wallet adapter)
|
|
228
|
+
*
|
|
229
|
+
* @private
|
|
230
|
+
*/
|
|
231
|
+
async sendAndConfirmTx(transaction) {
|
|
232
|
+
if (this.keypair) {
|
|
233
|
+
// Backend mode: manual signing
|
|
234
|
+
transaction.sign(this.keypair);
|
|
235
|
+
const serialized = transaction.serialize();
|
|
236
|
+
const txHash = await this.connection.sendRawTransaction(serialized, {
|
|
237
|
+
skipPreflight: false,
|
|
238
|
+
maxRetries: 5,
|
|
239
|
+
});
|
|
240
|
+
await this.connection.confirmTransaction(txHash, 'confirmed');
|
|
241
|
+
return txHash;
|
|
242
|
+
}
|
|
243
|
+
else if (this.signTransaction) {
|
|
244
|
+
// Frontend mode: Use wallet adapter signing (non-custodial)
|
|
245
|
+
try {
|
|
246
|
+
const signedTx = await this.signTransaction(transaction);
|
|
247
|
+
const serialized = signedTx.serialize();
|
|
248
|
+
const txHash = await this.connection.sendRawTransaction(serialized, {
|
|
249
|
+
skipPreflight: false,
|
|
250
|
+
maxRetries: 5,
|
|
251
|
+
});
|
|
252
|
+
await this.connection.confirmTransaction(txHash, 'confirmed');
|
|
253
|
+
return txHash;
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
throw new Error(`Transaction signing failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
throw new Error('No signer available');
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Format errors consistently
|
|
265
|
+
*
|
|
266
|
+
* @private
|
|
267
|
+
*/
|
|
268
|
+
formatError(code, error) {
|
|
269
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
270
|
+
return new Error(`[OracleClient] ${code}: ${message}`);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Logging utility
|
|
274
|
+
*
|
|
275
|
+
* @private
|
|
276
|
+
*/
|
|
277
|
+
log(message) {
|
|
278
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
279
|
+
console.log(`[OracleClient] ${message}`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
exports.OracleClient = OracleClient;
|
|
284
|
+
exports.default = OracleClient;
|
|
285
|
+
//# sourceMappingURL=oracle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oracle.js","sourceRoot":"","sources":["../../src/clients/oracle.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,6CAMyB;AACzB,iDAI2B;AAQ3B;;;;;;;;;;;;GAYG;AACH,MAAa,YAAY;IAqBvB;;;;;;OAMG;IACH,YAAY,MAAuB;QApBnC,oDAAoD;QACnC,oBAAe,GAAG,IAAI,mBAAS,CAC9C,8CAA8C,CAC/C,CAAC;QAEF,uBAAuB;QACN,cAAS,GAAG,IAAI,mBAAS,CACxC,6CAA6C,CAC9C,CAAC;QAEF,wDAAwD;QACvC,yBAAoB,GAAG,CAAC,CAAC;QAUxC,IAAI,CAAC,UAAU,GAAG,IAAI,oBAAU,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,IAAI,WAAW,CAAC,CAAC;QAElF,+CAA+C;QAC/C,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YACnB,IAAI,CAAC,OAAO,GAAG,iBAAO,CAAC,aAAa,CAAC,MAAM,CAAC,OAAO,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
|
|
3
|
+
*
|
|
4
|
+
* Handles all NeuralVault operations without requiring
|
|
5
|
+
* developers to understand Anchor PDAs or cryptography.
|
|
6
|
+
*
|
|
7
|
+
* Simple API:
|
|
8
|
+
* - vault.initializeVault()
|
|
9
|
+
* - vault.deposit(amountUsdc)
|
|
10
|
+
* - vault.setRiskLimits({ maxPositionBps })
|
|
11
|
+
*/
|
|
12
|
+
import type { SentinelyConfig, VaultInitParams, RiskLimits, DepositParams, VaultState, VaultInitResponse, DepositResponse, RiskUpdateResponse } from '../types';
|
|
13
|
+
/**
|
|
14
|
+
* VaultClient: Institutional vault management without Anchor complexity
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const vault = new VaultClient(config);
|
|
19
|
+
* await vault.initializeVault({ maxPositionBps: 500 });
|
|
20
|
+
* await vault.deposit({ amountUsdc: 10000 });
|
|
21
|
+
* await vault.setRiskLimits({ maxPositionBps: 300 });
|
|
22
|
+
* const state = await vault.getVaultState();
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare class VaultClient {
|
|
26
|
+
private connection;
|
|
27
|
+
private keypair?;
|
|
28
|
+
private signTransaction?;
|
|
29
|
+
private programId;
|
|
30
|
+
private userPublicKey;
|
|
31
|
+
/** USDC Devnet mint */
|
|
32
|
+
private readonly USDC_MINT;
|
|
33
|
+
/**
|
|
34
|
+
* Initialize VaultClient
|
|
35
|
+
*
|
|
36
|
+
* Supports two modes:
|
|
37
|
+
* 1. Backend mode: Pass keypair for server-side signing
|
|
38
|
+
* 2. Frontend mode: Pass signTransaction + publicKey for wallet adapter (non-custodial)
|
|
39
|
+
*
|
|
40
|
+
* @param config - SDK configuration
|
|
41
|
+
*/
|
|
42
|
+
constructor(config: SentinelyConfig);
|
|
43
|
+
/**
|
|
44
|
+
* Initialize a new NeuralVault PDA
|
|
45
|
+
*
|
|
46
|
+
* Abstracts away:
|
|
47
|
+
* - PDA derivation
|
|
48
|
+
* - Associated token account creation
|
|
49
|
+
* - Anchor instruction building
|
|
50
|
+
* - Rent funding
|
|
51
|
+
*
|
|
52
|
+
* @param params - Vault initialization parameters
|
|
53
|
+
* @returns Vault public key and transaction hash
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const response = await vault.initializeVault({
|
|
58
|
+
* maxPositionBps: 500, // 5% max position
|
|
59
|
+
* riskLevel: 60,
|
|
60
|
+
* });
|
|
61
|
+
* console.log('Vault created:', response.vaultPubkey);
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
initializeVault(params: VaultInitParams): Promise<VaultInitResponse>;
|
|
65
|
+
/**
|
|
66
|
+
* Deposit USDC into vault
|
|
67
|
+
*
|
|
68
|
+
* Abstracts away:
|
|
69
|
+
* - SPL token transfer
|
|
70
|
+
* - Anchor instruction encoding
|
|
71
|
+
* - Transaction signing
|
|
72
|
+
*
|
|
73
|
+
* @param params - Deposit parameters (amount in USDC)
|
|
74
|
+
* @returns Deposit confirmation and new balance
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const response = await vault.deposit({ amountUsdc: 5000 });
|
|
79
|
+
* console.log('Deposited:', response.amountUsdc);
|
|
80
|
+
* console.log('New balance:', response.newBalanceUsdc);
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
deposit(params: DepositParams): Promise<DepositResponse>;
|
|
84
|
+
/**
|
|
85
|
+
* Update vault risk parameters
|
|
86
|
+
*
|
|
87
|
+
* Allows programmatic adjustment of:
|
|
88
|
+
* - Max position size (in basis points)
|
|
89
|
+
* - Risk tolerance level
|
|
90
|
+
*
|
|
91
|
+
* Abstracts away:
|
|
92
|
+
* - Anchor instruction building
|
|
93
|
+
* - Fixed-point BPS math
|
|
94
|
+
* - Transaction signing
|
|
95
|
+
*
|
|
96
|
+
* @param limits - New risk limits
|
|
97
|
+
* @returns Confirmation and updated parameters
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const response = await vault.setRiskLimits({
|
|
102
|
+
* maxPositionBps: 300, // Reduce to 3%
|
|
103
|
+
* riskLevel: 40,
|
|
104
|
+
* });
|
|
105
|
+
* console.log('Updated risk limit to:', response.maxPositionBps / 100, '%');
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
setRiskLimits(limits: RiskLimits): Promise<RiskUpdateResponse>;
|
|
109
|
+
/**
|
|
110
|
+
* Fetch current vault state from chain
|
|
111
|
+
*
|
|
112
|
+
* Returns:
|
|
113
|
+
* - Balance
|
|
114
|
+
* - Risk parameters
|
|
115
|
+
* - Activity status
|
|
116
|
+
* - Fee metrics
|
|
117
|
+
*
|
|
118
|
+
* @returns Current vault state
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const state = await vault.getVaultState();
|
|
123
|
+
* console.log('Balance:', state.balanceCents / 100, 'USDC');
|
|
124
|
+
* console.log('Max position:', state.maxPositionBps / 100, '%');
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
getVaultState(): Promise<VaultState>;
|
|
128
|
+
/**
|
|
129
|
+
* Withdraw USDC from vault
|
|
130
|
+
*
|
|
131
|
+
* @param amountUsdc - Amount to withdraw
|
|
132
|
+
* @returns Withdrawal confirmation
|
|
133
|
+
*/
|
|
134
|
+
withdraw(amountUsdc: number): Promise<DepositResponse>;
|
|
135
|
+
/**
|
|
136
|
+
* Send and confirm transaction (supports both keypair and wallet adapter)
|
|
137
|
+
*
|
|
138
|
+
* @private
|
|
139
|
+
*/
|
|
140
|
+
private sendAndConfirmTx;
|
|
141
|
+
/**
|
|
142
|
+
* Read BigUInt64LE from buffer (for u64 fields)
|
|
143
|
+
*
|
|
144
|
+
* @private
|
|
145
|
+
*/
|
|
146
|
+
private readBigUInt64LE;
|
|
147
|
+
/**
|
|
148
|
+
* Build instruction data for initialize_vault
|
|
149
|
+
*
|
|
150
|
+
* @private
|
|
151
|
+
*/
|
|
152
|
+
private buildInitializeVaultIx;
|
|
153
|
+
/**
|
|
154
|
+
* Get sysvar rent address
|
|
155
|
+
*
|
|
156
|
+
* @private
|
|
157
|
+
*/
|
|
158
|
+
private getSysvarRent;
|
|
159
|
+
/**
|
|
160
|
+
* Format errors with context and consistency
|
|
161
|
+
*
|
|
162
|
+
* @private
|
|
163
|
+
*/
|
|
164
|
+
private formatError;
|
|
165
|
+
/**
|
|
166
|
+
* Logging utility
|
|
167
|
+
*
|
|
168
|
+
* @private
|
|
169
|
+
*/
|
|
170
|
+
private log;
|
|
171
|
+
}
|
|
172
|
+
export default VaultClient;
|
|
173
|
+
//# sourceMappingURL=vault.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vault.d.ts","sourceRoot":"","sources":["../../src/clients/vault.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAgBH,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EACf,UAAU,EACV,aAAa,EACb,UAAU,EACV,iBAAiB,EACjB,eAAe,EACf,kBAAkB,EACnB,MAAM,UAAU,CAAC;AAElB;;;;;;;;;;;GAWG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,OAAO,CAAC,CAAU;IAC1B,OAAO,CAAC,eAAe,CAAC,CAAqC;IAC7D,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,aAAa,CAAY;IAEjC,uBAAuB;IACvB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAExB;IAEF;;;;;;;;OAQG;gBACS,MAAM,EAAE,eAAe;IAuBnC;;;;;;;;;;;;;;;;;;;;OAoBG;IACG,eAAe,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAwE1E;;;;;;;;;;;;;;;;;OAiBG;IACG,OAAO,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,eAAe,C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