From d0144ce382253bda9e512188a3ac7ad1d2a5bf1c Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Tue, 4 Aug 2020 11:11:30 -0600 Subject: [PATCH] Fix token rpc-client methods (#11361) * Convert None to error in parse_keyed_accounts * Allow encoding configuration in getTokenAccounts methods --- client/src/rpc_client.rs | 10 +++++++++- core/src/rpc.rs | 28 ++++++++++++++++------------ docs/src/apps/jsonrpc-api.md | 18 ++++++++++++------ 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/client/src/rpc_client.rs b/client/src/rpc_client.rs index 2ded618b19..127bd59a10 100644 --- a/client/src/rpc_client.rs +++ b/client/src/rpc_client.rs @@ -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) } diff --git a/core/src/rpc.rs b/core/src/rpc.rs index 37f3915fae..5af7f348bf 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -939,9 +939,11 @@ impl JsonRpcRequestProcessor { &self, owner: &Pubkey, token_account_filter: TokenAccountsFilter, - commitment: Option, + config: Option, ) -> Result>> { - 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![ @@ -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)) @@ -975,9 +977,11 @@ impl JsonRpcRequestProcessor { &self, delegate: &Pubkey, token_account_filter: TokenAccountsFilter, - commitment: Option, + config: Option, ) -> Result>> { - 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![ @@ -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)) @@ -1402,7 +1406,7 @@ pub trait RpcSol { meta: Self::Metadata, owner_str: String, token_account_filter: RpcTokenAccountsFilter, - commitment: Option, + config: Option, ) -> Result>>; #[rpc(meta, name = "getTokenAccountsByDelegate")] @@ -1411,7 +1415,7 @@ pub trait RpcSol { meta: Self::Metadata, delegate_str: String, token_account_filter: RpcTokenAccountsFilter, - commitment: Option, + config: Option, ) -> Result>>; } @@ -2050,7 +2054,7 @@ impl RpcSol for RpcSolImpl { meta: Self::Metadata, owner_str: String, token_account_filter: RpcTokenAccountsFilter, - commitment: Option, + config: Option, ) -> Result>> { debug!( "get_token_accounts_by_owner rpc request received: {:?}", @@ -2058,7 +2062,7 @@ impl RpcSol for RpcSolImpl { ); 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( @@ -2066,7 +2070,7 @@ impl RpcSol for RpcSolImpl { meta: Self::Metadata, delegate_str: String, token_account_filter: RpcTokenAccountsFilter, - commitment: Option, + config: Option, ) -> Result>> { debug!( "get_token_accounts_by_delegate rpc request received: {:?}", @@ -2074,7 +2078,7 @@ impl RpcSol for RpcSolImpl { ); 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) } } diff --git a/docs/src/apps/jsonrpc-api.md b/docs/src/apps/jsonrpc-api.md index bce2172d2e..87dd1f354e 100644 --- a/docs/src/apps/jsonrpc-api.md +++ b/docs/src/apps/jsonrpc-api.md @@ -1052,7 +1052,10 @@ Returns all SPL Token accounts by approved Delegate. - `` - Either: * `mint: ` - Pubkey of the specific token Mint to limit accounts to, as base-58 encoded string; or * `programId: ` - Pubkey of the Token program ID that owns the accounts, as base-58 encoded string -- `` - (optional) [Commitment](jsonrpc-api.md#configuring-state-commitment) +- `` - (optional) Configuration object containing the following optional fields: + - (optional) [Commitment](jsonrpc-api.md#configuring-state-commitment) + - (optional) `encoding: ` - 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 ``. #### Results: @@ -1062,7 +1065,7 @@ The result will be an RpcResponse JSON object with `value` equal to an array of - `account: ` - a JSON object, with the following sub fields: - `lamports: `, number of lamports assigned to this account, as a u64 - `owner: `, base-58 encoded Pubkey of the program this account has been assigned to - `data: `, Token state data associated with the account, in JSON format `{: }` + - `data: `, Token state data associated with the account, either as base-58 encoded binary data or in JSON format `{: }` - `executable: `, boolean indicating if the account contains a program \(and is strictly read-only\) - `rentEpoch: `, the epoch at which this account will next owe rent, as u64 @@ -1070,7 +1073,7 @@ The result will be an RpcResponse JSON object with `value` equal to an array of ```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} ``` @@ -1085,7 +1088,10 @@ Returns all SPL Token accounts by token owner. - `` - Either: * `mint: ` - Pubkey of the specific token Mint to limit accounts to, as base-58 encoded string; or * `programId: ` - Pubkey of the Token program ID that owns the accounts, as base-58 encoded string -- `` - (optional) [Commitment](jsonrpc-api.md#configuring-state-commitment) +- `` - (optional) Configuration object containing the following optional fields: + - (optional) [Commitment](jsonrpc-api.md#configuring-state-commitment) + - (optional) `encoding: ` - 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 ``. #### Results: @@ -1095,7 +1101,7 @@ The result will be an RpcResponse JSON object with `value` equal to an array of - `account: ` - a JSON object, with the following sub fields: - `lamports: `, number of lamports assigned to this account, as a u64 - `owner: `, base-58 encoded Pubkey of the program this account has been assigned to - `data: `, Token state data associated with the account, in JSON format `{: }` + - `data: `, Token state data associated with the account, either as base-58 encoded binary data or in JSON format `{: }` - `executable: `, boolean indicating if the account contains a program \(and is strictly read-only\) - `rentEpoch: `, the epoch at which this account will next owe rent, as u64 @@ -1103,7 +1109,7 @@ The result will be an RpcResponse JSON object with `value` equal to an array of ```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} ```