|
1 | | -import { publicKey, u128, u64 } from '@project-serum/borsh'; |
| 1 | +import { publicKey, u128, u64 } from '@project-serum/borsh' |
2 | 2 | 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, |
4 | 8 | TransactionInstruction |
5 | | -} from '@solana/web3.js'; |
| 9 | +} from '@solana/web3.js' |
6 | 10 | // @ts-ignore |
7 | | -import { blob, nu64, seq, struct, u8 } from 'buffer-layout'; |
| 11 | +import { blob, nu64, seq, struct, u8 } from 'buffer-layout' |
8 | 12 |
|
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' |
12 | 16 | 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' |
17 | 24 |
|
18 | 25 | // deposit |
19 | 26 | export async function deposit( |
@@ -476,6 +483,136 @@ export async function withdrawV5( |
476 | 483 | return await sendTransaction(connection, wallet, transaction, signers) |
477 | 484 | } |
478 | 485 |
|
| 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 | + |
479 | 616 | export async function emergencyWithdrawV4( |
480 | 617 | connection: Connection | undefined | null, |
481 | 618 | wallet: any | undefined | null, |
|
0 commit comments