Skip to content

Commit

Permalink
feat: improving example script
Browse files Browse the repository at this point in the history
  • Loading branch information
wei3erHase committed Oct 22, 2024
1 parent 36a08a8 commit a9003e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
15 changes: 6 additions & 9 deletions js/src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { Numberu64, generateRandomSeed } from './utils';
import { CreateSchedule } from './state';
import {
create,
VESTING_PROGRAM_ID,
DEVNET_VESTING_PROGRAM_ID,
TOKEN_MINT,
initializeUnlock,
unlock,
getTokenMint,
getProgramId,
} from './main';
import { signAndSendInstructions } from '@bonfida/utils';

Expand Down Expand Up @@ -39,8 +38,6 @@ const LOCKED_AMOUNT = 10;

/** Your RPC connection */
const connection = new Connection('');
const DEVNET = true;
const program = DEVNET ? DEVNET_VESTING_PROGRAM_ID : VESTING_PROGRAM_ID;

/** Do some checks before sending the tokens */
const checks = async () => {
Expand All @@ -50,7 +47,7 @@ const checks = async () => {

// @ts-ignore
const parsed = tokenInfo.value.data.parsed;
if (parsed.info.mint !== TOKEN_MINT.toBase58()) {
if (parsed.info.mint !== getTokenMint(connection).toBase58()) {
throw new Error('Invalid mint');
}
if (parsed.info.owner !== LOCK_OWNER.toBase58()) {
Expand All @@ -77,7 +74,7 @@ const lock = async () => {

const instruction = await create(
connection,
program,
getProgramId(connection),
// @ts-ignore
Buffer.from(seed),
wallet.publicKey,
Expand All @@ -95,7 +92,7 @@ const initUnlock = async () => {

const instruction = await initializeUnlock(
connection,
VESTING_PROGRAM_ID,
getProgramId(connection),
// @ts-ignore
Buffer.from(LOCK_SEED),
);
Expand All @@ -110,7 +107,7 @@ const withdraw = async () => {

const instruction = await unlock(
connection,
program,
getProgramId(connection),
// @ts-ignore
Buffer.from(LOCK_SEED),
);
Expand Down
11 changes: 11 additions & 0 deletions js/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,14 @@ export function getProgramId(connection: Connection): PublicKey {
return DEVNET_VESTING_PROGRAM_ID;
} else return VESTING_PROGRAM_ID;
}

/**
* This function can be used to retrieve the token mint based on the connection
* @param connection The Solana RPC connection object
* @returns A PublicKey object representing the token mint
*/
export function getTokenMint(connection: Connection): PublicKey {
if (isDevnetConnection(connection)) {
return DEVNET_TOKEN_MINT;
} else return TOKEN_MINT;
}

0 comments on commit a9003e4

Please sign in to comment.