Skip to content

Commit

Permalink
adjust prettier to 100 characters (compound-finance#83)
Browse files Browse the repository at this point in the history
* bump prettier to 100 chars

* run prettier at 100 chars
  • Loading branch information
scott-silver authored Jan 4, 2022
1 parent a8b949b commit ca664a5
Show file tree
Hide file tree
Showing 26 changed files with 145 additions and 499 deletions.
3 changes: 2 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": true
"singleQuote": true,
"printWidth": 100
}
4 changes: 2 additions & 2 deletions deployments/fuji/roots.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"AsteroidRaffle": "0xDe06391D36D9D14E3E5a7aa8E3529B82e6E9e8B0"
}
"AsteroidRaffle": "0xDe06391D36D9D14E3E5a7aa8E3529B82e6E9e8B0"
}
16 changes: 4 additions & 12 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import './tasks/spider/task.ts';
import './tasks/scenario/task.ts';

task('accounts', 'Prints the list of accounts', async (taskArgs, hre) => {
for (const account of await hre.ethers.getSigners())
console.log(account.address);
for (const account of await hre.ethers.getSigners()) console.log(account.address);
});

/* note: boolean environment variables are imported as strings */
Expand All @@ -37,14 +36,8 @@ function throwIfMissing(envVariable, msg: string) {
}

// required environment variables
throwIfMissing(
ETHERSCAN_KEY,
'Missing required environment variable: ETHERSCAN_KEY'
);
throwIfMissing(
SNOWTRACE_KEY,
'Missing required environment variable: SNOWTRACE_KEY'
);
throwIfMissing(ETHERSCAN_KEY, 'Missing required environment variable: ETHERSCAN_KEY');
throwIfMissing(SNOWTRACE_KEY, 'Missing required environment variable: SNOWTRACE_KEY');
throwIfMissing(INFURA_KEY, 'Missing required environment variable: INFURA_KEY');

// Networks
Expand Down Expand Up @@ -131,8 +124,7 @@ const config: HardhatUserConfig = {
gasPrice: 'auto',
blockGasLimit: 12000000,
accounts: {
mnemonic:
'myth like bonus scare over problem client lizard pioneer submit female collect',
mnemonic: 'myth like bonus scare over problem client lizard pioneer submit female collect',
},
},
},
Expand Down
105 changes: 21 additions & 84 deletions plugins/deployment_manager/DeploymentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ export class DeploymentManager {
config: DeploymentConfig;
contracts: ContractMap;

constructor(
deployment: string,
hre: HardhatRuntimeEnvironment,
config: DeploymentConfig = {}
) {
constructor(deployment: string, hre: HardhatRuntimeEnvironment, config: DeploymentConfig = {}) {
this.deployment = deployment;
this.hre = hre;
this.config = config;
Expand Down Expand Up @@ -121,10 +117,7 @@ export class DeploymentManager {
}

// Write an object to file cache
private async writeObjectToCache(
object: Object,
filePath: string
): Promise<void> {
private async writeObjectToCache(object: Object, filePath: string): Promise<void> {
if (!(await fileExists(this.cacheDir()))) {
await fs.mkdir(this.cacheDir());
}
Expand All @@ -139,9 +132,7 @@ export class DeploymentManager {

// Read relation configuration for given deployment
private async getRelationConfig(): Promise<RelationConfigMap> {
return JSON.parse(
await fs.readFile(this.relationsFile(), 'utf8')
) as RelationConfigMap;
return JSON.parse(await fs.readFile(this.relationsFile(), 'utf8')) as RelationConfigMap;
}

// Reads all cached contracts for given deployment into a map
Expand All @@ -166,9 +157,7 @@ export class DeploymentManager {

// Reads the cached proxy map for a given deployment into a map
private async getCachedProxies(): Promise<ProxiesMap> {
return JSON.parse(
await fs.readFile(this.proxiesFile(), 'utf8')
) as ProxiesMap;
return JSON.parse(await fs.readFile(this.proxiesFile(), 'utf8')) as ProxiesMap;
}

// Builds a pointer map from aliases to addresses
Expand All @@ -181,9 +170,7 @@ export class DeploymentManager {
for (let [address, buildFile] of buildMap) {
const metadata = getPrimaryContract(buildFile);
if (aliasesMap.has(metadata.address)) {
aliasesMap
.get(metadata.address)
.forEach((alias) => (pointers[alias] = metadata.address));
aliasesMap.get(metadata.address).forEach((alias) => (pointers[alias] = metadata.address));
} else {
pointers[metadata.name] = metadata.address;
}
Expand All @@ -193,15 +180,9 @@ export class DeploymentManager {
}

// Returns an ethers' wrapped contract from a given build file (based on its name and address)
private getContractFromBuildFile(
buildFile: BuildFile,
signer: Signer
): [string, Contract] {
private getContractFromBuildFile(buildFile: BuildFile, signer: Signer): [string, Contract] {
let metadata = getPrimaryContract(buildFile);
return [
metadata.name,
new this.hre.ethers.Contract(metadata.address, metadata.abi, signer),
];
return [metadata.name, new this.hre.ethers.Contract(metadata.address, metadata.abi, signer)];
}

// Builds ether contract wrappers around a map of contract metadata
Expand All @@ -215,9 +196,7 @@ export class DeploymentManager {
for (let [address, buildFile] of buildMap) {
let [name, contract] = this.getContractFromBuildFile(buildFile, signer);
if (aliasesMap.has(address)) {
aliasesMap
.get(address)
.forEach((alias) => (contracts[alias] = contract));
aliasesMap.get(address).forEach((alias) => (contracts[alias] = contract));
} else {
contracts[name] = contract;
}
Expand All @@ -227,17 +206,10 @@ export class DeploymentManager {
}

// Deploys a contract given a build file (e.g. something imported or spidered)
private async deployFromBuildFile(
buildFile: BuildFile,
deployArgs: any[]
): Promise<Contract> {
private async deployFromBuildFile(buildFile: BuildFile, deployArgs: any[]): Promise<Contract> {
let metadata = getPrimaryContract(buildFile);
const [signer] = await this.hre.ethers.getSigners(); // TODO: Hmm?
const contractFactory = new this.hre.ethers.ContractFactory(
metadata.abi,
metadata.bin,
signer
);
const contractFactory = new this.hre.ethers.ContractFactory(metadata.abi, metadata.bin, signer);
const contract = await contractFactory.deploy(...deployArgs);
return await contract.deployed();
}
Expand All @@ -247,10 +219,7 @@ export class DeploymentManager {
buildMap: BuildMap,
aliasesMap: AliasesMap
): Promise<ContractMap> {
let newContracts = await this.getContractsFromBuildMap(
buildMap,
aliasesMap
);
let newContracts = await this.getContractsFromBuildMap(buildMap, aliasesMap);
this.contracts = mergeContracts(this.contracts, newContracts);
return newContracts;
}
Expand Down Expand Up @@ -308,10 +277,7 @@ export class DeploymentManager {
// TODO: Consider using a Java-like Optional here?
let implContractMetadata: ContractMetadata | null = null;
if (relationConfig.implementation) {
let [implAddress] = await getRelations(
baseContract,
relationConfig.implementation
);
let [implAddress] = await getRelations(baseContract, relationConfig.implementation);
proxies[address] = implAddress;

let implBuildFile: BuildFile;
Expand All @@ -327,24 +293,15 @@ export class DeploymentManager {

let contract = new this.hre.ethers.Contract(
address,
implContractMetadata
? implContractMetadata.abi
: contractMetadata.abi,
implContractMetadata ? implContractMetadata.abi : contractMetadata.abi,
this.hre.ethers.provider
);

let alias = await getAlias(
contract,
contractMetadata,
relationConfig.alias
);
let alias = await getAlias(contract, contractMetadata, relationConfig.alias);
this.addAlias(alias, address, aliases);

// If there is an impl contract, add its alias as well.
if (
implContractMetadata &&
relationConfigMap[implContractMetadata.name]
) {
if (implContractMetadata && relationConfigMap[implContractMetadata.name]) {
let alias = await getAlias(
contract,
implContractMetadata,
Expand All @@ -358,41 +315,24 @@ export class DeploymentManager {
relations.map((relation) => getRelations(contract, relation))
);

discovered.push(
...relatedAddresses.flat().filter((address) => !visited.has(address))
);
discovered.push(...relatedAddresses.flat().filter((address) => !visited.has(address)));
}
}

return await this.runSpider(
relationConfigMap,
discovered,
visited,
aliases,
proxies
);
return await this.runSpider(relationConfigMap, discovered, visited, aliases, proxies);
}

// Wrapper for pulling contract data from Etherscan
private async importContract(
address: Address,
retries: number
): Promise<BuildFile> {
private async importContract(address: Address, retries: number): Promise<BuildFile> {
let buildFile;
try {
buildFile = (await loadContract(
'etherscan',
this.deployment,
address
)) as BuildFile;
buildFile = (await loadContract('etherscan', this.deployment, address)) as BuildFile;
} catch (e) {
if (retries === 0) {
throw e;
}

await new Promise((resolve) =>
setTimeout(resolve, this.importRetryDelay())
);
await new Promise((resolve) => setTimeout(resolve, this.importRetryDelay()));
return await this.importContract(address, retries - 1);
}

Expand Down Expand Up @@ -455,10 +395,7 @@ export class DeploymentManager {
/**
* Deploy a new contract from a build file, e.g. something imported or crawled
*/
async deployBuild(
buildFile: BuildFile,
deployArgs: any[]
): Promise<Contract> {
async deployBuild(buildFile: BuildFile, deployArgs: any[]): Promise<Contract> {
return this.deployFromBuildFile(buildFile, deployArgs);
}

Expand Down
14 changes: 3 additions & 11 deletions plugins/deployment_manager/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ import * as fs from 'fs/promises';
import { Contract } from 'ethers';
import { Address, BuildFile, ContractMap, ContractMetadata } from './Types';

async function asAddresses(
contract: Contract,
fnName: string
): Promise<Address[]> {
async function asAddresses(contract: Contract, fnName: string): Promise<Address[]> {
let fn = contract.functions[fnName];
if (!fn) {
// TODO: `contract.name` is undefined. Find a better way to log this error.
throw new Error(
`Cannot find contract function ${contract.name}.${fnName}()`
);
throw new Error(`Cannot find contract function ${contract.name}.${fnName}()`);
}
let val = (await fn())[0]; // Return val is always stored as first item in array

Expand Down Expand Up @@ -76,10 +71,7 @@ export async function getAlias(
}

// TODO: Should this raise or do something more interesting if it fails?
export async function getRelations(
contract: Contract,
relationFnName: string
): Promise<Address[]> {
export async function getRelations(contract: Contract, relationFnName: string): Promise<Address[]> {
return await asAddresses(contract, relationFnName);
}

Expand Down
41 changes: 8 additions & 33 deletions plugins/import/import.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
get,
getEtherscanApiKey,
getEtherscanApiUrl,
getEtherscanUrl,
} from './etherscan';
import { get, getEtherscanApiKey, getEtherscanApiUrl, getEtherscanUrl } from './etherscan';
import { memoizeAsync } from '../../shared/memoize';

/**
Expand All @@ -13,18 +8,12 @@ import { memoizeAsync } from '../../shared/memoize';
* are temporarily moving it back into the protocol repo for easier development.
*/

export async function loadContract(
source: string,
network: string,
address: string
) {
export async function loadContract(source: string, network: string, address: string) {
switch (source) {
case 'etherscan':
return await loadEtherscanContractMemoized(network, address);
default:
throw new Error(
`Unknown source \`${source}\`, expected one of [etherscan]`
);
throw new Error(`Unknown source \`${source}\`, expected one of [etherscan]`);
}
}

Expand All @@ -41,11 +30,7 @@ interface EtherscanSource {
SwarmSource: string;
}

async function getEtherscanApiData(
network: string,
address: string,
apiKey: string
) {
async function getEtherscanApiData(network: string, address: string, apiKey: string) {
let apiUrl = await getEtherscanApiUrl(network);

let result = await get(apiUrl, {
Expand Down Expand Up @@ -79,16 +64,13 @@ async function getEtherscanApiData(
async function getContractCreationCode(network: string, address: string) {
let url = `${await getEtherscanUrl(network)}/address/${address}#code`;
let result = <string>await get(url, {}, null);
let regex =
/<div id='verifiedbytecode2'>[\s\r\n]*([0-9a-fA-F]*)[\s\r\n]*<\/div>/g;
let regex = /<div id='verifiedbytecode2'>[\s\r\n]*([0-9a-fA-F]*)[\s\r\n]*<\/div>/g;
let matches = [...result.matchAll(regex)];
if (matches.length === 0) {
if (result.match(/request throttled/i)) {
throw new Error(`Request throttled by Etherscan: ${url}`);
} else {
throw new Error(
`Failed to pull deployed contract code from Etherscan: ${url}`
);
throw new Error(`Failed to pull deployed contract code from Etherscan: ${url}`);
}
}
return matches[0][1];
Expand All @@ -98,15 +80,8 @@ export async function loadEtherscanContract(network: string, address: string) {
const apiKey = getEtherscanApiKey(network);

const networkName = network;
let { source, abi, contract, compiler } = await getEtherscanApiData(
networkName,
address,
apiKey
);
let contractCreationCode = await getContractCreationCode(
networkName,
address
);
let { source, abi, contract, compiler } = await getEtherscanApiData(networkName, address, apiKey);
let contractCreationCode = await getContractCreationCode(networkName, address);
let encodedABI = JSON.stringify(abi);
let contractSource = `contracts/${contract}.sol:${contract}`;
let contractBuild = {
Expand Down
Loading

0 comments on commit ca664a5

Please sign in to comment.