fibx 0.2.6 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -195
- package/dist/commands/auth/login.d.ts.map +1 -1
- package/dist/commands/auth/login.js +2 -21
- package/dist/commands/auth/login.js.map +1 -1
- package/dist/commands/auth/verify.d.ts.map +1 -1
- package/dist/commands/auth/verify.js +11 -44
- package/dist/commands/auth/verify.js.map +1 -1
- package/dist/commands/defi/aave.js +1 -3
- package/dist/commands/defi/aave.js.map +1 -1
- package/dist/commands/trade/swap.d.ts.map +1 -1
- package/dist/commands/trade/swap.js +19 -7
- package/dist/commands/trade/swap.js.map +1 -1
- package/dist/commands/wallet/list.d.ts +1 -1
- package/dist/commands/wallet/list.d.ts.map +1 -1
- package/dist/commands/wallet/list.js +18 -26
- package/dist/commands/wallet/list.js.map +1 -1
- package/dist/commands/wallet/send.d.ts.map +1 -1
- package/dist/commands/wallet/send.js +1 -3
- package/dist/commands/wallet/send.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/lib/config.d.ts +1 -0
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +3 -0
- package/dist/lib/config.js.map +1 -1
- package/dist/services/api/client.d.ts +34 -0
- package/dist/services/api/client.d.ts.map +1 -0
- package/dist/services/api/client.js +68 -0
- package/dist/services/api/client.js.map +1 -0
- package/dist/services/chain/client.d.ts +1 -2
- package/dist/services/chain/client.d.ts.map +1 -1
- package/dist/services/chain/client.js +6 -6
- package/dist/services/chain/client.js.map +1 -1
- package/dist/services/defi/aave.d.ts +3 -0
- package/dist/services/defi/aave.d.ts.map +1 -1
- package/dist/services/defi/aave.js +170 -50
- package/dist/services/defi/aave.js.map +1 -1
- package/dist/services/defi/abi/aave.d.ts +60 -0
- package/dist/services/defi/abi/aave.d.ts.map +1 -1
- package/dist/services/defi/abi/aave.js +41 -0
- package/dist/services/defi/abi/aave.js.map +1 -1
- package/dist/services/defi/constants.d.ts +1 -0
- package/dist/services/defi/constants.d.ts.map +1 -1
- package/dist/services/defi/constants.js +2 -0
- package/dist/services/defi/constants.js.map +1 -1
- package/dist/services/privy/account.d.ts +5 -2
- package/dist/services/privy/account.d.ts.map +1 -1
- package/dist/services/privy/account.js +19 -21
- package/dist/services/privy/account.js.map +1 -1
- package/dist/services/privy/client.d.ts +1 -14
- package/dist/services/privy/client.d.ts.map +1 -1
- package/dist/services/privy/client.js +6 -62
- package/dist/services/privy/client.js.map +1 -1
- package/package.json +1 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"list.js","sourceRoot":"","sources":["../../../src/commands/wallet/list.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"list.js","sourceRoot":"","sources":["../../../src/commands/wallet/list.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,WAAW,EAAsB,MAAM,qBAAqB,CAAC;AAEpF,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,MAAc,EAAE,IAAmB;IACvE,IAAI,CAAC;QACJ,MAAM,OAAO,GAAG,WAAW,EAAE,CAAC;QAE9B,IAAI,CAAC,OAAO,EAAE,CAAC;YACd,YAAY,CACX,EAAE,OAAO,EAAE,yDAAyD,EAAE,EACtE,IAAI,CACJ,CAAC;YACF,OAAO;QACR,CAAC;QAED,MAAM,UAAU,GAAG;YAClB,OAAO,EAAE,OAAO,CAAC,aAAa;YAC9B,EAAE,EAAE,OAAO,CAAC,QAAQ,IAAI,KAAK;YAC7B,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,SAAS,EAAE,OAAO,CAAC,SAAS;SAC5B,CAAC;QAEF,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YACf,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;QACpD,CAAC;aAAM,CAAC;YACP,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;YAC9B,OAAO,CAAC,GAAG,CAAC,cAAc,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;YAChD,OAAO,CAAC,GAAG,CAAC,YAAY,UAAU,CAAC,EAAE,EAAE,CAAC,CAAC;YACzC,OAAO,CAAC,GAAG,CAAC,YAAY,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC;YAC3C,OAAO,CAAC,GAAG,CAAC,YAAY,UAAU,CAAC,SAAS,EAAE,CAAC,CAAC;QACjD,CAAC;IACF,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,WAAW,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC1B,CAAC;AACF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"send.d.ts","sourceRoot":"","sources":["../../../src/commands/wallet/send.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"send.d.ts","sourceRoot":"","sources":["../../../src/commands/wallet/send.ts"],"names":[],"mappings":"AAQA,OAAO,EAA0C,KAAK,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEjG,wBAAsB,WAAW,CAChC,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,MAAM,EACnB,IAAI,EAAE,aAAa,GACjB,OAAO,CAAC,IAAI,CAAC,CAiFf"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { requireSession } from "../../services/auth/session.js";
|
|
2
|
-
import { getPrivyClient } from "../../services/privy/client.js";
|
|
3
2
|
import { getWalletClient, getPublicClient } from "../../services/chain/client.js";
|
|
4
3
|
import { getChainConfig } from "../../services/chain/constants.js";
|
|
5
4
|
import { ERC20_ABI } from "../../services/chain/erc20.js";
|
|
@@ -15,8 +14,7 @@ export async function sendCommand(amount, recipient, tokenSymbol, opts) {
|
|
|
15
14
|
const chainName = globalOpts.chain || "base";
|
|
16
15
|
const chain = getChainConfig(chainName);
|
|
17
16
|
const session = requireSession();
|
|
18
|
-
const
|
|
19
|
-
const walletClient = getWalletClient(privy, session, chain);
|
|
17
|
+
const walletClient = getWalletClient(session, chain);
|
|
20
18
|
const publicClient = getPublicClient(chain);
|
|
21
19
|
const wallet = session.walletAddress;
|
|
22
20
|
let txHash;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"send.js","sourceRoot":"","sources":["../../../src/commands/wallet/send.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"send.js","sourceRoot":"","sources":["../../../src/commands/wallet/send.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAClF,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAsB,MAAM,qBAAqB,CAAC;AAEjG,MAAM,CAAC,KAAK,UAAU,WAAW,CAChC,MAAc,EACd,SAAiB,EACjB,WAAmB,EACnB,IAAmB;IAEnB,IAAI,CAAC;QACJ,cAAc,CAAC,MAAM,CAAC,CAAC;QACvB,eAAe,CAAC,SAAS,CAAC,CAAC;QAE3B,MAAM,UAAU,GAAG,IAAqC,CAAC;QACzD,MAAM,SAAS,GAAG,UAAU,CAAC,KAAK,IAAI,MAAM,CAAC;QAC7C,MAAM,KAAK,GAAG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AC,EACD,IAAI,CACJ,CAAC;QACH,CAAC;aAAM,CAAC;YACP,MAAM,KAAK,GAAG,MAAM,YAAY,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;YACrD,YAAY,GAAG,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;YAEnD,MAAM,GAAG,MAAM,WAAW,CACzB,WAAW,MAAM,IAAI,KAAK,CAAC,MAAM,OAAO,KAAK,CAAC,IAAI,KAAK,EACvD,KAAK,IAAI,EAAE;gBACV,0BAA0B;gBAC1B,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,YAAY,CAAC,gBAAgB,CAAC;oBACvD,OAAO,EAAE,KAAK,CAAC,OAAkB;oBACjC,GAAG,EAAE,SAAS;oBACd,YAAY,EAAE,UAAU;oBACxB,IAAI,EAAE,CAAC,SAAoB,EAAE,YAAY,CAAC;oBAC1C,OAAO,EAAE,MAAM;iBACf,CAAC,CAAC;gBAEH,OAAO,YAAY,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;YAC5C,CAAC,EACD,IAAI,CACJ,CAAC;QACH,CAAC;QAED,YAAY,CACX;YACC,MAAM;YACN,MAAM;YACN,SAAS;YACT,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,cAAc,CAAC,WAAW,EAAE;YAC/D,KAAK,EAAE,KAAK,CAAC,IAAI;SACjB,EACD,IAAI,CACJ,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,WAAW,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC1B,CAAC;AACF,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,7 @@ const program = new Command();
|
|
|
16
16
|
program
|
|
17
17
|
.name("fibx")
|
|
18
18
|
.description("Fibrous DeFi CLI — wallet, transfer, swap")
|
|
19
|
-
.version("0.
|
|
19
|
+
.version("0.3.1")
|
|
20
20
|
.option("-c, --chain <chain>", "Chain to use (base, citrea, hyperevm, monad)", "base")
|
|
21
21
|
.option("--json", "Output results as JSON", false)
|
|
22
22
|
.showHelpAfterError();
|
package/dist/lib/config.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export declare const paths: import("env-paths").Paths;
|
|
2
2
|
export declare const FIBROUS_BASE_URL = "https://api.fibrous.finance";
|
|
3
3
|
export declare const FIBROUS_GRAPH_URL = "https://graph.fibrous.finance";
|
|
4
|
+
export declare const FIBX_API_URL_DEFAULT = "https://fibx-server.vercel.app";
|
|
4
5
|
export declare const DEFAULT_SLIPPAGE = 0.5;
|
|
5
6
|
export declare const TOKEN_CACHE_TTL_MS: number;
|
|
6
7
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/lib/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/lib/config.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,KAAK,2BAAmB,CAAC;AAEtC,eAAO,MAAM,gBAAgB,gCAAgC,CAAC;AAC9D,eAAO,MAAM,iBAAiB,kCAAkC,CAAC;
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/lib/config.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,KAAK,2BAAmB,CAAC;AAEtC,eAAO,MAAM,gBAAgB,gCAAgC,CAAC;AAC9D,eAAO,MAAM,iBAAiB,kCAAkC,CAAC;AAIjE,eAAO,MAAM,oBAAoB,mCAAmC,CAAC;AAErE,eAAO,MAAM,gBAAgB,MAAM,CAAC;AACpC,eAAO,MAAM,kBAAkB,QAAsB,CAAC"}
|
package/dist/lib/config.js
CHANGED
|
@@ -2,6 +2,9 @@ import envPaths from "env-paths";
|
|
|
2
2
|
export const paths = envPaths("fibx");
|
|
3
3
|
export const FIBROUS_BASE_URL = "https://api.fibrous.finance";
|
|
4
4
|
export const FIBROUS_GRAPH_URL = "https://graph.fibrous.finance";
|
|
5
|
+
// Hardcoded default API URL for the package
|
|
6
|
+
// Users can override this with FIBX_API_URL env var if needed (e.g. for local dev)
|
|
7
|
+
export const FIBX_API_URL_DEFAULT = "https://fibx-server.vercel.app";
|
|
5
8
|
export const DEFAULT_SLIPPAGE = 0.5;
|
|
6
9
|
export const TOKEN_CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
7
10
|
//# sourceMappingURL=config.js.map
|
package/dist/lib/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/lib/config.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,WAAW,CAAC;AAEjC,MAAM,CAAC,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC;AAEtC,MAAM,CAAC,MAAM,gBAAgB,GAAG,6BAA6B,CAAC;AAC9D,MAAM,CAAC,MAAM,iBAAiB,GAAG,+BAA+B,CAAC;AAEjE,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,CAAC;AACpC,MAAM,CAAC,MAAM,kBAAkB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC"}
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/lib/config.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,WAAW,CAAC;AAEjC,MAAM,CAAC,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC;AAEtC,MAAM,CAAC,MAAM,gBAAgB,GAAG,6BAA6B,CAAC;AAC9D,MAAM,CAAC,MAAM,iBAAiB,GAAG,+BAA+B,CAAC;AAEjE,4CAA4C;AAC5C,mFAAmF;AACnF,MAAM,CAAC,MAAM,oBAAoB,GAAG,gCAAgC,CAAC;AAErE,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,CAAC;AACpC,MAAM,CAAC,MAAM,kBAAkB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export declare function apiLogin(email: string): Promise<{
|
|
2
|
+
success: boolean;
|
|
3
|
+
message: string;
|
|
4
|
+
}>;
|
|
5
|
+
export interface VerifyResponse {
|
|
6
|
+
userId: string;
|
|
7
|
+
walletId: string;
|
|
8
|
+
walletAddress: string;
|
|
9
|
+
token: string;
|
|
10
|
+
isExisting: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare function apiVerify(email: string, code: string): Promise<VerifyResponse>;
|
|
13
|
+
export declare function apiFindWallet(email: string, token: string): Promise<{
|
|
14
|
+
wallet: {
|
|
15
|
+
id: string;
|
|
16
|
+
address: string;
|
|
17
|
+
} | null;
|
|
18
|
+
}>;
|
|
19
|
+
export declare function apiCreateWallet(token: string, userId?: string): Promise<{
|
|
20
|
+
wallet: {
|
|
21
|
+
id: string;
|
|
22
|
+
address: string;
|
|
23
|
+
};
|
|
24
|
+
}>;
|
|
25
|
+
export declare function apiSignTransaction(walletId: string, transaction: Record<string, unknown>, token: string): Promise<{
|
|
26
|
+
signedTransaction: string;
|
|
27
|
+
}>;
|
|
28
|
+
export declare function apiSignMessage(walletId: string, message: string | Record<string, unknown>, token: string): Promise<{
|
|
29
|
+
signature: string;
|
|
30
|
+
}>;
|
|
31
|
+
export declare function apiSignTypedData(walletId: string, typedData: Record<string, unknown>, token: string): Promise<{
|
|
32
|
+
signature: string;
|
|
33
|
+
}>;
|
|
34
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/services/api/client.ts"],"names":[],"mappings":"AAuEA,wBAAsB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,CAE5F;AAED,MAAM,WAAW,cAAc;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,QAA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@@
|
|
|
1
|
+
import { ErrorCode, FibxError } from "../../lib/errors.js";
|
|
2
|
+
import { FIBX_API_URL_DEFAULT } from "../../lib/config.js";
|
|
3
|
+
const REQUEST_TIMEOUT_MS = 30_000; // 30 seconds
|
|
4
|
+
function getBaseUrl() {
|
|
5
|
+
return process.env.FIBX_API_URL ?? FIBX_API_URL_DEFAULT;
|
|
6
|
+
}
|
|
7
|
+
async function request(path, body, options) {
|
|
8
|
+
const url = `${getBaseUrl()}${path}`;
|
|
9
|
+
const timeout = options?.timeoutMs ?? REQUEST_TIMEOUT_MS;
|
|
10
|
+
const headers = {
|
|
11
|
+
"Content-Type": "application/json",
|
|
12
|
+
};
|
|
13
|
+
if (options?.token) {
|
|
14
|
+
headers.Authorization = `Bearer ${options.token}`;
|
|
15
|
+
}
|
|
16
|
+
const controller = new AbortController();
|
|
17
|
+
const timer = setTimeout(() => controller.abort(), timeout);
|
|
18
|
+
try {
|
|
19
|
+
const res = await fetch(url, {
|
|
20
|
+
method: "POST",
|
|
21
|
+
headers,
|
|
22
|
+
body: JSON.stringify(body),
|
|
23
|
+
signal: controller.signal,
|
|
24
|
+
});
|
|
25
|
+
if (!res.ok) {
|
|
26
|
+
const errorBody = await res
|
|
27
|
+
.json()
|
|
28
|
+
.catch(() => ({ error: { message: res.statusText } }));
|
|
29
|
+
const msg = errorBody?.error?.message ??
|
|
30
|
+
`API error (${res.status})`;
|
|
31
|
+
throw new FibxError(ErrorCode.PRIVY_ERROR, msg);
|
|
32
|
+
}
|
|
33
|
+
return (await res.json());
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (error instanceof FibxError)
|
|
37
|
+
throw error;
|
|
38
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
39
|
+
throw new FibxError(ErrorCode.PRIVY_ERROR, `Request to ${path} timed out after ${timeout / 1000}s. Is fibx-server running?`);
|
|
40
|
+
}
|
|
41
|
+
throw new FibxError(ErrorCode.PRIVY_ERROR, `Failed to connect to fibx-server at ${getBaseUrl()}. Is the server running?`);
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
clearTimeout(timer);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export async function apiLogin(email) {
|
|
48
|
+
return request("/auth/login", { email });
|
|
49
|
+
}
|
|
50
|
+
export async function apiVerify(email, code) {
|
|
51
|
+
return request("/auth/verify", { email, code });
|
|
52
|
+
}
|
|
53
|
+
export async function apiFindWallet(email, token) {
|
|
54
|
+
return request("/wallet/find", { email }, { token });
|
|
55
|
+
}
|
|
56
|
+
export async function apiCreateWallet(token, userId) {
|
|
57
|
+
return request("/wallet/create", { userId }, { token });
|
|
58
|
+
}
|
|
59
|
+
export async function apiSignTransaction(walletId, transaction, token) {
|
|
60
|
+
return request("/sign/transaction", { walletId, transaction }, { token });
|
|
61
|
+
}
|
|
62
|
+
export async function apiSignMessage(walletId, message, token) {
|
|
63
|
+
return request("/sign/message", { walletId, message }, { token });
|
|
64
|
+
}
|
|
65
|
+
export async function apiSignTypedData(walletId, typedData, token) {
|
|
66
|
+
return request("/sign/typed-data", { walletId, typedData }, { token });
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/services/api/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAE3D,MAAM,kBAAkB,GAAG,MAAM,CAAC,CAAC,aAAa;AAEhD,SAAS,UAAU;IAClB,OAAO,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,oBAAoB,CAAC;AACzD,CAAC;AAOD,KAAK,UAAU,OAAO,CACrB,IAAY,EACZ,IAA6B,EAC7B,OAAoB;IAEpB,MAAM,GAAG,GAAG,GAAG,UAAU,EAAE,GAAG,IAAI,EAAE,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AAe,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;AACnE,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACrC,QAAgB,EAChB,SAAkC,EAClC,KAAa;IAEb,OAAO,OAAO,CAAC,kBAAkB,EAAE,EAAE,QAAQ,EAAE,SAAS,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;AACxE,CAAC"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { PrivyClient } from "@privy-io/node";
|
|
2
1
|
import type { Session } from "../auth/session.js";
|
|
3
2
|
import type { ChainConfig } from "./constants.js";
|
|
4
3
|
export declare function getPublicClient(chain: ChainConfig): {
|
|
@@ -3643,7 +3642,7 @@ export declare function getPublicClient(chain: ChainConfig): {
|
|
|
3643
3642
|
uid?: undefined;
|
|
3644
3643
|
} & import("viem").ExactPartial<Pick<import("viem").PublicActions<import("viem").HttpTransport<undefined, false>, import("viem").Chain, undefined>, "prepareTransactionRequest" | "call" | "createContractEventFilter" | "createEventFilter" | "estimateContractGas" | "estimateGas" | "getBlock" | "getBlockNumber" | "getChainId" | "getContractEvents" | "getEnsText" | "getFilterChanges" | "getGasPrice" | "getLogs" | "getTransaction" | "getTransactionCount" | "getTransactionReceipt" | "readContract" | "sendRawTransaction" | "simulateContract" | "uninstallFilter" | "watchBlockNumber" | "watchContractEvent"> & Pick<import("viem").WalletActions<import("viem").Chain, undefined>, "sendTransaction" | "writeContract">>>(fn: (client: import("viem").Client<import("viem").HttpTransport<undefined, false>, import("viem").Chain, undefined, import("viem").PublicRpcSchema, import("viem").PublicActions<import("viem").HttpTransport<undefined, false>, import("viem").Chain>>) => client) => import("viem").Client<import("viem").HttpTransport<undefined, false>, import("viem").Chain, undefined, import("viem").PublicRpcSchema, { [K in keyof client]: client[K]; } & import("viem").PublicActions<import("viem").HttpTransport<undefined, false>, import("viem").Chain>>;
|
|
3645
3644
|
};
|
|
3646
|
-
export declare function getWalletClient(
|
|
3645
|
+
export declare function getWalletClient(session: Session, chain: ChainConfig): {
|
|
3647
3646
|
account: {
|
|
3648
3647
|
address: `0x${string}`;
|
|
3649
3648
|
type: "local";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/services/chain/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/services/chain/client.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAGlD,wBAAgB,eAAe,CAAC,KAAK,EAAE,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;irBA4BqoX,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;yBAA+qR,CAAC;mCAA6lC,CAAC;;;;;;;;;;;;;;;;yeAA81mB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EADr6P"}
|
|
@@ -7,20 +7,20 @@ export function getPublicClient(chain) {
|
|
|
7
7
|
transport: http(chain.rpcUrl),
|
|
8
8
|
});
|
|
9
9
|
}
|
|
10
|
-
export function getWalletClient(
|
|
10
|
+
export function getWalletClient(session, chain) {
|
|
11
11
|
let account;
|
|
12
12
|
if (session.type === "private-key" && session.privateKey) {
|
|
13
13
|
account = privateKeyToAccount(session.privateKey);
|
|
14
14
|
}
|
|
15
15
|
else {
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
// Privy session: use backend-backed signing via JWT
|
|
17
|
+
const token = session.userJwt;
|
|
18
|
+
if (!token)
|
|
19
|
+
throw new Error("Session JWT required for privy session type");
|
|
20
20
|
const walletId = session.walletId;
|
|
21
21
|
if (!walletId)
|
|
22
22
|
throw new Error("Wallet ID required for privy session");
|
|
23
|
-
account = toPrivyViemAccount(
|
|
23
|
+
account = toPrivyViemAccount(token, walletId, session.walletAddress);
|
|
24
24
|
}
|
|
25
25
|
return createWalletClient({
|
|
26
26
|
account,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/services/chain/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/services/chain/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAIpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAEzD,MAAM,UAAU,eAAe,CAAC,KAAkB;IACjD,OAAO,kBAAkB,CAAC;QACzB,KAAK,EAAE,KAAK,CAAC,SAAS;QACtB,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;KAC7B,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,OAAgB,EAAE,KAAkB;IACnE,IAAI,OAAO,CAAC;IAEZ,IAAI,OAAO,CAAC,IAAI,KAAK,aAAa,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;QAC1D,OAAO,GAAG,mBAAmB,CAAC,OAAO,CAAC,UAA2B,CAAC,CAAC;IACpE,CAAC;SAAM,CAAC;QACP,oDAAoD;QACpD,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC;QAC9B,IAAI,CAAC,KAAK;YAAE,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;QAC3E,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QAClC,IAAI,CAAC,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;QAEvE,OAAO,GAAG,kBAAkB,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;IACtE,CAAC;IAED,OAAO,kBAAkB,CAAC;QACzB,OAAO;QACP,KAAK,EAAE,KAAK,CAAC,SAAS;QACtB,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;KAC7B,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -28,5 +28,8 @@ export declare class AaveService {
|
|
|
28
28
|
repay(tokenAddress: Address, amountStr: string): Promise<Hash>;
|
|
29
29
|
private ensureWalletConnection;
|
|
30
30
|
private getTokenDecimals;
|
|
31
|
+
private waitForAllowance;
|
|
32
|
+
private getDebts;
|
|
33
|
+
private handleLiquidationError;
|
|
31
34
|
}
|
|
32
35
|
//# sourceMappingURL=aave.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aave.d.ts","sourceRoot":"","sources":["../../../src/services/defi/aave.ts"],"names":[],"mappings":"AAAA,OAAO,EAMN,KAAK,OAAO,EACZ,KAAK,IAAI,EAGT,KAAK,YAAY,EAEjB,MAAM,MAAM,CAAC;AACd,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"aave.d.ts","sourceRoot":"","sources":["../../../src/services/defi/aave.ts"],"names":[],"mappings":"AAAA,OAAO,EAMN,KAAK,OAAO,EACZ,KAAK,IAAI,EAGT,KAAK,YAAY,EAEjB,MAAM,MAAM,CAAC;AACd,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAezD,MAAM,WAAW,eAAe;IAC/B,kBA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EpC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createPublicClient, http, parseUnits, formatUnits, maxUint256, erc20Abi, } from "viem";
|
|
2
|
-
import { POOL_ADDRESSES_PROVIDER_ABI, POOL_ABI, WETH_ABI } from "./abi/aave.js";
|
|
3
|
-
import { AAVE_V3_POOL_ADDRESSES_PROVIDER, InterestRateMode } from "./constants.js";
|
|
2
|
+
import { POOL_ADDRESSES_PROVIDER_ABI, POOL_ABI, POOL_DATA_PROVIDER_ABI, WETH_ABI, } from "./abi/aave.js";
|
|
3
|
+
import { AAVE_V3_POOL_ADDRESSES_PROVIDER, AAVE_V3_POOL_DATA_PROVIDER, InterestRateMode, } from "./constants.js";
|
|
4
4
|
import { NonceManager } from "../chain/nonceManager.js";
|
|
5
5
|
import { ErrorCode, FibxError } from "../../lib/errors.js";
|
|
6
6
|
export class AaveService {
|
|
@@ -63,7 +63,6 @@ export class AaveService {
|
|
|
63
63
|
const poolAddress = await this.getPoolAddress();
|
|
64
64
|
const decimals = await this.getTokenDecimals(tokenAddress);
|
|
65
65
|
const amount = parseUnits(amountStr, decimals);
|
|
66
|
-
// Check Allowance & Approve if necessary
|
|
67
66
|
const allowance = await this.publicClient.readContract({
|
|
68
67
|
address: tokenAddress,
|
|
69
68
|
abi: erc20Abi,
|
|
@@ -82,8 +81,8 @@ export class AaveService {
|
|
|
82
81
|
nonce: nonceApprove,
|
|
83
82
|
});
|
|
84
83
|
await this.publicClient.waitForTransactionReceipt({ hash: txApprove });
|
|
84
|
+
await this.waitForAllowance(tokenAddress, poolAddress, amount);
|
|
85
85
|
}
|
|
86
|
-
// Simulate Supply
|
|
87
86
|
const { request: supplyRequest } = await this.publicClient.simulateContract({
|
|
88
87
|
address: poolAddress,
|
|
89
88
|
abi: POOL_ABI,
|
|
@@ -92,7 +91,6 @@ export class AaveService {
|
|
|
92
91
|
account: this.account,
|
|
93
92
|
chain: this.chainConfig.viemChain,
|
|
94
93
|
});
|
|
95
|
-
// Execute Supply
|
|
96
94
|
const nonceSupply = await NonceManager.getInstance().getNextNonce();
|
|
97
95
|
const txSupply = await this.walletClient.writeContract({
|
|
98
96
|
...supplyRequest,
|
|
@@ -103,8 +101,7 @@ export class AaveService {
|
|
|
103
101
|
}
|
|
104
102
|
async wrapETH(amountStr) {
|
|
105
103
|
this.ensureWalletConnection();
|
|
106
|
-
const amount = parseUnits(amountStr, 18);
|
|
107
|
-
// Simulate Deposit
|
|
104
|
+
const amount = parseUnits(amountStr, 18);
|
|
108
105
|
const { request } = await this.publicClient.simulateContract({
|
|
109
106
|
address: this.chainConfig.wrappedNativeAddress,
|
|
110
107
|
abi: WETH_ABI,
|
|
@@ -125,7 +122,6 @@ export class AaveService {
|
|
|
125
122
|
async unwrapWETH(amountStr) {
|
|
126
123
|
this.ensureWalletConnection();
|
|
127
124
|
const amount = parseUnits(amountStr, 18);
|
|
128
|
-
// Simulate Withdraw
|
|
129
125
|
const { request } = await this.publicClient.simulateContract({
|
|
130
126
|
address: this.chainConfig.wrappedNativeAddress,
|
|
131
127
|
abi: WETH_ABI,
|
|
@@ -154,7 +150,6 @@ export class AaveService {
|
|
|
154
150
|
const decimals = await this.getTokenDecimals(tokenAddress);
|
|
155
151
|
amount = parseUnits(amountStr, decimals);
|
|
156
152
|
}
|
|
157
|
-
// Simulate first to catch errors
|
|
158
153
|
let request;
|
|
159
154
|
try {
|
|
160
155
|
const result = await this.publicClient.simulateContract({
|
|
@@ -168,28 +163,8 @@ export class AaveService {
|
|
|
168
163
|
request = result.request;
|
|
169
164
|
}
|
|
170
165
|
catch (error) {
|
|
171
|
-
|
|
172
|
-
//
|
|
173
|
-
const userData = await this.getUserAccountData(userAddress);
|
|
174
|
-
const totalDebt = parseFloat(userData.totalDebtUSD);
|
|
175
|
-
if (err.message?.includes("HealthFactorLowerThanLiquidationThreshold") ||
|
|
176
|
-
err.cause?.message?.includes("0x6679996d")) {
|
|
177
|
-
let details = "This amount would lower your Health Factor below 1.0.";
|
|
178
|
-
if (totalDebt > 0 && totalDebt < 0.01) {
|
|
179
|
-
details = `You have tiny "dust" debt ($${totalDebt.toFixed(6)}) preventing full withdrawal.`;
|
|
180
|
-
}
|
|
181
|
-
const totalCollateral = parseFloat(userData.totalCollateralUSD);
|
|
182
|
-
const lt = parseFloat(userData.currentLiquidationThreshold) / 10000;
|
|
183
|
-
if (lt > 0) {
|
|
184
|
-
const requiredCollateral = totalDebt / lt;
|
|
185
|
-
const maxSafeUSD = Math.max(0, totalCollateral - requiredCollateral);
|
|
186
|
-
if (maxSafeUSD < totalCollateral) {
|
|
187
|
-
details += ` You need keep ~$${requiredCollateral.toFixed(2)} collateral to cover your debt. Max safe withdraw is approx $${maxSafeUSD.toFixed(2)}.`;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
throw new FibxError(ErrorCode.WALLET_ERROR, `Cannot withdraw: ${details} (Try repaying all debt first)`);
|
|
191
|
-
}
|
|
192
|
-
throw error;
|
|
166
|
+
await this.handleLiquidationError(error, userAddress);
|
|
167
|
+
throw error; // detailed error thrown by handleLiquidationError, but needed for TS control flow
|
|
193
168
|
}
|
|
194
169
|
const nonce = await NonceManager.getInstance().getNextNonce();
|
|
195
170
|
const txWithdraw = await this.walletClient.writeContract({
|
|
@@ -204,7 +179,6 @@ export class AaveService {
|
|
|
204
179
|
const poolAddress = await this.getPoolAddress();
|
|
205
180
|
const decimals = await this.getTokenDecimals(tokenAddress);
|
|
206
181
|
const amount = parseUnits(amountStr, decimals);
|
|
207
|
-
// Simulate first to catch errors
|
|
208
182
|
let request;
|
|
209
183
|
try {
|
|
210
184
|
const result = await this.publicClient.simulateContract({
|
|
@@ -263,30 +237,46 @@ export class AaveService {
|
|
|
263
237
|
address: tokenAddress,
|
|
264
238
|
abi: erc20Abi,
|
|
265
239
|
functionName: "approve",
|
|
266
|
-
args: [poolAddress, amount],
|
|
240
|
+
args: [poolAddress, amount],
|
|
267
241
|
chain: this.chainConfig.viemChain,
|
|
268
242
|
account: this.account,
|
|
269
243
|
nonce: nonceApprove,
|
|
270
244
|
});
|
|
271
245
|
await this.publicClient.waitForTransactionReceipt({ hash: txApprove });
|
|
246
|
+
await this.waitForAllowance(tokenAddress, poolAddress, amount);
|
|
247
|
+
}
|
|
248
|
+
try {
|
|
249
|
+
const { request: repayRequest } = await this.publicClient.simulateContract({
|
|
250
|
+
address: poolAddress,
|
|
251
|
+
abi: POOL_ABI,
|
|
252
|
+
functionName: "repay",
|
|
253
|
+
args: [
|
|
254
|
+
tokenAddress,
|
|
255
|
+
amount,
|
|
256
|
+
BigInt(InterestRateMode.Variable),
|
|
257
|
+
this.account.address,
|
|
258
|
+
],
|
|
259
|
+
account: this.account,
|
|
260
|
+
chain: this.chainConfig.viemChain,
|
|
261
|
+
});
|
|
262
|
+
const nonceRepay = await NonceManager.getInstance().getNextNonce();
|
|
263
|
+
const txRepay = await this.walletClient.writeContract({
|
|
264
|
+
...repayRequest,
|
|
265
|
+
nonce: nonceRepay,
|
|
266
|
+
});
|
|
267
|
+
await this.publicClient.waitForTransactionReceipt({ hash: txRepay });
|
|
268
|
+
return txRepay;
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
const err = error;
|
|
272
|
+
// Check for NoDebtOfSelectedType (0xf0788fb2)
|
|
273
|
+
if (err.message?.includes("NoDebtOfSelectedType") ||
|
|
274
|
+
err.cause?.message?.includes("0xf0788fb2")) {
|
|
275
|
+
// This is actually a success state for "repay max" - we are already debt free
|
|
276
|
+
return "0x0"; // Return dummy hash to indicate success without tx
|
|
277
|
+
}
|
|
278
|
+
throw error;
|
|
272
279
|
}
|
|
273
|
-
// Simulate Repay
|
|
274
|
-
const { request: repayRequest } = await this.publicClient.simulateContract({
|
|
275
|
-
address: poolAddress,
|
|
276
|
-
abi: POOL_ABI,
|
|
277
|
-
functionName: "repay",
|
|
278
|
-
args: [tokenAddress, amount, BigInt(InterestRateMode.Variable), this.account.address],
|
|
279
|
-
account: this.account,
|
|
280
|
-
chain: this.chainConfig.viemChain,
|
|
281
|
-
});
|
|
282
|
-
// Execute Repay
|
|
283
|
-
const nonceRepay = await NonceManager.getInstance().getNextNonce();
|
|
284
|
-
const txRepay = await this.walletClient.writeContract({
|
|
285
|
-
...repayRequest,
|
|
286
|
-
nonce: nonceRepay,
|
|
287
|
-
});
|
|
288
|
-
await this.publicClient.waitForTransactionReceipt({ hash: txRepay });
|
|
289
|
-
return txRepay;
|
|
290
280
|
}
|
|
291
281
|
ensureWalletConnection() {
|
|
292
282
|
if (!this.walletClient || !this.account) {
|
|
@@ -300,5 +290,135 @@ export class AaveService {
|
|
|
300
290
|
functionName: "decimals",
|
|
301
291
|
});
|
|
302
292
|
}
|
|
293
|
+
async waitForAllowance(tokenAddress, spender, targetAmount) {
|
|
294
|
+
let retries = 0;
|
|
295
|
+
const maxRetries = 15; // 30 seconds
|
|
296
|
+
while (retries < maxRetries) {
|
|
297
|
+
const allowance = await this.publicClient.readContract({
|
|
298
|
+
address: tokenAddress,
|
|
299
|
+
abi: erc20Abi,
|
|
300
|
+
functionName: "allowance",
|
|
301
|
+
args: [this.account.address, spender],
|
|
302
|
+
});
|
|
303
|
+
if (allowance >= targetAmount) {
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
307
|
+
retries++;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
async getDebts(dataProviderAddress, reserves) {
|
|
311
|
+
const debtDetails = [];
|
|
312
|
+
// 1. Batch fetch all reserve data using multicall
|
|
313
|
+
const reserveDataCalls = reserves.map((asset) => ({
|
|
314
|
+
address: dataProviderAddress,
|
|
315
|
+
abi: POOL_DATA_PROVIDER_ABI,
|
|
316
|
+
functionName: "getUserReserveData",
|
|
317
|
+
args: [asset, this.account.address],
|
|
318
|
+
}));
|
|
319
|
+
const results = await this.publicClient.multicall({
|
|
320
|
+
contracts: reserveDataCalls,
|
|
321
|
+
allowFailure: true, // Continue even if some fail
|
|
322
|
+
});
|
|
323
|
+
// 2. Identify assets with debt
|
|
324
|
+
const assetsWithDebt = [];
|
|
325
|
+
const debts = [];
|
|
326
|
+
results.forEach((result, index) => {
|
|
327
|
+
if (result.status === "success" && result.result) {
|
|
328
|
+
const data = result.result;
|
|
329
|
+
const stableDebt = data[1];
|
|
330
|
+
const variableDebt = data[2];
|
|
331
|
+
if (stableDebt > 0n || variableDebt > 0n) {
|
|
332
|
+
assetsWithDebt.push(reserves[index]);
|
|
333
|
+
debts.push(stableDebt + variableDebt);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
// 3. Fetch symbol/decimals for assets with debt using multicall
|
|
338
|
+
if (assetsWithDebt.length > 0) {
|
|
339
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
340
|
+
const detailCalls = [];
|
|
341
|
+
assetsWithDebt.forEach((asset) => {
|
|
342
|
+
detailCalls.push({
|
|
343
|
+
address: asset,
|
|
344
|
+
abi: erc20Abi,
|
|
345
|
+
functionName: "symbol",
|
|
346
|
+
});
|
|
347
|
+
detailCalls.push({
|
|
348
|
+
address: asset,
|
|
349
|
+
abi: erc20Abi,
|
|
350
|
+
functionName: "decimals",
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
try {
|
|
354
|
+
const detailResults = await this.publicClient.multicall({
|
|
355
|
+
contracts: detailCalls,
|
|
356
|
+
allowFailure: false, // If these fail, we can't display details properly
|
|
357
|
+
});
|
|
358
|
+
for (let i = 0; i < assetsWithDebt.length; i++) {
|
|
359
|
+
const symbol = detailResults[i * 2];
|
|
360
|
+
const decimals = detailResults[i * 2 + 1];
|
|
361
|
+
const debt = debts[i];
|
|
362
|
+
debtDetails.push(`${symbol}: $${formatUnits(debt, decimals)}`);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
catch (e) {
|
|
366
|
+
console.warn("Failed to fetch debt token details via multicall:", e);
|
|
367
|
+
// Fallback: just list addresses and raw amounts if metadata fails
|
|
368
|
+
assetsWithDebt.forEach((asset, i) => {
|
|
369
|
+
debtDetails.push(`${asset}: raw units ${debts[i]}`);
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return debtDetails;
|
|
374
|
+
}
|
|
375
|
+
async handleLiquidationError(error, userAddress) {
|
|
376
|
+
const err = error;
|
|
377
|
+
const userData = await this.getUserAccountData(userAddress);
|
|
378
|
+
const totalDebt = parseFloat(userData.totalDebtUSD);
|
|
379
|
+
if (err.message?.includes("HealthFactorLowerThanLiquidationThreshold") ||
|
|
380
|
+
err.cause?.message?.includes("0x6679996d")) {
|
|
381
|
+
let details = "This amount would lower your Health Factor below 1.0.";
|
|
382
|
+
// Check for cross-asset dust debt
|
|
383
|
+
if (totalDebt > 0) {
|
|
384
|
+
try {
|
|
385
|
+
const poolContract = {
|
|
386
|
+
address: await this.getPoolAddress(),
|
|
387
|
+
abi: POOL_ABI,
|
|
388
|
+
};
|
|
389
|
+
const reserves = await this.publicClient.readContract({
|
|
390
|
+
...poolContract,
|
|
391
|
+
functionName: "getReservesList",
|
|
392
|
+
});
|
|
393
|
+
const debts = await this.getDebts(AAVE_V3_POOL_DATA_PROVIDER, reserves);
|
|
394
|
+
if (debts.length > 0) {
|
|
395
|
+
details = `You have remaining debt in other assets: ${debts.join(", ")}. You must repay these debts first to withdraw max collateral.`;
|
|
396
|
+
}
|
|
397
|
+
else if (totalDebt < 0.01) {
|
|
398
|
+
// If scanning returned nothing but we have dust debt, show generic message
|
|
399
|
+
details = `You have tiny "dust" debt ($${totalDebt.toFixed(6)}) in an asset (scan failed to identify) preventing full withdrawal.`;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
catch (e) {
|
|
403
|
+
console.error("Error scanning debts:", e);
|
|
404
|
+
// Fallback if scanning fails
|
|
405
|
+
if (totalDebt < 0.01) {
|
|
406
|
+
details = `You have tiny "dust" debt ($${totalDebt.toFixed(6)}) in an asset preventing full withdrawal.`;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
const totalCollateral = parseFloat(userData.totalCollateralUSD);
|
|
411
|
+
const lt = parseFloat(userData.currentLiquidationThreshold) / 10000;
|
|
412
|
+
if (lt > 0) {
|
|
413
|
+
const requiredCollateral = totalDebt / lt;
|
|
414
|
+
const maxSafeUSD = Math.max(0, totalCollateral - requiredCollateral);
|
|
415
|
+
if (maxSafeUSD < totalCollateral) {
|
|
416
|
+
details += ` You need keep ~$${requiredCollateral.toFixed(2)} collateral to cover your debt. Max safe withdraw is approx $${maxSafeUSD.toFixed(2)}.`;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
throw new FibxError(ErrorCode.WALLET_ERROR, `Cannot withdraw: ${details}`);
|
|
420
|
+
}
|
|
421
|
+
throw error;
|
|
422
|
+
}
|
|
303
423
|
}
|
|
304
424
|
//# sourceMappingURL=aave.js.map
|