#[error] generates a Result<T> type

This commit is contained in:
Armani Ferrante 2021-01-23 17:07:38 -08:00
parent 9da5bccb07
commit f22479fff9
No known key found for this signature in database
GPG Key ID: D597A80BCF8E12B7
5 changed files with 29 additions and 36 deletions

View File

@ -5,15 +5,15 @@ use anchor_lang::prelude::*;
#[program]
mod errors {
use super::*;
pub fn hello(ctx: Context<Hello>) -> Result<(), Error> {
pub fn hello(ctx: Context<Hello>) -> Result<()> {
Err(MyError::Hello.into())
}
pub fn hello_no_msg(ctx: Context<HelloNoMsg>) -> Result<(), Error> {
pub fn hello_no_msg(ctx: Context<HelloNoMsg>) -> Result<()> {
Err(MyError::HelloNoMsg.into())
}
pub fn hello_next(ctx: Context<HelloNext>) -> Result<(), Error> {
pub fn hello_next(ctx: Context<HelloNext>) -> Result<()> {
Err(MyError::HelloNext.into())
}
}

View File

@ -10,8 +10,6 @@ use anchor_spl::token::{self, TokenAccount, Transfer};
mod calculator;
type Result<T> = std::result::Result<T, Error>;
#[program]
pub mod lockup {
use super::*;

View File

@ -19,7 +19,7 @@ mod registry {
}
impl Registry {
pub fn new<'info>(ctx: Context<Ctor>) -> Result<Self, Error> {
pub fn new<'info>(ctx: Context<Ctor>) -> Result<Self> {
Ok(Registry {
lockup_program: *ctx.accounts.lockup_program.key,
})
@ -35,7 +35,7 @@ mod registry {
withdrawal_timelock: i64,
stake_rate: u64,
reward_q_len: u32,
) -> Result<(), Error> {
) -> Result<()> {
let registrar = &mut ctx.accounts.registrar;
registrar.authority = authority;
@ -58,7 +58,7 @@ mod registry {
ctx: Context<UpdateRegistrar>,
new_authority: Option<Pubkey>,
withdrawal_timelock: Option<i64>,
) -> Result<(), Error> {
) -> Result<()> {
let registrar = &mut ctx.accounts.registrar;
if let Some(new_authority) = new_authority {
@ -73,7 +73,7 @@ mod registry {
}
#[access_control(CreateMember::accounts(&ctx, nonce))]
pub fn create_member(ctx: Context<CreateMember>, nonce: u8) -> Result<(), Error> {
pub fn create_member(ctx: Context<CreateMember>, nonce: u8) -> Result<()> {
let member = &mut ctx.accounts.member;
member.registrar = *ctx.accounts.registrar.to_account_info().key;
member.beneficiary = *ctx.accounts.beneficiary.key;
@ -83,10 +83,7 @@ mod registry {
Ok(())
}
pub fn update_member(
ctx: Context<UpdateMember>,
metadata: Option<Pubkey>,
) -> Result<(), Error> {
pub fn update_member(ctx: Context<UpdateMember>, metadata: Option<Pubkey>) -> Result<()> {
let member = &mut ctx.accounts.member;
if let Some(m) = metadata {
member.metadata = m;
@ -95,12 +92,12 @@ mod registry {
}
// Deposits that can only come directly from the member beneficiary.
pub fn deposit(ctx: Context<Deposit>, amount: u64) -> Result<(), Error> {
pub fn deposit(ctx: Context<Deposit>, amount: u64) -> Result<()> {
token::transfer(ctx.accounts.into(), amount).map_err(Into::into)
}
// Deposits that can only come from the beneficiary's vesting accounts.
pub fn deposit_locked(ctx: Context<DepositLocked>, amount: u64) -> Result<(), Error> {
pub fn deposit_locked(ctx: Context<DepositLocked>, amount: u64) -> Result<()> {
token::transfer(ctx.accounts.into(), amount).map_err(Into::into)
}
@ -110,7 +107,7 @@ mod registry {
&ctx.accounts.balances,
&ctx.accounts.balances_locked,
))]
pub fn stake(ctx: Context<Stake>, spt_amount: u64, locked: bool) -> Result<(), Error> {
pub fn stake(ctx: Context<Stake>, spt_amount: u64, locked: bool) -> Result<()> {
let balances = {
if locked {
&ctx.accounts.balances_locked
@ -172,11 +169,7 @@ mod registry {
&ctx.accounts.balances,
&ctx.accounts.balances_locked,
))]
pub fn start_unstake(
ctx: Context<StartUnstake>,
spt_amount: u64,
locked: bool,
) -> Result<(), Error> {
pub fn start_unstake(ctx: Context<StartUnstake>, spt_amount: u64, locked: bool) -> Result<()> {
let balances = {
if locked {
&ctx.accounts.balances_locked
@ -241,7 +234,7 @@ mod registry {
Ok(())
}
pub fn end_unstake(ctx: Context<EndUnstake>) -> Result<(), Error> {
pub fn end_unstake(ctx: Context<EndUnstake>) -> Result<()> {
if ctx.accounts.pending_withdrawal.end_ts > ctx.accounts.clock.unix_timestamp {
return Err(ErrorCode::UnstakeTimelock.into());
}
@ -289,7 +282,7 @@ mod registry {
Ok(())
}
pub fn withdraw(ctx: Context<Withdraw>, amount: u64) -> Result<(), Error> {
pub fn withdraw(ctx: Context<Withdraw>, amount: u64) -> Result<()> {
let seeds = &[
ctx.accounts.registrar.to_account_info().key.as_ref(),
ctx.accounts.member.to_account_info().key.as_ref(),
@ -307,7 +300,7 @@ mod registry {
token::transfer(cpi_ctx, amount).map_err(Into::into)
}
pub fn withdraw_locked(ctx: Context<WithdrawLocked>, amount: u64) -> Result<(), Error> {
pub fn withdraw_locked(ctx: Context<WithdrawLocked>, amount: u64) -> Result<()> {
let seeds = &[
ctx.accounts.registrar.to_account_info().key.as_ref(),
ctx.accounts.member.to_account_info().key.as_ref(),
@ -333,7 +326,7 @@ mod registry {
expiry_ts: i64,
expiry_receiver: Pubkey,
nonce: u8,
) -> Result<(), Error> {
) -> Result<()> {
if total < ctx.accounts.pool_mint.supply {
return Err(ErrorCode::InsufficientReward.into());
}
@ -370,7 +363,7 @@ mod registry {
}
#[access_control(reward_eligible(&ctx.accounts.cmn))]
pub fn claim_reward(ctx: Context<ClaimReward>) -> Result<(), Error> {
pub fn claim_reward(ctx: Context<ClaimReward>) -> Result<()> {
if RewardVendorKind::Unlocked != ctx.accounts.cmn.vendor.kind {
return Err(ErrorCode::ExpectedUnlockedVendor.into());
}
@ -413,7 +406,7 @@ mod registry {
pub fn claim_reward_locked<'a, 'b, 'c, 'info>(
ctx: Context<'a, 'b, 'c, 'info, ClaimRewardLocked<'info>>,
nonce: u8,
) -> Result<(), Error> {
) -> Result<()> {
let (end_ts, period_count) = match ctx.accounts.cmn.vendor.kind {
RewardVendorKind::Unlocked => return Err(ErrorCode::ExpectedLockedVendor.into()),
RewardVendorKind::Locked {
@ -467,7 +460,7 @@ mod registry {
Ok(())
}
pub fn expire_reward(ctx: Context<ExpireReward>) -> Result<(), Error> {
pub fn expire_reward(ctx: Context<ExpireReward>) -> Result<()> {
if ctx.accounts.clock.unix_timestamp < ctx.accounts.vendor.expiry_ts {
return Err(ErrorCode::VendorNotYetExpired.into());
}
@ -509,7 +502,7 @@ pub struct Initialize<'info> {
}
impl<'info> Initialize<'info> {
fn accounts(ctx: &Context<Initialize<'info>>, nonce: u8) -> Result<(), Error> {
fn accounts(ctx: &Context<Initialize<'info>>, nonce: u8) -> Result<()> {
let registrar_signer = Pubkey::create_program_address(
&[
ctx.accounts.registrar.to_account_info().key.as_ref(),
@ -563,7 +556,7 @@ pub struct CreateMember<'info> {
}
impl<'info> CreateMember<'info> {
fn accounts(ctx: &Context<CreateMember>, nonce: u8) -> Result<(), Error> {
fn accounts(ctx: &Context<CreateMember>, nonce: u8) -> Result<()> {
let seeds = &[
ctx.accounts.registrar.to_account_info().key.as_ref(),
ctx.accounts.member.to_account_info().key.as_ref(),
@ -867,7 +860,7 @@ pub struct DropReward<'info> {
}
impl<'info> DropReward<'info> {
fn accounts(ctx: &Context<DropReward>, nonce: u8) -> Result<(), Error> {
fn accounts(ctx: &Context<DropReward>, nonce: u8) -> Result<()> {
let vendor_signer = Pubkey::create_program_address(
&[
ctx.accounts.registrar.to_account_info().key.as_ref(),
@ -1054,7 +1047,7 @@ pub struct RewardQueue {
}
impl RewardQueue {
pub fn append(&mut self, event: RewardEvent) -> Result<u32, Error> {
pub fn append(&mut self, event: RewardEvent) -> Result<u32> {
let cursor = self.head;
// Insert into next available slot.
@ -1217,7 +1210,7 @@ impl<'info> From<&BalanceSandboxAccounts<'info>> for BalanceSandbox {
}
}
fn reward_eligible(cmn: &ClaimRewardCommon) -> Result<(), Error> {
fn reward_eligible(cmn: &ClaimRewardCommon) -> Result<()> {
let vendor = &cmn.vendor;
let member = &cmn.member;
if vendor.expired {
@ -1239,7 +1232,7 @@ pub fn no_available_rewards<'info>(
member: &ProgramAccount<'info, Member>,
balances: &BalanceSandboxAccounts<'info>,
balances_locked: &BalanceSandboxAccounts<'info>,
) -> Result<(), Error> {
) -> Result<()> {
let mut cursor = member.rewards_cursor;
// If the member's cursor is less then the tail, then the ring buffer has

View File

@ -14,14 +14,14 @@ pub mod basic_4 {
}
impl Counter {
pub fn new(ctx: Context<Auth>) -> Result<Self, ProgramError> {
pub fn new(ctx: Context<Auth>) -> Result<Self> {
Ok(Self {
authority: *ctx.accounts.authority.key,
count: 0,
})
}
pub fn increment(&mut self, ctx: Context<Auth>) -> Result<(), Error> {
pub fn increment(&mut self, ctx: Context<Auth>) -> Result<()> {
if &self.authority != ctx.accounts.authority.key {
return Err(ErrorCode::Unauthorized.into());
}

View File

@ -5,6 +5,8 @@ pub fn generate(error: Error) -> proc_macro2::TokenStream {
let error_enum = error.raw_enum;
let enum_name = &error.ident;
quote! {
type Result<T> = std::result::Result<T, Error>;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]