1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
use jsonrpc_core::{BoxFuture, Result};
use jsonrpc_derive::rpc;
use solana_account_decoder::{
parse_account_data::SplTokenAdditionalDataV2,
parse_token::{parse_token_v3, TokenAccountType, UiTokenAmount},
UiAccount,
};
use solana_client::{
rpc_config::RpcAccountInfoConfig,
rpc_response::{RpcBlockCommitment, RpcResponseContext},
};
use solana_clock::Slot;
use solana_commitment_config::CommitmentConfig;
use solana_rpc_client_api::response::Response as RpcResponse;
use solana_runtime::commitment::BlockCommitmentArray;
use solana_sdk::program_pack::Pack;
use spl_token::state::{Account as TokenAccount, Mint};
use super::{not_implemented_err, RunloopContext, SurfnetRpcContext};
use crate::{
error::{SurfpoolError, SurfpoolResult},
rpc::{utils::verify_pubkey, State},
surfnet::locker::SvmAccessContext,
};
#[rpc]
pub trait AccountsData {
type Metadata;
/// Returns detailed information about an account given its public key.
///
/// This method queries the blockchain for the account associated with the provided
/// public key string. It can be used to inspect balances, ownership, and program-related metadata.
///
/// ## Parameters
/// - `pubkey_str`: A base-58 encoded string representing the account's public key.
/// - `config`: Optional configuration that controls encoding, commitment level,
/// data slicing, and other response details.
///
/// ## Returns
/// A [`RpcResponse`] containing an optional [`UiAccount`] object if the account exists.
/// If the account does not exist, the response will contain `null`.
///
/// ## Example Request (JSON-RPC)
/// ```json
/// {
/// "jsonrpc": "2.0",
/// "id": 1,
/// "method": "getAccountInfo",
/// "params": [
/// "9XQeWMPMPXwW1fzLEQeTTrfF5Eb9dj8Qs3tCPoMw3GiE",
/// {
/// "encoding": "jsonParsed",
/// "commitment": "finalized"
/// }
/// ]
/// }
/// ```
///
/// ## Example Response
/// ```json
/// {
/// "jsonrpc": "2.0",
/// "result": {
/// "context": {
/// "slot": 12345678
/// },
/// "value": {
/// "lamports": 10000000,
/// "data": {
/// "program": "spl-token",
/// "parsed": { ... },
/// "space": 165
/// },
/// "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
/// "executable": false,
/// "rentEpoch": 203,
/// "space": 165
/// }
/// },
/// "id": 1
/// }
/// ```
///
/// ## Errors
/// - Returns an error if the public key is malformed or invalid
/// - Returns an internal error if the ledger cannot be accessed
///
/// ## See also
/// - [`UiAccount`]: A readable structure representing on-chain accounts
#[rpc(meta, name = "getAccountInfo")]
fn get_account_info(
&self,
meta: Self::Metadata,
pubkey_str: String,
config: Option<RpcAccountInfoConfig>,
) -> BoxFuture<Result<RpcResponse<Option<UiAccount>>>>;
/// Returns commitment levels for a given block (slot).
///
/// This method provides insight into how many validators have voted for a specific block
/// and with what level of lockout. This can be used to analyze consensus progress and
/// determine finality confidence.
///
/// ## Parameters
/// - `block`: The target slot (block) to query.
///
/// ## Returns
/// A [`RpcBlockCommitment`] containing a [`BlockCommitmentArray`], which is an array of 32
/// integers representing the number of votes at each lockout level for that block. Each index
/// corresponds to a lockout level (i.e., confidence in finality).
///
/// ## Example Request (JSON-RPC)
/// ```json
/// {
/// "jsonrpc": "2.0",
/// "id": 1,
/// "method": "getBlockCommitment",
/// "params": [150000000]
/// }
/// ```
///
/// ## Example Response
/// ```json
/// {
/// "jsonrpc": "2.0",
/// "result": {
/// "commitment": [0, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
/// "totalStake": 100000000
/// },
/// "id": 1
/// }
/// ```
///
/// ## Errors
/// - If the slot is not found in the current bank or has been purged, this call may return an error.
/// - May fail if the RPC node is lagging behind or doesn't have voting history for the slot.
///
/// ## See also
/// - [`BlockCommitmentArray`]: An array representing votes by lockout level
/// - [`RpcBlockCommitment`]: Wrapper struct for the full response
#[rpc(meta, name = "getBlockCommitment")]
fn get_block_commitment(
&self,
meta: Self::Metadata,
block: Slot,
) -> Result<RpcBlockCommitment<BlockCommitmentArray>>;
/// Returns account information for multiple public keys in a single call.
///
/// This method allows batching of account lookups for improved performance and fewer
/// network roundtrips. It returns a list of `UiAccount` values in the same order as
/// the provided public keys.
///
/// ## Parameters
/// - `pubkey_strs`: A list of base-58 encoded public key strings representing accounts to query.
/// - `config`: Optional configuration to control encoding, commitment level, data slicing, etc.
///
/// ## Returns
/// A [`RpcResponse`] wrapping a vector of optional [`UiAccount`] objects.
/// Each element in the response corresponds to the public key at the same index in the request.
/// If an account is not found, the corresponding entry will be `null`.
///
/// ## Example Request (JSON-RPC)
/// ```json
/// {
/// "jsonrpc": "2.0",
/// "id": 1,
/// "method": "getMultipleAccounts",
/// "params": [
/// [
/// "9XQeWMPMPXwW1fzLEQeTTrfF5Eb9dj8Qs3tCPoMw3GiE",
/// "3nN8SBQ2HqTDNnaCzryrSv4YHd4d6GpVCEyDhKMPxN4o"
/// ],
/// {
/// "encoding": "jsonParsed",
/// "commitment": "confirmed"
/// }
/// ]
/// }
/// ```
///
/// ## Example Response
/// ```json
/// {
/// "jsonrpc": "2.0",
/// "result": {
/// "context": { "slot": 12345678 },
/// "value": [
/// {
/// "lamports": 10000000,
/// "data": {
/// "program": "spl-token",
/// "parsed": { ... },
/// "space": 165
/// },
/// "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
/// "executable": false,
/// "rentEpoch": 203,
/// "space": 165
/// },
/// null
/// ]
/// },
/// "id": 1
/// }
/// ```
///
/// ## Errors
/// - If any public key is malformed or invalid, the entire call may fail.
/// - Returns an internal error if the ledger cannot be accessed or some accounts are purged.
///
/// ## See also
/// - [`UiAccount`]: Human-readable representation of an account
/// - [`get_account_info`]: Use when querying a single account
#[rpc(meta, name = "getMultipleAccounts")]
fn get_multiple_accounts(
&self,
meta: Self::Metadata,
pubkey_strs: Vec<String>,
config: Option<RpcAccountInfoConfig>,
) -> BoxFuture<Result<RpcResponse<Vec<Option<UiAccount>>>>>;
/// Returns the balance of a token account, given its public key.
///
/// This method fetches the token balance of an account, including its amount and
/// user-friendly information (like the UI amount in human-readable format). It is useful
/// for token-related applications, such as checking balances in wallets or exchanges.
///
/// ## Parameters
/// - `pubkey_str`: The base-58 encoded string of the public key of the token account.
/// - `commitment`: Optional commitment configuration to specify the desired confirmation level of the query.
///
/// ## Returns
/// A [`RpcResponse`] containing the token balance in a [`UiTokenAmount`] struct.
/// If the account doesn't hold any tokens or is invalid, the response will contain `null`.
///
/// ## Example Request (JSON-RPC)
/// ```json
/// {
/// "jsonrpc": "2.0",
/// "id": 1,
/// "method": "getTokenAccountBalance",
/// "params": [
/// "3nN8SBQ2HqTDNnaCzryrSv4YHd4d6GpVCEyDhKMPxN4o",
/// {
/// "commitment": "confirmed"
/// }
/// ]
/// }
/// ```
///
/// ## Example Response
/// ```json
/// {
/// "jsonrpc": "2.0",
/// "result": {
/// "context": {
/// "slot": 12345678
/// },
/// "value": {
/// "uiAmount": 100.0,
/// "decimals": 6,
/// "amount": "100000000",
/// "uiAmountString": "100.000000"
/// }
/// },
/// "id": 1
/// }
/// ```
///
/// ## Errors
/// - If the provided public key is invalid or does not exist.
/// - If the account is not a valid token account or does not hold any tokens.
///
/// ## See also
/// - [`UiTokenAmount`]: Represents the token balance in user-friendly format.
#[rpc(meta, name = "getTokenAccountBalance")]
fn get_token_account_balance(
&self,
meta: Self::Metadata,
pubkey_str: String,
commitment: Option<CommitmentConfig>,
) -> BoxFuture<Result<RpcResponse<Option<UiTokenAmount>>>>;
/// Returns the total supply of a token, given its mint address.
///
/// This method provides the total circulating supply of a specific token, including the raw
/// amount and human-readable UI-formatted values. It can be useful for tracking token issuance
/// and verifying the supply of a token on-chain.
///
/// ## Parameters
/// - `mint_str`: The base-58 encoded string of the mint address for the token.
/// - `commitment`: Optional commitment configuration to specify the desired confirmation level of the query.
///
/// ## Returns
/// A [`RpcResponse`] containing the total token supply in a [`UiTokenAmount`] struct.
/// If the token does not exist or is invalid, the response will return an error.
///
/// ## Example Request (JSON-RPC)
/// ```json
/// {
/// "jsonrpc": "2.0",
/// "id": 1,
/// "method": "getTokenSupply",
/// "params": [
/// "So11111111111111111111111111111111111111112",
/// {
/// "commitment": "confirmed"
/// }
/// ]
/// }
/// ```
///
/// ## Example Response
/// ```json
/// {
/// "jsonrpc": "2.0",
/// "result": {
/// "context": {
/// "slot": 12345678
/// },
/// "value": {
/// "uiAmount": 1000000000.0,
/// "decimals": 6,
/// "amount": "1000000000000000",
/// "uiAmountString": "1000000000.000000"
/// }
/// },
/// "id": 1
/// }
/// ```
///
/// ## Errors
/// - If the mint address is invalid or does not correspond to a token.
/// - If the token supply cannot be fetched due to network issues or node synchronization problems.
///
/// ## See also
/// - [`UiTokenAmount`]: Represents the token balance or supply in a user-friendly format.
#[rpc(meta, name = "getTokenSupply")]
fn get_token_supply(
&self,
meta: Self::Metadata,
mint_str: String,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<UiTokenAmount>>;
}
#[derive(Clone)]
pub struct SurfpoolAccountsDataRpc;
impl AccountsData for SurfpoolAccountsDataRpc {
type Metadata = Option<RunloopContext>;
fn get_account_info(
&self,
meta: Self::Metadata,
pubkey_str: String,
config: Option<RpcAccountInfoConfig>,
) -> BoxFuture<Result<RpcResponse<Option<UiAccount>>>> {
let config = config.unwrap_or_default();
let pubkey = match verify_pubkey(&pubkey_str) {
Ok(res) => res,
Err(e) => return e.into(),
};
let SurfnetRpcContext {
svm_locker,
remote_ctx,
} = match meta.get_rpc_context(config.commitment.unwrap_or_default()) {
Ok(res) => res,
Err(e) => return e.into(),
};
Box::pin(async move {
let SvmAccessContext {
slot,
inner: account_update,
..
} = svm_locker.get_account(&remote_ctx, &pubkey, None).await?;
svm_locker.write_account_update(account_update.clone());
Ok(RpcResponse {
context: RpcResponseContext::new(slot),
value: account_update.try_into_ui_account(config.encoding, config.data_slice),
})
})
}
fn get_multiple_accounts(
&self,
meta: Self::Metadata,
pubkeys_str: Vec<String>,
config: Option<RpcAccountInfoConfig>,
) -> BoxFuture<Result<RpcResponse<Vec<Option<UiAccount>>>>> {
let config = config.unwrap_or_default();
let pubkeys = match pubkeys_str
.iter()
.map(|s| verify_pubkey(s))
.collect::<SurfpoolResult<Vec<_>>>()
{
Ok(p) => p,
Err(e) => return e.into(),
};
let SurfnetRpcContext {
svm_locker,
remote_ctx,
} = match meta.get_rpc_context(config.commitment.unwrap_or_default()) {
Ok(res) => res,
Err(e) => return e.into(),
};
Box::pin(async move {
let SvmAccessContext {
slot,
inner: account_updates,
..
} = svm_locker
.get_multiple_accounts(&remote_ctx, &pubkeys, None)
.await?;
svm_locker.write_multiple_account_updates(&account_updates);
let mut ui_accounts = vec![];
{
for account_update in account_updates.into_iter() {
ui_accounts.push(
account_update.try_into_ui_account(config.encoding, config.data_slice),
);
}
}
Ok(RpcResponse {
context: RpcResponseContext::new(slot),
value: ui_accounts,
})
})
}
fn get_block_commitment(
&self,
_meta: Self::Metadata,
_block: Slot,
) -> Result<RpcBlockCommitment<BlockCommitmentArray>> {
not_implemented_err("get_block_commitment")
}
// SPL Token-specific RPC endpoints
// See https://github.com/solana-labs/solana-program-library/releases/tag/token-v2.0.0 for
// program details
fn get_token_account_balance(
&self,
meta: Self::Metadata,
pubkey_str: String,
commitment: Option<CommitmentConfig>,
) -> BoxFuture<Result<RpcResponse<Option<UiTokenAmount>>>> {
let pubkey = match verify_pubkey(&pubkey_str) {
Ok(res) => res,
Err(e) => return e.into(),
};
let SurfnetRpcContext {
svm_locker,
remote_ctx,
} = match meta.get_rpc_context(commitment.unwrap_or_default()) {
Ok(res) => res,
Err(e) => return e.into(),
};
Box::pin(async move {
let token_account_result = svm_locker
.get_account(&remote_ctx, &pubkey, None)
.await?
.inner;
svm_locker.write_account_update(token_account_result.clone());
let token_account = token_account_result.map_account()?;
let unpacked_token_account =
TokenAccount::unpack(&token_account.data).map_err(|e| {
SurfpoolError::invalid_account_data(
pubkey,
"Invalid token account data",
Some(e.to_string()),
)
})?;
let SvmAccessContext {
slot,
inner: mint_account_result,
..
} = svm_locker
.get_account(&remote_ctx, &unpacked_token_account.mint, None)
.await?;
svm_locker.write_account_update(mint_account_result.clone());
let mint_account = mint_account_result.map_account()?;
let unpacked_mint_account = Mint::unpack(&mint_account.data).map_err(|e| {
SurfpoolError::invalid_account_data(
unpacked_token_account.mint,
"Invalid token mint account data",
Some(e.to_string()),
)
})?;
let token_decimals = unpacked_mint_account.decimals;
Ok(RpcResponse {
context: RpcResponseContext::new(slot),
value: {
parse_token_v3(
&token_account.data,
Some(&SplTokenAdditionalDataV2 {
decimals: token_decimals,
..Default::default()
}),
)
.ok()
.and_then(|t| match t {
TokenAccountType::Account(account) => Some(account.token_amount),
_ => None,
})
},
})
})
}
fn get_token_supply(
&self,
_meta: Self::Metadata,
_mint_str: String,
_commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<UiTokenAmount>> {
not_implemented_err("get_token_supply")
}
}
#[cfg(test)]
mod tests {
use solana_account::Account;
use solana_pubkey::Pubkey;
use solana_sdk::program_pack::Pack;
use spl_token::state::{Account as TokenAccount, AccountState, Mint};
use super::*;
use crate::{surfnet::GetAccountResult, tests::helpers::TestSetup};
#[ignore = "connection-required"]
#[tokio::test(flavor = "multi_thread")]
async fn test_get_token_account_balance() {
let setup = TestSetup::new(SurfpoolAccountsDataRpc);
let mint_pk = Pubkey::new_unique();
let minimum_rent = setup.context.svm_locker.with_svm_reader(|svm_reader| {
svm_reader
.inner
.minimum_balance_for_rent_exemption(Mint::LEN)
});
let mut data = [0; Mint::LEN];
let default = Mint {
decimals: 6,
supply: 1000000000000000,
is_initialized: true,
..Default::default()
};
default.pack_into_slice(&mut data);
let mint_account = Account {
lamports: minimum_rent,
owner: spl_token::ID,
executable: false,
rent_epoch: 0,
data: data.to_vec(),
};
setup
.context
.svm_locker
.write_account_update(GetAccountResult::FoundAccount(mint_pk, mint_account, true));
let token_account_pk = Pubkey::new_unique();
let minimum_rent = setup.context.svm_locker.with_svm_reader(|svm_reader| {
svm_reader
.inner
.minimum_balance_for_rent_exemption(TokenAccount::LEN)
});
let mut data = [0; TokenAccount::LEN];
let default = TokenAccount {
mint: mint_pk,
owner: spl_token::ID,
state: AccountState::Initialized,
amount: 100 * 1000000,
..Default::default()
};
default.pack_into_slice(&mut data);
let token_account = Account {
lamports: minimum_rent,
owner: spl_token::ID,
executable: false,
rent_epoch: 0,
data: data.to_vec(),
};
setup
.context
.svm_locker
.write_account_update(GetAccountResult::FoundAccount(
token_account_pk,
token_account,
true,
));
let res = setup
.rpc
.get_token_account_balance(Some(setup.context), token_account_pk.to_string(), None)
.await
.unwrap();
assert_eq!(
res.value.unwrap(),
UiTokenAmount {
amount: String::from("100000000"),
decimals: 6,
ui_amount: Some(100.0),
ui_amount_string: String::from("100")
}
);
}
}