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

Commit

Permalink
Add separate payer field to pay initialization fee when creating acco…
Browse files Browse the repository at this point in the history
…unts (#20)

* Add separate payer field to pay initialization fee when creating accounts

* Add tests for creating accounts with seperate fee payer
  • Loading branch information
broskicodes authored May 26, 2023
1 parent 99251ab commit a56a687
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 20 deletions.
5 changes: 3 additions & 2 deletions programs/gpl_core/src/instructions/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::events::{ConnectionDeleted, ConnectionNew};
// Create a connection between two profiles, ie from_profile -> to_profile
#[derive(Accounts, Session)]
pub struct CreateConnection<'info> {
#[account(mut)]
pub payer: Signer<'info>,
// The account that will be initialized as a Connection
#[account(
init,
Expand All @@ -20,7 +22,7 @@ pub struct CreateConnection<'info> {
to_profile.key().as_ref()
],
bump,
payer = authority,
payer = payer,
space = Connection::LEN
)]
pub connection: Account<'info, Connection>,
Expand Down Expand Up @@ -58,7 +60,6 @@ pub struct CreateConnection<'info> {
)]
pub session_token: Option<Account<'info, SessionToken>>,

#[account(mut)]
pub authority: Signer<'info>,
// The system program
pub system_program: Program<'info, System>,
Expand Down
12 changes: 7 additions & 5 deletions programs/gpl_core/src/instructions/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use gpl_session::{SessionError, SessionToken};
#[derive(Accounts, Session)]
#[instruction(metadata_uri: String, random_hash: [u8;32])]
pub struct CreatePost<'info> {
#[account(mut)]
pub payer: Signer<'info>,
// The account that will be initialized as a Post
#[account(
init,
Expand All @@ -22,7 +24,7 @@ pub struct CreatePost<'info> {
random_hash.as_ref(),
],
bump,
payer = authority,
payer = payer,
space = Post::LEN
)]
pub post: Account<'info, Post>,
Expand Down Expand Up @@ -50,8 +52,7 @@ pub struct CreatePost<'info> {
authority = user.authority.key()
)]
pub session_token: Option<Account<'info, SessionToken>>,

#[account(mut)]

pub authority: Signer<'info>,
// The system program
pub system_program: Program<'info, System>,
Expand Down Expand Up @@ -154,6 +155,8 @@ pub fn update_post_handler(ctx: Context<UpdatePost>, metadata_uri: String) -> Re
#[derive(Accounts, Session)]
#[instruction(metadata_uri: String, random_hash: [u8;32])]
pub struct CreateComment<'info> {
#[account(mut)]
pub payer: Signer<'info>,
// The account that will be initialized as a Post
#[account(
init,
Expand All @@ -162,7 +165,7 @@ pub struct CreateComment<'info> {
random_hash.as_ref(),
],
bump,
payer = authority,
payer = payer,
space = Post::LEN
)]
pub post: Account<'info, Post>,
Expand Down Expand Up @@ -197,7 +200,6 @@ pub struct CreateComment<'info> {
authority = user.authority.key()
)]
pub session_token: Option<Account<'info, SessionToken>>,
#[account(mut)]
pub authority: Signer<'info>,
// The system program
pub system_program: Program<'info, System>,
Expand Down
5 changes: 3 additions & 2 deletions programs/gpl_core/src/instructions/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::events::{ProfileDeleted, ProfileNew};
#[derive(Accounts)]
#[instruction(namespace: String)]
pub struct CreateProfile<'info> {
#[account(mut)]
pub payer: Signer<'info>,
// The account that will be initialized as a Profile
#[account(
init,
Expand All @@ -19,7 +21,7 @@ pub struct CreateProfile<'info> {
user.to_account_info().key.as_ref()
],
bump,
payer = authority,
payer = payer,
space = Profile::LEN
)]
pub profile: Account<'info, Profile>,
Expand All @@ -32,7 +34,6 @@ pub struct CreateProfile<'info> {
has_one = authority,
)]
pub user: Account<'info, User>,
#[account(mut)]
pub authority: Signer<'info>,
// The system program
pub system_program: Program<'info, System>,
Expand Down
5 changes: 3 additions & 2 deletions programs/gpl_core/src/instructions/profile_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::constants::*;
#[derive(Accounts)]
#[instruction(metadata_uri: String)]
pub struct CreateProfileMetadata<'info> {
#[account(mut)]
pub payer: Signer<'info>,
// The account that will be initialized as a ProfileMetadata
#[account(
init,
Expand All @@ -19,7 +21,7 @@ pub struct CreateProfileMetadata<'info> {
profile.to_account_info().key.as_ref(),
],
bump,
payer = authority,
payer = payer,
space = ProfileMetadata::LEN
)]
pub profile_metadata: Account<'info, ProfileMetadata>,
Expand All @@ -42,7 +44,6 @@ pub struct CreateProfileMetadata<'info> {
has_one = authority,
)]
pub user: Account<'info, User>,
#[account(mut)]
pub authority: Signer<'info>,
// The system program
pub system_program: Program<'info, System>,
Expand Down
5 changes: 3 additions & 2 deletions programs/gpl_core/src/instructions/reaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use gpl_session::{session_auth_or, Session, SessionError, SessionToken};
#[derive(Accounts, Session)]
#[instruction(reaction_type: String)]
pub struct CreateReaction<'info> {
#[account(mut)]
pub payer: Signer<'info>,
// The account that will be initialized as a Reaction
#[account(
init,
Expand All @@ -23,7 +25,7 @@ pub struct CreateReaction<'info> {
from_profile.to_account_info().key.as_ref(),
],
bump,
payer = authority,
payer = payer,
space = Reaction::LEN
)]
pub reaction: Account<'info, Reaction>,
Expand Down Expand Up @@ -60,7 +62,6 @@ pub struct CreateReaction<'info> {
)]
pub session_token: Option<Account<'info, SessionToken>>,

#[account(mut)]
pub authority: Signer<'info>,

// The system program
Expand Down
5 changes: 3 additions & 2 deletions programs/gpl_core/src/instructions/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::state::User;
#[derive(Accounts)]
#[instruction(random_hash: [u8;32])]
pub struct CreateUser<'info> {
#[account(mut)]
pub payer: Signer<'info>,
// The account that will be initialized as a user
#[account(
init,
Expand All @@ -16,12 +18,11 @@ pub struct CreateUser<'info> {
random_hash.as_ref(),
],
bump,
payer = authority,
payer = payer,
space = User::LEN
)]
pub user: Account<'info, User>,
// The authority of the user
#[account(mut)]
pub authority: Signer<'info>,
// The system program
pub system_program: Program<'info, System>,
Expand Down
27 changes: 27 additions & 0 deletions tests/gpl_core/comment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as anchor from "@project-serum/anchor";
import randombytes from "randombytes";
import { expect } from "chai";
import { sendAndConfirmTransaction } from "@solana/web3.js";
import { GplCore } from "../../target/types/gpl_core";
import { new_session, airdrop } from "../utils";

Expand All @@ -15,6 +16,7 @@ describe("Comment", async () => {
let profilePDA: anchor.web3.PublicKey;
let postPDA: anchor.web3.PublicKey;
let testUserKeypair: anchor.web3.Keypair;
let feePayer: anchor.web3.Keypair;
let testUserPDA: anchor.web3.PublicKey;
let fromProfilePDA: anchor.web3.PublicKey;

Expand Down Expand Up @@ -48,6 +50,10 @@ describe("Comment", async () => {
testUserKeypair = anchor.web3.Keypair.generate();
await airdrop(testUserKeypair.publicKey);

// Create fee payer keypair
feePayer = anchor.web3.Keypair.generate();
await airdrop(feePayer.publicKey);

// Create a test user pda
const testUserRandomhash = randombytes(32);
const testUserTx = program.methods.createUser(testUserRandomhash).accounts({
Expand Down Expand Up @@ -88,6 +94,27 @@ describe("Comment", async () => {
);
});

it("should create a comment when a seperate fee payer is specified", async () => {
const createComment = program.methods
// @ts-ignore
.createComment("This is a test comment", randombytes(32))
.accounts({
payer: feePayer.publicKey,
replyTo: postPDA,
profile: fromProfilePDA,
user: testUserPDA,
authority: testUserKeypair.publicKey,
sessionToken: null,
});
const pubKeys = await createComment.pubkeys();
const commentPDA = pubKeys.post as anchor.web3.PublicKey;
await createComment.signers([feePayer, testUserKeypair]).rpc();
const commentAccount = await program.account.post.fetch(commentPDA);
expect(commentAccount.profile.toString()).is.equal(
fromProfilePDA.toString()
);
});

describe("Comment with session token", async () => {
let sessionToken: anchor.web3.PublicKey;
let sessionKeypair: anchor.web3.Keypair;
Expand Down
45 changes: 43 additions & 2 deletions tests/gpl_core/connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ anchor.setProvider(anchor.AnchorProvider.env());
describe("Connection", async () => {
let rpcConnection: anchor.web3.Connection;
let testUser: anchor.web3.Keypair;
let feePayer: anchor.web3.Keypair;
let testUserWallet: NodeWallet;
let userPDA: anchor.web3.PublicKey;
let testUserPDA: anchor.web3.PublicKey;
Expand Down Expand Up @@ -46,10 +47,14 @@ describe("Connection", async () => {
testUserWallet = new NodeWallet(testUser);
await airdrop(testUser.publicKey);

// Create a feePayer
feePayer = anchor.web3.Keypair.generate();
await airdrop(feePayer.publicKey);

const randomTestHash = randombytes(32);
const createTestUser = program.methods
.createUser(randomTestHash)
.accounts({ authority: testUser.publicKey });
.accounts({ payer: testUser.publicKey, authority: testUser.publicKey });
const testUserPubKeys = await createTestUser.pubkeys();
testUserPDA = testUserPubKeys.user as anchor.web3.PublicKey;
const testUserTx = await createTestUser.transaction();
Expand All @@ -67,7 +72,7 @@ describe("Connection", async () => {
// Create a testProfile
const testProfile = program.methods
.createProfile("Personal")
.accounts({ user: testUserPDA, authority: testUser.publicKey });
.accounts({ payer: testUser.publicKey, user: testUserPDA, authority: testUser.publicKey });
const testProfilePubKeys = await testProfile.pubkeys();
testProfilePDA = testProfilePubKeys.profile as anchor.web3.PublicKey;
const testProfileTx = await testProfile.transaction();
Expand Down Expand Up @@ -127,6 +132,42 @@ describe("Connection", async () => {
}
});

it("should create a connection when a seperate fee payer is specified", async () => {
const createConnection = program.methods
.createConnection()
.accounts({
payer: feePayer.publicKey,
fromProfile: profilePDA,
toProfile: testProfilePDA,
user: userPDA,
sessionToken: null,
});
const pubKeys = await createConnection.pubkeys();
connectionPDA = pubKeys.connection as anchor.web3.PublicKey;
await createConnection.signers([feePayer]).rpc();

const connectionAccount = await program.account.connection.fetch(
connectionPDA
);
expect(connectionAccount.fromProfile.toBase58()).to.equal(
profilePDA.toBase58()
);
expect(connectionAccount.toProfile.toBase58()).to.equal(
testProfilePDA.toBase58()
);

// Cleanup for next tests
await program.methods.deleteConnection().accounts({
fromProfile: profilePDA,
toProfile: testProfilePDA,
connection: connectionPDA,
user: userPDA,
sessionToken: null,
// @ts-ignore
refundReceiver: provider.wallet.publicKey,
}).rpc();
});

describe("Connection with session token", async () => {
let sessionToken: anchor.web3.PublicKey;
let sessionKeypair: anchor.web3.Keypair;
Expand Down
23 changes: 21 additions & 2 deletions tests/gpl_core/post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("Post", async () => {
let userPDA: anchor.web3.PublicKey;
let profilePDA: anchor.web3.PublicKey;
let postPDA: anchor.web3.PublicKey;
let feePayer: anchor.web3.Keypair;

before(async () => {
// Create a user
Expand All @@ -31,6 +32,10 @@ describe("Post", async () => {
const profilePubKeys = await profileTx.pubkeys();
profilePDA = profilePubKeys.profile as anchor.web3.PublicKey;
await profileTx.rpc();

// Create fee payer keypair
feePayer = anchor.web3.Keypair.generate();
await airdrop(feePayer.publicKey);
});

it("should create a post", async () => {
Expand Down Expand Up @@ -80,6 +85,20 @@ describe("Post", async () => {
}
});

it("should create a post when a seperate fee payer is specified", async () => {
const randomHash = randombytes(32);
const metadataUri = "This is a test post";
const createPost = program.methods
.createPost(metadataUri, randomHash)
.accounts({ payer: feePayer.publicKey, user: userPDA, profile: profilePDA, sessionToken: null });
const pubKeys = await createPost.pubkeys();
postPDA = pubKeys.post as anchor.web3.PublicKey;
await createPost.signers([feePayer]).rpc();
const postAccount = await program.account.post.fetch(postPDA);
expect(postAccount.metadataUri).is.equal(metadataUri);
expect(postAccount.profile.toString()).is.equal(profilePDA.toString());
});

describe("Post with session token", async () => {
let rpcConnection: anchor.web3.Connection;
let sessionToken: anchor.web3.PublicKey;
Expand All @@ -104,7 +123,7 @@ describe("Post", async () => {
const randomHash = randombytes(32);
const userTx = program.methods
.createUser(randomHash)
.accounts({ authority: randomUser.publicKey });
.accounts({ payer: randomUser.publicKey, authority: randomUser.publicKey });
const userPubKeys = await userTx.pubkeys();
randomUserPDA = userPubKeys.user;
const tx = await userTx.transaction();
Expand All @@ -122,7 +141,7 @@ describe("Post", async () => {
// Create a profile
const testProfile = program.methods
.createProfile("Personal")
.accounts({ user: randomUserPDA, authority: randomUser.publicKey });
.accounts({ payer: randomUser.publicKey, user: randomUserPDA, authority: randomUser.publicKey });
const testProfilePubKeys = await testProfile.pubkeys();
randomProfilePDA = testProfilePubKeys.profile as anchor.web3.PublicKey;
const testProfileTx = await testProfile.transaction();
Expand Down
Loading

0 comments on commit a56a687

Please sign in to comment.