Skip to content

Commit

Permalink
chore: remove unused functions and move single-use function to where …
Browse files Browse the repository at this point in the history
…they are used
  • Loading branch information
fedgiac committed Aug 16, 2024
1 parent 1365923 commit 6ba90a9
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 228 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";

export 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.

44 changes: 27 additions & 17 deletions test/libraries/Order.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
pragma solidity ^0.8;

import {GPv2Order, IERC20} from "src/contracts/libraries/GPv2Order.sol";
import {GPv2Trade} from "src/contracts/libraries/GPv2Trade.sol";

library Order {
using GPv2Order for GPv2Order.Data;
using GPv2Order for bytes;
using GPv2Trade for uint256;
import {Eip712} from "./Eip712.sol";

library Order {
/// Order flags
struct Flags {
bytes32 kind;
Expand Down Expand Up @@ -134,10 +131,24 @@ library Order {
}
}

/// @dev Given a GPv2Trade encoded flags, decode them into a `Flags` struct
function toFlags(uint256 encodedFlags) internal pure returns (Flags memory flags) {
(flags.kind, flags.partiallyFillable, flags.sellTokenBalance, flags.buyTokenBalance,) =
encodedFlags.extractFlags();
/// @dev Given a GPv2Trade encoded flags, decode them into a `Flags` struct.
/// See GPv2Trade.extractFlags for a complete definition of each flag.
function toFlags(uint256 encodedFlags) internal pure returns (Flags memory) {
bytes32 sellTokenBalance;
if (encodedFlags & 0x08 == 0) {
sellTokenBalance = GPv2Order.BALANCE_ERC20;
} else if (encodedFlags & 0x04 == 0) {
sellTokenBalance = GPv2Order.BALANCE_EXTERNAL;
} else {
sellTokenBalance = GPv2Order.BALANCE_INTERNAL;
}

return Flags({
kind: (encodedFlags & 0x01 == 0) ? GPv2Order.KIND_SELL : GPv2Order.KIND_BUY,
partiallyFillable: encodedFlags & 0x02 != 0,
sellTokenBalance: sellTokenBalance,
buyTokenBalance: (encodedFlags & 0x10 == 0) ? GPv2Order.BALANCE_ERC20 : GPv2Order.BALANCE_INTERNAL
});
}

/// @dev Computes the order UID for an order and the given owner
Expand All @@ -146,17 +157,16 @@ library Order {
pure
returns (bytes memory)
{
return computeOrderUid(order.hash(domainSeparator), owner, order.validTo);
return computeOrderUid(hash(order, domainSeparator), owner, order.validTo);
}

/// @dev Computes the order UID for its base components
function computeOrderUid(bytes32 orderHash, address owner, uint32 validTo)
internal
pure
returns (bytes memory orderUid)
{
orderUid = new bytes(GPv2Order.UID_LENGTH);
orderUid.packOrderUidParams(orderHash, owner, validTo);
function computeOrderUid(bytes32 orderHash, address owner, uint32 validTo) internal pure returns (bytes memory) {
return abi.encodePacked(orderHash, owner, validTo);
}

function hash(GPv2Order.Data memory order, bytes32 domainSeparator) internal pure returns (bytes32) {
return Eip712.typedDataHash(Eip712.toEip712SignedStruct(order), domainSeparator);
}

function fuzz(Fuzzed memory params) internal pure returns (GPv2Order.Data memory) {
Expand Down
14 changes: 9 additions & 5 deletions test/libraries/Sign.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ pragma solidity ^0.8;

import {Vm} from "forge-std/Test.sol";

import {EIP1271Verifier, GPv2Order, GPv2Signing, GPv2Trade} from "src/contracts/mixins/GPv2Signing.sol";
import {EIP1271Verifier, GPv2Order, GPv2Signing} from "src/contracts/mixins/GPv2Signing.sol";

import {Bytes} from "./Bytes.sol";
import {Order} from "./Order.sol";

type PreSignSignature is address;

library Sign {
using GPv2Order for GPv2Order.Data;
using GPv2Trade for uint256;
using Bytes for bytes;
using Order for GPv2Order.Data;

// Copied from GPv2Signing.sol
uint256 internal constant PRE_SIGNED = uint256(keccak256("GPv2Signing.Scheme.PreSign"));
Expand Down Expand Up @@ -123,8 +123,12 @@ library Sign {
}

/// @dev Given a GPv2Trade encoded flags, decode them into a `GPv2Signing.Scheme`
function toSigningScheme(uint256 encodedFlags) internal pure returns (GPv2Signing.Scheme signingScheme) {
(,,,, signingScheme) = encodedFlags.extractFlags();
function toSigningScheme(uint256 encodedFlags) internal pure returns (GPv2Signing.Scheme) {
uint256 encodedScheme = (encodedFlags >> 5);
if (encodedScheme >= 4) {
revert("invalid flag encoding, trying to use invalid signature scheme");
}
return GPv2Signing.Scheme(encodedScheme);
}

/// @dev Internal helper function for EthSign signatures (non-EIP-712)
Expand Down
1 change: 0 additions & 1 deletion test/libraries/Trade.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {Sign} from "./Sign.sol";
import {GPv2Order, GPv2Signing, GPv2Trade, IERC20} from "src/contracts/mixins/GPv2Signing.sol";

library Trade {
using GPv2Trade for uint256;
using Order for Order.Flags;
using Order for uint256;
using Sign for GPv2Signing.Scheme;
Expand Down
1 change: 0 additions & 1 deletion test/libraries/encoders/SettlementEncoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {Trade} from "../Trade.sol";
import {Registry, TokenRegistry} from "./TokenRegistry.sol";

library SettlementEncoder {
using GPv2Order for GPv2Order.Data;
using Trade for GPv2Order.Data;
using Sign for Vm;
using TokenRegistry for TokenRegistry.State;
Expand Down
49 changes: 0 additions & 49 deletions test/sign.test.ts

This file was deleted.

Loading

0 comments on commit 6ba90a9

Please sign in to comment.