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

Let devs configure a top up amount #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions programs/gpl_session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ pub mod gpl_session {
ctx: Context<CreateSessionToken>,
top_up: Option<bool>,
valid_until: Option<i64>,
top_up_amount: Option<u64>,
) -> Result<()> {
// Set top up to false by default
let top_up = top_up.unwrap_or(false);
// Set valid until to 1 hour from now by default
let valid_until = valid_until.unwrap_or(Clock::get()?.unix_timestamp + 60 * 60 * 1);
create_session_token_handler(ctx, top_up, valid_until)
let top_up_amount = top_up_amount.unwrap_or(LAMPORTS_PER_SOL.checked_div(100).unwrap());
create_session_token_handler(ctx, top_up, valid_until, top_up_amount)
}

// revoke a session token
Expand Down Expand Up @@ -74,13 +76,20 @@ pub fn create_session_token_handler(
ctx: Context<CreateSessionToken>,
top_up: bool,
valid_until: i64,
top_up_amount: u64,
) -> Result<()> {
// Valid until can't be greater than a day
require!(
valid_until <= Clock::get()?.unix_timestamp + 60 * 60 * 24,
SessionError::ValidityTooLong
);

// Top up amount can't be greater than 1 SOL
require!(
top_up_amount <= LAMPORTS_PER_SOL,
SessionError::TopUpAmountTooHigh
);

let session_token = &mut ctx.accounts.session_token;
session_token.set_inner(SessionToken {
authority: ctx.accounts.authority.key(),
Expand All @@ -100,7 +109,7 @@ pub fn create_session_token_handler(
to: ctx.accounts.session_signer.to_account_info(),
},
),
LAMPORTS_PER_SOL.checked_div(100).unwrap(),
top_up_amount,
)?;
}

Expand Down Expand Up @@ -219,4 +228,6 @@ pub enum SessionError {
InvalidToken,
#[msg("No session token provided")]
NoToken,
#[msg("Top up amount is too high")]
TopUpAmountTooHigh,
}
15 changes: 3 additions & 12 deletions tests/gpl_core/connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,11 @@ describe("Connection", async () => {
payer: testUser.publicKey,
authority: testUser.publicKey,
screenName: testScreenName,
});
})
.signers([testUser]);
const testProfilePubKeys = await testProfile.pubkeys();
testProfilePDA = testProfilePubKeys.profile as anchor.web3.PublicKey;
const testProfileTx = await testProfile.transaction();
testProfileTx.recentBlockhash = (
await rpcConnection.getLatestBlockhash()
).blockhash;
testProfileTx.feePayer = testUser.publicKey;
const signedTransaction = await testUserWallet.signTransaction(
testProfileTx
);
await sendAndConfirmTransaction(rpcConnection, signedTransaction, [
testUser,
]);
await testProfile.rpc();
});

it("should create a connection", async () => {
Expand Down
16 changes: 4 additions & 12 deletions tests/gpl_core/post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,12 @@ describe("Post", async () => {
payer: randomUser.publicKey,
authority: randomUser.publicKey,
screenName,
});
})
.signers([randomUser]);
const testProfilePubKeys = await testProfile.pubkeys();
randomProfilePDA = testProfilePubKeys.profile as anchor.web3.PublicKey;
const testProfileTx = await testProfile.transaction();
testProfileTx.recentBlockhash = (
await rpcConnection.getLatestBlockhash()
).blockhash;
testProfileTx.feePayer = randomUser.publicKey;
const signedTransaction = await randomUserWallet.signTransaction(
testProfileTx
);
await sendAndConfirmTransaction(rpcConnection, signedTransaction, [
randomUser,
]);

await testProfile.rpc();

// Create a session
// @ts-ignore
Expand Down
12 changes: 7 additions & 5 deletions tests/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ export async function new_session(
authority?: Keypair
): Promise<{ sessionPDA: PublicKey; sessionSigner: Keypair }> {
const sessionSigner = Keypair.generate();
const sessionTx = gpl_session.methods.createSession(true, null).accounts({
authority: user,
sessionSigner: sessionSigner.publicKey,
targetProgram,
});
const sessionTx = gpl_session.methods
.createSession(true, null, null)
.accounts({
authority: user,
sessionSigner: sessionSigner.publicKey,
targetProgram,
});
const sessionPubKeys = await sessionTx.pubkeys();
const sessionPDA = sessionPubKeys.sessionToken as PublicKey;

Expand Down