Skip to content

Commit

Permalink
fix: final tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
wei3erHase committed Oct 6, 2024
1 parent 9094e72 commit b137e1c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
73 changes: 52 additions & 21 deletions js/src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { Connection, PublicKey, Keypair } from '@solana/web3.js';
import fs from 'fs';
import { Numberu64, generateRandomSeed } from './utils';
import { CreateSchedule } from './state';
import { create, TOKEN_VESTING_PROGRAM_ID } from './main';
import {
create,
VESTING_PROGRAM_ID,
DEVNET_VESTING_PROGRAM_ID,
TOKEN_MINT,
initializeUnlock,
unlock,
} from './main';
import { signAndSendInstructions } from '@bonfida/utils';

/**
*
* Simple example of a linear unlock.
*
* This is just an example, please be careful using the vesting contract and test it first with test tokens.
*
Expand All @@ -23,15 +28,19 @@ const wallet = Keypair.fromSecretKey(
const LOCK_OWNER = new PublicKey('');
const LOCK_OWNER_TOKEN_ACCOUNT = new PublicKey('');

/** Info about the deposit (to interact with) */
const LOCK_SEED = '';

/** Token info */
const MINT = new PublicKey('');
const DECIMALS = 0;
const DECIMALS = 9;

/** Amount to give per schedule */
const LOCKED_AMOUNT = 0;
/** Amount to lock */
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 @@ -41,7 +50,7 @@ const checks = async () => {

// @ts-ignore
const parsed = tokenInfo.value.data.parsed;
if (parsed.info.mint !== MINT.toBase58()) {
if (parsed.info.mint !== TOKEN_MINT.toBase58()) {
throw new Error('Invalid mint');
}
if (parsed.info.owner !== LOCK_OWNER.toBase58()) {
Expand All @@ -56,10 +65,9 @@ const checks = async () => {
const lock = async () => {
await checks();
const schedule: CreateSchedule = new CreateSchedule(
/** Has to be in seconds */
/** Has to be 0 | 3 | 6 | 12 mths (in seconds) */
// @ts-ignore
new Numberu64(60),
/** Don't forget to add decimals */
new Numberu64(0), // unlocked with withdrawal period
// @ts-ignore
new Numberu64(LOCKED_AMOUNT * Math.pow(10, DECIMALS)),
);
Expand All @@ -69,26 +77,49 @@ const lock = async () => {

const instruction = await create(
connection,
TOKEN_VESTING_PROGRAM_ID,
program,
// @ts-ignore
Buffer.from(seed),
wallet.publicKey,
LOCK_OWNER_TOKEN_ACCOUNT,
MINT,
schedule,
);

const tx = await signAndSendInstructions(connection, [], wallet, instruction);

console.log(`Transaction: ${tx}`);
};

const txInfo = await connection.getConfirmedTransaction(tx, 'confirmed');
if (txInfo && !txInfo.meta?.err) {
console.log(
txInfo?.transaction.instructions[2].data.slice(1, 32 + 1).toString('hex'),
);
} else {
throw new Error('Transaction not confirmed.');
}
const initUnlock = async () => {
await checks();

const instruction = await initializeUnlock(
connection,
VESTING_PROGRAM_ID,
// @ts-ignore
Buffer.from(LOCK_SEED),
);

const tx = await signAndSendInstructions(connection, [], wallet, instruction);

console.log(`Transaction: ${tx}`);
};

const withdraw = async () => {
await checks();

const instruction = await unlock(
connection,
program,
// @ts-ignore
Buffer.from(LOCK_SEED),
);

const tx = await signAndSendInstructions(connection, [], wallet, instruction);

console.log(`Transaction: ${tx}`);
};

lock();
// initUnlock();
// withdraw();
17 changes: 12 additions & 5 deletions js/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ import { ContractInfo, CreateSchedule } from './state';
import bs58 from 'bs58';

/**
* The vesting schedule program ID on testnet
* TODO: replace with mainnet ID
* The vesting schedule program ID
*/
export const TOKEN_VESTING_PROGRAM_ID = new PublicKey(
'HGhyAuNiYRa6oN55eGGP1MYGVve7epwT8WX6qbWxgYxM',
export const VESTING_PROGRAM_ID = new PublicKey(
'BHJWdCprG1HUiCZh1jhA4mJfAiXEGJXUn4pjnZXB3fGp'
);

export const TOKEN_MINT = new PublicKey(
'EWMA3o2kHLpsVYvmjtvYRywHrXD84sLxFb42seUShxAD',
'AxfBPA1yi6my7VAjqB9fqr1AgYczuuJy8tePnNUDDPpW'
);

export const DEVNET_VESTING_PROGRAM_ID = new PublicKey(
'HGhyAuNiYRa6oN55eGGP1MYGVve7epwT8WX6qbWxgYxM'
);

export const DEVNET_TOKEN_MINT = new PublicKey(
'FrnSwyMzw2u6DB2bQUTpia9mRHqeujdUF2bomY8Zt5BX',
);

/**
Expand Down
1 change: 0 additions & 1 deletion js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"extends": "@tsconfig/recommended/tsconfig.json",
"ts-node": {
"compilerOptions": {
"module": "commonjs",
Expand Down
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!("EWMA3o2kHLpsVYvmjtvYRywHrXD84sLxFb42seUShxAD");
solana_program::pubkey!("AxfBPA1yi6my7VAjqB9fqr1AgYczuuJy8tePnNUDDPpW");

pub struct Processor {}

Expand Down

0 comments on commit b137e1c

Please sign in to comment.