Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix token rpc-client methods #11361

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion client/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,15 @@ fn parse_keyed_accounts(
request,
)
})?;
pubkey_accounts.push((pubkey, account.decode().unwrap()));
pubkey_accounts.push((
pubkey,
account.decode().ok_or_else(|| {
ClientError::new_with_request(
RpcError::ParseError("Account from rpc".to_string()).into(),
request,
)
})?,
));
}
Ok(pubkey_accounts)
}
Expand Down
28 changes: 16 additions & 12 deletions core/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,9 +939,11 @@ impl JsonRpcRequestProcessor {
&self,
owner: &Pubkey,
token_account_filter: TokenAccountsFilter,
commitment: Option<CommitmentConfig>,
config: Option<RpcAccountInfoConfig>,
) -> Result<RpcResponse<Vec<RpcKeyedAccount>>> {
let bank = self.bank(commitment);
let config = config.unwrap_or_default();
let bank = self.bank(config.commitment);
let encoding = config.encoding.unwrap_or(UiAccountEncoding::Binary);
let (token_program_id, mint) = get_token_program_id_and_mint(&bank, token_account_filter)?;

let mut filters = vec![
Expand All @@ -965,7 +967,7 @@ impl JsonRpcRequestProcessor {
let accounts = get_filtered_program_accounts(&bank, &token_program_id, filters)
.map(|(pubkey, account)| RpcKeyedAccount {
pubkey: pubkey.to_string(),
account: UiAccount::encode(account, UiAccountEncoding::JsonParsed),
account: UiAccount::encode(account, encoding.clone()),
})
.collect();
Ok(new_response(&bank, accounts))
Expand All @@ -975,9 +977,11 @@ impl JsonRpcRequestProcessor {
&self,
delegate: &Pubkey,
token_account_filter: TokenAccountsFilter,
commitment: Option<CommitmentConfig>,
config: Option<RpcAccountInfoConfig>,
) -> Result<RpcResponse<Vec<RpcKeyedAccount>>> {
let bank = self.bank(commitment);
let config = config.unwrap_or_default();
let bank = self.bank(config.commitment);
let encoding = config.encoding.unwrap_or(UiAccountEncoding::Binary);
let (token_program_id, mint) = get_token_program_id_and_mint(&bank, token_account_filter)?;

let mut filters = vec![
Expand Down Expand Up @@ -1009,7 +1013,7 @@ impl JsonRpcRequestProcessor {
let accounts = get_filtered_program_accounts(&bank, &token_program_id, filters)
.map(|(pubkey, account)| RpcKeyedAccount {
pubkey: pubkey.to_string(),
account: UiAccount::encode(account, UiAccountEncoding::JsonParsed),
account: UiAccount::encode(account, encoding.clone()),
})
.collect();
Ok(new_response(&bank, accounts))
Expand Down Expand Up @@ -1402,7 +1406,7 @@ pub trait RpcSol {
meta: Self::Metadata,
owner_str: String,
token_account_filter: RpcTokenAccountsFilter,
commitment: Option<CommitmentConfig>,
config: Option<RpcAccountInfoConfig>,
) -> Result<RpcResponse<Vec<RpcKeyedAccount>>>;

#[rpc(meta, name = "getTokenAccountsByDelegate")]
Expand All @@ -1411,7 +1415,7 @@ pub trait RpcSol {
meta: Self::Metadata,
delegate_str: String,
token_account_filter: RpcTokenAccountsFilter,
commitment: Option<CommitmentConfig>,
config: Option<RpcAccountInfoConfig>,
) -> Result<RpcResponse<Vec<RpcKeyedAccount>>>;
}

Expand Down Expand Up @@ -2050,31 +2054,31 @@ impl RpcSol for RpcSolImpl {
meta: Self::Metadata,
owner_str: String,
token_account_filter: RpcTokenAccountsFilter,
commitment: Option<CommitmentConfig>,
config: Option<RpcAccountInfoConfig>,
) -> Result<RpcResponse<Vec<RpcKeyedAccount>>> {
debug!(
"get_token_accounts_by_owner rpc request received: {:?}",
owner_str
);
let owner = verify_pubkey(owner_str)?;
let token_account_filter = verify_token_account_filter(token_account_filter)?;
meta.get_token_accounts_by_owner(&owner, token_account_filter, commitment)
meta.get_token_accounts_by_owner(&owner, token_account_filter, config)
}

fn get_token_accounts_by_delegate(
&self,
meta: Self::Metadata,
delegate_str: String,
token_account_filter: RpcTokenAccountsFilter,
commitment: Option<CommitmentConfig>,
config: Option<RpcAccountInfoConfig>,
) -> Result<RpcResponse<Vec<RpcKeyedAccount>>> {
debug!(
"get_token_accounts_by_delegate rpc request received: {:?}",
delegate_str
);
let delegate = verify_pubkey(delegate_str)?;
let token_account_filter = verify_token_account_filter(token_account_filter)?;
meta.get_token_accounts_by_delegate(&delegate, token_account_filter, commitment)
meta.get_token_accounts_by_delegate(&delegate, token_account_filter, config)
}
}

Expand Down
18 changes: 12 additions & 6 deletions docs/src/apps/jsonrpc-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,10 @@ Returns all SPL Token accounts by approved Delegate.
- `<object>` - Either:
* `mint: <string>` - Pubkey of the specific token Mint to limit accounts to, as base-58 encoded string; or
* `programId: <string>` - Pubkey of the Token program ID that owns the accounts, as base-58 encoded string
- `<object>` - (optional) [Commitment](jsonrpc-api.md#configuring-state-commitment)
- `<object>` - (optional) Configuration object containing the following optional fields:
- (optional) [Commitment](jsonrpc-api.md#configuring-state-commitment)
- (optional) `encoding: <string>` - encoding for Account data, either "binary" or jsonParsed". If parameter not provided, the default encoding is binary.
Parsed-JSON encoding attempts to use program-specific state parsers to return more human-readable and explicit account state data. If parsed-JSON is requested but a parser cannot be found, the field falls back to binary encoding, detectable when the `data` field is type `<string>`.

#### Results:

Expand All @@ -1062,15 +1065,15 @@ The result will be an RpcResponse JSON object with `value` equal to an array of
- `account: <object>` - a JSON object, with the following sub fields:
- `lamports: <u64>`, number of lamports assigned to this account, as a u64
- `owner: <string>`, base-58 encoded Pubkey of the program this account has been assigned to
`data: <object>`, Token state data associated with the account, in JSON format `{<program>: <state>}`
- `data: <object>`, Token state data associated with the account, either as base-58 encoded binary data or in JSON format `{<program>: <state>}`
- `executable: <bool>`, boolean indicating if the account contains a program \(and is strictly read-only\)
- `rentEpoch: <u64>`, the epoch at which this account will next owe rent, as u64

#### Example:

```bash
// Request
curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0", "id":1, "method":"getTokenAccountsByDelegate", "params": ["4Nd1mBQtrMJVYVfKf2PJy9NZUZdTAsp7D4xWLs4gDB4T", {"programId": "TokenSVp5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o"}]}' http://localhost:8899
curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0", "id":1, "method":"getTokenAccountsByDelegate", "params": ["4Nd1mBQtrMJVYVfKf2PJy9NZUZdTAsp7D4xWLs4gDB4T", {"programId": "TokenSVp5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o"}, {"encoding": "jsonParsed"}]}' http://localhost:8899
// Result
{"jsonrpc":"2.0","result":{"context":{"slot":1114},"value":[{"data":{"token":{"account":{"amount":1,"delegate":"4Nd1mBQtrMJVYVfKf2PJy9NZUZdTAsp7D4xWLs4gDB4T","delegatedAmount":1,"isInitialized":true,"isNative":false,"mint":"3wyAj7Rt1TWVPZVteFJPLa26JmLvdb1CAKEFZm3NY75E","owner":"CnPoSPKXu7wJqxe59Fs72tkBeALovhsCxYeFwPCQH9TD"}}},"executable":false,"lamports":1726080,"owner":"TokenSVp5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o","rentEpoch":4},"pubkey":"CnPoSPKXu7wJqxe59Fs72tkBeALovhsCxYeFwPCQH9TD"}],"id":1}
```
Expand All @@ -1085,7 +1088,10 @@ Returns all SPL Token accounts by token owner.
- `<object>` - Either:
* `mint: <string>` - Pubkey of the specific token Mint to limit accounts to, as base-58 encoded string; or
* `programId: <string>` - Pubkey of the Token program ID that owns the accounts, as base-58 encoded string
- `<object>` - (optional) [Commitment](jsonrpc-api.md#configuring-state-commitment)
- `<object>` - (optional) Configuration object containing the following optional fields:
- (optional) [Commitment](jsonrpc-api.md#configuring-state-commitment)
- (optional) `encoding: <string>` - encoding for Account data, either "binary" or jsonParsed". If parameter not provided, the default encoding is binary.
Parsed-JSON encoding attempts to use program-specific state parsers to return more human-readable and explicit account state data. If parsed-JSON is requested but a parser cannot be found, the field falls back to binary encoding, detectable when the `data` field is type `<string>`.

#### Results:

Expand All @@ -1095,15 +1101,15 @@ The result will be an RpcResponse JSON object with `value` equal to an array of
- `account: <object>` - a JSON object, with the following sub fields:
- `lamports: <u64>`, number of lamports assigned to this account, as a u64
- `owner: <string>`, base-58 encoded Pubkey of the program this account has been assigned to
`data: <object>`, Token state data associated with the account, in JSON format `{<program>: <state>}`
- `data: <object>`, Token state data associated with the account, either as base-58 encoded binary data or in JSON format `{<program>: <state>}`
- `executable: <bool>`, boolean indicating if the account contains a program \(and is strictly read-only\)
- `rentEpoch: <u64>`, the epoch at which this account will next owe rent, as u64

#### Example:

```bash
// Request
curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0", "id":1, "method":"getTokenAccountsByOwner", "params": ["4Qkev8aNZcqFNSRhQzwyLMFSsi94jHqE8WNVTJzTP99F", {"mint":"3wyAj7Rt1TWVPZVteFJPLa26JmLvdb1CAKEFZm3NY75E"}]}' http://localhost:8899
curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0", "id":1, "method":"getTokenAccountsByOwner", "params": ["4Qkev8aNZcqFNSRhQzwyLMFSsi94jHqE8WNVTJzTP99F", {"mint":"3wyAj7Rt1TWVPZVteFJPLa26JmLvdb1CAKEFZm3NY75E"}, {"encoding": "jsonParsed"}]}' http://localhost:8899
// Result
{"jsonrpc":"2.0","result":{"context":{"slot":1114},"value":[{"data":{"token":{"account":{"amount":1,"delegate":null,"delegatedAmount":1,"isInitialized":true,"isNative":false,"mint":"3wyAj7Rt1TWVPZVteFJPLa26JmLvdb1CAKEFZm3NY75E","owner":"4Qkev8aNZcqFNSRhQzwyLMFSsi94jHqE8WNVTJzTP99F"}}},"executable":false,"lamports":1726080,"owner":"TokenSVp5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o","rentEpoch":4},"pubkey":"CnPoSPKXu7wJqxe59Fs72tkBeALovhsCxYeFwPCQH9TD"}],"id":1}
```
Expand Down