Skip to content

Commit

Permalink
chore: devnet redeploy (#28)
Browse files Browse the repository at this point in the history
* chore: devnet redeploy

* chore: devnet full redeploy

* feat: adding devnet behaviour on JS

* feat: improving example script
  • Loading branch information
wei3erHase authored Oct 23, 2024
1 parent f01d871 commit 9aafc5c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@defi-wonderland/solana-token-locking",
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
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
51 changes: 41 additions & 10 deletions js/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const TOKEN_MINT = new PublicKey(
);

export const DEVNET_VESTING_PROGRAM_ID = new PublicKey(
'5UmrfVDhyotfF6Dufved4yjFPCVJdNHu22u1e6ohSyn6'
'B6o6erKW2Vi9Nidtv4wfT8JRtdFS2W5GX1V9bJEVr9Lv'
);

export const DEVNET_TOKEN_MINT = new PublicKey(
Expand All @@ -44,10 +44,7 @@ export const DEVNET_TOKEN_MINT = new PublicKey(
* @param programId The token vesting program ID
* @param seedWord Seed words used to derive the vesting account
* @param payer The fee payer of the transaction
* @param sourceTokenOwner The owner of the source token account (i.e where locked tokens are originating from)
* @param possibleSourceTokenPubkey The source token account (i.e where locked tokens are originating from), if null it defaults to the ATA
* @param destinationTokenPubkey The destination token account i.e where unlocked tokens will be transfered
* @param mintAddress The mint of the tokens being vested
* @param schedule The vesting schedule
* @returns An array of `TransactionInstruction`
*/
Expand All @@ -62,7 +59,7 @@ export async function create(
// If no source token account was given, use the associated source account
if (possibleSourceTokenPubkey == null) {
possibleSourceTokenPubkey = await getAssociatedTokenAddress(
TOKEN_MINT,
isDevnetConnection(connection)? DEVNET_TOKEN_MINT : TOKEN_MINT,
payer,
true,
);
Expand All @@ -76,7 +73,7 @@ export async function create(
);

const vestingTokenAccountKey = await getAssociatedTokenAddress(
TOKEN_MINT,
isDevnetConnection(connection)? DEVNET_TOKEN_MINT : TOKEN_MINT,
vestingAccountKey,
true,
);
Expand Down Expand Up @@ -107,7 +104,7 @@ export async function create(
payer,
vestingTokenAccountKey,
vestingAccountKey,
TOKEN_MINT,
isDevnetConnection(connection)? DEVNET_TOKEN_MINT : TOKEN_MINT,
),
createCreateInstruction(
programId,
Expand All @@ -129,7 +126,6 @@ export async function create(
* @param connection The Solana RPC connection object
* @param programId The token vesting program ID
* @param seedWord Seed words used to derive the vesting account
* @param mintAddress The mint of the vested tokens
* @returns An array of `TransactionInstruction`
*/
export async function unlock(
Expand All @@ -145,7 +141,7 @@ export async function unlock(
seedWord = Buffer.from(seedWord.toString('hex') + bump.toString(16), 'hex');

const vestingTokenAccountKey = await getAssociatedTokenAddress(
TOKEN_MINT,
isDevnetConnection(connection)? DEVNET_TOKEN_MINT : TOKEN_MINT,
vestingAccountKey,
true,
);
Expand Down Expand Up @@ -188,7 +184,7 @@ export async function initializeUnlock(
seedWord = Buffer.from(seedWord.toString('hex') + bump.toString(16), 'hex');

const vestingTokenAccountKey = await getAssociatedTokenAddress(
TOKEN_MINT,
isDevnetConnection(connection)? DEVNET_TOKEN_MINT : TOKEN_MINT,
vestingAccountKey,
true,
);
Expand Down Expand Up @@ -234,3 +230,38 @@ export async function getContractInfo(
}
return info!;
}

/**
* This function can be used to retrieve the cluster of the connection ("mainnet" or "devnet")
* @param connection The Solana RPC connection object
* @returns A boolean value indicating if the connection is a devnet connection
*/
export function isDevnetConnection(connection: Connection): Boolean {
const endpoint = connection.rpcEndpoint;

if (endpoint.includes('devnet')) {
return true;
} else return false;
}

/**
* This function can be used to retrieve the program ID based on the connection
* @param connection The Solana RPC connection object
* @returns A PublicKey object representing the program ID
*/
export function getProgramId(connection: Connection): PublicKey {
if (isDevnetConnection(connection)) {
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;
}
2 changes: 1 addition & 1 deletion program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
};

pub const TOKEN_MINT: Pubkey =
solana_program::pubkey!("5k84VjAKoGPXa7ias1BNgKUrX7e61eMPWhZDqsiD4Bpe");
solana_program::pubkey!("FrnSwyMzw2u6DB2bQUTpia9mRHqeujdUF2bomY8Zt5BX");

pub struct Processor {}

Expand Down

0 comments on commit 9aafc5c

Please sign in to comment.