Skip to content

Commit

Permalink
Dependency update (AztecProtocol#283)
Browse files Browse the repository at this point in the history
* chore: dependency update

* chore: CI and command cleanup

* chore: CI and command cleanup 2

* chore: updated config + deps

* chore: updated tsconfig

* fix: imports after using nodenext

* style: formatting

* chore: pushed typechain generated files

* chore: forge fmt instead of prettier

* chore: eslint nodenext compatible config

* fix: contract compilation issue

* test: jest fix attempt

* test: fix attempt

* test: most of the client tests working

* test: disabled typechecks when using mockImplementation

* chore: updated test command

* chore: updated linter rules

* refactor: cleanup

* fix: remove unneeded part of config.yml

* fix: only build typechain for interfaces, rollup and bridges + rename ExampleBridgeContract to ExampleBridge

* fix: example bridge naming in tests and scripts

* fix: add DataProvider to typechain

* fix: add python script to esm-ish typechain

* fix: run python script to get esm-ish typechain

Co-authored-by: LHerskind <[email protected]>
Co-authored-by: LHerskind <[email protected]>
  • Loading branch information
3 people authored Nov 22, 2022
1 parent 828bd28 commit 0a3949e
Show file tree
Hide file tree
Showing 160 changed files with 49,508 additions and 907 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/ban-ts-comment": "off",
"no-constant-condition": "off",
camelcase: 1,
},
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ out/
node_modules/
yarn-error.log
client-dest/
typechain-types/
# typechain-types/
broadcast/
36 changes: 24 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
"description": "This repo contains the solidity files and typescript helper class for all of the Aztec Connect Bridge Contracts",
"repository": "[email protected]:AztecProtocol/aztec-connect-bridges.git",
"license": "Apache-2.0",
"type": "module",
"scripts": {
"install:foundryup": "curl -L https://foundry.paradigm.xyz | bash",
"install:foundry": "foundryup",
"install:dependencies": "git submodule update --init --recursive && yarn",
"setup": "yarn install:foundryup && yarn install:foundry && yarn",
"setup": "yarn install:foundryup && yarn install:foundry && yarn install:dependencies",
"clean": "rm -rf ./cache ./dest ./out ./typechain-types ./client-dest",
"compile:typechain": "forge build && typechain --target ethers-v5 --out-dir ./typechain-types './out/!(test*|Test*|*.t.sol|*.s.sol)/*.json'",
"compile:client-dest": "yarn compile:typechain && tsc --project tsconfig.client-dest.json",
"build": "forge build && yarn compile:client-dest",
"compile:typechain": "yarn clean && forge build --skip test --skip script && typechain --target ethers-v5 --out-dir ./typechain-types './out/?(DataProvider|RollupProcessor|*Bridge|I*).sol/*.json'",
"compile:client-dest": "tsc --project tsconfig.client-dest.json",
"build": "yarn compile:client-dest",
"test:pinned:14000000": "forge test --fork-block-number 14000000 --match-contract 'Element' --fork-url https://mainnet.infura.io/v3/9928b52099854248b3a096be07a6b23c",
"test:pinned:14970000": "forge test --fork-block-number 14970000 -m 'testRedistributionSuccessfulSwap|testRedistributionExitWhenICREqualsMCR' --fork-url https://mainnet.infura.io/v3/9928b52099854248b3a096be07a6b23c",
"test:pinned:14972000": "forge test --fork-block-number 14972000 -m 'testRedistributionFailingSwap' --fork-url https://mainnet.infura.io/v3/9928b52099854248b3a096be07a6b23c",
"test:pinned": "yarn test:pinned:14000000 && yarn test:pinned:14970000 && yarn test:pinned:14972000",
"test:contracts": "forge test --no-match-contract 'Element' --no-match-test 'testRedistribution' && yarn test:pinned",
"test:clients": "yarn compile:typechain && jest test",
"test:clients": "NODE_OPTIONS=--experimental-vm-modules yarn jest",
"test": "yarn test:contracts && yarn test:clients",
"formatting": "forge fmt && yarn prettier --write ./src/client/**/*.ts",
"formatting:check": "forge fmt --check && prettier --check ./src/client/**/*.ts",
Expand All @@ -27,15 +28,15 @@
"lint:clients": "eslint \"src/**/*.{ts,tsx}\""
},
"dependencies": {
"@aztec/barretenberg": "2.1.45",
"@ethersproject/providers": "^5.7.1",
"@aztec/barretenberg": "2.1.52",
"@ethersproject/providers": "^5.7.2",
"@openzeppelin/contracts": "^4.7.3",
"@typechain/ethers-v5": "^10.1.0",
"ethers": "^5.7.1",
"@typechain/ethers-v5": "^10.1.1",
"ethers": "^5.7.2",
"isomorphic-fetch": "^3.0.0",
"ts-node": "^10.9.1",
"typechain": "^8.1.0",
"typescript": "^4.8.4"
"typechain": "^8.1.1",
"typescript": "^4.9.3"
},
"devDependencies": {
"@types/jest": "^29.2.3",
Expand All @@ -45,14 +46,25 @@
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"prettier": "^2.7.1",
"solhint": "https://github.com/LHerskind/solhint",
"ts-jest": "^29.0.3"
},
"jest": {
"preset": "ts-jest/presets/default-esm",
"transform": {
"^.+\\.ts$": "ts-jest"
"^.+\\.ts$": [
"ts-jest",
{
"useESM": true
}
]
},
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"testEnvironment": "jsdom",
"testRegex": ".*\\.test\\.ts$",
"rootDir": "./src"
},
Expand Down
53 changes: 53 additions & 0 deletions py/esm-fixer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import re
from pathlib import Path

for path in Path('../typechain-types/').rglob('**/*'):

if Path.is_file(path):
with open(path, 'r') as file:
data = file.read()

# Fix the ones where we need index.js
outer_pattern = '\* as \w* from \".*\";'
to_fix = re.findall(outer_pattern, data)
y = re.split(outer_pattern, data)

replacements = []
for x in to_fix:
inner_pattern = "\* as \w* from \"(.*)\";"
_x = re.search(inner_pattern, x)
cleaned = "{0}/index.js".format(_x[1])
rep = _x[0].replace(_x[1], cleaned)
replacements.append(rep)

full = []
for i in range(len(y)):
full.append(y[i])
if i < len(replacements):
full.append(replacements[i])

data = "".join(full)

# The more direct .js ones
outer_pattern = '\} from \"[.]{1,2}.*\";'
to_fix = re.findall(outer_pattern, data)
y = re.split(outer_pattern, data)

replacements = []
for x in to_fix:
inner_pattern = "\} from \"[.]{1,2}(.*)\";"
_x = re.search(inner_pattern, x)
cleaned = "{0}.js".format(_x[1])
rep = _x[0].replace(_x[1], cleaned)
replacements.append(rep)

full = []
for i in range(len(y)):
full.append(y[i])
if i < len(replacements):
full.append(replacements[i])

data = "".join(full)

with open(path, 'w+') as f:
f.write(data)
2 changes: 1 addition & 1 deletion src/bridges/example/ExampleBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {BridgeBase} from "../base/BridgeBase.sol";
* @dev This bridge demonstrates the flow of assets in the convert function. This bridge simply returns what has been
* sent to it.
*/
contract ExampleBridgeContract is BridgeBase {
contract ExampleBridge is BridgeBase {
/**
* @notice Set address of rollup processor
* @param _rollupProcessor Address of rollup processor
Expand Down
24 changes: 13 additions & 11 deletions src/client/aavev2/aavev2-bridge-data.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { EthAddress } from "@aztec/barretenberg/address";
import { IERC4626, IERC4626__factory } from "../../../typechain-types";
import { AztecAsset, AztecAssetType } from "../bridge-data";
import { AaveV2BridgeData } from "./aavev2-bridge-data";

jest.mock("../aztec/provider", () => ({
createWeb3Provider: jest.fn(),
}));
import { IERC4626, IERC4626__factory } from "../../../typechain-types/index.js";
import { AztecAsset, AztecAssetType } from "../bridge-data.js";
import { AaveV2BridgeData } from "./aavev2-bridge-data.js";
import { JsonRpcProvider } from "../aztec/provider/json_rpc_provider.js";
import { jest } from "@jest/globals";

type Mockify<T> = {
[P in keyof T]: jest.Mock | any;
Expand All @@ -14,12 +12,16 @@ type Mockify<T> = {
describe("AaveV2 bridge data", () => {
let erc4626Contract: Mockify<IERC4626>;

let provider: JsonRpcProvider;

let ethAsset: AztecAsset;
let wa2DaiAsset: AztecAsset;
let daiAsset: AztecAsset;
let emptyAsset: AztecAsset;

beforeAll(() => {
provider = new JsonRpcProvider("https://mainnet.infura.io/v3/9928b52099854248b3a096be07a6b23c");

ethAsset = {
id: 0,
assetType: AztecAssetType.ETH,
Expand All @@ -45,24 +47,24 @@ describe("AaveV2 bridge data", () => {
it("should correctly fetch APR", async () => {
erc4626Contract = {
...erc4626Contract,
asset: jest.fn().mockResolvedValue(daiAsset.erc20Address.toString()),
asset: jest.fn().mockReturnValue(daiAsset.erc20Address.toString()),
};
IERC4626__factory.connect = () => erc4626Contract as any;

const aavev2BridgeData = AaveV2BridgeData.create({} as any);
const aavev2BridgeData = AaveV2BridgeData.create(provider);
const apr = await aavev2BridgeData.getAPR(wa2DaiAsset);
expect(apr).toBeGreaterThan(0);
});

it("should correctly fetch market size", async () => {
const aaveV2BridgeData = AaveV2BridgeData.create({} as any);
const aaveV2BridgeData = AaveV2BridgeData.create(provider);
const assetValue = (await aaveV2BridgeData.getMarketSize(daiAsset, emptyAsset, emptyAsset, emptyAsset, 0n))[0];
expect(assetValue.assetId).toBe(daiAsset.id);
expect(assetValue.value).toBeGreaterThan(0);
});

it("should correctly fetch market size for ETH", async () => {
const aaveV2BridgeData = AaveV2BridgeData.create({} as any);
const aaveV2BridgeData = AaveV2BridgeData.create(provider);
const assetValue = (await aaveV2BridgeData.getMarketSize(ethAsset, emptyAsset, emptyAsset, emptyAsset, 0n))[0];
expect(assetValue.assetId).toBe(ethAsset.id);
expect(assetValue.value).toBeGreaterThan(0);
Expand Down
6 changes: 3 additions & 3 deletions src/client/aavev2/aavev2-bridge-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { AssetValue } from "@aztec/barretenberg/asset";
import { EthereumProvider } from "@aztec/barretenberg/blockchain";
import { Web3Provider } from "@ethersproject/providers";
import "isomorphic-fetch";
import { createWeb3Provider } from "../aztec/provider";
import { AztecAsset, AztecAssetType } from "../bridge-data";
import { createWeb3Provider } from "../aztec/provider/web3_provider.js";
import { AztecAsset, AztecAssetType } from "../bridge-data.js";

import { ERC4626BridgeData } from "../erc4626/erc4626-bridge-data";
import { ERC4626BridgeData } from "../erc4626/erc4626-bridge-data.js";

export class AaveV2BridgeData extends ERC4626BridgeData {
private readonly subgraphWethId = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
Expand Down
26 changes: 14 additions & 12 deletions src/client/angle/angle-bridge-data.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { AngleBridgeData } from "./angle-bridge-data";
import { IStableMaster, IStableMaster__factory } from "../../../typechain-types";
import { AztecAsset, AztecAssetType } from "../bridge-data";
import { AngleBridgeData } from "./angle-bridge-data.js";
import { IStableMaster, IStableMaster__factory } from "../../../typechain-types/index.js";
import { AztecAsset, AztecAssetType } from "../bridge-data.js";
import { utils } from "ethers";
import { EthAddress } from "@aztec/barretenberg/address";

jest.mock("../aztec/provider", () => ({
createWeb3Provider: jest.fn(),
}));
import { JsonRpcProvider } from "../aztec/provider/json_rpc_provider.js";
import { jest } from "@jest/globals";

type Mockify<T> = {
[P in keyof T]: jest.Mock | any;
Expand All @@ -16,6 +14,8 @@ describe("Testing Angle Bridge", () => {
let angleBridgeData: AngleBridgeData;
let stableMasterContract: Mockify<IStableMaster>;

let provider: JsonRpcProvider;

let ethAsset: AztecAsset;
let emptyAsset: AztecAsset;

Expand All @@ -31,11 +31,13 @@ describe("Testing Angle Bridge", () => {

const createBridgeData = (stableMaster = stableMasterContract) => {
IStableMaster__factory.connect = () => stableMaster as any;
return AngleBridgeData.create({} as any);
return AngleBridgeData.create(provider);
};

beforeAll(() => {
angleBridgeData = AngleBridgeData.create({} as any);
provider = new JsonRpcProvider("https://mainnet.infura.io/v3/9928b52099854248b3a096be07a6b23c");

angleBridgeData = AngleBridgeData.create(provider);

ethAsset = {
id: 1,
Expand Down Expand Up @@ -95,13 +97,13 @@ describe("Testing Angle Bridge", () => {
);

angleBridgeData = createBridgeData({
collateralMap: jest.fn().mockResolvedValue({ sanToken: sanDAI.erc20Address.toString().toLowerCase() }),
collateralMap: jest.fn().mockReturnValue({ sanToken: sanDAI.erc20Address.toString().toLowerCase() }),
} as any);
expect(await angleBridgeData.getAuxData(sanDAI, emptyAsset, DAI, emptyAsset)).toEqual([1n]);
expect(await angleBridgeData.getAuxData(DAI, emptyAsset, sanDAI, emptyAsset)).toEqual([0n]);

angleBridgeData = createBridgeData({
collateralMap: jest.fn().mockResolvedValue({ sanToken: sanWETH.erc20Address.toString().toLowerCase() }),
collateralMap: jest.fn().mockReturnValue({ sanToken: sanWETH.erc20Address.toString().toLowerCase() }),
} as any);
await expect(() => angleBridgeData.getAuxData(DAI, emptyAsset, sanDAI, emptyAsset)).rejects.toEqual(
"invalid outputAssetA",
Expand All @@ -111,7 +113,7 @@ describe("Testing Angle Bridge", () => {

it("should get correct output", async () => {
angleBridgeData = createBridgeData({
collateralMap: jest.fn().mockResolvedValue({
collateralMap: jest.fn().mockReturnValue({
sanRate: utils.parseEther("1.2"),
sanToken: sanDAI.erc20Address.toString().toLowerCase(),
}),
Expand Down
6 changes: 3 additions & 3 deletions src/client/angle/angle-bridge-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import "isomorphic-fetch";
import { EthAddress } from "@aztec/barretenberg/address";
import { EthereumProvider } from "@aztec/barretenberg/blockchain";
import { Web3Provider } from "@ethersproject/providers";
import { IStableMaster, IStableMaster__factory } from "../../../typechain-types";
import { createWeb3Provider } from "../aztec/provider";
import { AuxDataConfig, AztecAsset, AztecAssetType, BridgeDataFieldGetters, SolidityType } from "../bridge-data";
import { IStableMaster, IStableMaster__factory } from "../../../typechain-types/index.js";
import { createWeb3Provider } from "../aztec/provider/web3_provider.js";
import { AuxDataConfig, AztecAsset, AztecAssetType, BridgeDataFieldGetters, SolidityType } from "../bridge-data.js";
import { AssetValue } from "@aztec/barretenberg/asset";

export class AngleBridgeData implements BridgeDataFieldGetters {
Expand Down
6 changes: 3 additions & 3 deletions src/client/aztec/data-provider/DataProvider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EthAddress } from "@aztec/barretenberg/address";
import { EthereumProvider } from "@aztec/barretenberg/blockchain";
import { DataProvider__factory } from "../../../../typechain-types";
import { DataProvider } from "../../../../typechain-types";
import { createWeb3Provider } from "../provider";
import { DataProvider__factory } from "../../../../typechain-types/index.js";
import { DataProvider } from "../../../../typechain-types/index.js";
import { createWeb3Provider } from "../provider/web3_provider.js";

export interface AssetData {
assetAddress: EthAddress;
Expand Down
2 changes: 1 addition & 1 deletion src/client/aztec/provider/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./web3_provider";
export * from "./web3_provider.js";
63 changes: 63 additions & 0 deletions src/client/aztec/provider/json_rpc_provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { EthereumProvider, RequestArguments } from "@aztec/barretenberg/blockchain";
import { fetch } from "@aztec/barretenberg/iso_fetch";
import { retry } from "@aztec/barretenberg/retry";
import { createDebugLogger } from "@aztec/barretenberg/log";

const log = createDebugLogger("json_rpc_provider");

export class JsonRpcProvider implements EthereumProvider {
private id = 0;

constructor(private host: string, private netMustSucceed = true) {}

public async request({ method, params }: RequestArguments): Promise<any> {
const body = {
jsonrpc: "2.0",
id: this.id++,
method,
params,
};
log(`->`, body);
const res = await this.fetch(body);
log(`<-`, res);
if (res.error) {
throw res.error;
}
return res.result;
}

on(): this {
throw new Error("Events not supported.");
}

removeListener(): this {
throw new Error("Events not supported.");
}

private async fetch(body: any) {
const fn = async () => {
const resp = await fetch(this.host, {
method: "POST",
body: JSON.stringify(body),
headers: { "content-type": "application/json" },
});

if (!resp.ok) {
throw new Error(resp.statusText);
}

const text = await resp.text();
try {
return JSON.parse(text);
} catch (err) {
throw new Error(`Failed to parse body as JSON: ${text}`);
}
};

if (this.netMustSucceed) {
return await retry(fn, "JsonRpcProvider request");
}

return await fn();
}
}
Loading

0 comments on commit 0a3949e

Please sign in to comment.