Skip to content

Commit

Permalink
fix: getOrCreateATAInstruction
Browse files Browse the repository at this point in the history
  • Loading branch information
McSam94 committed Feb 5, 2024
1 parent 3fa2749 commit 9e9bf95
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ts-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meteora-ag/dlmm",
"version": "1.0.7",
"version": "1.0.8-abc1234.0",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
48 changes: 27 additions & 21 deletions ts-client/src/dlmm/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { BN, EventParser } from "@coral-xyz/anchor";
import {
NATIVE_MINT,
TOKEN_PROGRAM_ID,
TokenAccountNotFoundError,
TokenInvalidAccountOwnerError,
createAssociatedTokenAccountInstruction,
createCloseAccountInstruction,
getAccount,
getAssociatedTokenAddress,
getAssociatedTokenAddressSync,
getMint,
} from "@solana/spl-token";
Expand Down Expand Up @@ -81,29 +82,34 @@ export const getOrCreateATAInstruction = async (
payer: PublicKey = owner,
allowOwnerOffCurve = true
): Promise<GetOrCreateATAResponse> => {
try {
const toAccount = await getAssociatedTokenAddress(
tokenMint,
owner,
allowOwnerOffCurve
);

const account = await connection.getAccountInfo(toAccount);

if (account) return { ataPubKey: toAccount, ix: undefined };
const toAccount = getAssociatedTokenAddressSync(
tokenMint,
owner,
allowOwnerOffCurve
);

const ix = createAssociatedTokenAccountInstruction(
payer,
toAccount,
owner,
tokenMint
);
try {
await getAccount(connection, toAccount);

return { ataPubKey: toAccount, ix };
return { ataPubKey: toAccount, ix: undefined };
} catch (e) {
/* handle error */
console.error("Error::getOrCreateATAInstruction", e);
throw e;
if (
e instanceof TokenAccountNotFoundError ||
e instanceof TokenInvalidAccountOwnerError
) {
const ix = createAssociatedTokenAccountInstruction(
payer,
toAccount,
owner,
tokenMint
);

return { ataPubKey: toAccount, ix };
} else {
/* handle error */
console.error("Error::getOrCreateATAInstruction", e);
throw e;
}
}
};

Expand Down

0 comments on commit 9e9bf95

Please sign in to comment.