Skip to content

Commit

Permalink
Adds unwrap_bump macro (#36)
Browse files Browse the repository at this point in the history
* Adds unwrap_bump macro

* changelog
  • Loading branch information
macalinao authored Feb 27, 2022
1 parent 9a65dbb commit 33371db
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased Changes

### Features

- Adds the `unwrap_bump!` macro, which extracts the bump from the instruction `Context` ([#36](https://github.com/saber-hq/vipers/pull/36)).

## [2.0.0] - 2022-02-21

### Features
Expand Down
41 changes: 41 additions & 0 deletions tests/vipers-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Vipers tests.
#![cfg(test)]

use std::collections::BTreeMap;

use anchor_lang::prelude::*;

declare_id!("VipersTest111111111111111111111111111111111");
Expand All @@ -23,6 +25,11 @@ struct TestData {
pub byte: u8,
}

#[derive(Accounts)]
struct MyAccounts<'info> {
random: AccountInfo<'info>,
}

#[test]
#[allow(deprecated)]
pub fn test_compiles_deprecated() -> Result<()> {
Expand Down Expand Up @@ -244,3 +251,37 @@ fn test_anchor_errors_ne_result() {
(err!(MyOtherError) as Result<()>).into_cmp_error(),
);
}

#[test]
fn test_unwrap_bump() {
let mut lamports = 0;
let random: AccountInfo = AccountInfo::new(
&crate::ID,
false,
false,
&mut lamports,
&mut [],
&crate::ID,
false,
0,
);
let accounts = &mut MyAccounts { random };
let mut bumps: BTreeMap<String, u8> = BTreeMap::new();
bumps.insert("test".to_string(), 1);
let ctx = Context {
program_id: &vipers::ID,
accounts,
remaining_accounts: &[],
bumps,
};
assert_does_not_throw!({
let bump = unwrap_bump!(ctx, "test");
assert_eq!(bump, 1);
});
assert_throws!(
{
let _bump2 = unwrap_bump!(ctx, "missing");
},
VipersError::UnknownBump
);
}
25 changes: 25 additions & 0 deletions vipers/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,31 @@ macro_rules! unwrap_int {
};
}

/// Unwraps a bump seed.
///
/// # Example
///
/// ```ignore
/// # use anchor_lang::prelude::*;
/// # #[macro_use] extern crate vipers; fn main() -> Result<()> {
/// fn my_instruction(ctx: Context<MyAccounts>) -> Result<()> {
/// let my_data = &mut ctx.accounts.my_data
/// my_data.bump = unwrap_bump!(ctx, "my_data");
/// Ok(())
/// }
/// # }
/// ```
#[macro_export]
macro_rules! unwrap_bump {
($ctx:expr, $bump:literal $(,)?) => {
*$crate::unwrap_opt!(
$ctx.bumps.get($bump),
$crate::VipersError::UnknownBump,
format!("Unknown bump: {}", $bump)
)
};
}

/// Tries to unwrap the [Result], otherwise returns the error
///
/// # Example
Expand Down
2 changes: 2 additions & 0 deletions vipers/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum VipersError {
KeysMustNotMatch,
#[msg("The provided token account is non-zero: amount must be zero, it should not have a delegate, and it should not have a close authority.")]
TokenAccountIsNonZero,
#[msg("Bump not found.")]
UnknownBump,
}

/// Conversions into a [CmpError].
Expand Down
4 changes: 2 additions & 2 deletions vipers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub mod prelude {
pub use super::{
assert_is_zero_token_account, assert_keys_eq, assert_keys_neq, invariant, try_or_err,
unwrap_checked, unwrap_int, unwrap_opt, unwrap_opt_block, unwrap_or_err, AsKeyRef,
CmpError, IntoCmpError, Validate, VipersError,
unwrap_bump, unwrap_checked, unwrap_int, unwrap_opt, unwrap_opt_block, unwrap_or_err,
AsKeyRef, CmpError, IntoCmpError, Validate, VipersError,
};
}

0 comments on commit 33371db

Please sign in to comment.