@axonfi/sdk 0.5.2 → 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 CHANGED
@@ -57,7 +57,6 @@ import {
57
57
  createAxonPublicClient, createAxonWalletClient,
58
58
  WINDOW, Chain,
59
59
  } from '@axonfi/sdk';
60
- import { parseUnits } from 'viem';
61
60
  import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
62
61
 
63
62
  // ── 1. Owner wallet (funded with ETH for gas) ─────────────────────
@@ -93,12 +92,10 @@ await addBot(ownerWallet, publicClient, vaultAddress, botAddress, {
93
92
 
94
93
  // ── 6. Deposit funds (on-chain tx, ~0.0005 ETH gas) ───────────────
95
94
  // Option A: Deposit ETH (vault accepts native ETH directly)
96
- await deposit(ownerWallet, publicClient, vaultAddress,
97
- 'ETH', parseUnits('0.1', 18));
95
+ await deposit(ownerWallet, publicClient, vaultAddress, 'ETH', 0.1);
98
96
 
99
97
  // Option B: Deposit USDC (SDK handles approve + deposit)
100
- await deposit(ownerWallet, publicClient, vaultAddress,
101
- 'USDC', parseUnits('500', 6));
98
+ await deposit(ownerWallet, publicClient, vaultAddress, 'USDC', 500);
102
99
 
103
100
  // ── 7. Bot is ready — gasless from here ────────────────────────────
104
101
  // Save botKey securely. The bot never needs ETH.
@@ -130,7 +127,7 @@ Vaults accept native ETH directly — no wrapping needed. You can start a vault
130
127
  // Deploy vault + deposit ETH — no USDC needed
131
128
  const vault = await deployVault(ownerWallet, publicClient, factory);
132
129
  await addBot(ownerWallet, publicClient, vault, botAddress, config);
133
- await deposit(ownerWallet, publicClient, vault, 'ETH', parseUnits('0.5', 18));
130
+ await deposit(ownerWallet, publicClient, vault, 'ETH', 0.5);
134
131
 
135
132
  // Bot can now pay in any token — the relayer swaps ETH → USDC automatically
136
133
  await axon.pay({ to: '0x...', token: 'USDC', amount: 10 });
package/dist/index.cjs CHANGED
@@ -2808,6 +2808,44 @@ function resolveToken(token, chainId) {
2808
2808
  return addr;
2809
2809
  }
2810
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
+
2811
2849
  // src/vault.ts
2812
2850
  function getChain(chainId) {
2813
2851
  switch (chainId) {
@@ -3105,12 +3143,20 @@ async function deposit(walletClient, publicClient, vaultAddress, token, amount,
3105
3143
  tokenAddress = resolveToken(token, chainId);
3106
3144
  }
3107
3145
  const isEth = tokenAddress.toLowerCase() === NATIVE_ETH.toLowerCase();
3146
+ let resolvedAmount;
3147
+ if (typeof amount === "bigint") {
3148
+ resolvedAmount = amount;
3149
+ } else if (isEth) {
3150
+ resolvedAmount = parseAmount(amount, "WETH");
3151
+ } else {
3152
+ resolvedAmount = parseAmount(amount, token);
3153
+ }
3108
3154
  if (!isEth) {
3109
3155
  const approveTx = await walletClient.writeContract({
3110
3156
  address: tokenAddress,
3111
3157
  abi: viem.erc20Abi,
3112
3158
  functionName: "approve",
3113
- args: [vaultAddress, amount],
3159
+ args: [vaultAddress, resolvedAmount],
3114
3160
  account: walletClient.account,
3115
3161
  chain: walletClient.chain ?? null
3116
3162
  });
@@ -3120,50 +3166,14 @@ async function deposit(walletClient, publicClient, vaultAddress, token, amount,
3120
3166
  address: vaultAddress,
3121
3167
  abi: AxonVaultAbi,
3122
3168
  functionName: "deposit",
3123
- args: [tokenAddress, amount, ref],
3169
+ args: [tokenAddress, resolvedAmount, ref],
3124
3170
  account: walletClient.account,
3125
3171
  chain: walletClient.chain ?? null,
3126
- ...isEth ? { value: amount } : {}
3172
+ ...isEth ? { value: resolvedAmount } : {}
3127
3173
  });
3128
3174
  await publicClient.waitForTransactionReceipt({ hash });
3129
3175
  return hash;
3130
3176
  }
3131
- function resolveTokenDecimals(token, chainId) {
3132
- if (typeof token === "string" && token.startsWith("0x")) {
3133
- const symbol = getTokenSymbolByAddress(token);
3134
- if (!symbol) {
3135
- throw new Error(
3136
- `Unknown token address ${token} \u2014 cannot determine decimals. Use a bigint amount instead, or pass a known token symbol.`
3137
- );
3138
- }
3139
- const entry2 = KNOWN_TOKENS[symbol];
3140
- return entry2.decimals;
3141
- }
3142
- const entry = KNOWN_TOKENS[token];
3143
- if (!entry) {
3144
- throw new Error(
3145
- `Unknown token symbol "${token}" \u2014 cannot determine decimals. Use a bigint amount instead, or use a known symbol (${Object.keys(KNOWN_TOKENS).join(", ")}).`
3146
- );
3147
- }
3148
- return entry.decimals;
3149
- }
3150
- function parseAmount(amount, token, chainId) {
3151
- if (typeof amount === "bigint") {
3152
- return amount;
3153
- }
3154
- const decimals = resolveTokenDecimals(token);
3155
- const str = typeof amount === "number" ? amount.toString() : amount;
3156
- const dotIndex = str.indexOf(".");
3157
- if (dotIndex !== -1) {
3158
- const decimalPlaces = str.length - dotIndex - 1;
3159
- if (decimalPlaces > decimals) {
3160
- throw new Error(
3161
- `Amount "${str}" has ${decimalPlaces} decimal places, but ${typeof token === "string" && token.startsWith("0x") ? "this token" : token} only supports ${decimals}. Truncate or round your amount.`
3162
- );
3163
- }
3164
- }
3165
- return viem.parseUnits(str, decimals);
3166
- }
3167
3177
 
3168
3178
  // src/utils.ts
3169
3179
  function generateUuid() {