Skip to content

Commit 3d946d4

Browse files
committed
Improve SPL error handling
1 parent 1fff407 commit 3d946d4

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@goat-sdk/plugin-spl-token": patch
3+
---
4+
5+
Improve error handling

typescript/packages/plugins/spl-token/src/parameters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class TransferTokenByMintAddressParameters extends createToolParameters(
1818
z.object({
1919
mintAddress: z.string().describe("The mint address of the token to transfer"),
2020
to: z.string().describe("The address to transfer the token to"),
21-
amount: z.string().describe("The amount of tokens to transfer"),
21+
amount: z.string().describe("The amount of tokens to transfer in base unit"),
2222
}),
2323
) {}
2424

typescript/packages/plugins/spl-token/src/spl-token.service.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,24 @@ export class SplTokenService {
4949
parameters: GetTokenBalanceByMintAddressParameters,
5050
) {
5151
const { walletAddress, mintAddress } = parameters;
52-
const tokenAccount = getAssociatedTokenAddressSync(new PublicKey(mintAddress), new PublicKey(walletAddress));
53-
const balance = walletClient.getConnection().getTokenAccountBalance(tokenAccount);
54-
return balance;
52+
try {
53+
const tokenAccount = getAssociatedTokenAddressSync(
54+
new PublicKey(mintAddress),
55+
new PublicKey(walletAddress),
56+
);
57+
58+
const accountExists = await doesAccountExist(walletClient.getConnection(), tokenAccount);
59+
60+
if (!accountExists) {
61+
return 0;
62+
}
63+
64+
const balance = await walletClient.getConnection().getTokenAccountBalance(tokenAccount);
65+
66+
return balance;
67+
} catch (error) {
68+
throw new Error(`Failed to get token balance: ${error}`);
69+
}
5570
}
5671

5772
@Tool({

0 commit comments

Comments
 (0)