Skip to content
This repository has been archived by the owner on Dec 13, 2019. It is now read-only.

Commit

Permalink
Replace ethers usage to always import from module (#364)
Browse files Browse the repository at this point in the history
* Replace all ethers utils with module imports

* Fixed a few places I missed the first time

* Update rollup for cf.js to include ethers.constants
  • Loading branch information
snario authored Dec 26, 2018
1 parent 8314c17 commit 99c3bc1
Show file tree
Hide file tree
Showing 22 changed files with 104 additions and 121 deletions.
3 changes: 2 additions & 1 deletion packages/cf.js/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default {
format: "iife",
exports: "named",
globals: {
"ethers/utils": "ethers.utils"
"ethers/utils": "ethers.utils",
"ethers/constants": "ethers.constants"
}
}
],
Expand Down
12 changes: 5 additions & 7 deletions packages/cf.js/src/legacy/app-instance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from "ethers";
import { formatParamType, Interface, ParamType } from "ethers/utils";

import { Terms } from "./app";
import { AbiEncodings, AppDefinition } from "./types";
Expand All @@ -13,25 +13,23 @@ export class AppInstance {

// TODO: temp hack necessary until ethers support https://github.com/ethers-io/ethers.js/issues/325
static generateAbiEncodings(
abi: string | (string | ethers.utils.ParamType)[]
abi: string | (string | ParamType)[]
): AbiEncodings {
const iface = new ethers.utils.Interface(abi);
const iface = new Interface(abi);
const appFunctionNames = Object.keys(iface.functions).filter(fn => {
return fn.indexOf("(") === -1;
});
const appActions = appFunctionNames.map(fn => {
const inputs = iface.functions[fn].inputs;
const tuples = inputs.map(input => {
return ethers.utils.formatParamType(input);
return formatParamType(input);
});

return `${fn}(${tuples.join(",")})`;
});

return {
appStateEncoding: ethers.utils.formatParamType(
iface.functions.resolve.inputs[0]
),
appStateEncoding: formatParamType(iface.functions.resolve.inputs[0]),
appActionEncoding: JSON.stringify([appActions.join(",")])
};
}
Expand Down
16 changes: 8 additions & 8 deletions packages/cf.js/src/legacy/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from "ethers";
import { defaultAbiCoder, keccak256 } from "ethers/utils";
import { HashZero } from "ethers/constants";
import { BigNumber, defaultAbiCoder, keccak256 } from "ethers/utils";

import * as abi from "../../utils/abi";
import { StateChannelInfo } from "../channel";
Expand Down Expand Up @@ -67,7 +67,7 @@ export class AppInterface {
console.error(
"WARNING: Can't compute hash for AppInterface because its address is 0x0"
);
return ethers.constants.HashZero;
return HashZero;
}
return keccak256(
abi.encode(
Expand All @@ -91,7 +91,7 @@ export class AppInterface {
export class Terms {
constructor(
readonly assetType: number,
readonly limit: ethers.utils.BigNumber,
readonly limit: BigNumber,
readonly token: Address
) {}

Expand All @@ -118,8 +118,8 @@ export interface UpdateData {
}

export interface UninstallOptions {
peerABalance: ethers.utils.BigNumber;
peerBBalance: ethers.utils.BigNumber;
peerABalance: BigNumber;
peerBBalance: BigNumber;
}

export interface InstallData {
Expand All @@ -138,8 +138,8 @@ export interface InstallOptions {
stateEncoding: string;
abiEncoding: string;
state: object;
peerABalance: ethers.utils.BigNumber;
peerBBalance: ethers.utils.BigNumber;
peerABalance: BigNumber;
peerBBalance: BigNumber;
}

export interface AppInstanceInfo {
Expand Down
9 changes: 2 additions & 7 deletions packages/cf.js/src/legacy/eth-balance-refund-app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import BuildArtifact from "@counterfactual/contracts/build/contracts/ETHBalanceRefundApp.json";

import { ethers } from "ethers";
import { AddressZero, Zero } from "ethers/constants";

import { Terms } from "./app";
import { AppInstance } from "./app-instance";
Expand All @@ -9,11 +8,7 @@ import { AppDefinition } from "./types";
export class ETHBalanceRefundApp extends AppInstance {
constructor(appAddress: string, signingKeys: string[]) {
const timeout = 100;
const terms = new Terms(
0,
ethers.utils.bigNumberify("0"),
ethers.constants.AddressZero
);
const terms = new Terms(0, Zero, AddressZero);
const abiEncodings = AppInstance.generateAbiEncodings(BuildArtifact.abi);

const appDefinition: AppDefinition = {
Expand Down
4 changes: 2 additions & 2 deletions packages/cf.js/src/legacy/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from "ethers";
import { Signature } from "ethers/utils";

// FIXME: move operation action names away from client action names
// https://github.com/counterfactual/monorepo/issues/144
Expand Down Expand Up @@ -47,5 +47,5 @@ export interface ClientActionMessage {
toAddress: string;
fromAddress: string;
seq: number;
signature?: ethers.utils.Signature;
signature?: Signature;
}
4 changes: 2 additions & 2 deletions packages/cf.js/src/legacy/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from "ethers";
import { BigNumber } from "ethers/utils";

export interface AppDefinition {
address: string;
Expand All @@ -12,7 +12,7 @@ export interface AbiEncodings {
}

export interface Deposits {
[s: string]: ethers.utils.BigNumber;
[s: string]: BigNumber;
}

export type DeserializationCondition = {
Expand Down
15 changes: 8 additions & 7 deletions packages/cf.js/src/legacy/utils/free-balance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ethers } from "ethers";
import { AddressZero } from "ethers/constants";
import { BigNumber, Interface, parseEther } from "ethers/utils";

import { AppInterface, Terms } from "../app";

Expand All @@ -15,14 +16,14 @@ export class FreeBalance {
// https://github.com/counterfactual/monorepo/issues/118
return new Terms(
0, // 0 means ETH
ethers.utils.parseEther("0.001"), // FIXME: un-hardcode (https://github.com/counterfactual/monorepo/issues/117)
ethers.constants.AddressZero
parseEther("0.001"), // FIXME: un-hardcode (https://github.com/counterfactual/monorepo/issues/117)
AddressZero
);
}

public static contractInterface(address: string): AppInterface {
const applyAction = "0x00000000"; // not used
const resolver = new ethers.utils.Interface([
const resolver = new Interface([
// TODO: Put this somewhere eh
// https://github.com/counterfactual/monorepo/issues/134
"resolve(tuple(address,address,uint256,uint256),tuple(uint8,uint256,address))"
Expand All @@ -41,16 +42,16 @@ export class FreeBalance {

constructor(
readonly alice: Address, // first person in free balance object
readonly aliceBalance: ethers.utils.BigNumber,
readonly aliceBalance: BigNumber,
readonly bob: Address, // second person in free balance object
readonly bobBalance: ethers.utils.BigNumber,
readonly bobBalance: BigNumber,
readonly uniqueId: number,
readonly localNonce: number,
readonly timeout: number,
readonly dependencyNonce: Nonce
) {}

public balanceOfAddress(address: Address): ethers.utils.BigNumber {
public balanceOfAddress(address: Address): BigNumber {
if (address === this.alice) return this.aliceBalance;
if (address === this.bob) return this.bobBalance;
throw Error(`address ${address} not in free balance`);
Expand Down
17 changes: 7 additions & 10 deletions packages/cf.js/src/legacy/utils/peer-balance.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ethers } from "ethers";
import { BigNumber, bigNumberify } from "ethers/utils";

import { Address } from ".";
import { Address } from "./index";

export class PeerBalance {
/**
* Returns an array of peer balance objects sorted by address ascendi.
*/
public static balances(
address1: Address,
balance1: ethers.utils.BigNumber,
balance1: BigNumber,
address2: Address,
balance2: ethers.utils.BigNumber
balance2: BigNumber
): CanonicalPeerBalance {
if (address2.localeCompare(address1) < 0) {
return new CanonicalPeerBalance(
Expand Down Expand Up @@ -61,13 +61,10 @@ export class PeerBalance {
)
];
}
public balance: ethers.utils.BigNumber;
public balance: BigNumber;

constructor(
readonly address: Address,
balance: number | ethers.utils.BigNumber
) {
this.balance = ethers.utils.bigNumberify(balance.toString());
constructor(readonly address: Address, balance: number | BigNumber) {
this.balance = bigNumberify(balance.toString());
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/cf.js/src/legacy/utils/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from "ethers";
import { bigNumberify } from "ethers/utils";

import { DeserializationCase } from "../types";

Expand All @@ -24,7 +24,7 @@ const isSignature = data =>

const deserializeArray = data => data.map(value => deserialize(value));

const deserializeBigNumber = data => ethers.utils.bigNumberify(data._hex);
const deserializeBigNumber = data => bigNumberify(data._hex);

const identity = data => data;

Expand Down
10 changes: 5 additions & 5 deletions packages/cf.js/src/utils/abi.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ethers } from "ethers";
import { Arrayish, defaultAbiCoder, solidityPack } from "ethers/utils";

export function encode(types: string[], values: any[]) {
return ethers.utils.defaultAbiCoder.encode(types, values);
return defaultAbiCoder.encode(types, values);
}

export function decode(types: string[], data: ethers.utils.Arrayish) {
return ethers.utils.defaultAbiCoder.decode(types, data);
export function decode(types: string[], data: Arrayish) {
return defaultAbiCoder.decode(types, data);
}

export function encodePacked(types: string[], values: any[]) {
return ethers.utils.solidityPack(types, values);
return solidityPack(types, values);
}
22 changes: 12 additions & 10 deletions packages/cf.js/src/utils/signature.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import { ethers } from "ethers";

import { Bytes32 } from "@counterfactual/common-types";
import {
BigNumber,
joinSignature,
recoverAddress,
Signature
} from "ethers/utils";

export function signaturesToBytes(
...signatures: ethers.utils.Signature[]
): string {
export function signaturesToBytes(...signatures: Signature[]): string {
const signaturesHexString = signatures
.map(ethers.utils.joinSignature)
.map(joinSignature)
.map(s => s.substr(2))
.join("");
return `0x${signaturesHexString}`;
}

export function signaturesToSortedBytes(
digest: Bytes32,
...signatures: ethers.utils.Signature[]
...signatures: Signature[]
): string {
const sigs = signatures.slice();
sigs.sort((sigA, sigB) => {
const addrA = ethers.utils.recoverAddress(digest, signaturesToBytes(sigA));
const addrB = ethers.utils.recoverAddress(digest, signaturesToBytes(sigB));
return new ethers.utils.BigNumber(addrA).lt(addrB) ? -1 : 1;
const addrA = recoverAddress(digest, signaturesToBytes(sigA));
const addrB = recoverAddress(digest, signaturesToBytes(sigB));
return new BigNumber(addrA).lt(addrB) ? -1 : 1;
});
return signaturesToBytes(...sigs);
}
14 changes: 7 additions & 7 deletions packages/cf.js/test/app-factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AssetType, Node } from "@counterfactual/common-types";
import { ethers } from "ethers";
import { parseEther } from "ethers/utils";

import { AppFactory } from "../src/app-factory";
import { Provider } from "../src/provider";
Expand Down Expand Up @@ -37,7 +37,7 @@ describe("CF.js AppFactory", () => {
expect(request.type).toBe(Node.MethodName.PROPOSE_INSTALL);
const params = request.params as Node.ProposeInstallParams;
expect(params.initialState).toBe(testState);
expect(params.myDeposit).toEqual(ethers.utils.parseEther("0.5"));
expect(params.myDeposit).toEqual(parseEther("0.5"));
nodeProvider.simulateMessageFromNode({
type: Node.MethodName.PROPOSE_INSTALL,
requestId: request.requestId,
Expand All @@ -51,8 +51,8 @@ describe("CF.js AppFactory", () => {
asset: {
assetType: AssetType.ETH
},
peerDeposit: ethers.utils.parseEther("0.5"),
myDeposit: ethers.utils.parseEther("0.5"),
peerDeposit: parseEther("0.5"),
myDeposit: parseEther("0.5"),
timeout: "100",
initialState: testState
});
Expand All @@ -66,8 +66,8 @@ describe("CF.js AppFactory", () => {
asset: {
assetType: AssetType.ETH
},
peerDeposit: ethers.utils.parseEther("0.5"),
myDeposit: ethers.utils.parseEther("0.5"),
peerDeposit: parseEther("0.5"),
myDeposit: parseEther("0.5"),
timeout: "100",
initialState: "4000"
});
Expand All @@ -86,7 +86,7 @@ describe("CF.js AppFactory", () => {
asset: {
assetType: AssetType.ETH
},
peerDeposit: ethers.utils.parseEther("0.5"),
peerDeposit: parseEther("0.5"),
myDeposit: "$%GARBAGE$%",
timeout: "100",
initialState: "4000"
Expand Down
8 changes: 4 additions & 4 deletions packages/cf.js/test/utils/signature.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ethers } from "ethers";
import { keccak256, SigningKey, toUtf8Bytes } from "ethers/utils";

import { signaturesToBytes, signaturesToSortedBytes } from "../../src/utils";

const privateKey =
"0x0123456789012345678901234567890123456789012345678901234567890123";
const signingKey = new ethers.utils.SigningKey(privateKey);
const signingKey = new SigningKey(privateKey);

const signaturesAndDigests = ["sig1", "sig3", "sig2"].map(message => {
const bytes = ethers.utils.toUtf8Bytes(message);
const digest = ethers.utils.keccak256(bytes);
const bytes = toUtf8Bytes(message);
const digest = keccak256(bytes);
const signature = signingKey.signDigest(digest);

return {
Expand Down
Loading

0 comments on commit 99c3bc1

Please sign in to comment.