-
Notifications
You must be signed in to change notification settings - Fork 17
/
optimize-abis.js
141 lines (123 loc) · 4.24 KB
/
optimize-abis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const yargs = require('yargs')
const fs = require('fs')
const fsExtra = require('fs-extra')
const equal = require('fast-deep-equal')
const absP = path => `${__dirname}/${path}`
/**
* Initialize the contracts-optimized directory
*/
function initDirectory () {
fsExtra.copySync(absP('./contracts'), absP('./contracts-optimized'), {
overwrite: true
})
}
/**
* Remove duplicate ABIs, and replacing the duplicate
* with a pointer to the original (root ABI)
*/
async function noDuplicates () {
const versionDirs = fs.readdirSync(absP('./contracts-optimized'))
// For each version (skipping the first)
for (let i = 1; i < versionDirs.length; ++i) {
const version = versionDirs[i]
const prevVersion = versionDirs[i - 1]
// For each ABI
const abis = fs.readdirSync(absP(`./contracts-optimized/${version}`))
for (const abi of abis) {
try {
const abiJson = JSON.parse(fs.readFileSync(absP(`./contracts-optimized/${version}/${abi}`), 'utf-8'))
let rootVersion = prevVersion
let rootAbiJson = JSON.parse(fs.readFileSync(absP(`./contracts-optimized/${rootVersion}/${abi}`), 'utf-8'))
if (rootAbiJson.rootVersion) {
rootVersion = rootAbiJson.rootVersion
rootAbiJson = JSON.parse(fs.readFileSync(absP(`./contracts-optimized/${rootVersion}/${abi}`), 'utf-8'))
}
// Check to see if they're the same
if (equal(abiJson, rootAbiJson)) {
// Replace the duplicate with a "Root ABI Pointer"
fs.writeFileSync(
absP(`./contracts-optimized/${version}/${abi}`),
JSON.stringify({ rootVersion })
)
}
} catch (e) { /* Do nothing because this is a newly added ABI */ }
}
}
}
/**
* Remove the bytecode from the ABIs to reduce package size.
* This can be used by external projects that use this library
* for the creation of new DAOs, and do not require instantiating
* new contracts. An example is the DAOcreator, which uses the
* DaoCreator Contract to create new DAOs.
*/
async function noBytecode () {
const versionDirs = fs.readdirSync(absP('./contracts-optimized'))
// For each version
for (let i = 0; i < versionDirs.length; ++i) {
const version = versionDirs[i]
// For each ABI
const abis = fs.readdirSync(absP(`./contracts-optimized/${version}`))
for (const abi of abis) {
const abiJson = JSON.parse(fs.readFileSync(absP(`./contracts-optimized/${version}/${abi}`), 'utf-8'))
if (abiJson.bytecode) {
delete abiJson.bytecode
}
if (abiJson.deployedBytecode) {
delete abiJson.deployedBytecode
}
fs.writeFileSync(
absP(`./contracts-optimized/${version}/${abi}`),
JSON.stringify(abiJson, null, 2)
)
}
}
}
/**
* Remove the whitespace from ABIs to reduce package size.
*/
async function noWhitespace () {
const versionDirs = fs.readdirSync(absP('./contracts-optimized'))
// For each version
for (let i = 0; i < versionDirs.length; ++i) {
const version = versionDirs[i]
// For each ABI
const abis = fs.readdirSync(absP(`./contracts-optimized/${version}`))
for (const abi of abis) {
const abiJson = JSON.parse(fs.readFileSync(absP(`./contracts-optimized/${version}/${abi}`), 'utf-8'))
fs.writeFileSync(
absP(`./contracts-optimized/${version}/${abi}`),
JSON.stringify(abiJson)
)
}
}
}
function cli () {
/* eslint no-unused-expressions: "off" */
yargs
.command('init', 'Initialize the contracts-optimized directory.', yargs => yargs, initDirectory)
.command('no-duplicates', 'Remove all duplicate ABIs.', yargs => yargs, noDuplicates)
.command('no-bytecode', 'Remove all bytecode from the ABIs.', yargs => yargs, noBytecode)
.command('no-whitespace', 'Remove all whitespace from the ABIs.', yargs => yargs, noWhitespace)
.command('$0', 'Remove duplicates, bytecode, and whitespace from the ABIs', yargs => yargs, async () => {
initDirectory()
await noDuplicates()
await noBytecode()
await noWhitespace()
})
.showHelpOnFail(false)
.completion()
.wrap(120)
.strict()
.help().argv
}
if (require.main === module) {
cli()
} else {
module.exports = {
initDirectory,
noDuplicates,
noBytecode,
noWhitespace
}
}