forked from mito-systems/sol-mem-gen
96 lines
3.0 KiB
TypeScript
96 lines
3.0 KiB
TypeScript
import assert from 'assert';
|
|
import BN from 'bn.js';
|
|
import 'dotenv/config';
|
|
import bs58 from 'bs58';
|
|
|
|
import * as anchor from "@coral-xyz/anchor";
|
|
import {
|
|
TOKEN_PROGRAM_ID,
|
|
} from "@solana/spl-token";
|
|
import { Connection, PublicKey } from "@solana/web3.js";
|
|
|
|
import { createVestingPlanV2 } from '../locker-utils';
|
|
|
|
assert(process.env.RPC_ENDPOINT);
|
|
assert(process.env.USER_PRIVATE_KEY);
|
|
//assert(process.env.RECIPIENT_PUBLIC_KEY);
|
|
assert(process.env.WSOL_MINT);
|
|
|
|
const RPC_ENDPOINT= process.env.RPC_ENDPOINT;
|
|
const WSOL_MINT = process.env.WSOL_MINT;
|
|
const USER_PRIVATE_KEY = process.env.USER_PRIVATE_KEY;
|
|
|
|
const userKP = anchor.web3.Keypair.fromSecretKey(bs58.decode(USER_PRIVATE_KEY));
|
|
|
|
const connection = new Connection(RPC_ENDPOINT);
|
|
const token = new PublicKey(WSOL_MINT);
|
|
|
|
const provider = new anchor.AnchorProvider(
|
|
connection,
|
|
new anchor.Wallet(userKP),
|
|
// Commitment level required for simulating transaction
|
|
{ preflightCommitment: 'processed' }
|
|
);
|
|
|
|
anchor.setProvider(provider);
|
|
|
|
export async function getMTMBalance (senderKeypair: anchor.web3.Keypair): Promise<BN> {
|
|
const mintPublicKey = new PublicKey(WSOL_MINT);
|
|
const publicKey = senderKeypair.publicKey;
|
|
const tokenAccounts = await connection.getTokenAccountsByOwner(publicKey, { mint: mintPublicKey });
|
|
|
|
let balance = new BN(0);
|
|
for (const tokenAccount of tokenAccounts.value) {
|
|
const accountInfo = await connection.getParsedAccountInfo(
|
|
tokenAccount.pubkey,
|
|
"confirmed"
|
|
);
|
|
|
|
if (!accountInfo.value || Buffer.isBuffer(accountInfo.value.data)) {
|
|
console.warn(
|
|
`Token account ${tokenAccount.pubkey.toBase58()} data is not parsed.`
|
|
);
|
|
continue;
|
|
}
|
|
|
|
const tokenAmount =
|
|
(accountInfo.value.data as any).parsed?.info?.tokenAmount?.amount || "0";
|
|
balance = balance.add(new BN(tokenAmount));
|
|
}
|
|
|
|
return balance;
|
|
};
|
|
|
|
export async function createLock(tokenLockerKeypair: anchor.web3.Keypair, recipientPubKey: anchor.web3.PublicKey, duration: BN, balance: BN): Promise<anchor.web3.PublicKey | void> {
|
|
//const balance = await getMTMBalance(tokenLockerKeypair);
|
|
|
|
if (balance.eq(new BN(0))) {
|
|
console.log('No balance available to create lock, skipping...');
|
|
return;
|
|
}
|
|
|
|
console.log('Creating a lock...');
|
|
|
|
const escrow = await createVestingPlanV2({
|
|
ownerKeypair: tokenLockerKeypair,
|
|
vestingStartTime: new BN(Math.floor(Date.now() / 1000) - 60), // Start immediately
|
|
tokenMint: token,
|
|
isAssertion: true,
|
|
cliffTime: duration,
|
|
frequency: new BN(1), // Not needed since full unlock happens at cliff
|
|
cliffUnlockAmount: balance, // The entire amount should be released at cliff
|
|
amountPerPeriod: new BN(0), // No tokens should be released before cliff
|
|
numberOfPeriod: new BN(1), // Only release tokens once
|
|
recipient: recipientPubKey,
|
|
updateRecipientMode: 0,
|
|
cancelMode: 1,
|
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
});
|
|
|
|
if (escrow) {
|
|
console.log('Lock created successfully: ', escrow.toString());
|
|
}
|
|
|
|
return escrow;
|
|
}
|