From 9aafc5c27190c3bd8dddc0a7e30681cf0d9090ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wei=C3=9Fer=20Hase?= Date: Wed, 23 Oct 2024 16:17:36 +0200 Subject: [PATCH] chore: devnet redeploy (#28) * chore: devnet redeploy * chore: devnet full redeploy * feat: adding devnet behaviour on JS * feat: improving example script --- js/package.json | 2 +- js/src/example.ts | 15 +++++------- js/src/main.ts | 51 ++++++++++++++++++++++++++++++++-------- program/src/processor.rs | 2 +- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/js/package.json b/js/package.json index 7e6b8ce..000f236 100644 --- a/js/package.json +++ b/js/package.json @@ -1,6 +1,6 @@ { "name": "@defi-wonderland/solana-token-locking", - "version": "1.0.0", + "version": "1.0.1", "license": "MIT", "repository": { "type": "git", diff --git a/js/src/example.ts b/js/src/example.ts index 52b302a..4dec7ae 100644 --- a/js/src/example.ts +++ b/js/src/example.ts @@ -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'; @@ -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 () => { @@ -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()) { @@ -77,7 +74,7 @@ const lock = async () => { const instruction = await create( connection, - program, + getProgramId(connection), // @ts-ignore Buffer.from(seed), wallet.publicKey, @@ -95,7 +92,7 @@ const initUnlock = async () => { const instruction = await initializeUnlock( connection, - VESTING_PROGRAM_ID, + getProgramId(connection), // @ts-ignore Buffer.from(LOCK_SEED), ); @@ -110,7 +107,7 @@ const withdraw = async () => { const instruction = await unlock( connection, - program, + getProgramId(connection), // @ts-ignore Buffer.from(LOCK_SEED), ); diff --git a/js/src/main.ts b/js/src/main.ts index 79bb836..55f9aab 100644 --- a/js/src/main.ts +++ b/js/src/main.ts @@ -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( @@ -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` */ @@ -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, ); @@ -76,7 +73,7 @@ export async function create( ); const vestingTokenAccountKey = await getAssociatedTokenAddress( - TOKEN_MINT, + isDevnetConnection(connection)? DEVNET_TOKEN_MINT : TOKEN_MINT, vestingAccountKey, true, ); @@ -107,7 +104,7 @@ export async function create( payer, vestingTokenAccountKey, vestingAccountKey, - TOKEN_MINT, + isDevnetConnection(connection)? DEVNET_TOKEN_MINT : TOKEN_MINT, ), createCreateInstruction( programId, @@ -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( @@ -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, ); @@ -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, ); @@ -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; +} diff --git a/program/src/processor.rs b/program/src/processor.rs index 81de3ef..926797d 100644 --- a/program/src/processor.rs +++ b/program/src/processor.rs @@ -24,7 +24,7 @@ use crate::{ }; pub const TOKEN_MINT: Pubkey = - solana_program::pubkey!("5k84VjAKoGPXa7ias1BNgKUrX7e61eMPWhZDqsiD4Bpe"); + solana_program::pubkey!("FrnSwyMzw2u6DB2bQUTpia9mRHqeujdUF2bomY8Zt5BX"); pub struct Processor {}