Skip to content

Commit

Permalink
replace deprecated try_to_vec with borsh::to_vec
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Feb 21, 2024
1 parent 59c3718 commit f313cd9
Show file tree
Hide file tree
Showing 36 changed files with 200 additions and 281 deletions.
4 changes: 2 additions & 2 deletions associated-token-account/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn build_associated_token_account_instruction(
AccountMeta::new_readonly(solana_program::system_program::id(), false),
AccountMeta::new_readonly(*token_program_id, false),
],
data: instruction.try_to_vec().unwrap(),
data: borsh::to_vec(&instruction).unwrap(),
}
}

Expand Down Expand Up @@ -156,6 +156,6 @@ pub fn recover_nested(
AccountMeta::new(*wallet_address, true),
AccountMeta::new_readonly(*token_program_id, false),
],
data: instruction_data.try_to_vec().unwrap(),
data: borsh::to_vec(&instruction_data).unwrap(),
}
}
16 changes: 7 additions & 9 deletions binary-option/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ pub fn initialize_binary_option(
AccountMeta::new_readonly(solana_program::system_program::id(), false),
AccountMeta::new_readonly(sysvar::rent::id(), false),
],
data: BinaryOptionInstruction::InitializeBinaryOption(InitializeBinaryOptionArgs {
decimals,
})
.try_to_vec()
data: borsh::to_vec(&BinaryOptionInstruction::InitializeBinaryOption(
InitializeBinaryOptionArgs { decimals },
))
.unwrap(),
}
}
Expand Down Expand Up @@ -107,12 +106,11 @@ pub fn trade(
AccountMeta::new_readonly(escrow_authority, false),
AccountMeta::new_readonly(spl_token::id(), false),
],
data: BinaryOptionInstruction::Trade(TradeArgs {
data: borsh::to_vec(&BinaryOptionInstruction::Trade(TradeArgs {
size,
buy_price,
sell_price,
})
.try_to_vec()
}))
.unwrap(),
}
}
Expand All @@ -131,7 +129,7 @@ pub fn settle(
AccountMeta::new_readonly(winning_mint, false),
AccountMeta::new_readonly(pool_authority, true),
],
data: BinaryOptionInstruction::Settle.try_to_vec().unwrap(),
data: borsh::to_vec(&BinaryOptionInstruction::Settle).unwrap(),
}
}

Expand Down Expand Up @@ -167,6 +165,6 @@ pub fn collect(
AccountMeta::new_readonly(solana_program::system_program::id(), false),
AccountMeta::new_readonly(sysvar::rent::id(), false),
],
data: BinaryOptionInstruction::Collect.try_to_vec().unwrap(),
data: borsh::to_vec(&BinaryOptionInstruction::Collect).unwrap(),
}
}
8 changes: 4 additions & 4 deletions binary-oracle-pair/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub fn init_pool(
init_args: InitArgs,
) -> Result<Instruction, ProgramError> {
let init_data = PoolInstruction::InitPool(init_args);
let data = init_data.try_to_vec()?;
let data = borsh::to_vec(&init_data)?;
let accounts = vec![
AccountMeta::new(*pool, false),
AccountMeta::new_readonly(*authority, false),
Expand Down Expand Up @@ -138,7 +138,7 @@ pub fn deposit(
amount: u64,
) -> Result<Instruction, ProgramError> {
let init_data = PoolInstruction::Deposit(amount);
let data = init_data.try_to_vec()?;
let data = borsh::to_vec(&init_data)?;

let accounts = vec![
AccountMeta::new_readonly(*pool, false),
Expand Down Expand Up @@ -180,7 +180,7 @@ pub fn withdraw(
amount: u64,
) -> Result<Instruction, ProgramError> {
let init_data = PoolInstruction::Withdraw(amount);
let data = init_data.try_to_vec()?;
let data = borsh::to_vec(&init_data)?;
let accounts = vec![
AccountMeta::new_readonly(*pool, false),
AccountMeta::new_readonly(*authority, false),
Expand Down Expand Up @@ -212,7 +212,7 @@ pub fn decide(
decision: bool,
) -> Result<Instruction, ProgramError> {
let init_data = PoolInstruction::Decide(decision);
let data = init_data.try_to_vec()?;
let data = borsh::to_vec(&init_data)?;
let accounts = vec![
AccountMeta::new(*pool, false),
AccountMeta::new_readonly(*decider, true),
Expand Down
2 changes: 1 addition & 1 deletion binary-oracle-pair/program/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ mod test {
decision: Decision::Fail,
};

let packed = p.try_to_vec().unwrap();
let packed = borsh::to_vec(&p).unwrap();

let unpacked = Pool::try_from_slice(packed.as_slice()).unwrap();

Expand Down
9 changes: 4 additions & 5 deletions feature-proposal/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Pack for FeatureProposalInstruction {

impl FeatureProposalInstruction {
fn pack_into_vec(&self) -> Vec<u8> {
self.try_to_vec().expect("try_to_vec")
borsh::to_vec(self).expect("try_to_vec")
}
}

Expand Down Expand Up @@ -177,19 +177,18 @@ mod tests {
#[test]
fn test_serialize_bytes() {
assert_eq!(
FeatureProposalInstruction::Tally.try_to_vec().unwrap(),
borsh::to_vec(&FeatureProposalInstruction::Tally).unwrap(),
vec![1]
);

assert_eq!(
FeatureProposalInstruction::Propose {
borsh::to_vec(&FeatureProposalInstruction::Propose {
tokens_to_mint: 42,
acceptance_criteria: AcceptanceCriteria {
tokens_required: 0xdeadbeefdeadbeef,
deadline: -1,
}
}
.try_to_vec()
})
.unwrap(),
vec![
0, 42, 0, 0, 0, 0, 0, 0, 0, 239, 190, 173, 222, 239, 190, 173, 222, 255, 255, 255,
Expand Down
9 changes: 4 additions & 5 deletions feature-proposal/program/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Pack for FeatureProposal {
const LEN: usize = 17; // see `test_get_packed_len()` for justification of "18"

fn pack_into_slice(&self, dst: &mut [u8]) {
let data = self.try_to_vec().unwrap();
let data = borsh::to_vec(self).unwrap();
dst[..data.len()].copy_from_slice(&data);
}

Expand Down Expand Up @@ -75,14 +75,13 @@ mod tests {

#[test]
fn test_serialize_bytes() {
assert_eq!(FeatureProposal::Expired.try_to_vec().unwrap(), vec![3]);
assert_eq!(borsh::to_vec(&FeatureProposal::Expired).unwrap(), vec![3]);

assert_eq!(
FeatureProposal::Pending(AcceptanceCriteria {
borsh::to_vec(&FeatureProposal::Pending(AcceptanceCriteria {
tokens_required: 0xdeadbeefdeadbeef,
deadline: -1,
})
.try_to_vec()
}))
.unwrap(),
vec![1, 239, 190, 173, 222, 239, 190, 173, 222, 255, 255, 255, 255, 255, 255, 255, 255],
);
Expand Down
4 changes: 2 additions & 2 deletions governance/addin-mock/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn setup_voter_weight_record(
Instruction {
program_id: *program_id,
accounts,
data: instruction.try_to_vec().unwrap(),
data: borsh::to_vec(&instruction).unwrap(),
}
}

Expand Down Expand Up @@ -129,6 +129,6 @@ pub fn setup_max_voter_weight_record(
Instruction {
program_id: *program_id,
accounts,
data: instruction.try_to_vec().unwrap(),
data: borsh::to_vec(&instruction).unwrap(),
}
}
2 changes: 1 addition & 1 deletion governance/chat/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ pub fn post_message(
Instruction {
program_id: *program_id,
accounts,
data: instruction.try_to_vec().unwrap(),
data: borsh::to_vec(&instruction).unwrap(),
}
}
2 changes: 1 addition & 1 deletion governance/chat/program/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mod test {
reply_to: Some(Pubkey::new_unique()),
body: MessageBody::Text("message".to_string()),
};
let size = message.try_to_vec().unwrap().len();
let size = borsh::to_vec(&message).unwrap().len();

assert_eq!(message.get_max_size(), Some(size));
}
Expand Down
Loading

0 comments on commit f313cd9

Please sign in to comment.