Skip to content

Commit

Permalink
lang: add sysvar custom error and failing test case (coral-xyz#1535)
Browse files Browse the repository at this point in the history
  • Loading branch information
callensm authored and NBNARADHYA committed Mar 9, 2022
1 parent daa2d4d commit 94f4994
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ incremented for features.

## [Unreleased]

### Features

* lang: Add new `AccountSysvarMismatch` error code and test cases for sysvars ([#1535](https://github.com/project-serum/anchor/pull/1535)).

## [0.22.1] - 2022-02-28

### Fixes
Expand Down
11 changes: 7 additions & 4 deletions lang/src/accounts/sysvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ impl<'info, T: solana_program::sysvar::Sysvar + fmt::Debug> fmt::Debug for Sysva

impl<'info, T: solana_program::sysvar::Sysvar> Sysvar<'info, T> {
pub fn from_account_info(acc_info: &AccountInfo<'info>) -> Result<Sysvar<'info, T>> {
Ok(Sysvar {
info: acc_info.clone(),
account: T::from_account_info(acc_info)?,
})
match T::from_account_info(acc_info) {
Ok(val) => Ok(Sysvar {
info: acc_info.clone(),
account: val,
}),
Err(_) => Err(ErrorCode::AccountSysvarMismatch.into()),
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions lang/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ pub enum ErrorCode {
/// 3014 - The given account is not the associated token account
#[msg("The given account is not the associated token account")]
AccountNotAssociatedTokenAccount,
/// 3015 - The given public key does not match the required sysvar
#[msg("The given public key does not match the required sysvar")]
AccountSysvarMismatch,

// State.
/// 4000 - The given state account does not have the correct address
Expand Down
31 changes: 26 additions & 5 deletions tests/sysvars/tests/sysvars.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
const anchor = require("@project-serum/anchor");
const assert = require("assert");

describe("sysvars", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.Provider.local());
const program = anchor.workspace.Sysvars;

it("Is initialized!", async () => {
const program = anchor.workspace.Sysvars;
const tx = await program.rpc.sysvars({
accounts: {
const tx = await program.methods
.sysvars()
.accounts({
clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
stakeHistory: anchor.web3.SYSVAR_STAKE_HISTORY_PUBKEY,
},
});
})
.rpc();
console.log("Your transaction signature", tx);
});

it("Fails when the wrote pubkeys are provided", async () => {
try {
await program.methods
.sysvars()
.accounts({
clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
stakeHistory: anchor.web3.SYSVAR_REWARDS_PUBKEY,
})
.rpc();
assert.ok(false);
} catch (err) {
const errMsg = "The given public key does not match the required sysvar";
assert.strictEqual(err.toString(), errMsg);
assert.strictEqual(err.msg, errMsg);
assert.strictEqual(err.code, 3015);
}
});
});
5 changes: 5 additions & 0 deletions ts/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const LangErrorCode = {
AccountNotInitialized: 3012,
AccountNotProgramData: 3013,
AccountNotAssociatedTokenAccount: 3014,
AccountSysvarMismatch: 3015,
// State.
StateInvalidAddress: 4000,

Expand Down Expand Up @@ -227,6 +228,10 @@ const LangErrorMessage = new Map([
LangErrorCode.AccountNotAssociatedTokenAccount,
"The given account is not the associated token account",
],
[
LangErrorCode.AccountSysvarMismatch,
"The given public key does not match the required sysvar",
],

// State.
[
Expand Down

0 comments on commit 94f4994

Please sign in to comment.