-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
60 lines (51 loc) · 1.6 KB
/
index.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
const fs = require('fs')
const chaintags = {
testnet: ['0x27'],
mainnet: ['0x4a', '0xc7'],
solo: ['0xa4']
}
module.exports = class ContractImportBuilder {
constructor() {
this.importOutput = ''
this.contractImport = {
VERSION: '1.0.0'
}
}
setOutput(output) {
this.importOutput = output
}
addContract(name, abi, address, chain) {
let addr = {}
if (this.importOutput.length < 5) {
throw new Error(
'A contract import must be set before using connex contract entity builder'
)
}
fs.exists(this.importOutput, exists => {
if (exists) {
// const existing = fs.readFileSync(this.importOutput)
const contractImportExisting = require(this.importOutput)
// get any existing addresses for the current selected chain
if (contractImportExisting && contractImportExisting[name]) {
addr = contractImportExisting[name].address
}
}
chaintags[chain].forEach(element => {
addr[element] = address
})
this.contractImport[name] = {
raw: {
abi: abi._jsonInterface
},
address: {
...addr
}
}
const output = JSON.stringify(this.contractImport);
const jsOutput = `module.exports = ${output}`;
if (this.onWrite) {
this.onWrite(jsOutput)
}
})
}
}