Skip to content

Commit

Permalink
Use try_deserialize rather than try_from_slice (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgarfield authored Mar 1, 2023
1 parent 03fac56 commit 19747a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
14 changes: 7 additions & 7 deletions cli/src/processor/delegation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anchor_lang::AnchorDeserialize;
use anchor_lang::AccountDeserialize;
use clockwork_client::{
network::state::{Config, Delegation, Worker},
Client,
Expand All @@ -13,15 +13,15 @@ pub fn create(client: &Client, worker_id: u64) -> Result<(), CliError> {
let config_data = client
.get_account_data(&config_pubkey)
.map_err(|_err| CliError::AccountNotFound(config_pubkey.to_string()))?;
let config = Config::try_from_slice(config_data.as_slice())
let config = Config::try_deserialize(&mut config_data.as_slice())
.map_err(|_err| CliError::AccountDataNotParsable(config_pubkey.to_string()))?;

// Get worker
let worker_pubkey = Worker::pubkey(worker_id);
let worker_data = client
.get_account_data(&worker_pubkey)
.map_err(|_err| CliError::AccountNotFound(worker_pubkey.to_string()))?;
let worker = Worker::try_from_slice(worker_data.as_slice())
let worker = Worker::try_deserialize(&mut worker_data.as_slice())
.map_err(|_err| CliError::AccountDataNotParsable(worker_pubkey.to_string()))?;

// Build ix
Expand All @@ -48,7 +48,7 @@ pub fn deposit(
let config_data = client
.get_account_data(&config_pubkey)
.map_err(|_err| CliError::AccountNotFound(config_pubkey.to_string()))?;
let config = Config::try_from_slice(config_data.as_slice())
let config = Config::try_deserialize(&mut config_data.as_slice())
.map_err(|_err| CliError::AccountDataNotParsable(config_pubkey.to_string()))?;

// TODO Map the amount using the mint's decimals.
Expand Down Expand Up @@ -78,7 +78,7 @@ pub fn withdraw(
let config_data = client
.get_account_data(&config_pubkey)
.map_err(|_err| CliError::AccountNotFound(config_pubkey.to_string()))?;
let config = Config::try_from_slice(config_data.as_slice())
let config = Config::try_deserialize(&mut config_data.as_slice())
.map_err(|_err| CliError::AccountDataNotParsable(config_pubkey.to_string()))?;

// TODO Map the amount using the mint's decimals.
Expand All @@ -103,7 +103,7 @@ pub fn get(client: &Client, delegation_id: u64, worker_id: u64) -> Result<(), Cl
let config_data = client
.get_account_data(&config_pubkey)
.map_err(|_err| CliError::AccountNotFound(config_pubkey.to_string()))?;
let config = Config::try_from_slice(config_data.as_slice())
let config = Config::try_deserialize(&mut config_data.as_slice())
.map_err(|_err| CliError::AccountDataNotParsable(config_pubkey.to_string()))?;

// Get the delegation account.
Expand All @@ -112,7 +112,7 @@ pub fn get(client: &Client, delegation_id: u64, worker_id: u64) -> Result<(), Cl
let delegation_data = client
.get_account_data(&delegation_pubkey)
.map_err(|_err| CliError::AccountNotFound(delegation_pubkey.to_string()))?;
let delegation = Delegation::try_from_slice(delegation_data.as_slice())
let delegation = Delegation::try_deserialize(&mut delegation_data.as_slice())
.map_err(|_err| CliError::AccountDataNotParsable(delegation_pubkey.to_string()))?;

// Get the delegation's token account.
Expand Down
14 changes: 8 additions & 6 deletions cli/src/processor/worker.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anchor_lang::AnchorDeserialize;
use anchor_lang::AccountDeserialize;
use clockwork_client::{
network::state::{
Config, Fee, Penalty, Registry, Snapshot, SnapshotFrame, Worker, WorkerSettings,
Expand Down Expand Up @@ -47,7 +47,7 @@ pub fn get(client: &Client, id: u64) -> Result<(), CliError> {
let registry_data = client
.get_account_data(&registry_pubkey)
.map_err(|_err| CliError::AccountNotFound(registry_pubkey.to_string()))?;
let registry = Registry::try_from_slice(registry_data.as_slice())
let registry = Registry::try_deserialize(&mut registry_data.as_slice())
.map_err(|_err| CliError::AccountDataNotParsable(registry_pubkey.to_string()))?;

// Get snapshot frame
Expand All @@ -56,8 +56,10 @@ pub fn get(client: &Client, id: u64) -> Result<(), CliError> {
match client.get_account_data(&snapshot_frame_pubkey) {
Err(_err) => {}
Ok(snapshot_frame_data) => {
let snapshot_frame = SnapshotFrame::try_from_slice(snapshot_frame_data.as_slice())
.map_err(|_err| CliError::AccountDataNotParsable(registry_pubkey.to_string()))?;
let snapshot_frame = SnapshotFrame::try_deserialize(
&mut snapshot_frame_data.as_slice(),
)
.map_err(|_err| CliError::AccountDataNotParsable(registry_pubkey.to_string()))?;
println!("{:#?}", snapshot_frame);
}
}
Expand All @@ -71,15 +73,15 @@ pub fn create(client: &Client, signatory: Keypair, silent: bool) -> Result<(), C
let config_data = client
.get_account_data(&config_pubkey)
.map_err(|_err| CliError::AccountNotFound(config_pubkey.to_string()))?;
let config = Config::try_from_slice(config_data.as_slice())
let config = Config::try_deserialize(&mut config_data.as_slice())
.map_err(|_err| CliError::AccountDataNotParsable(config_pubkey.to_string()))?;

// Get registry
let registry_pubkey = Registry::pubkey();
let registry_data = client
.get_account_data(&registry_pubkey)
.map_err(|_err| CliError::AccountNotFound(registry_pubkey.to_string()))?;
let registry = Registry::try_from_slice(registry_data.as_slice())
let registry = Registry::try_deserialize(&mut registry_data.as_slice())
.map_err(|_err| CliError::AccountDataNotParsable(registry_pubkey.to_string()))?;

// Build ix
Expand Down

0 comments on commit 19747a6

Please sign in to comment.