Skip to content

Commit 8dc020d

Browse files
refactor: customRules args as a record
Changed customRules args for `filterByTokensAndAmount`, which required a modification in the function implementation and args processing. Also, modified the error messages to be more granular and detailed on what was filtered. Finally, enabled testnets as default config. Co-authored-by: Lisandro Corbalan <lisandro@bootnode.dev>
1 parent bff2f30 commit 8dc020d

File tree

3 files changed

+97
-68
lines changed

3 files changed

+97
-68
lines changed

typescript/solver/solvers/hyperlane7683/config/metadata.ts

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { AddressZero } from "@ethersproject/constants";
2+
13
import {
24
type Hyperlane7683Metadata,
35
Hyperlane7683MetadataSchema,
@@ -15,14 +17,14 @@ const metadata: Hyperlane7683Metadata = {
1517
// address: "0x9245A985d2055CeA7576B293Da8649bb6C5af9D0",
1618
// chainName: "optimism",
1719
// },
18-
{
19-
address: "0x9245A985d2055CeA7576B293Da8649bb6C5af9D0",
20-
chainName: "arbitrum",
21-
},
22-
{
23-
address: "0x9245A985d2055CeA7576B293Da8649bb6C5af9D0",
24-
chainName: "base",
25-
},
20+
// {
21+
// address: "0x9245A985d2055CeA7576B293Da8649bb6C5af9D0",
22+
// chainName: "arbitrum",
23+
// },
24+
// {
25+
// address: "0x9245A985d2055CeA7576B293Da8649bb6C5af9D0",
26+
// chainName: "base",
27+
// },
2628
// {
2729
// address: "0x9245A985d2055CeA7576B293Da8649bb6C5af9D0",
2830
// chainName: "gnosis",
@@ -45,54 +47,54 @@ const metadata: Hyperlane7683Metadata = {
4547
// },
4648

4749
// testnet
48-
// {
49-
// address: "0x6d2175B89315A9EB6c7eA71fDE54Ac0f294aDC34",
50-
// chainName: "optimismsepolia",
51-
// },
52-
// {
53-
// address: "0x6d2175B89315A9EB6c7eA71fDE54Ac0f294aDC34",
54-
// chainName: "arbitrumsepolia",
55-
// },
56-
// {
57-
// address: "0x6d2175B89315A9EB6c7eA71fDE54Ac0f294aDC34",
58-
// chainName: "sepolia",
59-
// },
60-
// {
61-
// address: "0x6d2175B89315A9EB6c7eA71fDE54Ac0f294aDC34",
62-
// chainName: "basesepolia",
63-
// },
64-
// {
65-
// address: "0x6d2175B89315A9EB6c7eA71fDE54Ac0f294aDC34",
66-
// chainName: "basesepolia",
67-
// initialBlock: 21491220,
68-
// pollInterval: 1000,
69-
// confirmationBlocks: 2,
70-
// },
50+
{
51+
address: "0x6d2175B89315A9EB6c7eA71fDE54Ac0f294aDC34",
52+
chainName: "optimismsepolia",
53+
},
54+
{
55+
address: "0x6d2175B89315A9EB6c7eA71fDE54Ac0f294aDC34",
56+
chainName: "arbitrumsepolia",
57+
},
58+
{
59+
address: "0x6d2175B89315A9EB6c7eA71fDE54Ac0f294aDC34",
60+
chainName: "sepolia",
61+
},
62+
{
63+
address: "0x6d2175B89315A9EB6c7eA71fDE54Ac0f294aDC34",
64+
chainName: "basesepolia",
65+
initialBlock: 21491220,
66+
pollInterval: 1000,
67+
confirmationBlocks: 2,
68+
},
7169
],
7270
customRules: {
7371
rules: [
7472
{
7573
name: "filterByTokenAndAmount",
76-
args: [
77-
{
78-
// "1": ["0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"],
79-
// "10": ["0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85"],
80-
"42161": ["0xaf88d065e77c8cC2239327C5EDb3A432268e5831"],
81-
"8453": ["0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"],
82-
// "100": ["0x2a22f9c3b484c3629090feed35f17ff8f88f76f0"],
83-
// "80094": ["0x549943e04f40284185054145c6E4e9568C1D3241"],
84-
// "478": ["0xFBf489bb4783D4B1B2e7D07ba39873Fb8068507D"],
85-
// "130": ["0x078D782b760474a361dDA0AF3839290b0EF57AD6"],
86-
// "11820": ["0x8d9Bd7E9ec3cd799a659EE650DfF6C799309fA91"],
74+
args: {
75+
"11155420": {
76+
"0x5f94BC7Fb4A2779fef010F96b496cD36A909E818": BigInt(50e18),
77+
[AddressZero]: BigInt(5e15),
8778
},
88-
BigInt(50e6)
89-
]
79+
"84532": {
80+
"0x5f94BC7Fb4A2779fef010F96b496cD36A909E818": BigInt(50e18),
81+
[AddressZero]: BigInt(5e15),
82+
},
83+
"421614": {
84+
"0xaf88d065e77c8cC2239327C5EDb3A432268e5831": null,
85+
[AddressZero]: BigInt(5e15),
86+
},
87+
"11155111": {
88+
"0x5f94BC7Fb4A2779fef010F96b496cD36A909E818": BigInt(5e18),
89+
[AddressZero]: BigInt(5e10),
90+
},
91+
},
9092
},
9193
{
92-
name: "intentNotFilled"
93-
}
94-
]
95-
}
94+
name: "intentNotFilled",
95+
},
96+
],
97+
},
9698
};
9799

98100
Hyperlane7683MetadataSchema.parse(metadata);
Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,78 @@
1+
import { MaxUint256 } from "@ethersproject/constants";
12
import { bytes32ToAddress } from "@hyperlane-xyz/utils";
23
import z from "zod";
34

45
import { Hyperlane7683Rule } from "../filler.js";
56

6-
const FilterByTokenAndAmountArgs = z.union([
7-
z.tuple([
8-
z.record(z.string(), z.array(z.string()).nonempty()),
9-
z
10-
.bigint()
11-
.optional()
12-
.refine((max) => !max || max > 0n, { message: "Invalid maxAmount" }),
13-
]),
14-
z.tuple([z.record(z.string(), z.array(z.string()).nonempty())]),
15-
]);
7+
const FilterByTokenAndAmountArgs = z.record(
8+
z.string(),
9+
z.record(
10+
z.string(),
11+
z.union([
12+
z.null(),
13+
z.bigint().refine((max) => max > 0n, { message: "Invalid Max Amount" }),
14+
]),
15+
),
16+
);
1617

1718
export function filterByTokenAndAmount(
1819
args: z.infer<typeof FilterByTokenAndAmountArgs>,
1920
): Hyperlane7683Rule {
2021
FilterByTokenAndAmountArgs.parse(args);
2122

22-
const [allowedTokens, maxAmount] = args;
23+
const allowedTokens: Record<string, string[]> = {};
24+
25+
for (const [chainId, tokens] of Object.entries(args)) {
26+
allowedTokens[chainId] = [];
27+
28+
for (const token of Object.keys(tokens)) {
29+
allowedTokens[chainId].push(token.toLowerCase());
30+
}
31+
}
2332

2433
return async (parsedArgs) => {
2534
const tokenIn = bytes32ToAddress(
2635
parsedArgs.resolvedOrder.minReceived[0].token,
2736
);
28-
const amountIn = parsedArgs.resolvedOrder.minReceived[0].amount;
2937
const originChainId =
3038
parsedArgs.resolvedOrder.minReceived[0].chainId.toString();
3139

3240
const tokenOut = bytes32ToAddress(
3341
parsedArgs.resolvedOrder.maxSpent[0].token,
3442
);
35-
const amountOut = parsedArgs.resolvedOrder.maxSpent[0].amount;
3643
const destChainId = parsedArgs.resolvedOrder.maxSpent[0].chainId.toString();
3744

3845
if (
3946
!allowedTokens[originChainId] ||
40-
!allowedTokens[originChainId].includes(tokenIn) ||
47+
!allowedTokens[originChainId].includes(tokenIn.toLowerCase())
48+
) {
49+
return { error: "Input token is not allowed", success: false };
50+
}
51+
52+
if (
4153
!allowedTokens[destChainId] ||
42-
!allowedTokens[destChainId].includes(tokenOut) ||
43-
(maxAmount &&
44-
(amountIn.lt(amountOut) || amountOut.gt(maxAmount.toString())))
54+
!allowedTokens[destChainId].includes(tokenOut.toLowerCase())
4555
) {
46-
return { error: "Amounts and tokens are not ok", success: false };
56+
return { error: "Output token is not allowed", success: false };
57+
}
58+
59+
let maxAmount = args[originChainId][tokenIn];
60+
61+
if (maxAmount === null) {
62+
maxAmount = BigInt(MaxUint256.toString());
63+
}
64+
65+
const amountIn = parsedArgs.resolvedOrder.minReceived[0].amount;
66+
const amountOut = parsedArgs.resolvedOrder.maxSpent[0].amount;
67+
68+
if (amountIn.lte(amountOut)) {
69+
return { error: "Intent is not profitable", success: false };
70+
}
71+
72+
if (amountOut.gt(maxAmount.toString())) {
73+
return { error: "Output amount exceeds the maximum allowed", success: false };
4774
}
4875

49-
return { data: "Amounts and tokens are ok", success: true };
76+
return { data: "Amounts and tokens are Ok", success: true };
5077
};
5178
}

typescript/solver/solvers/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const BaseMetadataSchema = z.object({
2828
rules: z.array(
2929
z.object({
3030
name: z.string(),
31-
args: z.array(z.any()).optional(),
31+
args: z.any().optional(),
3232
}),
3333
),
3434
keepBaseRules: z.boolean().optional(),

0 commit comments

Comments
 (0)