Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Features/#116 (#121)
Browse files Browse the repository at this point in the history
* #116 fixed test for useBalance #117 modified wrapper for ContractContext

- removed old json way sof doing things, will be adding local contracts for testing
- updating hardhat mocks

* #116 lots of changes to tests

- cleaned up tests
- fixed old tests
- test wrapper changes to types
- fixed how mocks are loaded
- new mocks for external and hardhat contracts
- updated useContractLoader to be more simple

* #116 bug fixes and updates to contract context

- invalidation of old contract isntances
- key updates
- updates to validate adaptor
- fixes to sorting

* #116 bug fixes and updates to contract context.  Fix to EthersConnector signer bug

- signer should invalidate when chain is changed
- invalidation of old contract isntances
- key updates
- updates to validate adaptor
- fixes to sorting

* #116 changed order of invalidation of cache

* #116 package updates

* #116 package updates

* #116 package updates
  • Loading branch information
ShravanSunder authored Jan 12, 2022
1 parent 23cae02 commit 8d07513
Show file tree
Hide file tree
Showing 93 changed files with 6,424 additions and 1,328 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ cache
test/testresult.xml
testresult.xml
.yarn/install-state.gz
test-files/__mocks__/generated
3 changes: 0 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
"**/build/**/*": true,
"**/debug/": true,
"**/dist/": true,
"**/node_modules/": true,
"**/obj": true,
"node_modules": true,
"node_modules/*/**": true,
"lib": true
},
"explorerExclude.backup": null,
Expand Down
6 changes: 3 additions & 3 deletions esbuild.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const esbuild = require('esbuild');
import esbuild from 'esbuild';
// Automatically exclude all node_modules from the bundled version
const { nodeExternalsPlugin } = require('esbuild-node-externals');
const path = require('path');
import { nodeExternalsPlugin } from 'esbuild-node-externals';
import path from 'path';

function tsPathResolver(content) {
const relativePath = path.relative(path.dirname(this.resourcePath), path.resolve(__dirname, '../src'));
Expand Down
15 changes: 15 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import 'tsconfig-paths/register';
import 'global-jsdom/register';

import '@typechain/hardhat';
import '@nomiclabs/hardhat-waffle';
import '@nomiclabs/hardhat-ethers';
import 'hardhat-deploy';
import { HardhatUserConfig } from 'hardhat/types';

/**
* this is used for hook tests
*/
const config: HardhatUserConfig = {
solidity: '0.8.6',
defaultNetwork: 'hardhat',
Expand All @@ -21,12 +25,23 @@ const config: HardhatUserConfig = {
allowUncaught: false,
require: ['ts-node/register'],
timeout: 30000,
slow: 9900,
reporter: process.env.GITHUB_ACTIONS === 'true' ? 'mocha-junit-reporter' : 'spec',
reporterOptions: {
mochaFile: 'testresult.xml',
toConsole: true,
},
},
paths: {
cache: './test-files/__mocks__/generated/cache',
artifacts: './test-files/__mocks__/generated/artifacts',
deployments: './test-files/__mocks__/generated/deployments',
sources: './test-files/contracts',
deploy: './test-files/deploy',
},
typechain: {
outDir: './test-files/__mocks__/generated/contract-types',
},
};

module.exports = config;
46 changes: 26 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "eth-hooks",
"version": "4.0.4",
"version": "4.0.8",
"description": "A set of hooks to turbocharge buidling",
"author": "Austin Griffith <[email protected]>",
"author": "Shravan Sunder<[email protected]>",
"repository": "https://github.com/austintgriffith/eth-hooks.git",
"main": "index.js",
"module": "index.js",
"type": "module",
"types": "index.d.ts",
"engines": {
"node": ">=12",
Expand Down Expand Up @@ -48,8 +49,8 @@
"@ethersproject/bignumber": "^5.5.0",
"@ethersproject/bytes": "^5.5.0",
"@ethersproject/contracts": "^5.5.0",
"@ethersproject/networks": "^5.5.1",
"@ethersproject/providers": "^5.5.1",
"@ethersproject/networks": "^5.5.2",
"@ethersproject/providers": "^5.5.2",
"@ethersproject/signing-key": "^5.5.0",
"@ethersproject/solidity": "^5.5.0",
"@ethersproject/units": "^5.5.0",
Expand All @@ -58,15 +59,15 @@
"@web3-react/core": "^6.1.9",
"@web3-react/types": "^6.0.7",
"axios": "^0.24.0",
"ethers": "^5.5.2",
"ethers": "^5.5.3",
"lodash.isequal": "^4.5.0",
"merge-anything": "^4.0.2",
"react-query": "^3.34.7",
"react-use-promise": "^0.4.1",
"ts-invariant": "^0.9.4",
"use-debounce": "^7.0.1",
"usehooks-ts": "^2.1.1",
"web3modal": "^1.9.4"
"usehooks-ts": "^2.2.0",
"web3modal": "^1.9.5"
},
"peerDependencies": {
"@uniswap/sdk": "^3.0.3",
Expand All @@ -75,68 +76,73 @@
},
"devDependencies": {
"@atixlabs/hardhat-time-n-mine": "^0.0.5",
"@defi-wonderland/smock": "^2.0.7",
"@ethereum-waffle/ens": "^3.3.1",
"@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.10",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@testing-library/dom": "^8.11.1",
"@testing-library/react": "^12.1.2",
"@testing-library/react-hooks": "^7.0.2",
"@testing-library/user-event": "^13.5.0",
"@typechain/ethers-v5": "^9.0.0",
"@typechain/hardhat": "^4.0.0",
"@types/chai": "^4.3.0",
"@types/lodash.isequal": "^4.5.5",
"@types/mocha": "^9.0.0",
"@types/node": "^16.11.17",
"@types/node": "^16.11.19",
"@types/pretty-time": "^1.1.2",
"@types/qrcode.react": "^1.0.2",
"@types/react": "^17.0.38",
"@types/react-blockies": "^1.4.1",
"@types/react-dom": "^17.0.11",
"@types/sinon-chai": "^3.2.8",
"@typescript-eslint/eslint-plugin": "^5.8.1",
"@typescript-eslint/parser": "^5.8.1",
"@typescript-eslint/eslint-plugin": "^5.9.1",
"@typescript-eslint/parser": "^5.9.1",
"@uniswap/sdk": "^3.0.3",
"chai": "^4.3.4",
"cross-env": "^7.0.3",
"earljs": "^0.1.12",
"esbuild": "^0.14.10",
"esbuild": "^0.14.11",
"esbuild-node-externals": "^1.4.1",
"eslint": "^8.6.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^16.1.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-jest": "^25.3.3",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jest": "^25.3.4",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-mocha": "^10.0.3",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.28.0",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-testing-library": "^5.0.1",
"eslint-plugin-testing-library": "^5.0.3",
"eslint-plugin-unused-imports": "^2.0.0",
"ethereum-waffle": "^3.4.0",
"global-jsdom": "^8.4.0",
"hardhat": "^2.8.0",
"hardhat-deploy": "^0.9.23",
"hardhat": "^2.8.2",
"hardhat-deploy": "^0.9.24",
"hardhat-deploy-ethers": "^0.3.0-beta.10",
"hardhat-gas-reporter": "^1.0.6",
"hardhat-gas-reporter": "^1.0.7",
"husky": "^7.0.4",
"jsdom": "^19.0.0",
"lint-staged": "^12.1.4",
"lint-staged": "^12.1.7",
"mocha": "^9.1.3",
"mocha-github-actions-reporter": "^0.2.4",
"mocha-junit-reporter": "^2.0.2",
"prettier": "^2.5.1",
"prettier-eslint": "^13.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"shx": "^0.3.3",
"shx": "^0.3.4",
"sinon": "^12.0.1",
"sinon-chai": "^3.7.0",
"ts-node": "^10.4.0",
"ts-patch": "^1.4.5",
"ts-sinon": "^2.0.2",
"tsconfig-paths": "^3.12.0",
"typechain": "^7.0.0",
"typedoc": "^0.22.10",
"typedoc-plugin-markdown": "^3.11.8",
"typedoc-plugin-markdown": "^3.11.11",
"typescript": "^4.5.4",
"typescript-transform-paths": "^3.3.1"
},
Expand Down
64 changes: 43 additions & 21 deletions src/context/app-contracts/contractConnectors.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { BaseContract, ethers } from 'ethers';
import { BaseContract, ethers, utils } from 'ethers';

import { TConnectorConnectorBase, TContractConnector, TEthersProviderOrSigner } from '~~/models';
import { isValidEthersAdaptor } from '~~/functions';
import { TConnectorConnectorBase, TContractConnector, TEthersAdaptor } from '~~/models';
import {
TDeployedContractJsonData,
TBasicContractDataRecord,
TExternalContractsAddressMap,
THardhatContractsFileJson,
TDeployedHardhatContractsJson,
} from '~~/models/contractTypes';

const extractHardhatContracts = (configJson: THardhatContractsFileJson): TDeployedContractJsonData => {
const contractData: TDeployedContractJsonData = {};
const extractHardhatContracts = (configJson: TDeployedHardhatContractsJson): TBasicContractDataRecord => {
const contractData: TBasicContractDataRecord = {};
for (const chainIdStr in configJson) {
const chainId = parseInt(chainIdStr);
if (chainId == null || isNaN(chainId)) continue;
Expand All @@ -26,8 +27,8 @@ const extractHardhatContracts = (configJson: THardhatContractsFileJson): TDeploy
return contractData;
};

const extractExternalContracts = (configJson: TExternalContractsAddressMap): TDeployedContractJsonData => {
const contractData: TDeployedContractJsonData = {};
const extractExternalContracts = (configJson: TExternalContractsAddressMap): TBasicContractDataRecord => {
const contractData: TBasicContractDataRecord = {};
for (const chainIdStr in configJson) {
const chainId = parseInt(chainIdStr);
if (chainId == null || isNaN(chainId)) continue;
Expand All @@ -40,14 +41,14 @@ const extractExternalContracts = (configJson: TExternalContractsAddressMap): TDe
return contractData;
};

export const createConnectorsForHardhatContracts = <
export const createConnectorForHardhatContract = <
GContractNames extends string,
GBaseContract extends BaseContract,
GContractInterface extends ethers.utils.Interface
>(
contractName: GContractNames,
typechainFactory: TConnectorConnectorBase<GBaseContract, GContractInterface>,
deployedHardhatContractJson: THardhatContractsFileJson
deployedHardhatContractJson: TDeployedHardhatContractsJson
): TContractConnector<GContractNames, GBaseContract, GContractInterface> => {
const info = extractHardhatContracts(deployedHardhatContractJson)[contractName];

Expand All @@ -67,11 +68,10 @@ export const createConnectorsForHardhatContracts = <
address: info.address,
},
},
chainId: info.chainId,
};
};

export const createConnectorsForExternalContract = <
export const createConnectorForExternalContract = <
GContractNames extends string,
GBaseContract extends BaseContract,
GContractInterface extends ethers.utils.Interface
Expand All @@ -98,30 +98,52 @@ export const createConnectorsForExternalContract = <
address: info.address,
},
},
chainId: info.chainId,
};
};

export const connectToContractWithSignerOrProvider = <
export const createConnectorForExternalAbi = <GContractNames extends string>(
contractName: GContractNames,
config: { [key in number]: { address: string } },
abi: Record<string, any>[]
): TContractConnector<GContractNames, BaseContract, ethers.utils.Interface> => {
return {
contractName,
connect: (address: string, signerOrProvider: ethers.Signer | ethers.providers.Provider): BaseContract => {
return new BaseContract(address, abi, signerOrProvider);
},
createInterface: (): ethers.utils.Interface => new utils.Interface(abi),
abi: abi,
config: config,
};
};

export const connectToContractWithAdaptor = <
GContractNames extends string,
GContract extends BaseContract,
GContractInterface extends ethers.utils.Interface
>(
connector: TContractConnector<GContractNames, GContract, GContractInterface>,
singerOrProvider: TEthersProviderOrSigner,
chainId: number
adaptor: TEthersAdaptor
): GContract | undefined => {
const contractAddress = connector?.config?.[chainId]?.address;
if (chainId != null && contractAddress != null && singerOrProvider != null) {
const contract = connector.connect(connector.config[chainId].address, singerOrProvider);
if (adaptor == null || !isValidEthersAdaptor(adaptor)) {
console.warn('No valid ethers adaptor provided. Skipping contract connection');
return undefined;
}

if (chainId != null && contract != null) {
const { signer, provider } = adaptor;
const signerOrProvider = signer ?? provider;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const chainId = adaptor.chainId!;
const contractAddress = connector?.config?.[chainId]?.address;
if (contractAddress != null && signerOrProvider != null) {
const contract = connector.connect(connector.config[chainId].address, signerOrProvider);
if (contract != null) {
return contract;
}
}

// error handling
if (connector.chainId !== chainId) {
if (connector.config[chainId] != null) {
console.warn('ContractConnector requires signer with the same chainId to connect contract');
}
console.log(
Expand Down
Loading

0 comments on commit 8d07513

Please sign in to comment.