Skip to content

Commit 8aebe1c

Browse files
chore: remove get all tokens balances (#3)
1 parent 7811488 commit 8aebe1c

File tree

4 files changed

+51
-75
lines changed

4 files changed

+51
-75
lines changed

packages/plugin-bnb/src/actions/getBalance.ts

+38-61
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
type Memory,
99
type State,
1010
} from "@elizaos/core";
11-
import { getToken, getTokens, getTokenBalances, ChainId } from "@lifi/sdk";
11+
import { getToken } from "@lifi/sdk";
1212

1313
import {
1414
bnbWalletProvider,
@@ -17,7 +17,6 @@ import {
1717
} from "../providers/wallet";
1818
import { getBalanceTemplate } from "../templates";
1919
import type {
20-
Balance,
2120
GetBalanceParams,
2221
GetBalanceResponse,
2322
SupportedChain,
@@ -46,47 +45,37 @@ export class GetBalanceAction {
4645
let resp: GetBalanceResponse = {
4746
chain,
4847
address: address!,
49-
balances: [],
5048
};
5149

52-
// If no specific token is requested, get all token balances
53-
if (!token) {
54-
this.walletProvider.configureLiFiSdk(chain);
55-
const balances = await this.getTokenBalances(chainId, address!);
56-
resp.balances = balances;
57-
} else {
58-
// If specific token is requested and it's not the native token
59-
if (token.toLowerCase() !== nativeSymbol.toLowerCase()) {
60-
let balance: string;
61-
if (token.startsWith("0x")) {
62-
balance = await this.getERC20TokenBalance(
63-
chain,
64-
address!,
65-
token as `0x${string}`
66-
);
67-
} else {
68-
this.walletProvider.configureLiFiSdk(chain);
69-
const tokenInfo = await getToken(chainId, token);
70-
balance = await this.getERC20TokenBalance(
71-
chain,
72-
address!,
73-
tokenInfo.address as `0x${string}`
74-
);
75-
}
76-
77-
resp.balances = [{ token, balance }];
50+
// If ERC20 token is requested
51+
if (token.toLowerCase() !== nativeSymbol.toLowerCase()) {
52+
let amount: string;
53+
if (token.startsWith("0x")) {
54+
amount = await this.getERC20TokenBalance(
55+
chain,
56+
address!,
57+
token as `0x${string}`
58+
);
7859
} else {
79-
// If native token is requested
80-
const nativeBalanceWei = await this.walletProvider
81-
.getPublicClient(chain)
82-
.getBalance({ address: address! });
83-
resp.balances = [
84-
{
85-
token: nativeSymbol,
86-
balance: formatEther(nativeBalanceWei),
87-
},
88-
];
60+
this.walletProvider.configureLiFiSdk(chain);
61+
const tokenInfo = await getToken(chainId, token);
62+
amount = await this.getERC20TokenBalance(
63+
chain,
64+
address!,
65+
tokenInfo.address as `0x${string}`
66+
);
8967
}
68+
69+
resp.balance = { token, amount };
70+
} else {
71+
// If native token is requested
72+
const nativeBalanceWei = await this.walletProvider
73+
.getPublicClient(chain)
74+
.getBalance({ address: address! });
75+
resp.balance = {
76+
token: nativeSymbol,
77+
amount: formatEther(nativeBalanceWei),
78+
};
9079
}
9180

9281
return resp;
@@ -118,22 +107,6 @@ export class GetBalanceAction {
118107
return formatUnits(balance, decimals);
119108
}
120109

121-
async getTokenBalances(
122-
chainId: ChainId,
123-
address: Address
124-
): Promise<Balance[]> {
125-
const tokensResponse = await getTokens();
126-
const tokens = tokensResponse.tokens[chainId];
127-
128-
const tokenBalances = await getTokenBalances(address, tokens);
129-
return tokenBalances
130-
.filter((balance) => balance.amount && balance.amount !== 0n)
131-
.map((balance) => ({
132-
token: balance.symbol,
133-
balance: formatUnits(balance.amount!, balance.decimals),
134-
}));
135-
}
136-
137110
async validateAndNormalizeParams(params: GetBalanceParams): Promise<void> {
138111
if (!params.address) {
139112
params.address = this.walletProvider.getAddress();
@@ -143,11 +116,15 @@ export class GetBalanceAction {
143116
);
144117
}
145118

119+
if (!params.token || params.token === "") {
120+
params.token = "BNB";
121+
}
122+
146123
if (params.chain != "bsc") {
147124
// if token contract address is not provided, only BSC mainnet is supported
148-
if (!(params.token && params.token.startsWith("0x"))) {
125+
if (!params.token.startsWith("0x")) {
149126
throw new Error(
150-
"If token contract address is not provided, only BSC mainnet is supported"
127+
"Only BSC mainnet is supported for querying balance by token symbol"
151128
);
152129
}
153130
}
@@ -196,10 +173,10 @@ export const getBalanceAction = {
196173
const getBalanceResp = await action.getBalance(getBalanceOptions);
197174
if (callback) {
198175
let text = `No balance found for ${getBalanceOptions.address} on ${getBalanceOptions.chain}`;
199-
if (getBalanceResp.balances.length > 0) {
200-
text = `Balance of ${getBalanceResp.address} on ${getBalanceResp.chain}:\n${getBalanceResp.balances
201-
.map(({ token, balance }) => `${token}: ${balance}`)
202-
.join("\n")}`;
176+
if (getBalanceResp.balance) {
177+
text = `Balance of ${getBalanceResp.address} on ${getBalanceResp.chain}:\n${
178+
getBalanceResp.balance.token
179+
}: ${getBalanceResp.balance.amount}`;
203180
}
204181
callback({
205182
text,

packages/plugin-bnb/src/templates/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const getBalanceTemplate = `Given the recent messages and wallet informat
77
Extract the following information about the requested check balance:
88
- Chain to execute on. Must be one of ["bsc", "bscTestnet", "opBNB", "opBNBTestnet"]. Default is "bsc".
99
- Address to check balance for. Optional, must be a valid Ethereum address starting with "0x" or a web3 domain name. If not provided, use the BNB chain Wallet Address.
10-
- Token symbol or address. Optional. The address must be a valid Ethereum address starting with "0x".
10+
- Token symbol or address. Could be a token symbol or address. If the address is provided, it must be a valid Ethereum address starting with "0x". Default is "BNB".
1111
If any field is not provided, use the default value. If no default value is specified, use null.
1212
1313
Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined:
@@ -16,7 +16,7 @@ Respond with a JSON markdown block containing only the extracted values. Use nul
1616
{
1717
"chain": SUPPORTED_CHAINS,
1818
"address": string | null,
19-
"token": string | null
19+
"token": string
2020
}
2121
\`\`\`
2222
`;

packages/plugin-bnb/src/tests/getBalance.test.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe("GetBalance Action", () => {
2929
token: "BNB",
3030
};
3131
const resp = await ga.getBalance(input);
32-
console.log("BNB balance", resp.balances[0]);
32+
console.log("BNB balance", resp.balance);
3333
});
3434

3535
it("get USDC balance", async () => {
@@ -39,16 +39,20 @@ describe("GetBalance Action", () => {
3939
token: "USDC",
4040
};
4141
const resp = await ga.getBalance(input);
42-
console.log("USDC balance", resp.balances[0]);
42+
console.log("USDC balance", resp.balance);
4343
});
4444

45-
it("get all token balances", async () => {
45+
it("get balance by token contract address", async () => {
4646
const input: GetBalanceParams = {
4747
chain: "bsc",
4848
address: account.address,
49+
token: "0x55d398326f99059ff775485246999027b3197955",
4950
};
5051
const resp = await ga.getBalance(input);
51-
console.log("token balances", resp.balances);
52-
}, 50000);
52+
console.log(
53+
"0x55d398326f99059ff775485246999027b3197955 balance",
54+
resp.balance
55+
);
56+
});
5357
});
5458
});

packages/plugin-bnb/src/types/index.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@ import type { Address, Hash } from "viem";
33
export type SupportedChain = "bsc" | "bscTestnet" | "opBNB" | "opBNBTestnet";
44
export type StakeAction = "deposit" | "withdraw" | "claim";
55

6-
export interface Balance {
7-
token: string;
8-
balance: string;
9-
}
10-
116
// Action parameters
127
export interface GetBalanceParams {
138
chain: SupportedChain;
149
address?: Address;
15-
token?: string;
10+
token: string;
1611
}
1712

1813
export interface TransferParams {
@@ -55,7 +50,7 @@ export interface FaucetParams {
5550
export interface GetBalanceResponse {
5651
chain: SupportedChain;
5752
address: Address;
58-
balances: Balance[];
53+
balance?: { token: string; amount: string };
5954
}
6055

6156
export interface TransferResponse {

0 commit comments

Comments
 (0)