Skip to content

Commit

Permalink
fix address validation in ponder.config.ts (#1113)
Browse files Browse the repository at this point in the history
* fix address validation in ponder.config.ts

* add test
  • Loading branch information
kyscott18 committed Sep 19, 2024
1 parent 8c847ae commit 6c3b6cf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-peaches-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ponder/core": patch
---

Fixed address validation in config.
27 changes: 27 additions & 0 deletions packages/core/src/build/configAndIndexingFunctions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,33 @@ test("buildConfigAndIndexingFunctions() validates against specifying both factor
);
});

test("buildConfigAndIndexingFunctions() validates address empty string", async () => {
const config = createConfig({
networks: {
mainnet: { chainId: 1, transport: http("https://cloudflare-eth.com") },
},
contracts: {
a: {
network: "mainnet",
abi: [event0],
// @ts-expect-error
address: "",
},
},
}) as Config;

const result = await safeBuildConfigAndIndexingFunctions({
config,
rawIndexingFunctions: [{ name: "a:Event0", fn: () => {} }],
options,
});

expect(result.status).toBe("error");
expect(result.error?.message).toBe(
"Validation failed: Invalid prefix for address ''. Got '', expected '0x'.",
);
});

test("buildConfigAndIndexingFunctions() validates address prefix", async () => {
const config = createConfig({
networks: {
Expand Down
20 changes: 10 additions & 10 deletions packages/core/src/build/configAndIndexingFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,16 +571,10 @@ export async function buildConfigAndIndexingFunctions({
return [logSource];
}

const validatedAddress = Array.isArray(resolvedAddress)
? resolvedAddress.map((r) => toLowerCase(r))
: resolvedAddress
? toLowerCase(resolvedAddress)
: undefined;

if (validatedAddress !== undefined) {
for (const address of Array.isArray(validatedAddress)
? validatedAddress
: [validatedAddress]) {
if (resolvedAddress !== undefined) {
for (const address of Array.isArray(resolvedAddress)
? resolvedAddress
: [resolvedAddress]) {
if (!address.startsWith("0x"))
throw new Error(
`Validation failed: Invalid prefix for address '${address}'. Got '${address.slice(
Expand All @@ -595,6 +589,12 @@ export async function buildConfigAndIndexingFunctions({
}
}

const validatedAddress = Array.isArray(resolvedAddress)
? resolvedAddress.map((r) => toLowerCase(r))
: resolvedAddress !== undefined
? toLowerCase(resolvedAddress)
: undefined;

const logSource = {
...contractMetadata,
filter: {
Expand Down

0 comments on commit 6c3b6cf

Please sign in to comment.