Skip to content

Commit

Permalink
Improve SPL error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
0xaguspunk committed Dec 18, 2024
1 parent 1fff407 commit 3d946d4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
5 changes: 5 additions & 0 deletions typescript/.changeset/chilled-bugs-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@goat-sdk/plugin-spl-token": patch
---

Improve error handling
2 changes: 1 addition & 1 deletion typescript/packages/plugins/spl-token/src/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class TransferTokenByMintAddressParameters extends createToolParameters(
z.object({
mintAddress: z.string().describe("The mint address of the token to transfer"),
to: z.string().describe("The address to transfer the token to"),
amount: z.string().describe("The amount of tokens to transfer"),
amount: z.string().describe("The amount of tokens to transfer in base unit"),
}),
) {}

Expand Down
21 changes: 18 additions & 3 deletions typescript/packages/plugins/spl-token/src/spl-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,24 @@ export class SplTokenService {
parameters: GetTokenBalanceByMintAddressParameters,
) {
const { walletAddress, mintAddress } = parameters;
const tokenAccount = getAssociatedTokenAddressSync(new PublicKey(mintAddress), new PublicKey(walletAddress));
const balance = walletClient.getConnection().getTokenAccountBalance(tokenAccount);
return balance;
try {
const tokenAccount = getAssociatedTokenAddressSync(
new PublicKey(mintAddress),
new PublicKey(walletAddress),
);

const accountExists = await doesAccountExist(walletClient.getConnection(), tokenAccount);

if (!accountExists) {
return 0;
}

const balance = await walletClient.getConnection().getTokenAccountBalance(tokenAccount);

return balance;
} catch (error) {
throw new Error(`Failed to get token balance: ${error}`);
}
}

@Tool({
Expand Down

0 comments on commit 3d946d4

Please sign in to comment.