Skip to content

Commit 6ba90a9

Browse files
committed
chore: remove unused functions and move single-use function to where they are used
1 parent 1365923 commit 6ba90a9

16 files changed

+93
-228
lines changed

test/balancer.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,3 @@ export enum UserBalanceOpKind {
44
TRANSFER_INTERNAL = 2,
55
TRANSFER_EXTERNAL = 3,
66
}
7-
8-
export enum BalancerErrors {
9-
SWAP_LIMIT = "BAL#507",
10-
SWAP_DEADLINE = "BAL#508",
11-
}

test/bytecode.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

test/decoding.test.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from "chai";
2-
import { constants, utils, Wallet } from "ethers";
2+
import { constants, ethers, utils, Wallet } from "ethers";
33
import { waffle } from "hardhat";
44

55
import {
@@ -20,8 +20,6 @@ import {
2020
signOrder,
2121
} from "../src/ts";
2222

23-
import { fillDistinctBytes, SAMPLE_ORDER } from "./testHelpers";
24-
2523
type UnknownArray = unknown[] | readonly unknown[];
2624
// [A, B, C] -> [A, B]
2725
type RemoveLast<T extends UnknownArray> = T extends [...infer U, unknown]
@@ -107,6 +105,29 @@ function validFlags(): TradeFlags[] {
107105
/* eslint-enable @typescript-eslint/no-explicit-any */
108106
}
109107

108+
function fillDistinctBytes(count: number, start: number): string {
109+
return ethers.utils.hexlify(
110+
[...Array(count)].map((_, i) => (start + i) % 256),
111+
);
112+
}
113+
114+
function fillBytes(count: number, byte: number): string {
115+
return ethers.utils.hexlify([...Array(count)].map(() => byte));
116+
}
117+
118+
const SAMPLE_ORDER = {
119+
sellToken: fillBytes(20, 0x01),
120+
buyToken: fillBytes(20, 0x02),
121+
receiver: fillBytes(20, 0x03),
122+
sellAmount: ethers.utils.parseEther("42"),
123+
buyAmount: ethers.utils.parseEther("13.37"),
124+
validTo: 0xffffffff,
125+
appData: ethers.constants.HashZero,
126+
feeAmount: ethers.utils.parseEther("1.0"),
127+
kind: OrderKind.SELL,
128+
partiallyFillable: false,
129+
};
130+
110131
describe("Order flags", () => {
111132
it("encodeTradeFlags is the right inverse of decodeTradeFlags", () => {
112133
for (const tradeFlags of validFlags()) {

test/e2e/balancerSwap.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ import {
1414
domain,
1515
grantRequiredRoles,
1616
} from "../../src/ts";
17-
import { UserBalanceOpKind, BalancerErrors } from "../balancer";
17+
import { UserBalanceOpKind } from "../balancer";
1818

1919
import { deployTestContracts } from "./fixture";
2020

21+
export enum BalancerErrors {
22+
SWAP_LIMIT = "BAL#507",
23+
SWAP_DEADLINE = "BAL#508",
24+
}
25+
2126
const LOTS = ethers.utils.parseEther("10000.0");
2227
const debug = Debug("e2e:balancerSwap");
2328

test/e2e/deployment.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
implementationAddress,
1111
proxyInterface,
1212
} from "../../src/ts";
13-
import { builtAndDeployedMetadataCoincide } from "../bytecode";
1413

1514
import { deployTestContracts } from "./fixture";
1615

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

24+
async function builtAndDeployedMetadataCoincide(
25+
contractAddress: string,
26+
contractName: string,
27+
): Promise<boolean> {
28+
const contractArtifacts = await artifacts.readArtifact(contractName);
29+
30+
const code = await ethers.provider.send("eth_getCode", [
31+
contractAddress,
32+
"latest",
33+
]);
34+
35+
// NOTE: The last 53 bytes in a deployed contract's bytecode contains the
36+
// contract metadata. Compare the deployed contract's metadata with the
37+
// compiled contract's metadata.
38+
// <https://docs.soliditylang.org/en/v0.7.6/metadata.html>
39+
const metadata = (bytecode: string) => bytecode.slice(-106);
40+
41+
return metadata(code) === metadata(contractArtifacts.deployedBytecode);
42+
}
43+
2544
describe("E2E: Deployment", () => {
2645
let owner: Wallet;
2746
let manager: Wallet;

test/e2e/wineOilMarket.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ERC20 from "@openzeppelin/contracts/build/contracts/ERC20PresetMinterPauser.json";
22
import { expect } from "chai";
3-
import { BigNumber, Contract, Wallet } from "ethers";
3+
import { BigNumber, BigNumberish, Contract, Wallet } from "ethers";
44
import { ethers, waffle } from "hardhat";
55

66
import {
@@ -11,10 +11,13 @@ import {
1111
TypedDataDomain,
1212
domain,
1313
} from "../../src/ts";
14-
import { ceilDiv } from "../testHelpers";
1514

1615
import { deployTestContracts } from "./fixture";
1716

17+
function ceilDiv(p: BigNumberish, q: BigNumberish): BigNumber {
18+
return BigNumber.from(p).add(q).sub(1).div(q);
19+
}
20+
1821
describe("E2E: RetrETH Red Wine and Olive Oil Market", () => {
1922
let deployer: Wallet;
2023
let solver: Wallet;

test/encoding.ts

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import { BigNumber } from "ethers";
22
import { ethers } from "hardhat";
33

4-
import {
5-
Order,
6-
OrderBalance,
7-
OrderKind,
8-
normalizeOrder,
9-
FLAG_MASKS,
10-
FlagKey,
11-
FlagOptions,
12-
} from "../src/ts";
4+
import { Order, OrderBalance, OrderKind } from "../src/ts";
135

14-
export type AbiOrder = [
6+
type AbiOrder = [
157
string,
168
string,
179
string,
@@ -26,42 +18,6 @@ export type AbiOrder = [
2618
string,
2719
];
2820

29-
function optionsForKey<K extends FlagKey>(key: K): FlagOptions<K> {
30-
return FLAG_MASKS[key].options;
31-
}
32-
export const allOrderKinds = optionsForKey("kind");
33-
export const allPartiallyFillable = optionsForKey("partiallyFillable");
34-
export const allSigningSchemes = optionsForKey("signingScheme");
35-
36-
export function allOptions<
37-
K extends FlagKey,
38-
AllOptions extends Record<K, FlagOptions<K>>,
39-
>(): AllOptions {
40-
const result: Record<string, FlagOptions<K>> = {};
41-
Object.entries(FLAG_MASKS).map(
42-
([key, value]) => (result[key] = value.options),
43-
);
44-
return result as AllOptions;
45-
}
46-
47-
export function encodeOrder(order: Order): AbiOrder {
48-
const o = normalizeOrder(order);
49-
return [
50-
o.sellToken,
51-
o.buyToken,
52-
o.receiver,
53-
BigNumber.from(o.sellAmount),
54-
BigNumber.from(o.buyAmount),
55-
o.validTo,
56-
o.appData,
57-
BigNumber.from(o.feeAmount),
58-
ethers.utils.id(o.kind),
59-
o.partiallyFillable,
60-
ethers.utils.id(o.sellTokenBalance),
61-
ethers.utils.id(o.buyTokenBalance),
62-
];
63-
}
64-
6521
function decodeEnum<T>(hash: string, values: T[]): T {
6622
for (const value of values) {
6723
if (hash == ethers.utils.id(`${value}`)) {

test/hardhatNetwork.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/libraries/Order.sol

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
pragma solidity ^0.8;
33

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

7-
library Order {
8-
using GPv2Order for GPv2Order.Data;
9-
using GPv2Order for bytes;
10-
using GPv2Trade for uint256;
6+
import {Eip712} from "./Eip712.sol";
117

8+
library Order {
129
/// Order flags
1310
struct Flags {
1411
bytes32 kind;
@@ -134,10 +131,24 @@ library Order {
134131
}
135132
}
136133

137-
/// @dev Given a GPv2Trade encoded flags, decode them into a `Flags` struct
138-
function toFlags(uint256 encodedFlags) internal pure returns (Flags memory flags) {
139-
(flags.kind, flags.partiallyFillable, flags.sellTokenBalance, flags.buyTokenBalance,) =
140-
encodedFlags.extractFlags();
134+
/// @dev Given a GPv2Trade encoded flags, decode them into a `Flags` struct.
135+
/// See GPv2Trade.extractFlags for a complete definition of each flag.
136+
function toFlags(uint256 encodedFlags) internal pure returns (Flags memory) {
137+
bytes32 sellTokenBalance;
138+
if (encodedFlags & 0x08 == 0) {
139+
sellTokenBalance = GPv2Order.BALANCE_ERC20;
140+
} else if (encodedFlags & 0x04 == 0) {
141+
sellTokenBalance = GPv2Order.BALANCE_EXTERNAL;
142+
} else {
143+
sellTokenBalance = GPv2Order.BALANCE_INTERNAL;
144+
}
145+
146+
return Flags({
147+
kind: (encodedFlags & 0x01 == 0) ? GPv2Order.KIND_SELL : GPv2Order.KIND_BUY,
148+
partiallyFillable: encodedFlags & 0x02 != 0,
149+
sellTokenBalance: sellTokenBalance,
150+
buyTokenBalance: (encodedFlags & 0x10 == 0) ? GPv2Order.BALANCE_ERC20 : GPv2Order.BALANCE_INTERNAL
151+
});
141152
}
142153

143154
/// @dev Computes the order UID for an order and the given owner
@@ -146,17 +157,16 @@ library Order {
146157
pure
147158
returns (bytes memory)
148159
{
149-
return computeOrderUid(order.hash(domainSeparator), owner, order.validTo);
160+
return computeOrderUid(hash(order, domainSeparator), owner, order.validTo);
150161
}
151162

152163
/// @dev Computes the order UID for its base components
153-
function computeOrderUid(bytes32 orderHash, address owner, uint32 validTo)
154-
internal
155-
pure
156-
returns (bytes memory orderUid)
157-
{
158-
orderUid = new bytes(GPv2Order.UID_LENGTH);
159-
orderUid.packOrderUidParams(orderHash, owner, validTo);
164+
function computeOrderUid(bytes32 orderHash, address owner, uint32 validTo) internal pure returns (bytes memory) {
165+
return abi.encodePacked(orderHash, owner, validTo);
166+
}
167+
168+
function hash(GPv2Order.Data memory order, bytes32 domainSeparator) internal pure returns (bytes32) {
169+
return Eip712.typedDataHash(Eip712.toEip712SignedStruct(order), domainSeparator);
160170
}
161171

162172
function fuzz(Fuzzed memory params) internal pure returns (GPv2Order.Data memory) {

test/libraries/Sign.sol

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ pragma solidity ^0.8;
33

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

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

88
import {Bytes} from "./Bytes.sol";
9+
import {Order} from "./Order.sol";
910

1011
type PreSignSignature is address;
1112

1213
library Sign {
13-
using GPv2Order for GPv2Order.Data;
14-
using GPv2Trade for uint256;
1514
using Bytes for bytes;
15+
using Order for GPv2Order.Data;
1616

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

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

130134
/// @dev Internal helper function for EthSign signatures (non-EIP-712)

test/libraries/Trade.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {Sign} from "./Sign.sol";
66
import {GPv2Order, GPv2Signing, GPv2Trade, IERC20} from "src/contracts/mixins/GPv2Signing.sol";
77

88
library Trade {
9-
using GPv2Trade for uint256;
109
using Order for Order.Flags;
1110
using Order for uint256;
1211
using Sign for GPv2Signing.Scheme;

test/libraries/encoders/SettlementEncoder.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {Trade} from "../Trade.sol";
1818
import {Registry, TokenRegistry} from "./TokenRegistry.sol";
1919

2020
library SettlementEncoder {
21-
using GPv2Order for GPv2Order.Data;
2221
using Trade for GPv2Order.Data;
2322
using Sign for Vm;
2423
using TokenRegistry for TokenRegistry.State;

test/sign.test.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)