@axonfi/sdk 0.5.1 → 0.5.3
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 +15 -7
- package/dist/index.cjs +409 -386
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +410 -387
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2546,314 +2546,6 @@ var AxonVaultFactoryAbi = [
|
|
|
2546
2546
|
"inputs": []
|
|
2547
2547
|
}
|
|
2548
2548
|
];
|
|
2549
|
-
function getChain(chainId) {
|
|
2550
|
-
switch (chainId) {
|
|
2551
|
-
case 8453:
|
|
2552
|
-
return chains.base;
|
|
2553
|
-
case 84532:
|
|
2554
|
-
return chains.baseSepolia;
|
|
2555
|
-
case 42161:
|
|
2556
|
-
return chains.arbitrum;
|
|
2557
|
-
case 421614:
|
|
2558
|
-
return chains.arbitrumSepolia;
|
|
2559
|
-
default:
|
|
2560
|
-
throw new Error(
|
|
2561
|
-
`Unsupported chainId: ${chainId}. Supported: 8453 (Base), 84532 (Base Sepolia), 42161 (Arbitrum), 421614 (Arbitrum Sepolia)`
|
|
2562
|
-
);
|
|
2563
|
-
}
|
|
2564
|
-
}
|
|
2565
|
-
function createAxonPublicClient(chainId, rpcUrl) {
|
|
2566
|
-
return viem.createPublicClient({
|
|
2567
|
-
chain: getChain(chainId),
|
|
2568
|
-
transport: viem.http(rpcUrl)
|
|
2569
|
-
});
|
|
2570
|
-
}
|
|
2571
|
-
function createAxonWalletClient(privateKey, chainId) {
|
|
2572
|
-
const account = accounts.privateKeyToAccount(privateKey);
|
|
2573
|
-
return viem.createWalletClient({
|
|
2574
|
-
account,
|
|
2575
|
-
chain: getChain(chainId),
|
|
2576
|
-
transport: viem.http()
|
|
2577
|
-
// signing is local — transport is unused but required by viem
|
|
2578
|
-
});
|
|
2579
|
-
}
|
|
2580
|
-
var USDC_DECIMALS = 6n;
|
|
2581
|
-
var USDC_UNIT = 10n ** USDC_DECIMALS;
|
|
2582
|
-
function toBotConfigParams(input) {
|
|
2583
|
-
return {
|
|
2584
|
-
maxPerTxAmount: BigInt(Math.round(input.maxPerTxAmount * Number(USDC_UNIT))),
|
|
2585
|
-
maxRebalanceAmount: BigInt(Math.round(input.maxRebalanceAmount * Number(USDC_UNIT))),
|
|
2586
|
-
spendingLimits: input.spendingLimits.map((sl) => ({
|
|
2587
|
-
amount: BigInt(Math.round(sl.amount * Number(USDC_UNIT))),
|
|
2588
|
-
maxCount: BigInt(sl.maxCount),
|
|
2589
|
-
windowSeconds: BigInt(sl.windowSeconds)
|
|
2590
|
-
})),
|
|
2591
|
-
aiTriggerThreshold: BigInt(Math.round(input.aiTriggerThreshold * Number(USDC_UNIT))),
|
|
2592
|
-
requireAiVerification: input.requireAiVerification
|
|
2593
|
-
};
|
|
2594
|
-
}
|
|
2595
|
-
var DEFAULT_RELAYER_URL = "https://relay.axonfi.xyz";
|
|
2596
|
-
async function getFactoryAddress(chainId, relayerUrl) {
|
|
2597
|
-
const base2 = relayerUrl ?? DEFAULT_RELAYER_URL;
|
|
2598
|
-
const resp = await fetch(`${base2}/v1/chains`);
|
|
2599
|
-
if (!resp.ok) throw new Error(`Failed to fetch chain config from relayer [${resp.status}]`);
|
|
2600
|
-
const data = await resp.json();
|
|
2601
|
-
const chain = data.chains?.find((c) => c.chainId === chainId);
|
|
2602
|
-
if (!chain?.factoryAddress) {
|
|
2603
|
-
throw new Error(`No factory address available for chainId ${chainId}. Check the relayer's chain configuration.`);
|
|
2604
|
-
}
|
|
2605
|
-
return chain.factoryAddress;
|
|
2606
|
-
}
|
|
2607
|
-
async function getBotConfig(publicClient, vaultAddress, botAddress) {
|
|
2608
|
-
const result = await publicClient.readContract({
|
|
2609
|
-
address: vaultAddress,
|
|
2610
|
-
abi: AxonVaultAbi,
|
|
2611
|
-
functionName: "getBotConfig",
|
|
2612
|
-
args: [botAddress]
|
|
2613
|
-
});
|
|
2614
|
-
return {
|
|
2615
|
-
isActive: result.isActive,
|
|
2616
|
-
registeredAt: result.registeredAt,
|
|
2617
|
-
maxPerTxAmount: result.maxPerTxAmount,
|
|
2618
|
-
maxRebalanceAmount: result.maxRebalanceAmount,
|
|
2619
|
-
spendingLimits: result.spendingLimits.map((sl) => ({
|
|
2620
|
-
amount: sl.amount,
|
|
2621
|
-
maxCount: sl.maxCount,
|
|
2622
|
-
windowSeconds: sl.windowSeconds
|
|
2623
|
-
})),
|
|
2624
|
-
aiTriggerThreshold: result.aiTriggerThreshold,
|
|
2625
|
-
requireAiVerification: result.requireAiVerification
|
|
2626
|
-
};
|
|
2627
|
-
}
|
|
2628
|
-
async function isBotActive(publicClient, vaultAddress, botAddress) {
|
|
2629
|
-
return publicClient.readContract({
|
|
2630
|
-
address: vaultAddress,
|
|
2631
|
-
abi: AxonVaultAbi,
|
|
2632
|
-
functionName: "isBotActive",
|
|
2633
|
-
args: [botAddress]
|
|
2634
|
-
});
|
|
2635
|
-
}
|
|
2636
|
-
async function getOperatorCeilings(publicClient, vaultAddress) {
|
|
2637
|
-
const result = await publicClient.readContract({
|
|
2638
|
-
address: vaultAddress,
|
|
2639
|
-
abi: AxonVaultAbi,
|
|
2640
|
-
functionName: "operatorCeilings"
|
|
2641
|
-
});
|
|
2642
|
-
const [maxPerTxAmount, maxBotDailyLimit, maxOperatorBots, vaultDailyAggregate, minAiTriggerFloor] = result;
|
|
2643
|
-
return {
|
|
2644
|
-
maxPerTxAmount,
|
|
2645
|
-
maxBotDailyLimit,
|
|
2646
|
-
maxOperatorBots,
|
|
2647
|
-
vaultDailyAggregate,
|
|
2648
|
-
minAiTriggerFloor
|
|
2649
|
-
};
|
|
2650
|
-
}
|
|
2651
|
-
async function operatorMaxDrainPerDay(publicClient, vaultAddress) {
|
|
2652
|
-
return publicClient.readContract({
|
|
2653
|
-
address: vaultAddress,
|
|
2654
|
-
abi: AxonVaultAbi,
|
|
2655
|
-
functionName: "operatorMaxDrainPerDay"
|
|
2656
|
-
});
|
|
2657
|
-
}
|
|
2658
|
-
async function isVaultPaused(publicClient, vaultAddress) {
|
|
2659
|
-
return publicClient.readContract({
|
|
2660
|
-
address: vaultAddress,
|
|
2661
|
-
abi: AxonVaultAbi,
|
|
2662
|
-
functionName: "paused"
|
|
2663
|
-
});
|
|
2664
|
-
}
|
|
2665
|
-
async function getDomainSeparator(publicClient, vaultAddress) {
|
|
2666
|
-
return publicClient.readContract({
|
|
2667
|
-
address: vaultAddress,
|
|
2668
|
-
abi: AxonVaultAbi,
|
|
2669
|
-
functionName: "DOMAIN_SEPARATOR"
|
|
2670
|
-
});
|
|
2671
|
-
}
|
|
2672
|
-
async function getVaultVersion(publicClient, vaultAddress) {
|
|
2673
|
-
const version = await publicClient.readContract({
|
|
2674
|
-
address: vaultAddress,
|
|
2675
|
-
abi: AxonVaultAbi,
|
|
2676
|
-
functionName: "VERSION"
|
|
2677
|
-
});
|
|
2678
|
-
return Number(version);
|
|
2679
|
-
}
|
|
2680
|
-
async function getVaultOwner(publicClient, vaultAddress) {
|
|
2681
|
-
return publicClient.readContract({
|
|
2682
|
-
address: vaultAddress,
|
|
2683
|
-
abi: AxonVaultAbi,
|
|
2684
|
-
functionName: "owner"
|
|
2685
|
-
});
|
|
2686
|
-
}
|
|
2687
|
-
async function getVaultOperator(publicClient, vaultAddress) {
|
|
2688
|
-
return publicClient.readContract({
|
|
2689
|
-
address: vaultAddress,
|
|
2690
|
-
abi: AxonVaultAbi,
|
|
2691
|
-
functionName: "operator"
|
|
2692
|
-
});
|
|
2693
|
-
}
|
|
2694
|
-
async function isDestinationAllowed(publicClient, vaultAddress, botAddress, destination) {
|
|
2695
|
-
const isBlacklisted = await publicClient.readContract({
|
|
2696
|
-
address: vaultAddress,
|
|
2697
|
-
abi: AxonVaultAbi,
|
|
2698
|
-
functionName: "globalDestinationBlacklist",
|
|
2699
|
-
args: [destination]
|
|
2700
|
-
});
|
|
2701
|
-
if (isBlacklisted) {
|
|
2702
|
-
return { allowed: false, reason: "Destination is on the global blacklist" };
|
|
2703
|
-
}
|
|
2704
|
-
const globalCount = await publicClient.readContract({
|
|
2705
|
-
address: vaultAddress,
|
|
2706
|
-
abi: AxonVaultAbi,
|
|
2707
|
-
functionName: "globalDestinationCount"
|
|
2708
|
-
});
|
|
2709
|
-
if (globalCount > 0n) {
|
|
2710
|
-
const isGlobalWhitelisted = await publicClient.readContract({
|
|
2711
|
-
address: vaultAddress,
|
|
2712
|
-
abi: AxonVaultAbi,
|
|
2713
|
-
functionName: "globalDestinationWhitelist",
|
|
2714
|
-
args: [destination]
|
|
2715
|
-
});
|
|
2716
|
-
if (!isGlobalWhitelisted) {
|
|
2717
|
-
return { allowed: false, reason: "Destination is not on the global whitelist" };
|
|
2718
|
-
}
|
|
2719
|
-
}
|
|
2720
|
-
const botCount = await publicClient.readContract({
|
|
2721
|
-
address: vaultAddress,
|
|
2722
|
-
abi: AxonVaultAbi,
|
|
2723
|
-
functionName: "botDestinationCount",
|
|
2724
|
-
args: [botAddress]
|
|
2725
|
-
});
|
|
2726
|
-
if (botCount > 0n) {
|
|
2727
|
-
const isBotWhitelisted = await publicClient.readContract({
|
|
2728
|
-
address: vaultAddress,
|
|
2729
|
-
abi: AxonVaultAbi,
|
|
2730
|
-
functionName: "botDestinationWhitelist",
|
|
2731
|
-
args: [botAddress, destination]
|
|
2732
|
-
});
|
|
2733
|
-
if (!isBotWhitelisted) {
|
|
2734
|
-
return { allowed: false, reason: "Destination is not on the bot whitelist" };
|
|
2735
|
-
}
|
|
2736
|
-
}
|
|
2737
|
-
return { allowed: true };
|
|
2738
|
-
}
|
|
2739
|
-
async function getRebalanceTokenCount(publicClient, vaultAddress) {
|
|
2740
|
-
const count = await publicClient.readContract({
|
|
2741
|
-
address: vaultAddress,
|
|
2742
|
-
abi: AxonVaultAbi,
|
|
2743
|
-
functionName: "rebalanceTokenCount"
|
|
2744
|
-
});
|
|
2745
|
-
return Number(count);
|
|
2746
|
-
}
|
|
2747
|
-
async function isRebalanceTokenWhitelisted(publicClient, vaultAddress, token) {
|
|
2748
|
-
return publicClient.readContract({
|
|
2749
|
-
address: vaultAddress,
|
|
2750
|
-
abi: AxonVaultAbi,
|
|
2751
|
-
functionName: "rebalanceTokenWhitelist",
|
|
2752
|
-
args: [token]
|
|
2753
|
-
});
|
|
2754
|
-
}
|
|
2755
|
-
async function deployVault(walletClient, publicClient, relayerUrl) {
|
|
2756
|
-
if (!walletClient.account) {
|
|
2757
|
-
throw new Error("walletClient has no account attached");
|
|
2758
|
-
}
|
|
2759
|
-
const chainId = walletClient.chain?.id;
|
|
2760
|
-
if (!chainId) throw new Error("walletClient has no chain configured");
|
|
2761
|
-
const factoryAddress = await getFactoryAddress(chainId, relayerUrl);
|
|
2762
|
-
const hash = await walletClient.writeContract({
|
|
2763
|
-
address: factoryAddress,
|
|
2764
|
-
abi: AxonVaultFactoryAbi,
|
|
2765
|
-
functionName: "deployVault",
|
|
2766
|
-
args: [],
|
|
2767
|
-
account: walletClient.account,
|
|
2768
|
-
chain: walletClient.chain ?? null
|
|
2769
|
-
});
|
|
2770
|
-
const receipt = await publicClient.waitForTransactionReceipt({ hash });
|
|
2771
|
-
for (const log of receipt.logs) {
|
|
2772
|
-
try {
|
|
2773
|
-
if (log.topics.length >= 3 && log.topics[2]) {
|
|
2774
|
-
const vaultAddress = `0x${log.topics[2].slice(26)}`;
|
|
2775
|
-
return vaultAddress;
|
|
2776
|
-
}
|
|
2777
|
-
} catch {
|
|
2778
|
-
}
|
|
2779
|
-
}
|
|
2780
|
-
throw new Error("VaultDeployed event not found in transaction receipt");
|
|
2781
|
-
}
|
|
2782
|
-
async function addBot(walletClient, publicClient, vaultAddress, botAddress, config) {
|
|
2783
|
-
if (!walletClient.account) {
|
|
2784
|
-
throw new Error("walletClient has no account attached");
|
|
2785
|
-
}
|
|
2786
|
-
const params = toBotConfigParams(config);
|
|
2787
|
-
const hash = await walletClient.writeContract({
|
|
2788
|
-
address: vaultAddress,
|
|
2789
|
-
abi: AxonVaultAbi,
|
|
2790
|
-
functionName: "addBot",
|
|
2791
|
-
args: [botAddress, params],
|
|
2792
|
-
account: walletClient.account,
|
|
2793
|
-
chain: walletClient.chain ?? null
|
|
2794
|
-
});
|
|
2795
|
-
await publicClient.waitForTransactionReceipt({ hash });
|
|
2796
|
-
return hash;
|
|
2797
|
-
}
|
|
2798
|
-
async function updateBotConfig(walletClient, publicClient, vaultAddress, botAddress, config) {
|
|
2799
|
-
if (!walletClient.account) {
|
|
2800
|
-
throw new Error("walletClient has no account attached");
|
|
2801
|
-
}
|
|
2802
|
-
const params = toBotConfigParams(config);
|
|
2803
|
-
const hash = await walletClient.writeContract({
|
|
2804
|
-
address: vaultAddress,
|
|
2805
|
-
abi: AxonVaultAbi,
|
|
2806
|
-
functionName: "updateBotConfig",
|
|
2807
|
-
args: [botAddress, params],
|
|
2808
|
-
account: walletClient.account,
|
|
2809
|
-
chain: walletClient.chain ?? null
|
|
2810
|
-
});
|
|
2811
|
-
await publicClient.waitForTransactionReceipt({ hash });
|
|
2812
|
-
return hash;
|
|
2813
|
-
}
|
|
2814
|
-
async function removeBot(walletClient, publicClient, vaultAddress, botAddress) {
|
|
2815
|
-
if (!walletClient.account) {
|
|
2816
|
-
throw new Error("walletClient has no account attached");
|
|
2817
|
-
}
|
|
2818
|
-
const hash = await walletClient.writeContract({
|
|
2819
|
-
address: vaultAddress,
|
|
2820
|
-
abi: AxonVaultAbi,
|
|
2821
|
-
functionName: "removeBot",
|
|
2822
|
-
args: [botAddress],
|
|
2823
|
-
account: walletClient.account,
|
|
2824
|
-
chain: walletClient.chain ?? null
|
|
2825
|
-
});
|
|
2826
|
-
await publicClient.waitForTransactionReceipt({ hash });
|
|
2827
|
-
return hash;
|
|
2828
|
-
}
|
|
2829
|
-
async function deposit(walletClient, publicClient, vaultAddress, token, amount, ref = "0x0000000000000000000000000000000000000000000000000000000000000000") {
|
|
2830
|
-
if (!walletClient.account) {
|
|
2831
|
-
throw new Error("walletClient has no account attached");
|
|
2832
|
-
}
|
|
2833
|
-
const isEth = token.toLowerCase() === NATIVE_ETH.toLowerCase();
|
|
2834
|
-
if (!isEth) {
|
|
2835
|
-
const approveTx = await walletClient.writeContract({
|
|
2836
|
-
address: token,
|
|
2837
|
-
abi: viem.erc20Abi,
|
|
2838
|
-
functionName: "approve",
|
|
2839
|
-
args: [vaultAddress, amount],
|
|
2840
|
-
account: walletClient.account,
|
|
2841
|
-
chain: walletClient.chain ?? null
|
|
2842
|
-
});
|
|
2843
|
-
await publicClient.waitForTransactionReceipt({ hash: approveTx });
|
|
2844
|
-
}
|
|
2845
|
-
const hash = await walletClient.writeContract({
|
|
2846
|
-
address: vaultAddress,
|
|
2847
|
-
abi: AxonVaultAbi,
|
|
2848
|
-
functionName: "deposit",
|
|
2849
|
-
args: [token, amount, ref],
|
|
2850
|
-
account: walletClient.account,
|
|
2851
|
-
chain: walletClient.chain ?? null,
|
|
2852
|
-
...isEth ? { value: amount } : {}
|
|
2853
|
-
});
|
|
2854
|
-
await publicClient.waitForTransactionReceipt({ hash });
|
|
2855
|
-
return hash;
|
|
2856
|
-
}
|
|
2857
2549
|
|
|
2858
2550
|
// src/tokens.ts
|
|
2859
2551
|
var Token = /* @__PURE__ */ ((Token2) => {
|
|
@@ -3057,99 +2749,430 @@ var KNOWN_TOKENS = {
|
|
|
3057
2749
|
42161: "0xfc5A1A6EB076a2C7aD06eD22C90d7E710E35ad0a"
|
|
3058
2750
|
}
|
|
3059
2751
|
}
|
|
3060
|
-
};
|
|
3061
|
-
var DEFAULT_APPROVED_TOKENS = [
|
|
3062
|
-
"USDC",
|
|
3063
|
-
"USDT",
|
|
3064
|
-
"DAI",
|
|
3065
|
-
"WETH",
|
|
3066
|
-
"WBTC",
|
|
3067
|
-
"cbBTC",
|
|
3068
|
-
"wstETH",
|
|
3069
|
-
"weETH",
|
|
3070
|
-
"cbETH",
|
|
3071
|
-
"rETH"
|
|
3072
|
-
];
|
|
3073
|
-
function getDefaultApprovedTokens(chainId) {
|
|
3074
|
-
const addresses = [];
|
|
3075
|
-
for (const symbol of DEFAULT_APPROVED_TOKENS) {
|
|
3076
|
-
const entry = KNOWN_TOKENS[symbol];
|
|
3077
|
-
const addr = entry.addresses[chainId];
|
|
3078
|
-
if (addr) addresses.push(addr);
|
|
2752
|
+
};
|
|
2753
|
+
var DEFAULT_APPROVED_TOKENS = [
|
|
2754
|
+
"USDC",
|
|
2755
|
+
"USDT",
|
|
2756
|
+
"DAI",
|
|
2757
|
+
"WETH",
|
|
2758
|
+
"WBTC",
|
|
2759
|
+
"cbBTC",
|
|
2760
|
+
"wstETH",
|
|
2761
|
+
"weETH",
|
|
2762
|
+
"cbETH",
|
|
2763
|
+
"rETH"
|
|
2764
|
+
];
|
|
2765
|
+
function getDefaultApprovedTokens(chainId) {
|
|
2766
|
+
const addresses = [];
|
|
2767
|
+
for (const symbol of DEFAULT_APPROVED_TOKENS) {
|
|
2768
|
+
const entry = KNOWN_TOKENS[symbol];
|
|
2769
|
+
const addr = entry.addresses[chainId];
|
|
2770
|
+
if (addr) addresses.push(addr);
|
|
2771
|
+
}
|
|
2772
|
+
return addresses;
|
|
2773
|
+
}
|
|
2774
|
+
var addressToSymbol = /* @__PURE__ */ new Map();
|
|
2775
|
+
for (const token of Object.values(KNOWN_TOKENS)) {
|
|
2776
|
+
for (const addr of Object.values(token.addresses)) {
|
|
2777
|
+
addressToSymbol.set(addr.toLowerCase(), token.symbol);
|
|
2778
|
+
}
|
|
2779
|
+
}
|
|
2780
|
+
function getKnownTokensForChain(chainId) {
|
|
2781
|
+
const result = [];
|
|
2782
|
+
for (const token of Object.values(KNOWN_TOKENS)) {
|
|
2783
|
+
const addr = token.addresses[chainId];
|
|
2784
|
+
if (addr) {
|
|
2785
|
+
result.push({ ...token, address: addr });
|
|
2786
|
+
}
|
|
2787
|
+
}
|
|
2788
|
+
return result;
|
|
2789
|
+
}
|
|
2790
|
+
function getTokenSymbolByAddress(address) {
|
|
2791
|
+
return addressToSymbol.get(address.toLowerCase()) ?? null;
|
|
2792
|
+
}
|
|
2793
|
+
function resolveToken(token, chainId) {
|
|
2794
|
+
if (typeof token === "string" && token.startsWith("0x")) {
|
|
2795
|
+
if (token === "0x0000000000000000000000000000000000000000") {
|
|
2796
|
+
throw new Error("Token address cannot be the zero address");
|
|
2797
|
+
}
|
|
2798
|
+
return token;
|
|
2799
|
+
}
|
|
2800
|
+
const entry = KNOWN_TOKENS[token];
|
|
2801
|
+
if (!entry) {
|
|
2802
|
+
throw new Error(`Unknown token symbol: ${token}`);
|
|
2803
|
+
}
|
|
2804
|
+
const addr = entry.addresses[chainId];
|
|
2805
|
+
if (!addr) {
|
|
2806
|
+
throw new Error(`Token ${token} is not available on chain ${chainId}`);
|
|
2807
|
+
}
|
|
2808
|
+
return addr;
|
|
2809
|
+
}
|
|
2810
|
+
|
|
2811
|
+
// src/amounts.ts
|
|
2812
|
+
function resolveTokenDecimals(token, chainId) {
|
|
2813
|
+
if (typeof token === "string" && token.startsWith("0x")) {
|
|
2814
|
+
const symbol = getTokenSymbolByAddress(token);
|
|
2815
|
+
if (!symbol) {
|
|
2816
|
+
throw new Error(
|
|
2817
|
+
`Unknown token address ${token} \u2014 cannot determine decimals. Use a bigint amount instead, or pass a known token symbol.`
|
|
2818
|
+
);
|
|
2819
|
+
}
|
|
2820
|
+
const entry2 = KNOWN_TOKENS[symbol];
|
|
2821
|
+
return entry2.decimals;
|
|
2822
|
+
}
|
|
2823
|
+
const entry = KNOWN_TOKENS[token];
|
|
2824
|
+
if (!entry) {
|
|
2825
|
+
throw new Error(
|
|
2826
|
+
`Unknown token symbol "${token}" \u2014 cannot determine decimals. Use a bigint amount instead, or use a known symbol (${Object.keys(KNOWN_TOKENS).join(", ")}).`
|
|
2827
|
+
);
|
|
2828
|
+
}
|
|
2829
|
+
return entry.decimals;
|
|
2830
|
+
}
|
|
2831
|
+
function parseAmount(amount, token, chainId) {
|
|
2832
|
+
if (typeof amount === "bigint") {
|
|
2833
|
+
return amount;
|
|
2834
|
+
}
|
|
2835
|
+
const decimals = resolveTokenDecimals(token);
|
|
2836
|
+
const str = typeof amount === "number" ? amount.toString() : amount;
|
|
2837
|
+
const dotIndex = str.indexOf(".");
|
|
2838
|
+
if (dotIndex !== -1) {
|
|
2839
|
+
const decimalPlaces = str.length - dotIndex - 1;
|
|
2840
|
+
if (decimalPlaces > decimals) {
|
|
2841
|
+
throw new Error(
|
|
2842
|
+
`Amount "${str}" has ${decimalPlaces} decimal places, but ${typeof token === "string" && token.startsWith("0x") ? "this token" : token} only supports ${decimals}. Truncate or round your amount.`
|
|
2843
|
+
);
|
|
2844
|
+
}
|
|
2845
|
+
}
|
|
2846
|
+
return viem.parseUnits(str, decimals);
|
|
2847
|
+
}
|
|
2848
|
+
|
|
2849
|
+
// src/vault.ts
|
|
2850
|
+
function getChain(chainId) {
|
|
2851
|
+
switch (chainId) {
|
|
2852
|
+
case 8453:
|
|
2853
|
+
return chains.base;
|
|
2854
|
+
case 84532:
|
|
2855
|
+
return chains.baseSepolia;
|
|
2856
|
+
case 42161:
|
|
2857
|
+
return chains.arbitrum;
|
|
2858
|
+
case 421614:
|
|
2859
|
+
return chains.arbitrumSepolia;
|
|
2860
|
+
default:
|
|
2861
|
+
throw new Error(
|
|
2862
|
+
`Unsupported chainId: ${chainId}. Supported: 8453 (Base), 84532 (Base Sepolia), 42161 (Arbitrum), 421614 (Arbitrum Sepolia)`
|
|
2863
|
+
);
|
|
2864
|
+
}
|
|
2865
|
+
}
|
|
2866
|
+
function createAxonPublicClient(chainId, rpcUrl) {
|
|
2867
|
+
return viem.createPublicClient({
|
|
2868
|
+
chain: getChain(chainId),
|
|
2869
|
+
transport: viem.http(rpcUrl)
|
|
2870
|
+
});
|
|
2871
|
+
}
|
|
2872
|
+
function createAxonWalletClient(privateKey, chainId) {
|
|
2873
|
+
const account = accounts.privateKeyToAccount(privateKey);
|
|
2874
|
+
return viem.createWalletClient({
|
|
2875
|
+
account,
|
|
2876
|
+
chain: getChain(chainId),
|
|
2877
|
+
transport: viem.http()
|
|
2878
|
+
// signing is local — transport is unused but required by viem
|
|
2879
|
+
});
|
|
2880
|
+
}
|
|
2881
|
+
var USDC_DECIMALS = 6n;
|
|
2882
|
+
var USDC_UNIT = 10n ** USDC_DECIMALS;
|
|
2883
|
+
function toBotConfigParams(input) {
|
|
2884
|
+
return {
|
|
2885
|
+
maxPerTxAmount: BigInt(Math.round(input.maxPerTxAmount * Number(USDC_UNIT))),
|
|
2886
|
+
maxRebalanceAmount: BigInt(Math.round(input.maxRebalanceAmount * Number(USDC_UNIT))),
|
|
2887
|
+
spendingLimits: input.spendingLimits.map((sl) => ({
|
|
2888
|
+
amount: BigInt(Math.round(sl.amount * Number(USDC_UNIT))),
|
|
2889
|
+
maxCount: BigInt(sl.maxCount),
|
|
2890
|
+
windowSeconds: BigInt(sl.windowSeconds)
|
|
2891
|
+
})),
|
|
2892
|
+
aiTriggerThreshold: BigInt(Math.round(input.aiTriggerThreshold * Number(USDC_UNIT))),
|
|
2893
|
+
requireAiVerification: input.requireAiVerification
|
|
2894
|
+
};
|
|
2895
|
+
}
|
|
2896
|
+
var DEFAULT_RELAYER_URL = "https://relay.axonfi.xyz";
|
|
2897
|
+
async function getFactoryAddress(chainId, relayerUrl) {
|
|
2898
|
+
const base2 = relayerUrl ?? DEFAULT_RELAYER_URL;
|
|
2899
|
+
const resp = await fetch(`${base2}/v1/chains`);
|
|
2900
|
+
if (!resp.ok) throw new Error(`Failed to fetch chain config from relayer [${resp.status}]`);
|
|
2901
|
+
const data = await resp.json();
|
|
2902
|
+
const chain = data.chains?.find((c) => c.chainId === chainId);
|
|
2903
|
+
if (!chain?.factoryAddress) {
|
|
2904
|
+
throw new Error(`No factory address available for chainId ${chainId}. Check the relayer's chain configuration.`);
|
|
2905
|
+
}
|
|
2906
|
+
return chain.factoryAddress;
|
|
2907
|
+
}
|
|
2908
|
+
async function getBotConfig(publicClient, vaultAddress, botAddress) {
|
|
2909
|
+
const result = await publicClient.readContract({
|
|
2910
|
+
address: vaultAddress,
|
|
2911
|
+
abi: AxonVaultAbi,
|
|
2912
|
+
functionName: "getBotConfig",
|
|
2913
|
+
args: [botAddress]
|
|
2914
|
+
});
|
|
2915
|
+
return {
|
|
2916
|
+
isActive: result.isActive,
|
|
2917
|
+
registeredAt: result.registeredAt,
|
|
2918
|
+
maxPerTxAmount: result.maxPerTxAmount,
|
|
2919
|
+
maxRebalanceAmount: result.maxRebalanceAmount,
|
|
2920
|
+
spendingLimits: result.spendingLimits.map((sl) => ({
|
|
2921
|
+
amount: sl.amount,
|
|
2922
|
+
maxCount: sl.maxCount,
|
|
2923
|
+
windowSeconds: sl.windowSeconds
|
|
2924
|
+
})),
|
|
2925
|
+
aiTriggerThreshold: result.aiTriggerThreshold,
|
|
2926
|
+
requireAiVerification: result.requireAiVerification
|
|
2927
|
+
};
|
|
2928
|
+
}
|
|
2929
|
+
async function isBotActive(publicClient, vaultAddress, botAddress) {
|
|
2930
|
+
return publicClient.readContract({
|
|
2931
|
+
address: vaultAddress,
|
|
2932
|
+
abi: AxonVaultAbi,
|
|
2933
|
+
functionName: "isBotActive",
|
|
2934
|
+
args: [botAddress]
|
|
2935
|
+
});
|
|
2936
|
+
}
|
|
2937
|
+
async function getOperatorCeilings(publicClient, vaultAddress) {
|
|
2938
|
+
const result = await publicClient.readContract({
|
|
2939
|
+
address: vaultAddress,
|
|
2940
|
+
abi: AxonVaultAbi,
|
|
2941
|
+
functionName: "operatorCeilings"
|
|
2942
|
+
});
|
|
2943
|
+
const [maxPerTxAmount, maxBotDailyLimit, maxOperatorBots, vaultDailyAggregate, minAiTriggerFloor] = result;
|
|
2944
|
+
return {
|
|
2945
|
+
maxPerTxAmount,
|
|
2946
|
+
maxBotDailyLimit,
|
|
2947
|
+
maxOperatorBots,
|
|
2948
|
+
vaultDailyAggregate,
|
|
2949
|
+
minAiTriggerFloor
|
|
2950
|
+
};
|
|
2951
|
+
}
|
|
2952
|
+
async function operatorMaxDrainPerDay(publicClient, vaultAddress) {
|
|
2953
|
+
return publicClient.readContract({
|
|
2954
|
+
address: vaultAddress,
|
|
2955
|
+
abi: AxonVaultAbi,
|
|
2956
|
+
functionName: "operatorMaxDrainPerDay"
|
|
2957
|
+
});
|
|
2958
|
+
}
|
|
2959
|
+
async function isVaultPaused(publicClient, vaultAddress) {
|
|
2960
|
+
return publicClient.readContract({
|
|
2961
|
+
address: vaultAddress,
|
|
2962
|
+
abi: AxonVaultAbi,
|
|
2963
|
+
functionName: "paused"
|
|
2964
|
+
});
|
|
2965
|
+
}
|
|
2966
|
+
async function getDomainSeparator(publicClient, vaultAddress) {
|
|
2967
|
+
return publicClient.readContract({
|
|
2968
|
+
address: vaultAddress,
|
|
2969
|
+
abi: AxonVaultAbi,
|
|
2970
|
+
functionName: "DOMAIN_SEPARATOR"
|
|
2971
|
+
});
|
|
2972
|
+
}
|
|
2973
|
+
async function getVaultVersion(publicClient, vaultAddress) {
|
|
2974
|
+
const version = await publicClient.readContract({
|
|
2975
|
+
address: vaultAddress,
|
|
2976
|
+
abi: AxonVaultAbi,
|
|
2977
|
+
functionName: "VERSION"
|
|
2978
|
+
});
|
|
2979
|
+
return Number(version);
|
|
2980
|
+
}
|
|
2981
|
+
async function getVaultOwner(publicClient, vaultAddress) {
|
|
2982
|
+
return publicClient.readContract({
|
|
2983
|
+
address: vaultAddress,
|
|
2984
|
+
abi: AxonVaultAbi,
|
|
2985
|
+
functionName: "owner"
|
|
2986
|
+
});
|
|
2987
|
+
}
|
|
2988
|
+
async function getVaultOperator(publicClient, vaultAddress) {
|
|
2989
|
+
return publicClient.readContract({
|
|
2990
|
+
address: vaultAddress,
|
|
2991
|
+
abi: AxonVaultAbi,
|
|
2992
|
+
functionName: "operator"
|
|
2993
|
+
});
|
|
2994
|
+
}
|
|
2995
|
+
async function isDestinationAllowed(publicClient, vaultAddress, botAddress, destination) {
|
|
2996
|
+
const isBlacklisted = await publicClient.readContract({
|
|
2997
|
+
address: vaultAddress,
|
|
2998
|
+
abi: AxonVaultAbi,
|
|
2999
|
+
functionName: "globalDestinationBlacklist",
|
|
3000
|
+
args: [destination]
|
|
3001
|
+
});
|
|
3002
|
+
if (isBlacklisted) {
|
|
3003
|
+
return { allowed: false, reason: "Destination is on the global blacklist" };
|
|
3004
|
+
}
|
|
3005
|
+
const globalCount = await publicClient.readContract({
|
|
3006
|
+
address: vaultAddress,
|
|
3007
|
+
abi: AxonVaultAbi,
|
|
3008
|
+
functionName: "globalDestinationCount"
|
|
3009
|
+
});
|
|
3010
|
+
if (globalCount > 0n) {
|
|
3011
|
+
const isGlobalWhitelisted = await publicClient.readContract({
|
|
3012
|
+
address: vaultAddress,
|
|
3013
|
+
abi: AxonVaultAbi,
|
|
3014
|
+
functionName: "globalDestinationWhitelist",
|
|
3015
|
+
args: [destination]
|
|
3016
|
+
});
|
|
3017
|
+
if (!isGlobalWhitelisted) {
|
|
3018
|
+
return { allowed: false, reason: "Destination is not on the global whitelist" };
|
|
3019
|
+
}
|
|
3020
|
+
}
|
|
3021
|
+
const botCount = await publicClient.readContract({
|
|
3022
|
+
address: vaultAddress,
|
|
3023
|
+
abi: AxonVaultAbi,
|
|
3024
|
+
functionName: "botDestinationCount",
|
|
3025
|
+
args: [botAddress]
|
|
3026
|
+
});
|
|
3027
|
+
if (botCount > 0n) {
|
|
3028
|
+
const isBotWhitelisted = await publicClient.readContract({
|
|
3029
|
+
address: vaultAddress,
|
|
3030
|
+
abi: AxonVaultAbi,
|
|
3031
|
+
functionName: "botDestinationWhitelist",
|
|
3032
|
+
args: [botAddress, destination]
|
|
3033
|
+
});
|
|
3034
|
+
if (!isBotWhitelisted) {
|
|
3035
|
+
return { allowed: false, reason: "Destination is not on the bot whitelist" };
|
|
3036
|
+
}
|
|
3079
3037
|
}
|
|
3080
|
-
return
|
|
3038
|
+
return { allowed: true };
|
|
3081
3039
|
}
|
|
3082
|
-
|
|
3083
|
-
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
|
|
3040
|
+
async function getRebalanceTokenCount(publicClient, vaultAddress) {
|
|
3041
|
+
const count = await publicClient.readContract({
|
|
3042
|
+
address: vaultAddress,
|
|
3043
|
+
abi: AxonVaultAbi,
|
|
3044
|
+
functionName: "rebalanceTokenCount"
|
|
3045
|
+
});
|
|
3046
|
+
return Number(count);
|
|
3087
3047
|
}
|
|
3088
|
-
function
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3048
|
+
async function isRebalanceTokenWhitelisted(publicClient, vaultAddress, token) {
|
|
3049
|
+
return publicClient.readContract({
|
|
3050
|
+
address: vaultAddress,
|
|
3051
|
+
abi: AxonVaultAbi,
|
|
3052
|
+
functionName: "rebalanceTokenWhitelist",
|
|
3053
|
+
args: [token]
|
|
3054
|
+
});
|
|
3055
|
+
}
|
|
3056
|
+
async function deployVault(walletClient, publicClient, relayerUrl) {
|
|
3057
|
+
if (!walletClient.account) {
|
|
3058
|
+
throw new Error("walletClient has no account attached");
|
|
3059
|
+
}
|
|
3060
|
+
const chainId = walletClient.chain?.id;
|
|
3061
|
+
if (!chainId) throw new Error("walletClient has no chain configured");
|
|
3062
|
+
const factoryAddress = await getFactoryAddress(chainId, relayerUrl);
|
|
3063
|
+
const hash = await walletClient.writeContract({
|
|
3064
|
+
address: factoryAddress,
|
|
3065
|
+
abi: AxonVaultFactoryAbi,
|
|
3066
|
+
functionName: "deployVault",
|
|
3067
|
+
args: [],
|
|
3068
|
+
account: walletClient.account,
|
|
3069
|
+
chain: walletClient.chain ?? null
|
|
3070
|
+
});
|
|
3071
|
+
const receipt = await publicClient.waitForTransactionReceipt({ hash });
|
|
3072
|
+
for (const log of receipt.logs) {
|
|
3073
|
+
try {
|
|
3074
|
+
if (log.topics.length >= 3 && log.topics[2]) {
|
|
3075
|
+
const vaultAddress = `0x${log.topics[2].slice(26)}`;
|
|
3076
|
+
return vaultAddress;
|
|
3077
|
+
}
|
|
3078
|
+
} catch {
|
|
3094
3079
|
}
|
|
3095
3080
|
}
|
|
3096
|
-
|
|
3097
|
-
}
|
|
3098
|
-
function getTokenSymbolByAddress(address) {
|
|
3099
|
-
return addressToSymbol.get(address.toLowerCase()) ?? null;
|
|
3081
|
+
throw new Error("VaultDeployed event not found in transaction receipt");
|
|
3100
3082
|
}
|
|
3101
|
-
function
|
|
3102
|
-
if (
|
|
3103
|
-
|
|
3104
|
-
throw new Error("Token address cannot be the zero address");
|
|
3105
|
-
}
|
|
3106
|
-
return token;
|
|
3083
|
+
async function addBot(walletClient, publicClient, vaultAddress, botAddress, config) {
|
|
3084
|
+
if (!walletClient.account) {
|
|
3085
|
+
throw new Error("walletClient has no account attached");
|
|
3107
3086
|
}
|
|
3108
|
-
const
|
|
3109
|
-
|
|
3110
|
-
|
|
3087
|
+
const params = toBotConfigParams(config);
|
|
3088
|
+
const hash = await walletClient.writeContract({
|
|
3089
|
+
address: vaultAddress,
|
|
3090
|
+
abi: AxonVaultAbi,
|
|
3091
|
+
functionName: "addBot",
|
|
3092
|
+
args: [botAddress, params],
|
|
3093
|
+
account: walletClient.account,
|
|
3094
|
+
chain: walletClient.chain ?? null
|
|
3095
|
+
});
|
|
3096
|
+
await publicClient.waitForTransactionReceipt({ hash });
|
|
3097
|
+
return hash;
|
|
3098
|
+
}
|
|
3099
|
+
async function updateBotConfig(walletClient, publicClient, vaultAddress, botAddress, config) {
|
|
3100
|
+
if (!walletClient.account) {
|
|
3101
|
+
throw new Error("walletClient has no account attached");
|
|
3111
3102
|
}
|
|
3112
|
-
const
|
|
3113
|
-
|
|
3114
|
-
|
|
3103
|
+
const params = toBotConfigParams(config);
|
|
3104
|
+
const hash = await walletClient.writeContract({
|
|
3105
|
+
address: vaultAddress,
|
|
3106
|
+
abi: AxonVaultAbi,
|
|
3107
|
+
functionName: "updateBotConfig",
|
|
3108
|
+
args: [botAddress, params],
|
|
3109
|
+
account: walletClient.account,
|
|
3110
|
+
chain: walletClient.chain ?? null
|
|
3111
|
+
});
|
|
3112
|
+
await publicClient.waitForTransactionReceipt({ hash });
|
|
3113
|
+
return hash;
|
|
3114
|
+
}
|
|
3115
|
+
async function removeBot(walletClient, publicClient, vaultAddress, botAddress) {
|
|
3116
|
+
if (!walletClient.account) {
|
|
3117
|
+
throw new Error("walletClient has no account attached");
|
|
3115
3118
|
}
|
|
3116
|
-
|
|
3119
|
+
const hash = await walletClient.writeContract({
|
|
3120
|
+
address: vaultAddress,
|
|
3121
|
+
abi: AxonVaultAbi,
|
|
3122
|
+
functionName: "removeBot",
|
|
3123
|
+
args: [botAddress],
|
|
3124
|
+
account: walletClient.account,
|
|
3125
|
+
chain: walletClient.chain ?? null
|
|
3126
|
+
});
|
|
3127
|
+
await publicClient.waitForTransactionReceipt({ hash });
|
|
3128
|
+
return hash;
|
|
3117
3129
|
}
|
|
3118
|
-
function
|
|
3119
|
-
if (
|
|
3120
|
-
|
|
3121
|
-
if (!symbol) {
|
|
3122
|
-
throw new Error(
|
|
3123
|
-
`Unknown token address ${token} \u2014 cannot determine decimals. Use a bigint amount instead, or pass a known token symbol.`
|
|
3124
|
-
);
|
|
3125
|
-
}
|
|
3126
|
-
const entry2 = KNOWN_TOKENS[symbol];
|
|
3127
|
-
return entry2.decimals;
|
|
3130
|
+
async function deposit(walletClient, publicClient, vaultAddress, token, amount, ref = "0x0000000000000000000000000000000000000000000000000000000000000000") {
|
|
3131
|
+
if (!walletClient.account) {
|
|
3132
|
+
throw new Error("walletClient has no account attached");
|
|
3128
3133
|
}
|
|
3129
|
-
const
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
+
const isEthSymbol = token === "ETH" || token === "eth";
|
|
3135
|
+
let tokenAddress;
|
|
3136
|
+
if (isEthSymbol) {
|
|
3137
|
+
tokenAddress = NATIVE_ETH;
|
|
3138
|
+
} else if (token.startsWith("0x")) {
|
|
3139
|
+
tokenAddress = token;
|
|
3140
|
+
} else {
|
|
3141
|
+
const chainId = walletClient.chain?.id;
|
|
3142
|
+
if (!chainId) throw new Error("walletClient has no chain \u2014 cannot resolve token symbol");
|
|
3143
|
+
tokenAddress = resolveToken(token, chainId);
|
|
3134
3144
|
}
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
function parseAmount(amount, token, chainId) {
|
|
3145
|
+
const isEth = tokenAddress.toLowerCase() === NATIVE_ETH.toLowerCase();
|
|
3146
|
+
let resolvedAmount;
|
|
3138
3147
|
if (typeof amount === "bigint") {
|
|
3139
|
-
|
|
3148
|
+
resolvedAmount = amount;
|
|
3149
|
+
} else if (isEth) {
|
|
3150
|
+
resolvedAmount = parseAmount(amount, "WETH");
|
|
3151
|
+
} else {
|
|
3152
|
+
resolvedAmount = parseAmount(amount, token);
|
|
3140
3153
|
}
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
}
|
|
3154
|
+
if (!isEth) {
|
|
3155
|
+
const approveTx = await walletClient.writeContract({
|
|
3156
|
+
address: tokenAddress,
|
|
3157
|
+
abi: viem.erc20Abi,
|
|
3158
|
+
functionName: "approve",
|
|
3159
|
+
args: [vaultAddress, resolvedAmount],
|
|
3160
|
+
account: walletClient.account,
|
|
3161
|
+
chain: walletClient.chain ?? null
|
|
3162
|
+
});
|
|
3163
|
+
await publicClient.waitForTransactionReceipt({ hash: approveTx });
|
|
3151
3164
|
}
|
|
3152
|
-
|
|
3165
|
+
const hash = await walletClient.writeContract({
|
|
3166
|
+
address: vaultAddress,
|
|
3167
|
+
abi: AxonVaultAbi,
|
|
3168
|
+
functionName: "deposit",
|
|
3169
|
+
args: [tokenAddress, resolvedAmount, ref],
|
|
3170
|
+
account: walletClient.account,
|
|
3171
|
+
chain: walletClient.chain ?? null,
|
|
3172
|
+
...isEth ? { value: resolvedAmount } : {}
|
|
3173
|
+
});
|
|
3174
|
+
await publicClient.waitForTransactionReceipt({ hash });
|
|
3175
|
+
return hash;
|
|
3153
3176
|
}
|
|
3154
3177
|
|
|
3155
3178
|
// src/utils.ts
|