-
Notifications
You must be signed in to change notification settings - Fork 6
/
deploy.js
193 lines (182 loc) · 6.79 KB
/
deploy.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Deploy script used for testing
const Web3 = require('web3')
const Ganache = require('ganache-core')
const provider = Ganache.provider({
"gasLimit": 7000000,
"locked" : false
})
const web3 = new Web3(provider)
const TruffleContract = require('truffle-contract')
const getArtifact = name => {
const contract = TruffleContract(require(`./lib/build/contracts/${name}.json`))
contract.setProvider(provider)
contract.detectNetwork()
return contract
}
const getAccounts = web3 => {
return new Promise((resolve, reject) => {
web3.eth.getAccounts((err, accounts) => {
if (!err) resolve(accounts)
else reject(err)
})
})
}
async function main() {
// const BaseScheduler = getArtifact('BaseScheduler')
const BlockScheduler = getArtifact('BlockScheduler')
const ClaimLib = getArtifact('ClaimLib')
const ExecutionLib = getArtifact('ExecutionLib')
const IterTools = getArtifact('IterTools')
const MathLib = getArtifact('MathLib')
const PaymentLib = getArtifact('PaymentLib')
const RequestFactory = getArtifact('RequestFactory')
const RequestLib = getArtifact('RequestLib')
const RequestMetaLib = getArtifact('RequestMetaLib')
const RequestScheduleLib = getArtifact('RequestScheduleLib')
const SafeMath = getArtifact('SafeMath')
const TimestampScheduler = getArtifact('TimestampScheduler')
const TransactionRequestCore= getArtifact('TransactionRequestCore')
let blockScheduler,
claimLib,
executionLib,
iterTools,
mathLib,
paymentLib,
requestFactory,
requestLib,
requestMetaLib,
requestScheduleLib,
safeMath,
timestampScheduler,
transactionRequestCore
return new Promise((resolve, reject) => {
let ___
getAccounts(web3)
.then(accounts => {
web3.eth.defaultAccount = accounts[0]
___ = {from: web3.eth.defaultAccount, gas: 7000000}
return ExecutionLib.new(___)
})
.then(instance => {
executionLib = instance
return IterTools.new(___)
})
.then(instance => {
iterTools = instance
return MathLib.new(___)
})
.then(instance => {
mathLib = instance
return RequestMetaLib.new(___)
})
.then(instance => {
requestMetaLib = instance
return SafeMath.new(___)
})
.then(instance => {
safeMath = instance
return ClaimLib.new(___)
})
.then(instance => {
claimLib = instance
return PaymentLib.new(___)
})
.then(instance => {
paymentLib = instance
return RequestScheduleLib.new(___)
})
.then(instance => {
requestScheduleLib = instance
linkLibrary(RequestLib, mathLib)
linkLibrary(RequestLib, paymentLib)
linkLibrary(RequestLib, requestScheduleLib)
return RequestLib.new(___)
})
.then(instance => {
requestLib = instance
linkLibrary(TransactionRequestCore, claimLib)
linkLibrary(TransactionRequestCore, executionLib)
linkLibrary(TransactionRequestCore, mathLib)
linkLibrary(TransactionRequestCore, paymentLib)
linkLibrary(TransactionRequestCore, requestMetaLib)
linkLibrary(TransactionRequestCore, requestLib)
linkLibrary(TransactionRequestCore, requestScheduleLib)
linkLibrary(TransactionRequestCore, safeMath)
return TransactionRequestCore.new(___)
})
.then(instance => {
transactionRequestCore = instance
linkLibrary(RequestFactory, claimLib)
linkLibrary(RequestFactory, mathLib)
linkLibrary(RequestFactory, requestScheduleLib)
linkLibrary(RequestFactory, iterTools)
linkLibrary(RequestFactory, paymentLib)
linkLibrary(RequestFactory, requestLib)
return RequestFactory.new(
transactionRequestCore.address,
___
)
})
.then(instance => {
requestFactory = instance
linkLibrary(BlockScheduler, paymentLib)
linkLibrary(BlockScheduler, requestScheduleLib)
linkLibrary(BlockScheduler, requestLib)
linkLibrary(BlockScheduler, mathLib)
return BlockScheduler.new(requestFactory.address, web3.eth.defaultAccount, ___)
})
.then(instance => {
blockScheduler = instance
linkLibrary(TimestampScheduler, paymentLib)
linkLibrary(TimestampScheduler, requestScheduleLib)
linkLibrary(TimestampScheduler, requestLib)
linkLibrary(TimestampScheduler, mathLib)
return TimestampScheduler.new(requestFactory.address, web3.eth.defaultAccount, ___)
})
.then(instance => {
timestampScheduler = instance
return Promise.resolve()
})
.then(___ => {
const contracts = {
requestFactory: requestFactory.address,
blockScheduler: blockScheduler.address,
timestampScheduler: timestampScheduler.address
}
const fs = require('fs')
fs.writeFileSync('./lib/assets/tester.json', JSON.stringify(contracts))
resolve({
// Ganache attached web3
web3: web3,
// Truffle contracts with methods attached
requestFactory: requestFactory,
blockScheduler: blockScheduler,
timestampScheduler: timestampScheduler
})
})
.catch(err => reject(err))
})
}
module.exports = main
main()
// Some Utils that can be abstracted to another module later
const linkLibrary = (contract, lib) => {
const bytecode = contract.bytecode
const libAddr = lib.address.slice(2) // takes off the '0x'
const libName = lib.constructor._json.contractName
const stringToReplace = `__${libName}`.padEnd(40, '_')
assert(libAddr.length === stringToReplace.length, 'Search string must match the length of library address.')
const newBytecode = replaceBytecode(bytecode, stringToReplace, libAddr)
contract.bytecode = newBytecode
return contract
}
const replaceBytecode = (bytecode, stringToReplace, newString) => {
if (bytecode.indexOf(stringToReplace) != -1) {
bytecode = bytecode.replace(stringToReplace, newString)
return replaceBytecode(bytecode, stringToReplace, newString)
}
return bytecode
}
const assert = (condition, errMsg) => {
if (!condition) throw new Error(errMsg)
}