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