Skip to content

Commit 55272a8

Browse files
committed
feat: error pool creation using WBBA
1 parent 525d851 commit 55272a8

2 files changed

Lines changed: 35 additions & 42 deletions

File tree

src/features/liquidityPool/services.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
createInitializeMintInstruction,
88
getMint,
99
NATIVE_MINT,
10-
createSyncNativeInstruction
10+
createSyncNativeInstruction,
11+
createApproveInstruction
1112
} from '@bbachain/spl-token'
1213
import {
1314
CurveType,
@@ -735,12 +736,19 @@ export const useCreatePool = () => {
735736
console.log('💰 BBA/Token pool (BBA as base)')
736737

737738
// Check BBA balance (native daltons)
738-
const userBBABalance = await connection.getBalance(ownerAddress)
739-
const requiredBBA = bbaTodaltons(liquidityBaseAmount)
739+
const userWBBATokenAccount = await getAssociatedTokenAddress(NATIVE_MINT, ownerAddress)
740+
console.log('ini lewat bang')
741+
const userWBBAInfo = await connection.getAccountInfo(userWBBATokenAccount)
742+
if (!userWBBAInfo) {
743+
throw new Error(
744+
'WBBA token account not found. Please ensure you have the required tokens.'
745+
)
746+
}
740747

741-
if (userBBABalance < requiredBBA) {
748+
const userWBBABalance = new BN(userWBBAInfo.data.slice(64, 72), 'le')
749+
if (userWBBABalance.lt(new BN(baseAmountDaltons))) {
742750
throw new Error(
743-
`Insufficient BBA balance. Required: ${liquidityBaseAmount} BBA, Available: ${daltonsToBBA(userBBABalance)} BBA`
751+
`Insufficient ${payload.baseToken.symbol} balance. Required: ${liquidityBaseAmount}, Available: ${userWBBABalance.div(new BN(1000000)).toString()}`
744752
)
745753
}
746754

@@ -761,28 +769,34 @@ export const useCreatePool = () => {
761769
)
762770
}
763771

764-
// Transfer BBA to pool (using special BBA handling)
765-
console.log('🔄 Transferring BBA to pool account...')
766-
const transferBBAIx = SystemProgram.transfer({
767-
fromPubkey: ownerAddress,
768-
toPubkey: swapTokenAAccount,
769-
daltons: requiredBBA
770-
})
771-
772-
const { createSyncNativeInstruction } = await import('@bbachain/spl-token')
773-
const syncBBAIx = createSyncNativeInstruction(swapTokenAAccount)
774-
775772
// Transfer quote token (standard SPL transfer)
776773
const { createTransferInstruction } = await import('@bbachain/spl-token')
774+
775+
const transferBBAIx = createTransferInstruction(
776+
userWBBATokenAccount,
777+
swapTokenAAccount,
778+
ownerAddress,
779+
baseAmountDaltons
780+
)
781+
777782
const transferQuoteIx = createTransferInstruction(
778783
userQuoteTokenAccount,
779784
swapTokenBAccount,
780785
ownerAddress,
781786
quoteAmountDaltons
782787
)
783788

789+
console.log('re-check', {
790+
swapTokenA: swapTokenAAccount.toBase58(),
791+
wbbaTokenAccount: userWBBATokenAccount.toBase58(),
792+
wbbaDaltons: baseAmountDaltons,
793+
wbbaAmount: baseAmount
794+
})
795+
784796
// Combine all transfers
785-
const liquidityTx = new Transaction().add(transferBBAIx, syncBBAIx, transferQuoteIx)
797+
const liquidityTx = new Transaction().add(transferBBAIx, transferQuoteIx)
798+
799+
console.log('this causes error')
786800
const liquiditySig = await sendTransactionWithRetry(
787801
liquidityTx,
788802
connection,

src/features/swap/services.ts

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,25 @@
11
import * as BufferLayout from '@bbachain/buffer-layout'
22
import {
33
getAssociatedTokenAddress,
4-
createAssociatedTokenAccountInstruction,
54
TOKEN_PROGRAM_ID,
65
NATIVE_MINT,
7-
createApproveInstruction,
8-
createSyncNativeInstruction,
9-
createCloseAccountInstruction,
10-
ASSOCIATED_TOKEN_PROGRAM_ID
6+
createApproveInstruction
117
} from '@bbachain/spl-token'
128
import {
139
createSwapInstruction,
1410
PROGRAM_ID as TOKEN_SWAP_PROGRAM_ID
1511
} from '@bbachain/spl-token-swap'
1612
import { useConnection, useWallet } from '@bbachain/wallet-adapter-react'
17-
import {
18-
PublicKey,
19-
Transaction,
20-
SystemProgram,
21-
Keypair,
22-
TransactionInstruction
23-
} from '@bbachain/web3.js'
13+
import { PublicKey, Transaction, TransactionInstruction } from '@bbachain/web3.js'
2414
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
2515
import axios from 'axios'
2616

2717
import { TGetTokensResponse } from '@/app/api/tokens/route'
2818
import ENDPOINTS from '@/constants/endpoint'
2919
import SERVICES_KEY from '@/constants/service'
30-
import {
31-
wrapBBAtoWBBA,
32-
unwrapWBBAtoBBA,
33-
createTokenAccountManual,
34-
bbaTodaltons,
35-
daltonsToBBA
36-
} from '@/lib/bbaWrapping'
20+
import { bbaTodaltons } from '@/lib/bbaWrapping'
3721
import { getTokenAccounts2 } from '@/lib/tokenAccount'
38-
import {
39-
isNativeBBA,
40-
getWBBAMintAddress,
41-
isBBAPool,
42-
getBBAPositionInPool
43-
} from '@/staticData/tokens'
22+
import { isNativeBBA } from '@/staticData/tokens'
4423

4524
import { getAllPoolsFromOnchain, OnchainPoolData } from '../liquidityPool/onchain'
4625
import { TGetTokenDataResponse, TGetTokenResponse } from '../tokens/types'

0 commit comments

Comments
 (0)