From 8167ce99e2a3f444aaf704d056b550d2b5b917d5 Mon Sep 17 00:00:00 2001 From: abishekk92 Date: Mon, 17 Jul 2023 17:23:24 +0530 Subject: [PATCH] Let devs configure a top up amount --- programs/gpl_session/src/lib.rs | 15 +++++++++++++-- tests/gpl_core/connection.spec.ts | 15 +++------------ tests/gpl_core/post.spec.ts | 16 ++++------------ tests/utils/index.ts | 12 +++++++----- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/programs/gpl_session/src/lib.rs b/programs/gpl_session/src/lib.rs index f75a11f..5d4626b 100644 --- a/programs/gpl_session/src/lib.rs +++ b/programs/gpl_session/src/lib.rs @@ -26,12 +26,14 @@ pub mod gpl_session { ctx: Context, top_up: Option, valid_until: Option, + top_up_amount: Option, ) -> 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 @@ -74,6 +76,7 @@ pub fn create_session_token_handler( ctx: Context, top_up: bool, valid_until: i64, + top_up_amount: u64, ) -> Result<()> { // Valid until can't be greater than a day require!( @@ -81,6 +84,12 @@ pub fn create_session_token_handler( 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(), @@ -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, )?; } @@ -219,4 +228,6 @@ pub enum SessionError { InvalidToken, #[msg("No session token provided")] NoToken, + #[msg("Top up amount is too high")] + TopUpAmountTooHigh, } diff --git a/tests/gpl_core/connection.spec.ts b/tests/gpl_core/connection.spec.ts index 7f1b681..e082216 100644 --- a/tests/gpl_core/connection.spec.ts +++ b/tests/gpl_core/connection.spec.ts @@ -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 () => { diff --git a/tests/gpl_core/post.spec.ts b/tests/gpl_core/post.spec.ts index 08212ef..f2a3b56 100644 --- a/tests/gpl_core/post.spec.ts +++ b/tests/gpl_core/post.spec.ts @@ -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 diff --git a/tests/utils/index.ts b/tests/utils/index.ts index 5864ad4..f27cc40 100644 --- a/tests/utils/index.ts +++ b/tests/utils/index.ts @@ -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;