Skip to content

Commit

Permalink
chore: remove unused functions and move single-use function (#211)
Browse files Browse the repository at this point in the history
## Description

I wanted to see how many of the helper TS functions/contracts were still
needed.

Notably, I removed `sign.test.ts` because it just tests a specific
feature of the Typescript library and has no relevance to the contract
code.

I also wanted to remove the Chi token, but this required simplifying the
bench and already on current main we see that `yarn bench:trace` doesn't
work anymore (probably because experimental tracing stopped working). I
didn't want to possible aggravate the issue with this PR so I left
everything as it was to be solved at a later point.

I didn't remove `decoding.test.ts` so that in the future we double check
that our Solidity test libraries have the same tests.

## Test Plan

CI.

## Related Issues

Closes #128.
  • Loading branch information
fedgiac authored Sep 3, 2024
1 parent 1365923 commit 5957d67
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 204 deletions.
5 changes: 0 additions & 5 deletions test/balancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,3 @@ export enum UserBalanceOpKind {
TRANSFER_INTERNAL = 2,
TRANSFER_EXTERNAL = 3,
}

export enum BalancerErrors {
SWAP_LIMIT = "BAL#507",
SWAP_DEADLINE = "BAL#508",
}
21 changes: 0 additions & 21 deletions test/bytecode.ts

This file was deleted.

27 changes: 24 additions & 3 deletions test/decoding.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import { constants, utils, Wallet } from "ethers";
import { constants, ethers, utils, Wallet } from "ethers";
import { waffle } from "hardhat";

import {
Expand All @@ -20,8 +20,6 @@ import {
signOrder,
} from "../src/ts";

import { fillDistinctBytes, SAMPLE_ORDER } from "./testHelpers";

type UnknownArray = unknown[] | readonly unknown[];
// [A, B, C] -> [A, B]
type RemoveLast<T extends UnknownArray> = T extends [...infer U, unknown]
Expand Down Expand Up @@ -107,6 +105,29 @@ function validFlags(): TradeFlags[] {
/* eslint-enable @typescript-eslint/no-explicit-any */
}

function fillDistinctBytes(count: number, start: number): string {
return ethers.utils.hexlify(
[...Array(count)].map((_, i) => (start + i) % 256),
);
}

function fillBytes(count: number, byte: number): string {
return ethers.utils.hexlify([...Array(count)].map(() => byte));
}

const SAMPLE_ORDER = {
sellToken: fillBytes(20, 0x01),
buyToken: fillBytes(20, 0x02),
receiver: fillBytes(20, 0x03),
sellAmount: ethers.utils.parseEther("42"),
buyAmount: ethers.utils.parseEther("13.37"),
validTo: 0xffffffff,
appData: ethers.constants.HashZero,
feeAmount: ethers.utils.parseEther("1.0"),
kind: OrderKind.SELL,
partiallyFillable: false,
};

describe("Order flags", () => {
it("encodeTradeFlags is the right inverse of decodeTradeFlags", () => {
for (const tradeFlags of validFlags()) {
Expand Down
7 changes: 6 additions & 1 deletion test/e2e/balancerSwap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ import {
domain,
grantRequiredRoles,
} from "../../src/ts";
import { UserBalanceOpKind, BalancerErrors } from "../balancer";
import { UserBalanceOpKind } from "../balancer";

import { deployTestContracts } from "./fixture";

enum BalancerErrors {
SWAP_LIMIT = "BAL#507",
SWAP_DEADLINE = "BAL#508",
}

const LOTS = ethers.utils.parseEther("10000.0");
const debug = Debug("e2e:balancerSwap");

Expand Down
21 changes: 20 additions & 1 deletion test/e2e/deployment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
implementationAddress,
proxyInterface,
} from "../../src/ts";
import { builtAndDeployedMetadataCoincide } from "../bytecode";

import { deployTestContracts } from "./fixture";

Expand All @@ -22,6 +21,26 @@ async function contractAddress<C extends ContractName>(
return deterministicDeploymentAddress(artifact, deploymentArguments);
}

async function builtAndDeployedMetadataCoincide(
contractAddress: string,
contractName: string,
): Promise<boolean> {
const contractArtifacts = await artifacts.readArtifact(contractName);

const code = await ethers.provider.send("eth_getCode", [
contractAddress,
"latest",
]);

// NOTE: The last 53 bytes in a deployed contract's bytecode contains the
// contract metadata. Compare the deployed contract's metadata with the
// compiled contract's metadata.
// <https://docs.soliditylang.org/en/v0.7.6/metadata.html>
const metadata = (bytecode: string) => bytecode.slice(-106);

return metadata(code) === metadata(contractArtifacts.deployedBytecode);
}

describe("E2E: Deployment", () => {
let owner: Wallet;
let manager: Wallet;
Expand Down
7 changes: 5 additions & 2 deletions test/e2e/wineOilMarket.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ERC20 from "@openzeppelin/contracts/build/contracts/ERC20PresetMinterPauser.json";
import { expect } from "chai";
import { BigNumber, Contract, Wallet } from "ethers";
import { BigNumber, BigNumberish, Contract, Wallet } from "ethers";
import { ethers, waffle } from "hardhat";

import {
Expand All @@ -11,10 +11,13 @@ import {
TypedDataDomain,
domain,
} from "../../src/ts";
import { ceilDiv } from "../testHelpers";

import { deployTestContracts } from "./fixture";

function ceilDiv(p: BigNumberish, q: BigNumberish): BigNumber {
return BigNumber.from(p).add(q).sub(1).div(q);
}

describe("E2E: RetrETH Red Wine and Olive Oil Market", () => {
let deployer: Wallet;
let solver: Wallet;
Expand Down
48 changes: 2 additions & 46 deletions test/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { BigNumber } from "ethers";
import { ethers } from "hardhat";

import {
Order,
OrderBalance,
OrderKind,
normalizeOrder,
FLAG_MASKS,
FlagKey,
FlagOptions,
} from "../src/ts";
import { Order, OrderBalance, OrderKind } from "../src/ts";

export type AbiOrder = [
type AbiOrder = [
string,
string,
string,
Expand All @@ -26,42 +18,6 @@ export type AbiOrder = [
string,
];

function optionsForKey<K extends FlagKey>(key: K): FlagOptions<K> {
return FLAG_MASKS[key].options;
}
export const allOrderKinds = optionsForKey("kind");
export const allPartiallyFillable = optionsForKey("partiallyFillable");
export const allSigningSchemes = optionsForKey("signingScheme");

export function allOptions<
K extends FlagKey,
AllOptions extends Record<K, FlagOptions<K>>,
>(): AllOptions {
const result: Record<string, FlagOptions<K>> = {};
Object.entries(FLAG_MASKS).map(
([key, value]) => (result[key] = value.options),
);
return result as AllOptions;
}

export function encodeOrder(order: Order): AbiOrder {
const o = normalizeOrder(order);
return [
o.sellToken,
o.buyToken,
o.receiver,
BigNumber.from(o.sellAmount),
BigNumber.from(o.buyAmount),
o.validTo,
o.appData,
BigNumber.from(o.feeAmount),
ethers.utils.id(o.kind),
o.partiallyFillable,
ethers.utils.id(o.sellTokenBalance),
ethers.utils.id(o.buyTokenBalance),
];
}

function decodeEnum<T>(hash: string, values: T[]): T {
for (const value of values) {
if (hash == ethers.utils.id(`${value}`)) {
Expand Down
12 changes: 0 additions & 12 deletions test/hardhatNetwork.ts

This file was deleted.

49 changes: 0 additions & 49 deletions test/sign.test.ts

This file was deleted.

18 changes: 0 additions & 18 deletions test/src/FeeClaimingERC20.sol

This file was deleted.

11 changes: 0 additions & 11 deletions test/src/TestERC20.sol

This file was deleted.

35 changes: 0 additions & 35 deletions test/testHelpers.ts

This file was deleted.

0 comments on commit 5957d67

Please sign in to comment.