Skip to content

Commit

Permalink
Fix token rpc-client methods (bp #11361) (#11362)
Browse files Browse the repository at this point in the history
* Fix token rpc-client methods (#11361)

* Convert None to error in parse_keyed_accounts

* Allow encoding configuration in getTokenAccounts methods

(cherry picked from commit d0144ce)

# Conflicts:
#	core/src/rpc.rs

* Fix conflicts

Co-authored-by: Tyera Eulberg <[email protected]>
Co-authored-by: Tyera Eulberg <[email protected]>
  • Loading branch information
3 people authored Aug 4, 2020
1 parent 53bb826 commit 5871462
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
10 changes: 9 additions & 1 deletion client/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,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 @@ -889,9 +889,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 @@ -915,7 +917,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();
new_response(&bank, accounts)
Expand All @@ -925,9 +927,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 @@ -959,7 +963,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();
new_response(&bank, accounts)
Expand Down Expand Up @@ -1371,7 +1375,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 @@ -1380,7 +1384,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 @@ -2017,31 +2021,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

0 comments on commit 5871462

Please sign in to comment.