-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathContractLoader.js
140 lines (129 loc) · 3.95 KB
/
ContractLoader.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
const fs = require(`fs`)
const AWS = require(`aws-sdk`)
const config = require(`config`)
const s3 = new AWS.S3()
const Contract = require(`./Contract`)
const IpfsAPI = require(`ipfs-api`)
const Web3 = require(`web3`)
const web3 = {
http: new Web3(new Web3.providers.HttpProvider(config.get(`ethereum.web3HttpUrl`))),
websocket: new Web3(new Web3.providers.WebsocketProvider(config.get(`ethereum.web3WebsocketUrl`)))
}
let load
switch (config.get(`contractSource.type`)) {
case `ipfs`:
load = loadFromIpfs
break
case `s3`:
load = loadFromS3
break
default:
load = loadFromFilesystem
}
exports.loadContracts = async () => {
const contractJsons = await load()
const contracts = []
let onlyLoadContracts
if (config.has(`contractSource.contracts`)) {
onlyLoadContracts = config.get(`contractSource.contracts`)
}
contractJsons.forEach((contractJson) => {
if (Contract.getAddressFromJson(contractJson) != null &&
Contract.getTransactionHashFromJson(contractJson) != null) {
const contract = new Contract(web3, contractJson)
if (contract.hasEvents() && (!onlyLoadContracts || onlyLoadContracts.includes(contract.name))) {
contracts.push(contract)
console.log(`Loaded ${contract.name}`)
}
}
})
if (contracts.length === 0) {
throw new Error(`Found no contracts to watch`)
}
return contracts
}
async function loadFromIpfs () {
const directoryHash = config.get(`contractSource.directoryHash`)
const ipfs = new IpfsAPI({
host: config.get(`contractSource.host`),
port: config.get(`contractSource.port`),
protocol: config.get(`contractSource.protocol`)
})
return new Promise((resolve, reject) => {
const contractJsons = []
ipfs.get(directoryHash, (err, files) => {
if (err) {
return reject(err)
}
files.forEach((file) => {
if (file.content) {
contractJsons.push(JSON.parse(String(file.content)))
}
})
return resolve(contractJsons)
})
})
}
function loadFromFilesystem () {
const localDirectory = config.get(`contractSource.directory`)
const contractJsons = []
fs.readdirSync(localDirectory)
.forEach(fileName => contractJsons.push(JSON.parse(fs.readFileSync(`${localDirectory}/${fileName}`)
.toString())))
if (contractJsons.length === 0) {
throw new Error(`Found no contracts to load`)
}
return contractJsons
}
async function loadFromS3 () {
const accessKeyId = config.get(`aws.accessKeyId`)
const secretAccessKey = config.get(`aws.secretAccessKey`)
const region = config.get(`aws.region`)
AWS.config.update(
{
accessKeyId,
secretAccessKey,
region
}
)
const bucketName = config.get(`contractSource.bucketName`)
const keyPrefix = config.get(`contractSource.keyPrefix`)
const params = {
Bucket: bucketName,
Prefix: keyPrefix
}
return new Promise((resolve, reject) => {
s3.listObjects(params, async (err, data) => {
if (err) {
reject(err)
} else if (data.Contents && data.Contents.length > 0) {
const keys = data.Contents.map(item => item.Key)
const promises = []
keys.forEach(key => promises.push(getContractFromS3(bucketName, key)))
const contractObjects = await Promise.all(promises)
const contractJsons = []
contractObjects.forEach(contractObject => contractJsons.push(JSON.parse(contractObject.Body.toString())))
resolve(contractJsons)
} else {
reject(new Error(`Found no contracts to load from bucket ${bucketName}, prefix ${keyPrefix}`))
}
})
})
}
async function getContractFromS3 (bucketName, key) {
const params = {
Bucket: bucketName,
Key: key
}
return new Promise((resolve, reject) => {
s3.getObject(params, (err, data) => {
if (err) {
reject(err)
} else if (data) {
resolve(data)
} else {
reject(new Error(`Could not retrieve contract ${key} from bucket ${bucketName}`))
}
})
})
}