Skip to content

Commit

Permalink
refactor/script.js (#1144)
Browse files Browse the repository at this point in the history
* refactor: script.js

* update: network.ts

* remove: verify.js

* refactor: network.ts

* fix: typo
  • Loading branch information
lionseal authored Sep 5, 2023
1 parent 8e7de80 commit ce86b22
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 134 deletions.
213 changes: 90 additions & 123 deletions packages/core/scripts.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#!/usr/bin/env node
'use strict';
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
const {spawn} = require('child_process');
require('dotenv').config();

const {spawnSync} = require('child_process');
const dotenv = require('dotenv');
const rawArgs = process.argv.slice(2);

dotenv.config();

function parseArgs(numFixedArgs, expectedOptions) {
const fixedArgs = [];
const options = {};
Expand All @@ -25,134 +22,104 @@ function parseArgs(numFixedArgs, expectedOptions) {
i++;
options[optionName] = rawArgs[i];
}
} else {
if (fixedArgs.length < numFixedArgs) {
throw new Error(
`expected ${numFixedArgs} fied agrs, got only ${fixedArgs.length}`
);
} else {
extra.push(rawArg);
}
continue;
}
} else {
if (fixedArgs.length < numFixedArgs) {
fixedArgs.push(rawArg);
} else {
for (const opt of Object.keys(expectedOptions)) {
alreadyCounted[opt] = true;
}
extra.push(rawArg);
}
if (fixedArgs.length < numFixedArgs) {
if (Object.keys(alreadyCounted).length) {
throw new Error(
`expected ${numFixedArgs} fixed args, got only ${fixedArgs.length}`
);
}
fixedArgs.push(rawArg);
} else {
extra.push(rawArg);
}
}
return {options, extra, fixedArgs};
}

function execute(command) {
return new Promise((resolve, reject) => {
const onExit = (error) => {
if (error) {
return reject(error);
}
resolve();
};
spawn(command.split(' ')[0], command.split(' ').slice(1), {
stdio: 'inherit',
shell: true,
}).on('exit', onExit);
return spawnSync(command.split(' ')[0], command.split(' ').slice(1), {
stdio: 'inherit',
shell: true,
});
}

void (async () => {
const firstArg = rawArgs[0];
if (firstArg === 'run') {
const {fixedArgs, extra} = parseArgs(3, {});
await execute(
`cross-env HARDHAT_NETWORK=${fixedArgs[1]} ts-node --files ${
fixedArgs[2]
} ${extra.join(' ')}`
);
} else if (firstArg === 'deploy') {
const {fixedArgs, extra} = parseArgs(2, {});
await execute(
`hardhat --network ${fixedArgs[1]} deploy ${extra.join(' ')}`
);
} else if (firstArg === 'export') {
const {fixedArgs} = parseArgs(3, {});
await execute(
`hardhat --network ${fixedArgs[1]} export --export ${fixedArgs[2]}`
);
} else if (firstArg === 'fork:run') {
const {fixedArgs, options, extra} = parseArgs(3, {
deploy: 'boolean',
blockNumber: 'string',
'no-impersonation': 'boolean',
});
await execute(
`cross-env ${
options.deploy ? 'HARDHAT_DEPLOY_FIXTURE=true' : ''
} HARDHAT_DEPLOY_ACCOUNTS_NETWORK=${fixedArgs[1]} HARDHAT_FORK=${
fixedArgs[1]
} ${
options.blockNumber ? `HARDHAT_FORK_NUMBER=${options.blockNumber}` : ''
} ${
options['no-impersonation']
? `HARDHAT_DEPLOY_NO_IMPERSONATION=true`
: ''
} ts-node --files ${fixedArgs[2]} ${extra.join(' ')}`
);
} else if (firstArg === 'fork:deploy') {
const {fixedArgs, options, extra} = parseArgs(2, {
blockNumber: 'string',
'no-impersonation': 'boolean',
});
const [command, network, file] = rawArgs;
const {options, extra} = parseArgs(getFixedArgsCount(command), {
blockNumber: 'string',
'no-impersonation': 'boolean',
'skip-test-deployments': 'boolean',
ntd: 'boolean',
});
const crossEnv = command.includes('fork')
? getForkCrossEnv({network, options, extra})
: getCrossEnv({options, extra});
const extraArgs = extra.join(' ');
switch (command) {
case 'run':
await execute(
`${crossEnv} HARDHAT_NETWORK=${network} ts-node --files ${file} ${extraArgs}`
);
break;
case 'deploy':
await execute(
`${crossEnv} hardhat --network ${network} deploy ${extraArgs}`
);
break;
case 'export':
await execute(
`${crossEnv} hardhat --network ${network} export --export ${file}`
);
break;
case 'fork:run':
await execute(`${crossEnv} ts-node --files ${file} ${extraArgs}`);
break;
case 'fork:deploy':
await execute(`${crossEnv} hardhat deploy ${extraArgs}`);
break;
case 'fork:test':
await execute(
`${crossEnv} HARDHAT_DEPLOY_FIXTURE=true HARDHAT_COMPILE=true mocha --bail --recursive test ${extraArgs}`
);
break;
case 'fork:dev':
await execute(
`${crossEnv} hardhat node --watch --export contractsInfo.json ${extraArgs}`
);
break;
}
})();

await execute(
`cross-env HARDHAT_DEPLOY_ACCOUNTS_NETWORK=${fixedArgs[1]} HARDHAT_FORK=${
fixedArgs[1]
} ${
options.blockNumber ? `HARDHAT_FORK_NUMBER=${options.blockNumber}` : ''
} ${
options['no-impersonation']
? `HARDHAT_DEPLOY_NO_IMPERSONATION=true`
: ''
} hardhat deploy ${extra.join(' ')}`
).catch((error) => {
process.exit(error);
});
} else if (firstArg === 'fork:test') {
const {fixedArgs, options, extra} = parseArgs(2, {
blockNumber: 'string',
'no-impersonation': 'boolean',
});
await execute(
`cross-env HARDHAT_DEPLOY_ACCOUNTS_NETWORK=${fixedArgs[1]} HARDHAT_FORK=${
fixedArgs[1]
} ${
options.blockNumber ? `HARDHAT_FORK_NUMBER=${options.blockNumber}` : ''
} ${
options['no-impersonation']
? `HARDHAT_DEPLOY_NO_IMPERSONATION=true`
: ''
} HARDHAT_DEPLOY_FIXTURE=true HARDHAT_COMPILE=true mocha --bail --recursive test ${extra.join(
' '
)}`
);
} else if (firstArg === 'fork:dev') {
const {fixedArgs, options, extra} = parseArgs(2, {
blockNumber: 'string',
'no-impersonation': 'boolean',
function getFixedArgsCount(command) {
return ['run', 'export', 'fork:run'].includes(command) ? 3 : 2;
}

function getCrossEnv({options, extra}) {
const env = [];
if (options.blockNumber)
env.push(`HARDHAT_FORK_NUMBER=${options.blockNumber}`);
if (options['no-impersonation'])
env.push(`HARDHAT_DEPLOY_NO_IMPERSONATION=true`);
if (options['skip-test-deployments'] || options.ntd) {
env.push(`SKIP_TEST_DEPLOYMENTS=true`);
['--skip-test-deployments', '--ntd'].forEach((key) => {
if (extra.includes(key))
extra.slice(
extra.findIndex((e) => e === key),
1
);
});
await execute(
`cross-env HARDHAT_DEPLOY_ACCOUNTS_NETWORK=${fixedArgs[1]} HARDHAT_FORK=${
fixedArgs[1]
} ${
options.blockNumber ? `HARDHAT_FORK_NUMBER=${options.blockNumber}` : ''
} ${
options['no-impersonation']
? `HARDHAT_DEPLOY_NO_IMPERSONATION=true`
: ''
} hardhat node --watch --export contractsInfo.json ${extra.join(' ')}`
);
}
})();
return `cross-env ${env.join(' ')}`;
}

function getForkCrossEnv({network, options, extra}) {
const env = [
`HARDHAT_DEPLOY_ACCOUNTS_NETWORK=${network}`,
`HARDHAT_FORK=${network}`,
];
return `${getCrossEnv({options, extra})} ${env.join(' ')}`;
}
22 changes: 11 additions & 11 deletions packages/core/utils/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,18 @@ export function isTestnet(hre: HardhatRuntimeEnvironment): boolean {
}

export function isTest(hre: HardhatRuntimeEnvironment): boolean {
return (
hre.network.name === HARDHAT_NETWORK_NAME ||
hre.network.name === 'localhost' ||
isFork()
);
if (shouldSkipTestDeployments()) return false;
return isTestNetwork(hre) || isFork();
}

function shouldSkipTestDeployments(): boolean {
return !!process.env.SKIP_TEST_DEPLOYMENTS;
}

function isTestNetwork(hre: HardhatRuntimeEnvironment): boolean {
return [HARDHAT_NETWORK_NAME, 'localhost'].includes(hre.network.name);
}

function isFork(): boolean {
return (
!!process.env.HARDHAT_FORK &&
!['--skip-test-deployments', '-ntd'].some((arg) =>
process.argv.includes(arg)
)
);
return !!process.env.HARDHAT_FORK;
}

1 comment on commit ce86b22

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

83.32%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/core/src/solc_0.8.15/common/BaseWithStorage/ERC2771
   ERC2771HandlerUpgradeable.sol44.44%25%60%44.44%23, 27, 30, 39, 39, 39–40, 42
packages/core/src/solc_0.8.15/common/OperatorFilterer
   IOperatorFilterRegistry.sol100%100%100%100%
   UpdatableOperatorFiltererUpgradeable.sol11.11%10%25%9.52%18, 23–24, 24, 24–25, 27, 27, 27–28, 30, 38, 38, 38, 42, 42, 42–44, 46, 46, 46–47, 50, 55, 55, 55–56, 56, 56–57, 60, 68, 68, 68–69, 71
packages/core/src/solc_0.8.15/raffle
   DanceFight.sol75%50%100%100%20
   FistOfTheNorthStar.sol75%50%100%100%20
   GenericRaffle.sol61.32%41.11%58.62%81.72%177, 188–194, 224–228, 251–258, 265–266, 269, 287, 287–288, 288, 288–289, 307, 335–336, 346, 346–347, 347, 347–349, 357–358, 380, 389, 397–398, 408, 408–409, 418, 418, 420, 429, 429, 431, 441, 441–442, 452, 452–453, 464, 464–465, 565, 574, 625–626, 629, 632
   HellsKitchen.sol75%50%100%100%20
   MadBalls.sol75%50%100%100%20
   ParisHilton.sol75%50%100%100%20
   Rabbids.sol75%50%100%100%20
packages/core/src/solc_0.8/Game
   GameBaseToken.sol87.78%74.26%100%95.60%101, 139–140, 142–143, 185, 189, 189–190, 193–194, 213, 216–217, 231, 248, 251, 266, 286, 352, 415, 420, 436, 441–442, 442, 442–443, 485–486, 507–508, 510, 523, 523, 523, 523, 523, 542, 547, 55, 618, 81
   GameMinter.sol96.15%83.33%100%100%77
   GameV1.sol100%100%100%100%
packages/core/src/solc_0.8/OperatorFilterer/contracts
   OperatorFilterRegistrant.sol62.50%50%100%66.67%17, 19–20
packages/core/src/solc_0.8/OperatorFilterer/contracts/upgradeable
   DefaultOperatorFiltererUpgradeable.sol0%0%0%0%12, 12–13
   OperatorFiltererUpgradeable.sol88.64%85%100%90%16, 52–53, 62–63
packages/core/src/solc_0.8/OperatorFilterer/interfaces
   IOperatorFilterRegistry.sol100%100%100%100%
packages/core/src/solc_0.8/Sand
   SandBaseToken.sol100%100%100%100%
packages/core/src/solc_0.8/StarterPack
   PurchaseValidator.sol95.12%83.33%100%100%29, 37
   StarterPackV2.sol92.96%82.35%95.65%99.07%108–111, 309, 378, 67–72, 84
packages/core/src/solc_0.8/Utils
   Batch.sol85.48%59.09%100%100%34, 38, 44, 50, 56, 65, 75, 82, 88
packages/core/src/solc_0.8/asset
   AssetAttributesRegistry.sol66.91%56%83.33%71.43%103, 103, 103–104, 104, 109, 109, 109–112, 112, 112–113, 116, 118–119, 121, 123, 151–152, 160–161, 169–171, 185, 200–201, 201, 201–202, 202, 202–203, 205, 205, 205–206, 228, 232, 67, 69–70
   AssetMinter.sol85.71%73.75%87.50%94.34%113, 117, 137, 137, 137–138, 138, 141–142, 226, 235, 248, 284, 284–285, 287, 415, 44–46, 53–54, 65, 70, 75, 80, 85
   AssetSignedAuctionWithAuth.sol78%65.15%84.62%88.73%109, 111, 180, 180, 193, 202, 211, 227, 227, 240, 249, 258, 272, 285, 298, 303, 307–308, 311–312, 316, 346–347, 347, 347, 351–352, 352, 352, 373, 89
   AssetUpgrader.sol80.81%61.76%84.62%92.31%113–114, 122, 127, 168, 192–193, 195, 221, 221–222, 224, 232, 76–77, 96–97
packages/core/src/solc_0.8/asset/libraries
   AssetHelper.sol0%0%0%0%16–17, 17, 17–18, 18, 18–19, 35–38, 40, 47, 49, 51, 59–60, 62, 65–67, 67, 67–72, 72, 72–74, 78, 86, 88–90, 90, 90–91, 94
   ERC1155ERC721Helper.sol100%100%100%100%
packages/core/src/solc_0.8/assetERC1155
   AssetBaseERC1155.sol80.12%65.45%83.72%88.76%110, 112, 133, 152, 152, 152, 152, 152–153, 179, 190, 196, 204, 204, 204, 204, 204, 216, 218, 263, 301, 301, 301–304, 317–318, 339, 355, 355, 355–356, 358, 367, 369–370, 390, 437, 447–449, 449, 449–450, 452, 491,

Please sign in to comment.