@0xweb/hardhat 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  require("@nomiclabs/hardhat-web3");
2
2
  require("@nomiclabs/hardhat-waffle");
3
3
  require("@nomiclabs/hardhat-etherscan");
4
+ require("@0xweb/hardhat");
4
5
 
5
6
  module.exports = {
6
7
  solidity: {
package/lib/index.js ADDED
@@ -0,0 +1,192 @@
1
+
2
+ // source ./RootModule.js
3
+ (function(){
4
+
5
+ var _src_config = {};
6
+ var _src_constants = {};
7
+
8
+ // source ./ModuleSimplified.js
9
+ var _src_config;
10
+ (function () {
11
+ // ensure AMD is not active for the model, so that any UMD exports as commonjs
12
+ var define = null;
13
+ var exports = _src_config != null ? _src_config : {};
14
+ var module = { exports: exports };
15
+
16
+ "use strict";
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.resolveConfig = void 0;
19
+ function resolveConfig(config) {
20
+ var _a;
21
+ const defaultConfig = {};
22
+ return {
23
+ ...defaultConfig,
24
+ ...((_a = config['0xweb']) !== null && _a !== void 0 ? _a : {}),
25
+ };
26
+ }
27
+ exports.resolveConfig = resolveConfig;
28
+ //# sourceMappingURL=config.js.map
29
+ //# sourceMappingURL=config.ts.map;
30
+
31
+ function __isObj(x) {
32
+ return x != null && typeof x === 'object' && x.constructor === Object;
33
+ }
34
+ if (_src_config === module.exports) {
35
+ // do nothing if
36
+ } else if (__isObj(_src_config) && __isObj(module.exports)) {
37
+ Object.assign(_src_config, module.exports);
38
+ } else {
39
+ _src_config = module.exports;
40
+ }
41
+
42
+ ;
43
+ }());
44
+
45
+ // end:source ./ModuleSimplified.js
46
+
47
+
48
+ // source ./ModuleSimplified.js
49
+ var _src_constants;
50
+ (function () {
51
+ // ensure AMD is not active for the model, so that any UMD exports as commonjs
52
+ var define = null;
53
+ var exports = _src_constants != null ? _src_constants : {};
54
+ var module = { exports: exports };
55
+
56
+ "use strict";
57
+ Object.defineProperty(exports, "__esModule", { value: true });
58
+ exports.TASK_0xWEB_GENERATE = exports.TASK_0xWEB = void 0;
59
+ exports.TASK_0xWEB = '0xweb';
60
+ exports.TASK_0xWEB_GENERATE = '0xweb:generate';
61
+ //# sourceMappingURL=constants.js.map
62
+ //# sourceMappingURL=constants.ts.map;
63
+
64
+ function __isObj(x) {
65
+ return x != null && typeof x === 'object' && x.constructor === Object;
66
+ }
67
+ if (_src_constants === module.exports) {
68
+ // do nothing if
69
+ } else if (__isObj(_src_constants) && __isObj(module.exports)) {
70
+ Object.assign(_src_constants, module.exports);
71
+ } else {
72
+ _src_constants = module.exports;
73
+ }
74
+
75
+ ;
76
+ }());
77
+
78
+ // end:source ./ModuleSimplified.js
79
+
80
+ "use strict";
81
+ Object.defineProperty(exports, "__esModule", { value: true });
82
+ const alot_1 = require("alot");
83
+ const path = require("path");
84
+ const task_names_1 = require("hardhat/builtin-tasks/task-names");
85
+ const config_1 = require("hardhat/config");
86
+ const config_2 = _src_config;
87
+ const constants_1 = _src_constants;
88
+ const atma_io_1 = require("atma-io");
89
+ const _0xweb_1 = require("0xweb");
90
+ const taskArgsStore = { compileAll: false };
91
+ (0, config_1.extendConfig)((config) => {
92
+ config['0xweb'] = (0, config_2.resolveConfig)(config);
93
+ });
94
+ (0, config_1.task)(task_names_1.TASK_COMPILE, 'Compiles the entire project, building all artifacts')
95
+ .addOptionalParam('sources', 'Override the sources directory')
96
+ .addOptionalParam('artifacts', 'Override the artifacts output directory')
97
+ .addOptionalParam('watch', 'Re-runs compilation task on changes')
98
+ .setAction(async (compilationArgs, { run, config }, runSuper) => {
99
+ if (compilationArgs.sources) {
100
+ config.paths.sources = path.join(process.cwd(), compilationArgs.sources);
101
+ }
102
+ if (compilationArgs.artifacts) {
103
+ config.paths.artifacts = path.join(process.cwd(), compilationArgs.artifacts);
104
+ }
105
+ if (compilationArgs.watch != null) {
106
+ const directory = `file://${config.paths.sources}/`;
107
+ atma_io_1.Directory.watch(directory, async (...args) => {
108
+ console.log('XX', args);
109
+ await runSuper();
110
+ });
111
+ await runSuper();
112
+ await new Promise(resolve => { });
113
+ return;
114
+ }
115
+ await runSuper();
116
+ });
117
+ (0, config_1.subtask)(task_names_1.TASK_COMPILE_SOLIDITY_COMPILE_JOBS, 'Compiles the entire project, building all artifacts')
118
+ .setAction(async (taskArgs, { run }, runSuper) => {
119
+ const compileSolOutput = await runSuper(taskArgs);
120
+ await run(constants_1.TASK_0xWEB_GENERATE, { compileSolOutput });
121
+ return compileSolOutput;
122
+ });
123
+ (0, config_1.subtask)(constants_1.TASK_0xWEB_GENERATE)
124
+ .setAction(async (a, b) => {
125
+ let { compileSolOutput } = a;
126
+ let { config, artifacts } = b;
127
+ const contracts = await getCompiledAbis(config, compileSolOutput);
128
+ const app = new _0xweb_1.App();
129
+ await (0, alot_1.default)(contracts)
130
+ .forEachAsync(async (contract, i) => {
131
+ console.log(`Generation ${contract.name}(${contract.path}) ${i}/${contracts.length}`);
132
+ await app.execute([`install`, `${contract.path}`, '--name', contract.name, '--chain', 'hardhat']);
133
+ })
134
+ .toArrayAsync({ threads: 1 });
135
+ });
136
+ (0, config_1.task)(constants_1.TASK_0xWEB, 'Generate 0xWeb classes for compiled contracts')
137
+ .setAction(async (_, { run }) => {
138
+ taskArgsStore.compileAll = true;
139
+ await run(task_names_1.TASK_COMPILE, { quiet: true });
140
+ });
141
+ (0, config_1.task)(task_names_1.TASK_CLEAN, 'Clears the cache and deletes all artifacts')
142
+ .setAction(async ({ global }, { config }, runSuper) => {
143
+ if (global) {
144
+ return;
145
+ }
146
+ const dir = `/0xweb/hardhat/`;
147
+ if (await atma_io_1.Directory.existsAsync(dir)) {
148
+ await atma_io_1.Directory.removeAsync(dir);
149
+ }
150
+ await runSuper();
151
+ });
152
+ async function getCompiledAbis(config, compileSolOutput) {
153
+ const emitedArtifacts = (0, alot_1.default)(compileSolOutput.artifactsEmittedPerJob).mapMany((a) => {
154
+ return (0, alot_1.default)(a.artifactsEmittedPerFile).mapMany((artifactPerFile) => {
155
+ return (0, alot_1.default)(artifactPerFile.artifactsEmitted).map((artifactName) => {
156
+ return {
157
+ name: artifactName,
158
+ sourceFile: 'file://' + artifactPerFile.file.absolutePath
159
+ };
160
+ }).toArray();
161
+ }).toArray();
162
+ }).toArray();
163
+ let namesHash = (0, alot_1.default)(emitedArtifacts).toDictionary(x => x.name);
164
+ let files = await atma_io_1.Directory.readFilesAsync(`file://${config.paths.artifacts}/`, '**.json');
165
+ let compileAll = taskArgsStore.compileAll;
166
+ let arr = files
167
+ .map(file => {
168
+ let path = file.uri.toString();
169
+ let match = /(?<name>[^\\\/]+)\.sol[\\\/]/.exec(path);
170
+ if (match == null) {
171
+ return null;
172
+ }
173
+ let name = match.groups.name;
174
+ if (compileAll !== true && name in namesHash === false) {
175
+ return null;
176
+ }
177
+ if (new RegExp(`${name}\\.json$`).test(path) === false) {
178
+ return null;
179
+ }
180
+ return {
181
+ name: name,
182
+ path: path
183
+ };
184
+ })
185
+ .filter(Boolean);
186
+ return arr;
187
+ }
188
+ //# sourceMappingURL=index.js.map
189
+ //# sourceMappingURL=index.ts.map
190
+
191
+ }());
192
+ // end:source ./RootModule.js
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@0xweb/hardhat",
3
- "description": "Hardhat addon for 0xweb",
4
- "version": "0.1.0",
5
- "main": "./index.js",
3
+ "description": "Hardhat plugin for 0xweb",
4
+ "version": "0.1.2",
5
+ "main": "./lib/index.js",
6
6
  "author": {
7
7
  "name": "Alex Kit",
8
8
  "email": "alex.kit@atmajs.com"
@@ -18,16 +18,103 @@
18
18
  ],
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
+ "0xweb": "^0.9.25",
22
+ "alot": "^0.3.0",
23
+ "atma-io": "^1.2.65",
21
24
  "memd": "^0.3.10"
22
25
  },
23
26
  "peerDependencies": {
24
27
  "@nomiclabs/hardhat-etherscan": ">=3.0.3",
25
28
  "@nomiclabs/hardhat-waffle": ">=2.0.3",
26
29
  "@nomiclabs/hardhat-web3": ">=2.0.0",
27
- "ethers": ">=5.6.4",
28
30
  "hardhat": ">=2.9.3"
29
31
  },
30
32
  "scripts": {
31
-
33
+ "build": "app-bundler",
34
+ "watch": "app-bundler --watch",
35
+ "test": "atma --openssl-legacy-provider test",
36
+ "release": "echo \"Run atma pub -m \"message\"\""
37
+ },
38
+ "devDependencies": {
39
+ "app-bundler": "^0.1.7",
40
+ "atma": "^0.15.17",
41
+ "shellbee": "^0.5.22"
42
+ },
43
+ "app-bundler": {
44
+ "file": "src/index.ts",
45
+ "outputMain": "lib/index.js",
46
+ "package": {
47
+ "module": "commonjs",
48
+ "commonjs": {
49
+ "output": "simplified"
50
+ }
51
+ },
52
+ "extensions": {
53
+ "io": {
54
+ "type": "js"
55
+ }
56
+ },
57
+ "defaultExtensions": {
58
+ "js": "ts"
59
+ },
60
+ "dynamicDependencies": [
61
+ "alot",
62
+ "atma-io",
63
+ "0xweb",
64
+ "^hardhat",
65
+ "path"
66
+ ],
67
+ "middlewares": {
68
+ "ts": [
69
+ "atma-loader-ts:read"
70
+ ]
71
+ },
72
+ "settings": {
73
+ "atma-loader-ts": {
74
+ "typescript": {
75
+ "compilerOptions": {
76
+ "module": "CommonJS",
77
+ "lib": [
78
+ "ES2019"
79
+ ],
80
+ "target": "ES2019"
81
+ }
82
+ }
83
+ }
84
+ }
85
+ },
86
+ "atma": {
87
+ "plugins": [
88
+ "atma-loader-ts"
89
+ ],
90
+ "settings": {
91
+ "atma-utest": {
92
+ "include": {
93
+ "amd": true,
94
+ "extentionDefault": {
95
+ "js": "ts"
96
+ }
97
+ }
98
+ },
99
+ "include": {
100
+ "amd": true,
101
+ "extentionDefault": {
102
+ "js": "ts"
103
+ }
104
+ },
105
+ "atma-loader-ts": {
106
+ "typescript": {
107
+ "compilerOptions": {
108
+ "#if (TEST)": {
109
+ "module": "AMD"
110
+ },
111
+ "#if (!TEST)": {
112
+ "module": "commonjs"
113
+ },
114
+ "sourceMap": false
115
+ }
116
+ }
117
+ }
118
+ }
32
119
  }
33
120
  }
package/readme.md CHANGED
@@ -1,8 +1,99 @@
1
- # [Hardhat](https://hardhat.org/) addon for [`0xWeb`](https://0xweb.org)
1
+ # [`0xWeb`](https://0xweb.org) plugin for [Hardhat](https://hardhat.org/)
2
2
 
3
3
 
4
- ### Distribution `as-Source-Code`
4
+ ----
5
+ [![npm version](https://badge.fury.io/js/@0xweb%2Fhardhat.svg)](https://badge.fury.io/js/@0xweb%2Fhardhat)
6
+ [![CircleCI](https://circleci.com/gh/0xweb-org/hardhat.svg?style=svg)](https://circleci.com/gh/0xweb-org/hardhat)
7
+
8
+
9
+ The plugin generates `0xWeb` classes for compiled solidity contracts, making the blockchain development transparent.
10
+
11
+ > We use [📦 dequanto library](https://github.com/0xweb-org/dequanto) for the classes
12
+
13
+ ---
14
+ [Documentation 📜](https://docs.0xweb.org/hardhat/info)
15
+ ---
16
+
17
+ # Install
18
+
19
+ ### Install automatically the dependencies and configurations
20
+ ```bash
21
+ # install 0xweb
22
+ $ npm i 0xweb -g
23
+
24
+ # initiallize 0xweb and hardhat project
25
+ $ 0xweb init --hardhat
26
+ ```
27
+
28
+
29
+
30
+ # Compile
31
+
32
+ > `any/directory/Foo.sol`
33
+
34
+ ```solidity
35
+ pragma solidity ^0.8.2;
36
+
37
+ contract Foo {
38
+ string public name;
39
+
40
+ function setName(string memory _name) public {
41
+ name = _name;
42
+ }
43
+ }
44
+
45
+ ```
46
+
47
+ > Plugin adds also ability to specify the `sources` folder. As per default this is `/contracts/**.sol`
48
+
49
+ ```bash
50
+ $ npx hardhat compile --sources ./any/directory/
51
+ ```
52
+
53
+ # Use in local development
54
+
55
+ > `example.ts`
56
+ ```ts
57
+ import { Foo } from '@0xweb/hardhat/Foo/Foo.ts'
58
+ import { HardhatProvider } from '@dequanto/hardhat/HardhatProvider'
59
+
60
+ // automatically deploys the contract to hardhat chain
61
+ const deployer = new HardhatProvider();
62
+ const foo = await deployer.resolve(Foo);
63
+
64
+ // write
65
+ const tx = await foo.setName('Hello world')
66
+ const receipt = await tx.wait();
67
+
68
+ // read
69
+ const text = await foo.name();
70
+
71
+ ```
72
+
73
+ # Use already deployed contracts to any chain
74
+
75
+ If the contract is already deployed, initialize the contract with the Address as normal class. If the contract is deployed to any other chain - set also the client in constructor
76
+
77
+ > `example.ts`
78
+ ```ts
79
+ import { Foo } from '@0xweb/hardhat/Foo/Foo.ts'
80
+ import { EthWeb3Client } from '@dequanto/clients/EthWeb3Client'
81
+
82
+
83
+ const client = new EthWeb3Client();
84
+ const foo = new Foo('0x12345...', client);
85
+
86
+ // write
87
+ const tx = await foo.setName('Hello world');
88
+ const receipt = tx.wait();
89
+ // read
90
+ const text = await foo.name();
91
+ ```
92
+
93
+
94
+ # Additional parameters
5
95
 
6
- A factory class to initialize and configurate the generated contract class by `0xweb` to be used in `Hardhat` network - `in-process` or `localhost`
96
+ - `npx hardhat compile --source /foo/bar/qux` - compiles solidity files which are located outside the `/contracts` folder
97
+ - `npx hardhat compile --artifacts /dist` - set custom folder for artifacts (ABI JSONs and TS contracts)
7
98
 
8
99
  ----
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "exclude": ["node_modules"],
3
+ "compilerOptions": {
4
+ "outDir": "ts-temp",
5
+ "lib": ["es2020"],
6
+ "typeRoots": [
7
+ "node_modules/@types",
8
+ "typings",
9
+ "typings/globals",
10
+ "typings-other"
11
+ ],
12
+ "types": ["node", "assertion", "atma-utest"],
13
+ "declaration": true,
14
+ "target": "es2020",
15
+ "module": "commonjs",
16
+ "sourceMap": false,
17
+ "experimentalDecorators": true
18
+ }
19
+ }
package/index.ts DELETED
@@ -1,49 +0,0 @@
1
- import hre from "hardhat";
2
- import memd from 'memd';
3
- import type Ethers from 'ethers'
4
- import { ContractBase } from '@dequanto/contracts/ContractBase';
5
- import { type Constructor } from 'atma-utils/mixin';
6
- import { ChainAccount } from '@dequanto/ChainAccounts';
7
- import { HardhatWeb3Client } from '@dequanto/clients/HardhatWeb3Client';
8
-
9
- export class HardhatWeb {
10
-
11
- @memd.deco.memoize()
12
- static deployer(index: number = 0): ChainAccount {
13
- const ethers: typeof Ethers = (hre as any).ethers;
14
- const accounts: any = hre.config.networks.hardhat.accounts;
15
- const wallet = ethers.Wallet.fromMnemonic(accounts.mnemonic, accounts.path + `/${index}`);
16
- return {
17
- key: wallet.privateKey,
18
- address: wallet.address,
19
- };
20
- }
21
-
22
- @memd.deco.memoize()
23
- static async resolve<T extends ContractBase>(Ctor: Constructor<T>): Promise<T> {
24
-
25
- await ContractProvider.compile();
26
-
27
- const ethers = (hre as any).ethers;
28
-
29
- const Factory: Ethers.ContractFactory = await ethers.getContractFactory(Ctor.name);
30
- const contract = await Factory.deploy();
31
- const receipt = await contract.deployed();
32
-
33
- console.log(`Contract ${Ctor.name} deployed to ${contract.address}`);
34
-
35
- const client = ContractProvider.client();
36
- return new Ctor(contract.address, client);
37
- }
38
-
39
- static client() {
40
- const web3 = (hre as any).web3;
41
- const client = new HardhatWeb3Client({ web3, chainId: 1337 });
42
- return client;
43
- }
44
-
45
- @memd.deco.memoize()
46
- private static async compile () {
47
- await hre.run('compile');
48
- }
49
- }