Skip to content

Commit f74640d

Browse files
committed
support native token
1 parent 8f65ef8 commit f74640d

File tree

2 files changed

+74
-32
lines changed

2 files changed

+74
-32
lines changed

typescript/solver/solvers/hyperlane7683/filler.ts

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { type MultiProvider } from "@hyperlane-xyz/sdk";
1+
import { AddressZero } from "@ethersproject/constants";
2+
import type { MultiProvider } from "@hyperlane-xyz/sdk";
23
import {
34
addressToBytes32,
45
bytes32ToAddress,
@@ -20,8 +21,8 @@ import {
2021
settleOrder,
2122
} from "./utils.js";
2223

23-
import { allowBlockLists, metadata } from "./config/index.js";
2424
import { chainIdsToName, isAllowedIntent } from "../../config/index.js";
25+
import { allowBlockLists, metadata } from "./config/index.js";
2526

2627
export const create = (multiProvider: MultiProvider) => {
2728
const { protocolName } = setup();
@@ -146,38 +147,48 @@ async function fill(
146147
});
147148

148149
await Promise.all(
149-
maxSpent.map(async ({ chainId, token, amount, recipient }) => {
150-
token = bytes32ToAddress(token);
151-
recipient = bytes32ToAddress(recipient);
152-
const _chainId = chainId.toString();
153-
154-
const filler = multiProvider.getSigner(_chainId);
155-
const tx = await Erc20__factory.connect(token, filler).approve(
156-
recipient,
157-
amount,
158-
);
150+
maxSpent.map(
151+
async ({ amount, chainId, recipient, token: tokenAddress }) => {
152+
tokenAddress = bytes32ToAddress(tokenAddress);
153+
recipient = bytes32ToAddress(recipient);
154+
const _chainId = chainId.toString();
155+
156+
const filler = multiProvider.getSigner(_chainId);
159157

160-
const receipt = await tx.wait();
161-
const baseUrl =
162-
multiProvider.getChainMetadata(_chainId).blockExplorers?.[0].url;
158+
if (tokenAddress === AddressZero) {
159+
// native token
160+
return;
161+
}
163162

164-
if (baseUrl) {
165-
log.debug(
166-
`${protocolName} - Approval Tx: ${baseUrl}/tx/${receipt.transactionHash}`,
163+
const tx = await Erc20__factory.connect(tokenAddress, filler).approve(
164+
recipient,
165+
amount,
167166
);
168-
} else {
169-
log.debug(`${protocolName} - Approval Tx: ${receipt.transactionHash}`);
170-
}
171167

172-
log.debug(
173-
`${protocolName} - Approved ${amount.toString()} of ${token} to ${recipient} on ${_chainId}`,
174-
);
175-
}),
168+
const receipt = await tx.wait();
169+
const baseUrl =
170+
multiProvider.getChainMetadata(_chainId).blockExplorers?.[0].url;
171+
172+
if (baseUrl) {
173+
log.debug(
174+
`${protocolName} - Approval Tx: ${baseUrl}/tx/${receipt.transactionHash}`,
175+
);
176+
} else {
177+
log.debug(
178+
`${protocolName} - Approval Tx: ${receipt.transactionHash}`,
179+
);
180+
}
181+
182+
log.debug(
183+
`${protocolName} - Approved ${amount.toString()} of ${tokenAddress} to ${recipient} on ${_chainId}`,
184+
);
185+
},
186+
),
176187
);
177188

178189
await Promise.all(
179190
fillInstructions.map(
180-
async ({ destinationChainId, destinationSettler, originData }) => {
191+
async ({ destinationChainId, destinationSettler, originData }, index) => {
181192
destinationSettler = bytes32ToAddress(destinationSettler);
182193
const _chainId = destinationChainId.toString();
183194

@@ -195,6 +206,7 @@ async function fill(
195206
orderId,
196207
originData,
197208
addressToBytes32(fillerAddress),
209+
{ value: maxSpent[index].amount },
198210
);
199211

200212
const receipt = await tx.wait();

typescript/solver/solvers/hyperlane7683/utils.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { Erc20__factory } from "../../typechain/factories/contracts/Erc20__facto
77

88
import type { Provider } from "@ethersproject/abstract-provider";
99
import { bytes32ToAddress } from "@hyperlane-xyz/utils";
10+
import { chainIdsToName } from "../../config/index.js";
1011
import { createLogger } from "../../logger.js";
1112
import { Hyperlane7683__factory } from "../../typechain/factories/hyperlane7683/contracts/Hyperlane7683__factory.js";
12-
import type { ResolvedCrossChainOrder } from "./types.js";
13-
import { chainIdsToName } from "../../config/index.js";
1413
import { metadata } from "./config/index.js";
14+
import type { ResolvedCrossChainOrder } from "./types.js";
1515

1616
export const log = createLogger(metadata.protocolName);
1717

@@ -138,8 +138,23 @@ export async function retrieveOriginInfo(
138138
): Promise<Array<string>> {
139139
const originInfo = await Promise.all(
140140
resolvedOrder.minReceived.map(async ({ amount, chainId, token }) => {
141+
const tokenAddress = bytes32ToAddress(token);
142+
143+
// native token
144+
if (tokenAddress === AddressZero) {
145+
const { nativeToken } = multiProvider.getChainMetadata(
146+
chainId.toString(),
147+
);
148+
149+
return {
150+
amount,
151+
decimals: nativeToken?.decimals ?? 18,
152+
symbol: nativeToken?.symbol ?? "ETH",
153+
};
154+
}
155+
141156
const erc20 = Erc20__factory.connect(
142-
bytes32ToAddress(token),
157+
tokenAddress,
143158
multiProvider.getProvider(chainId.toString()),
144159
);
145160
const [decimals, symbol] = await Promise.all([
@@ -166,20 +181,35 @@ export async function retrieveTargetInfo(
166181
): Promise<Array<string>> {
167182
const targetInfo = await Promise.all(
168183
resolvedOrder.maxSpent.map(async ({ amount, chainId, token }) => {
184+
const tokenAddress = bytes32ToAddress(token);
185+
186+
if (tokenAddress === AddressZero) {
187+
const { nativeToken } = multiProvider.getChainMetadata(
188+
chainId.toString(),
189+
);
190+
191+
return {
192+
amount,
193+
chainId,
194+
decimals: nativeToken?.decimals ?? 18,
195+
symbol: nativeToken?.symbol ?? "ETH",
196+
};
197+
}
198+
169199
const erc20 = Erc20__factory.connect(
170-
bytes32ToAddress(token),
200+
tokenAddress,
171201
multiProvider.getProvider(chainId.toString()),
172202
);
173203
const [decimals, symbol] = await Promise.all([
174204
erc20.decimals(),
175205
erc20.symbol(),
176206
]);
177207

178-
return { amount, decimals, symbol, chainId };
208+
return { amount, chainId, decimals, symbol };
179209
}),
180210
);
181211

182-
return targetInfo.map(({ amount, decimals, symbol, chainId }) => {
212+
return targetInfo.map(({ amount, chainId, decimals, symbol }) => {
183213
const destinationChain =
184214
chainIdsToName[chainId.toNumber()] ?? "UNKNOWN_CHAIN";
185215
return `${formatUnits(amount, decimals)} ${symbol} on ${destinationChain}`;

0 commit comments

Comments
 (0)