Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit 9c03c20

Browse files
committed
add debug farm not ata btn
1 parent 3f8924d commit 9c03c20

File tree

2 files changed

+225
-20
lines changed

2 files changed

+225
-20
lines changed

src/pages/debug.vue

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
<template>
22
<div class="debug container">
3-
<Button v-if="!wallet.connected" ghost @click="$accessor.wallet.openModal"> Connect wallet </Button>
4-
<div v-else class="debug-section">
3+
<Button v-if="!wallet.connected"
4+
ghost
5+
@click="$accessor.wallet.openModal"> Connect wallet </Button>
6+
<div v-else
7+
class="debug-section">
58
<div class="debug-header">
69
Unwrap WSOL
710
<div class="debug-card">
8-
<Button ghost @click="unwrap">Unwrap WSOL</Button>
11+
<Button ghost
12+
@click="unwrap">Unwrap WSOL</Button>
913
</div>
1014
</div>
1115
<div class="debug-header">
1216
Token Accounts
1317
<div class="debug-card">
14-
<Button ghost @click="getTokenAccounts">Fetch token accounts</Button>
18+
<Button ghost
19+
@click="getTokenAccounts">Fetch token accounts</Button>
1520
<table>
1621
<thead>
1722
<tr>
@@ -21,7 +26,8 @@
2126
</tr>
2227
</thead>
2328
<tbody>
24-
<tr v-for="tokenAccount in tokenAccounts" :key="tokenAccount.pubkey.toBase58()">
29+
<tr v-for="tokenAccount in tokenAccounts"
30+
:key="tokenAccount.pubkey.toBase58()">
2531
{{
2632
void ((address = tokenAccount.pubkey.toBase58()), (info = tokenAccount.account.data.parsed.info))
2733
}}
@@ -40,7 +46,9 @@
4046
<div class="debug-header">
4147
All Farms
4248
<div class="debug-card">
43-
<div v-for="(pool, poolId) in farm.infos" :key="poolId" class="debug-card">
49+
<div v-for="(pool, poolId) in farm.infos"
50+
:key="poolId"
51+
class="debug-card">
4452
{{
4553
void ((lp = pool.lp),
4654
(reward = pool.reward),
@@ -74,12 +82,24 @@
7482
<div>Reward B: {{ rewardB.symbol }}</div>
7583
<div>Your deposited: {{ userInfo.depositBalance ? userInfo.depositBalance.format() : 0 }}</div>
7684
<div>Your LP balance: {{ userLpAccount.balance ? userLpAccount.balance.format() : 0 }}</div>
77-
<Input :ref="`input-${poolId}`" size="small" />
85+
<Input :ref="`input-${poolId}`"
86+
size="small" />
7887
<div>
79-
<Button size="small" type="danger" ghost @click="emergencyWithdraw(pool)">
88+
<Button size="small"
89+
type="danger"
90+
ghost
91+
@click="emergencyWithdraw(pool)">
8092
Emergency withdraw all LP and wipe all rewards
8193
</Button>
8294
</div>
95+
<div>
96+
<Button size="small"
97+
type="danger"
98+
ghost
99+
@click="notATAWithdrawBtn(pool)">
100+
Not ATA withdraw all LP and rewards
101+
</Button>
102+
</div>
83103
</td>
84104
</tr>
85105
</tbody>
@@ -104,7 +124,8 @@ import { TOKENS } from '@/utils/tokens'
104124
import { sendTransaction } from '@/utils/web3'
105125
import { getUnixTs } from '@/utils'
106126
import { TOKEN_PROGRAM_ID } from '@/utils/ids'
107-
import { emergencyWithdrawV4 } from '@/utils/stake'
127+
import { emergencyWithdrawV4, notATAWithdraw } from '@/utils/stake'
128+
import { FarmInfo } from '@/utils/farms'
108129
109130
@Component({
110131
head: {
@@ -244,6 +265,53 @@ export default class Debug extends Vue {
244265
})
245266
})
246267
}
268+
269+
notATAWithdrawBtn(poolInfo: FarmInfo) {
270+
const conn = this.$web3
271+
const wallet = (this as any).$wallet
272+
273+
const key = getUnixTs().toString()
274+
this.$notify.info({
275+
key,
276+
message: 'Making transaction...',
277+
description: '',
278+
duration: 0
279+
})
280+
281+
// @ts-ignore
282+
const depositInfo = this.farm.stakeAccounts[poolInfo.poolId]
283+
if (!depositInfo) {
284+
this.$notify.info({
285+
key,
286+
message: 'Deposit info not found',
287+
description: (h: any) => h('div', [''])
288+
})
289+
return
290+
}
291+
292+
notATAWithdraw(conn, wallet, poolInfo, this.wallet.tokenAccounts, depositInfo)
293+
.then((txid) => {
294+
this.$notify.info({
295+
key,
296+
message: 'Transaction has been sent',
297+
description: (h: any) =>
298+
h('div', [
299+
'Confirmation is in progress. Check your transaction on ',
300+
h('a', { attrs: { href: `${this.url.explorer}/tx/${txid}`, target: '_blank' } }, 'here')
301+
])
302+
})
303+
304+
const description = `Unstake all LP and rewards`
305+
this.$accessor.transaction.sub({ txid, description })
306+
})
307+
.catch((error) => {
308+
this.$notify.error({
309+
key,
310+
message: 'Unstake failed',
311+
description: error.message
312+
})
313+
})
314+
}
247315
}
248316
</script>
249317

src/utils/stake.ts

Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
import { publicKey, u128, u64 } from '@project-serum/borsh';
1+
import { publicKey, u128, u64 } from '@project-serum/borsh'
22
import {
3-
Connection, PublicKey, SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY, Transaction,
3+
Connection,
4+
PublicKey,
5+
SYSVAR_CLOCK_PUBKEY,
6+
SYSVAR_RENT_PUBKEY,
7+
Transaction,
48
TransactionInstruction
5-
} from '@solana/web3.js';
9+
} from '@solana/web3.js'
610
// @ts-ignore
7-
import { blob, nu64, seq, struct, u8 } from 'buffer-layout';
11+
import { blob, nu64, seq, struct, u8 } from 'buffer-layout'
812

9-
import { FarmInfo } from '@/utils/farms';
10-
import { SYSTEM_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@/utils/ids';
11-
import { TokenAmount } from '@/utils/safe-math';
13+
import { FarmInfo } from '@/utils/farms'
14+
import { SYSTEM_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@/utils/ids'
15+
import { TokenAmount } from '@/utils/safe-math'
1216
import {
13-
createAssociatedTokenAccountIfNotExist, createProgramAccountIfNotExist,
14-
findAssociatedStakeInfoAddress, sendTransaction
15-
} from '@/utils/web3';
16-
import { getBigNumber } from './layouts';
17+
createAssociatedTokenAccountIfNotExist,
18+
createProgramAccountIfNotExist,
19+
createTokenAccountIfNotExist,
20+
findAssociatedStakeInfoAddress,
21+
sendTransaction
22+
} from '@/utils/web3'
23+
import { getBigNumber } from './layouts'
1724

1825
// deposit
1926
export async function deposit(
@@ -476,6 +483,136 @@ export async function withdrawV5(
476483
return await sendTransaction(connection, wallet, transaction, signers)
477484
}
478485

486+
export async function notATAWithdraw(
487+
connection: Connection | undefined | null,
488+
wallet: any | undefined | null,
489+
farmInfo: FarmInfo | undefined | null,
490+
walletAccount: { [mint: string]: { tokenAccountAddress: string; balance: TokenAmount } },
491+
depositInfo: any
492+
) {
493+
if (!connection || !wallet) throw new Error('Miss connection')
494+
if (!farmInfo) throw new Error('Miss pool infomations')
495+
496+
const transaction = new Transaction()
497+
const signers: any = []
498+
499+
const owner = wallet.publicKey
500+
501+
const infoAccount = depositInfo.stakeAccountAddress
502+
const pda = await findAssociatedStakeInfoAddress(
503+
new PublicKey(farmInfo.poolId),
504+
wallet.publicKey,
505+
new PublicKey(farmInfo.programId)
506+
)
507+
// if no associated userinfo account, create new one
508+
if (pda.toBase58() !== infoAccount) {
509+
transaction.add(
510+
createAssociatedLedgerAccountInstructionV5(
511+
new PublicKey(farmInfo.programId),
512+
new PublicKey(farmInfo.poolId),
513+
pda,
514+
wallet.publicKey
515+
)
516+
)
517+
}
518+
519+
const userLpAccount = await createTokenAccountIfNotExist(
520+
connection,
521+
walletAccount[farmInfo.lp.mintAddress]?.tokenAccountAddress,
522+
owner,
523+
farmInfo.lp.mintAddress,
524+
null,
525+
transaction,
526+
signers
527+
)
528+
const userRewardTokenAccount = await createTokenAccountIfNotExist(
529+
connection,
530+
walletAccount[farmInfo.reward.mintAddress]?.tokenAccountAddress,
531+
owner,
532+
farmInfo.reward.mintAddress,
533+
null,
534+
transaction,
535+
signers
536+
)
537+
538+
if (farmInfo.version === 5 || farmInfo.version === 4) {
539+
const userRewardTokenAccountB = await createTokenAccountIfNotExist(
540+
connection,
541+
walletAccount[farmInfo.rewardB?.mintAddress ?? '']?.tokenAccountAddress,
542+
owner,
543+
farmInfo.rewardB?.mintAddress ?? '',
544+
null,
545+
transaction,
546+
signers
547+
)
548+
549+
transaction.add(
550+
withdrawInstructionV5(
551+
new PublicKey(farmInfo.programId),
552+
new PublicKey(farmInfo.poolId),
553+
new PublicKey(farmInfo.poolAuthority),
554+
pda,
555+
[],
556+
wallet.publicKey,
557+
userLpAccount,
558+
new PublicKey(farmInfo.poolLpTokenAccount),
559+
userRewardTokenAccount,
560+
new PublicKey(farmInfo.poolRewardTokenAccount),
561+
userRewardTokenAccountB,
562+
// @ts-ignore
563+
new PublicKey(farmInfo.poolRewardTokenAccountB),
564+
getBigNumber(depositInfo.depositBalance.wei)
565+
)
566+
)
567+
} else if (farmInfo.version === 4) {
568+
const userRewardTokenAccountB = await createTokenAccountIfNotExist(
569+
connection,
570+
walletAccount[farmInfo.rewardB?.mintAddress ?? '']?.tokenAccountAddress,
571+
owner,
572+
farmInfo.rewardB?.mintAddress ?? '',
573+
null,
574+
transaction,
575+
signers
576+
)
577+
578+
transaction.add(
579+
withdrawInstructionV4(
580+
new PublicKey(farmInfo.programId),
581+
new PublicKey(farmInfo.poolId),
582+
new PublicKey(farmInfo.poolAuthority),
583+
pda,
584+
wallet.publicKey,
585+
userLpAccount,
586+
new PublicKey(farmInfo.poolLpTokenAccount),
587+
userRewardTokenAccount,
588+
new PublicKey(farmInfo.poolRewardTokenAccount),
589+
userRewardTokenAccountB,
590+
// @ts-ignore
591+
new PublicKey(farmInfo.poolRewardTokenAccountB),
592+
getBigNumber(depositInfo.depositBalance.wei)
593+
)
594+
)
595+
} else if (farmInfo.version === 3) {
596+
transaction.add(
597+
withdrawInstruction(
598+
new PublicKey(farmInfo.programId),
599+
new PublicKey(farmInfo.poolId),
600+
new PublicKey(farmInfo.poolAuthority),
601+
pda,
602+
[],
603+
wallet.publicKey,
604+
userLpAccount,
605+
new PublicKey(farmInfo.poolLpTokenAccount),
606+
userRewardTokenAccount,
607+
new PublicKey(farmInfo.poolRewardTokenAccount),
608+
getBigNumber(depositInfo.depositBalance.wei)
609+
)
610+
)
611+
}
612+
613+
return await sendTransaction(connection, wallet, transaction, signers)
614+
}
615+
479616
export async function emergencyWithdrawV4(
480617
connection: Connection | undefined | null,
481618
wallet: any | undefined | null,

0 commit comments

Comments
 (0)