Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
refactor: move the constraints to seed to make usage explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
abishekk92 committed Mar 29, 2023
1 parent d6dfe58 commit a19903f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
16 changes: 10 additions & 6 deletions programs/gpl_core/src/instructions/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ pub struct CreateConnection<'info> {
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
crate::id().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
// Session Signer
authority.key().as_ref(),
// User Authority
user.authority.as_ref(),
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()? && session_token.session_signer.key() == authority.key(),
constraint = session_token.is_valid()?
)]
pub session_token: Option<Account<'info, SessionToken>>,

Expand Down Expand Up @@ -146,12 +148,14 @@ pub struct DeleteConnection<'info> {
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
crate::id().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
// Session Signer
authority.key().as_ref(),
// User Authority
user.authority.as_ref(),
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()? && session_token.session_signer.key() == authority.key(),
constraint = session_token.is_valid()?
)]
pub session_token: Option<Account<'info, SessionToken>>,

Expand Down
26 changes: 16 additions & 10 deletions programs/gpl_core/src/instructions/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ pub struct CreatePost<'info> {
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
crate::id().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
// Session Signer
authority.key().as_ref(),
// User Authority
user.authority.as_ref(),
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()? && session_token.session_signer.key() == authority.key(),
constraint = session_token.is_valid()?,
)]
pub session_token: Option<Account<'info, SessionToken>>,

Expand Down Expand Up @@ -207,13 +209,15 @@ pub struct CreateComment<'info> {
#[account(
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
session_token.target_program.key().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
crate::id().as_ref(),
// Session Signer
authority.key().as_ref(),
// User Authority
user.authority.as_ref(),
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()? && session_token.session_signer.key() == authority.key(),
constraint = session_token.is_valid()?
)]
pub session_token: Option<Account<'info, SessionToken>>,
#[account(mut)]
Expand Down Expand Up @@ -289,12 +293,14 @@ pub struct DeletePost<'info> {
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
crate::id().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
// Session Signer
authority.key().as_ref(),
// User Authority
user.authority.as_ref(),
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()? && session_token.session_signer.key() == authority.key(),
constraint = session_token.is_valid()?
)]
pub session_token: Option<Account<'info, SessionToken>>,
#[account(mut)]
Expand Down
16 changes: 10 additions & 6 deletions programs/gpl_core/src/instructions/reaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ pub struct CreateReaction<'info> {
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
crate::id().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
// Session Signer
authority.key().as_ref(),
// User Authority
user.authority.as_ref(),
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()? && session_token.session_signer.key() == authority.key(),
constraint = session_token.is_valid()?
)]
pub session_token: Option<Account<'info, SessionToken>>,

Expand Down Expand Up @@ -145,12 +147,14 @@ pub struct DeleteReaction<'info> {
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
crate::id().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
// Session Signer
authority.key().as_ref(),
// User Authority
user.authority.as_ref(),
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()? && session_token.session_signer.key() == authority.key(),
constraint = session_token.is_valid()?
)]
pub session_token: Option<Account<'info, SessionToken>>,
#[account(mut)]
Expand Down
2 changes: 1 addition & 1 deletion tests/gpl_core/post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe("Post", async () => {
await post.signers([randomUser]).rpc();
} catch (error: any) {
expect(error).to.be.an("error");
expect(error.toString()).to.contain("Error Code: ConstraintRaw");
expect(error.toString()).to.contain("Error Code: ConstraintSeeds");
}
});

Expand Down

0 comments on commit a19903f

Please sign in to comment.