hermes-swap 0.0.13 → 0.0.15

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/dist/cjs/index.js CHANGED
@@ -35,6 +35,7 @@ module.exports = __toCommonJS(src_exports);
35
35
  var import_ethers = require("ethers");
36
36
  var import_ethers2 = require("ethers");
37
37
  var import_quoter = __toESM(require("./abis/quoter"));
38
+ var import_aggregator = __toESM(require("./abis/aggregator"));
38
39
  var Hermes = class {
39
40
  constructor(config) {
40
41
  this.providerMap = /* @__PURE__ */ new Map();
@@ -84,8 +85,69 @@ var Hermes = class {
84
85
  }
85
86
  async swap(params) {
86
87
  this.validateParams(params);
87
- const receipt = {};
88
- return Promise.resolve(receipt);
88
+ if (!params.path.length) {
89
+ throw new Error("Swap path not provided");
90
+ }
91
+ const fromTokenAddress = params.path[0].fromCoinAddress;
92
+ const toTokenAddress = params.path[params.path.length - 1].toCoinAddress;
93
+ const aggregatorAddress = this.getAggregatorAddress(params.chain);
94
+ const executorWallet = this.executorMap.get(params.chain);
95
+ if (!executorWallet) {
96
+ throw new Error(`Executor wallet not configured for chain: ${params.chain}`);
97
+ }
98
+ const aggregator = new import_ethers2.Contract(aggregatorAddress, import_aggregator.default, executorWallet);
99
+ const swapParams = params.path.map((pathItem) => ({
100
+ dexType: pathItem.dexType,
101
+ pool: pathItem.poolAddress,
102
+ fromCoin: pathItem.fromCoinAddress,
103
+ toCoin: pathItem.toCoinAddress,
104
+ extra: pathItem.extra ?? "0x"
105
+ }));
106
+ const erc20 = new import_ethers2.Contract(fromTokenAddress, ["function balanceOf(address) view returns (uint256)", "function allowance(address, address) view returns (uint256)"], executorWallet);
107
+ const userBalance = await erc20.balanceOf(params.user);
108
+ if (userBalance < params.amountInWei) {
109
+ throw new Error("Insufficient balance for swap");
110
+ }
111
+ const currentAllowance = await erc20.allowance(params.user, aggregatorAddress);
112
+ console.log(currentAllowance);
113
+ if (currentAllowance < params.amountInWei) {
114
+ throw new Error("Insufficient allowance token amount for swap");
115
+ }
116
+ try {
117
+ const gas = await aggregator.getFunction("swap").estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, { from: executorWallet.address });
118
+ console.log(`Estimated gas for swap: ${BigInt(gas).toString()}`);
119
+ } catch (error) {
120
+ console.warn("Aggregator estimateGas.swap failed", error);
121
+ throw error;
122
+ }
123
+ let txResponse;
124
+ try {
125
+ txResponse = await aggregator.getFunction("swap")(params.user, params.amountInWei, swapParams, params.minAmountOutList, { from: executorWallet.address });
126
+ } catch (error) {
127
+ console.error("Aggregator swap transaction failed", error);
128
+ throw error;
129
+ }
130
+ const receipt = await txResponse.wait();
131
+ console.log(`Swap transaction mined in block ${receipt.blockNumber} (tx: ${receipt.hash})`);
132
+ const iface = new import_ethers.ethers.Interface(import_aggregator.default);
133
+ let amountOut = params.amountInWei;
134
+ for (const log of receipt.logs) {
135
+ if (log.address.toLowerCase() === aggregatorAddress.toLowerCase()) {
136
+ try {
137
+ const parsed = iface.parseLog(log);
138
+ if (parsed && parsed.name === "Swapped" && parsed.args && parsed.args.amountOut !== void 0) {
139
+ amountOut = parsed.args.amountOut;
140
+ break;
141
+ }
142
+ } catch {
143
+ }
144
+ }
145
+ }
146
+ return {
147
+ fromToken: fromTokenAddress,
148
+ toToken: toTokenAddress,
149
+ amountOut
150
+ };
89
151
  }
90
152
  async bridge(params) {
91
153
  this.validateParams(params);
@@ -8,13 +8,13 @@ export interface IConfig {
8
8
  export interface IExpectParams {
9
9
  chain: ChainNameEnum;
10
10
  amountInWei: bigint;
11
- path: IPath[];
11
+ path: IRouterPath[];
12
12
  }
13
13
  export interface ISwapParams {
14
14
  user: string;
15
15
  chain: ChainNameEnum;
16
16
  amountInWei: bigint;
17
- path: IPath[];
17
+ path: IRouterPath[];
18
18
  minAmountOutList: bigint[];
19
19
  }
20
20
  export interface IBridgeParams {
@@ -31,7 +31,7 @@ export interface ISwapAndBridgeParams {
31
31
  chain: ChainNameEnum;
32
32
  user: string;
33
33
  amountInWei: bigint;
34
- path: IPath[];
34
+ path: IRouterPath[];
35
35
  minAmountOutList: bigint[];
36
36
  bridgeType: string;
37
37
  bridgeAddress: string;
@@ -42,7 +42,7 @@ export interface ISwapAndBridgeParams {
42
42
  export interface IReceipt {
43
43
  fromToken?: string;
44
44
  toToken?: string;
45
- amount?: bigint;
45
+ amountOut?: bigint;
46
46
  }
47
47
  export declare enum DexType {
48
48
  FX = "f(x)"
@@ -222,7 +222,7 @@ export declare const AddressConst: {
222
222
  };
223
223
  export interface IExpectPayload {
224
224
  }
225
- export interface IPath {
225
+ export interface IRouterPath {
226
226
  dexType: string;
227
227
  poolAddress: string;
228
228
  fromCoinAddress: string;