sdk: Add Borsh support for types and utilities (#15290)

* sdk: Add Borsh to Pubkey

* Add serialization error for easier borsh integration

* Add Borsh usage to banks-client and sdk

* Rename SerializationError -> IOError

* Add new errors to proto

* Update Cargo lock

* Update Cargo.lock based on CI

* Clippy

* Update ABI on bank

* Address review feedback

* Update sanity program instruction count test
This commit is contained in:
Jon Cinque
2021-02-18 11:14:56 +01:00
committed by GitHub
parent fb1f2d54a5
commit 0f6f6080f3
16 changed files with 288 additions and 12 deletions

View File

@ -3,6 +3,7 @@
use crate::sanitize::Sanitize;
use crate::{pubkey::Pubkey, short_vec};
use bincode::serialize;
use borsh::BorshSerialize;
use serde::Serialize;
use thiserror::Error;
@ -186,6 +187,12 @@ pub enum InstructionError {
#[error("Incorrect authority provided")]
IncorrectAuthority,
#[error("Failed to serialize or deserialize account data: {0}")]
IOError(String),
#[error("An account does not have enough lamports to be rent-exempt")]
AccountNotRentExempt,
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
@ -207,6 +214,19 @@ impl Instruction {
accounts,
}
}
pub fn new_with_borsh<T: BorshSerialize>(
program_id: Pubkey,
data: &T,
accounts: Vec<AccountMeta>,
) -> Self {
let data = data.try_to_vec().unwrap();
Self {
program_id,
data,
accounts,
}
}
}
pub fn checked_add(a: u64, b: u64) -> Result<u64, InstructionError> {