-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBonkService.js
More file actions
210 lines (187 loc) · 10.6 KB
/
BonkService.js
File metadata and controls
210 lines (187 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import {Connection, PublicKey, Keypair, SystemProgram, SendTransactionError} from '@solana/web3.js';
import {TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount} from '@solana/spl-token';
import * as anchor from '@coral-xyz/anchor';
import BN from 'bn.js';
import dotenv from 'dotenv';
import chalk from 'chalk';
dotenv.config();
import idl from './idl/spl_token_staking.json' assert {type: "json"};
class BonkService {
static connection = new Connection('https://mainnet.helius-rpc.com/?api-key=2cf79b07-ffc1-47dd-ad43-ccca3833bcd1');
static provider = new anchor.AnchorProvider(
BonkService.connection,
new anchor.Wallet(Keypair.generate()), // Use a dummy wallet here, will be replaced with userKeyPair in methods
{
preflightCommitment: 'confirmed',
}
);
static PROGRAM_ID = new PublicKey('STAKEkKzbdeKkqzKpLkNQD3SUuLgshDKCD7U8duxAbB');
static MINT_PUBLIC_KEY = new PublicKey('DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263');
static VAULT_PUBLIC_KEY = new PublicKey('4XHP9YQeeXPXHAjNXuKio1na1ypcxFSqFYBHtptQticd');
static STAKE_MINT_PUBLIC_KEY = new PublicKey('FYUjeMAFjbTzdMG91RSW5P4HT2sT7qzJQgDPiPG9ez9o');
static TOKEN_PUBLIC_KEY = new PublicKey('DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263');
static STAKE_POOL_PDA = new PublicKey('9AdEE8AAm1XgJrPEs4zkTPozr3o4U5iGbgvPwkNdLDJ3');
static program = new anchor.Program(idl,
BonkService.PROGRAM_ID,
BonkService.provider);
static async sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
static async getAccountBalance(accountPublicKey) {
const accountInfo = await BonkService.connection.getTokenAccountBalance(accountPublicKey);
return new BN(accountInfo.value.amount); // Return the balance as BN
}
static async ensureSufficientBalance(userPublicKey, requiredBalance) {
const balance = await BonkService.connection.getBalance(userPublicKey);
if (balance < requiredBalance) {
throw new Error("Insufficient SOL balance for transaction fees.");
}
}
static async lockBonk(userKeyPair, amount, days) {
const userPublicKey = userKeyPair.publicKey;
// const stakeDepositReceipts = await BonkService.getStakeCount(userPublicKey);
const stakeDepositReceipts = await BonkService.findCurrentNonce(userPublicKey);
console.log("Stake count:", stakeDepositReceipts);
const nonce = stakeDepositReceipts
const lockupDuration = new BN(days * 24 * 60 * 60); // Convert days to seconds and then to BN
const amountBN = new BN(amount).mul(new BN(1e5)); // Convert amount to BN with 5 decimals
const requiredBalance = 0.01 * anchor.web3.LAMPORTS_PER_SOL; // Adjust based on expected fees
await BonkService.ensureSufficientBalance(userPublicKey, requiredBalance);
const [stakeDepositReceiptPDA, stakeDepositReceiptBump] = await PublicKey.findProgramAddress(
[
userPublicKey.toBuffer(),
BonkService.STAKE_POOL_PDA.toBuffer(),
Buffer.from(new Uint8Array(new BN(nonce).toArray('le', 4))),
Buffer.from('stakeDepositReceipt')
],
BonkService.PROGRAM_ID
);
console.log("[Parameters are]", BonkService.STAKE_POOL_PDA.toBase58(), stakeDepositReceiptPDA.toBase58(), stakeDepositReceiptBump);
const tokenPublicKey = BonkService.TOKEN_PUBLIC_KEY; // The address of the Bonk token mint
let userTokenAccount;
try {
userTokenAccount = await getOrCreateAssociatedTokenAccount(
BonkService.connection,
userKeyPair,
tokenPublicKey,
userPublicKey
);
} catch (error) {
console.error("Error getting or creating the user's token account:", error);
throw error;
}
const userTokenBalance = await BonkService.getAccountBalance(userTokenAccount.address);
console.log("User Token Balance before staking:", userTokenBalance.toString());
if (userTokenBalance.lt(amountBN)) {
throw new Error("Insufficient token balance for staking.");
}
await BonkService.sleep(5000);
const vaultPublicKey = BonkService.VAULT_PUBLIC_KEY;
const stakePoolPDA = BonkService.STAKE_POOL_PDA;
const stakeMintPublicKey = BonkService.STAKE_MINT_PUBLIC_KEY;
let destinationTokenAccount;
try {
destinationTokenAccount = await getOrCreateAssociatedTokenAccount(
BonkService.connection,
userKeyPair,
stakeMintPublicKey,
userPublicKey
);
} catch (error) {
console.error("Error getting or creating the destination token account:", error);
throw error;
}
try {
console.log(chalk.blue(`
Transaction Parameters:
- Payer: ${chalk.yellow(userPublicKey.toBase58())} ${userPublicKey.toBase58() === 'HFJEhqTUPKKWvhwVeQS5qjSP373kMUFpNuiqMMyXZ2Gr' ? chalk.green("✅") : chalk.red("❌")}
- Owner: ${chalk.yellow(userPublicKey.toBase58())} ${userPublicKey.toBase58() === 'HFJEhqTUPKKWvhwVeQS5qjSP373kMUFpNuiqMMyXZ2Gr' ? chalk.green("✅") : chalk.red("❌")}
- From: ${chalk.yellow(userTokenAccount.address.toBase58())} ${userTokenAccount.address.toBase58() === 'Dr8Mkn8Yja4pKQamNhDLyMdEuG4g4gM7G4Et3CkUiHiA' ? chalk.green("✅") : chalk.red("❌")}
- Vault: ${chalk.yellow(vaultPublicKey.toBase58())} ${vaultPublicKey.toBase58() === '4XHP9YQeeXPXHAjNXuKio1na1ypcxFSqFYBHtptQticd' ? chalk.green("✅") : chalk.red("❌")}
- Stake Mint: ${chalk.yellow(stakeMintPublicKey.toBase58())} ${stakeMintPublicKey.toBase58() === 'FYUjeMAFjbTzdMG91RSW5P4HT2sT7qzJQgDPiPG9ez9o' ? chalk.green("✅") : chalk.red("❌")}
- Destination: ${chalk.yellow(destinationTokenAccount.address.toBase58())} ${destinationTokenAccount.address.toBase58() === '12fbrbQ6EQ9bgnRrPw6avrT9TKrd4BJ2d5NKxNgSUWks' ? chalk.green("✅") : chalk.red("❌")}
- Stake Pool: ${chalk.yellow(stakePoolPDA.toBase58())} ${stakePoolPDA.toBase58() === '9AdEE8AAm1XgJrPEs4zkTPozr3o4U5iGbgvPwkNdLDJ3' ? chalk.green("✅") : chalk.red("❌")}
- Stake Deposit Receipt: ${chalk.yellow(stakeDepositReceiptPDA.toBase58())} ${stakeDepositReceiptPDA.toBase58() === 'Au3iYP4wNjm8mvYnN5FDrCodMg1asqa3kcpsTTncnuAF' ? chalk.green("✅") : chalk.red("❌")}
- Token Program: ${chalk.yellow(TOKEN_PROGRAM_ID.toBase58())} ${TOKEN_PROGRAM_ID.toBase58() === 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' ? chalk.green("✅") : chalk.red("❌")}
- Rent: ${chalk.yellow(anchor.web3.SYSVAR_RENT_PUBKEY.toBase58())} ${anchor.web3.SYSVAR_RENT_PUBKEY.toBase58() === 'SysvarRent111111111111111111111111111111111' ? chalk.green("✅") : chalk.red("❌")}
- System Program: ${chalk.yellow(SystemProgram.programId.toBase58())} ${SystemProgram.programId.toBase58() === '11111111111111111111111111111111' ? chalk.green("✅") : chalk.red("❌")}
- Account: ${chalk.yellow('2PPAJ8P5JgKZjkxq4h3kFSwLcuakFYr4fbV68jGghWxi')} ${chalk.green("✅")}
Function Parameters for deposit:
* Nonce: ${chalk.yellow(nonce.toString())}
* Amount: ${chalk.yellow(amountBN.toString())}
* Lockup Duration: ${chalk.yellow(lockupDuration.toString())}
`));
const ix = await BonkService.program.methods
.deposit(nonce, amountBN, lockupDuration)
.accounts({
payer: userPublicKey,
owner: userPublicKey,
from: userTokenAccount.address,
vault: vaultPublicKey,
stakeMint: stakeMintPublicKey,
destination: destinationTokenAccount.address,
stakePool: BonkService.STAKE_POOL_PDA,
stakeDepositReceipt: stakeDepositReceiptPDA,
tokenProgram: TOKEN_PROGRAM_ID,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
systemProgram: SystemProgram.programId,
})
.instruction();
ix.keys.push({
pubkey: new PublicKey("2PPAJ8P5JgKZjkxq4h3kFSwLcuakFYr4fbV68jGghWxi"),
isSigner: false,
isWritable: false,
});
const tx = new anchor.web3.Transaction();
tx.add(anchor.web3.ComputeBudgetProgram.setComputeUnitPrice({microLamports: 100000}));
tx.add(ix);
tx.feePayer = userPublicKey;
tx.recentBlockhash = (await BonkService.connection.getLatestBlockhash()).blockhash;
tx.partialSign(userKeyPair);
const txid = await anchor.web3.sendAndConfirmRawTransaction(BonkService.connection, tx.serialize(), {
skipPreflight: true,
});
console.log("Stake successful. Transaction ID:", txid);
console.log(`https://solscan.io/tx/${txid}`);
return txid;
} catch (error) {
console.error(`Error:`, error);
}
}
static async findCurrentNonce(userPublicKey) {
let nonce = 0;
for (let i = 0; i < 10; i++) {
const [stakeDepositReceiptPDA, stakeDepositReceiptBump] = await PublicKey.findProgramAddress(
[
userPublicKey.toBuffer(),
BonkService.STAKE_POOL_PDA.toBuffer(),
Buffer.from(new Uint8Array(new BN(i).toArray('le', 4))),
Buffer.from('stakeDepositReceipt')
],
BonkService.PROGRAM_ID
);
const stakeDepositInfo = await BonkService.connection.getAccountInfo(stakeDepositReceiptPDA);
if (!stakeDepositInfo) {
console.log(i, "this is the nonce to use");
nonce = i;
break;
}
}
return nonce;
}
static async getMinimumStakeDetails() {
try {
const stakePool = await BonkService.program.account.stakePool.fetch(BonkService.STAKE_POOL_PDA);
const minDuration = stakePool.minDuration.toNumber(); // Convert to number
const minStakeAmount = new BN(1).mul(new BN(10 ** 5)); // Assuming 0.00001 BONK as the smallest stakeable amount
return {
minDuration,
minStakeAmount: minStakeAmount.toString(),
};
} catch (error) {
console.error("Error fetching stake pool details:", error);
throw error;
}
}
}
export default BonkService;