Skip to content

Commit

Permalink
Intermediary Utils (#269)
Browse files Browse the repository at this point in the history
No logical changes, just a few utility functions to make code and tests
more coherent for upcoming Full Pipeline PR.

We add one convenience method that attempts to simulate on incrementing
consecutive blocks, until a max attempts is reached. Its lightly tested
on an example case that is known to fail the first time.
  • Loading branch information
bh2smith authored May 8, 2023
1 parent 16593f0 commit 5f0793b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
13 changes: 12 additions & 1 deletion internal_transfers/actions/src/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code Reference: https://www.atdatabases.org/docs/pg-guide-typescript

import createConnectionPool, { sql } from "@databases/pg";
import createConnectionPool, { Queryable, sql } from "@databases/pg";
import tables from "@databases/pg-typed";
import ConnectionPool from "@databases/pg/lib/types/Queryable";
import DatabaseSchema from "./__generated__";
Expand All @@ -20,6 +20,17 @@ function getDB(dbURL: string): ConnectionPool {
bigIntMode: "bigint",
});
}

export async function recordExists(
db: Queryable,
txHash: string
): Promise<boolean> {
const pgHash = txHash.replace("0x", "\\x");
const query = sql`SELECT count(*) from settlements where tx_hash = ${pgHash};`;
const { count: numRecords } = (await db.query(query))[0];
return numRecords > 0;
}

async function insertSettlementEvent(
db: ConnectionPool,
eventMeta: EventMeta,
Expand Down
13 changes: 13 additions & 0 deletions internal_transfers/actions/tests/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
insertTokenImbalances,
jsonFromSettlementData,
insertPipelineResults,
recordExists,
} from "../src/database";
import * as process from "process";
import { sql } from "@databases/pg";
Expand Down Expand Up @@ -163,6 +164,18 @@ describe("All Database Tests", () => {
'duplicate key value violates unique constraint "settlement_simulations_pkey"'
);
});
test("recordExists(txHash) accurately performs its job", async () => {
const txHash =
"0x45f52ee09622eac16d0fe27b90a76749019b599c9566f10e21e8d0955a0e428e";

expect(await recordExists(db, txHash)).toEqual(false);
await insertSettlementEvent(
db,
{ txHash: txHash, blockNumber: 0 },
{ solver: "0xc9ec550bea1c64d779124b23a26292cc223327b6", logIndex: 0 }
);
expect(await recordExists(db, txHash)).toEqual(true);
});
});

describe("insertPipelineResults", () => {
Expand Down
10 changes: 10 additions & 0 deletions internal_transfers/actions/tests/e2e/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { MinimalTxData } from "../../src/accounting";
import { ethers } from "ethers";
import { getTxDataFromHash } from "../../src/utils";

export async function getTxData(txHash: string): Promise<MinimalTxData> {
const provider = ethers.getDefaultProvider(
process.env["NODE_URL"] || "NODE_URL"
);
return getTxDataFromHash(provider, txHash);
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import {
getInternalizedImbalance,
MinimalTxData,
simulateSolverSolution,
} from "../../src/accounting";
import { TenderlySimulator } from "../../src/simulate/tenderly";
import { getTxDataFromHash } from "../../src/utils";
import { ethers } from "ethers";
import { getTxData } from "./helper";

const simulator = new TenderlySimulator(
process.env["TENDERLY_USER"] || "INVALID_USER",
process.env["TENDERLY_PROJECT"] || "TENDERLY_PROJECT",
process.env["TENDERLY_ACCESS_KEY"] || "TENDERLY_ACCESS_KEY"
);

async function getTxData(txHash: string): Promise<MinimalTxData> {
const provider = ethers.getDefaultProvider(
process.env["NODE_URL"] || "NODE_URL"
);
return getTxDataFromHash(provider, txHash);
}
describe.skip("simulateSolverSolution(transaction, simulator)", () => {
test("throws when no competition found", async () => {
const txHash =
Expand Down

0 comments on commit 5f0793b

Please sign in to comment.