forked from austintgriffith/clevis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclevis.js
249 lines (226 loc) · 9.34 KB
/
clevis.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const clevis = require("clevis")
const colors = require('colors')
const chai = require("chai")
const assert = chai.assert
const expect = chai.expect;
const should = chai.should();
const fs = require('fs')
const Web3 = require('web3')
const clevisConfig = JSON.parse(fs.readFileSync("clevis.json").toString().trim())
web3 = new Web3(new Web3.providers.HttpProvider(clevisConfig.provider))
function localContractAddress(contract){
return fs.readFileSync(contract+"/"+contract+".address").toString().trim()
}
function localContractAbi(contract){
return JSON.parse(fs.readFileSync(contract+"/"+contract+".abi").toString().trim())
}
function printTxResult(result){
if(!result||!result.transactionHash){
console.log("ERROR".red,"MISSING TX HASH".yellow)
}else{
console.log(tab,result.transactionHash.gray,(""+result.gasUsed).yellow)
}
}
function bigHeader(str){
return "########### "+str+" "+Array(128-str.length).join("#")
}
function rand(min, max) {
return Math.floor( Math.random() * (max - min) + min );
}
function getPaddedHexFromNumber(num,digits){
let hexIs = web3.utils.numberToHex(num).replace("0x","");
while(hexIs.length<digits){
hexIs = "0"+hexIs
}
return hexIs
}
const tab = "\t\t";
module.exports = {
web3:web3,
localContractAddress,localContractAddress,
contracts:fs.readFileSync("contracts.clevis").toString().trim().split("\n"),
reload:()=>{
describe('#reload() ', function() {
it('should force browser to reload', async function() {
fs.writeFileSync("public/reload.txt",Date.now());
});
});
},
version:()=>{
describe('#version() ', function() {
it('should get version', async function() {
this.timeout(90000)
const result = await clevis("version")
console.log(result)
});
});
},
blockNumber:()=>{
describe('#blockNumber() ', function() {
it('should get blockNumber', async function() {
this.timeout(90000)
const result = await clevis("blockNumber")
console.log(result)
});
});
},
compile:(contract)=>{
describe('#compile() '+contract.magenta, function() {
it('should compile '+contract.magenta+' contract to bytecode', async function() {
this.timeout(90000)
const result = await clevis("compile",contract)
console.log(result)
assert(Object.keys(result.contracts).length>0, "No compiled contacts found.")
let count = 0
for(let c in result.contracts){
console.log("\t\t"+"contract "+c.blue+": ",result.contracts[c].bytecode.length)
if(count++==0){
assert(result.contracts[c].bytecode.length > 1, "No bytecode for contract "+c)
}
}
});
});
},
deploy:(contract,accountindex)=>{
describe('#deploy() '+contract.magenta, function() {
it('should deploy '+contract.magenta+' as account '+accountindex, async function() {
this.timeout(360000)
const result = await clevis("deploy",contract,accountindex)
printTxResult(result)
console.log(tab+"Address: "+result.contractAddress.blue)
assert(result.contractAddress)
});
});
},
publish:()=>{
describe('#publish() ', function() {
it('should inject contract address and abi into web app', async function() {
this.timeout(120000)
const fs = require("fs")
if(!fs.existsSync("src")){
fs.mkdirSync("src");
}
if(!fs.existsSync("src/contracts")){
fs.mkdirSync("src/contracts");
}
for(let c in module.exports.contracts){
let thisContract = module.exports.contracts[c]
console.log(tab,thisContract.magenta)
let address = fs.readFileSync(thisContract+"/"+thisContract+".address").toString().trim()
console.log(tab,"ADDRESS:",address.blue)
assert(address,"No Address!?")
fs.writeFileSync("src/contracts/"+thisContract+".address.js","module.exports = \""+address+"\"");
let blockNumber = fs.readFileSync(thisContract+"/"+thisContract+".blockNumber").toString().trim()
console.log(tab,"blockNumber:",blockNumber.blue)
assert(blockNumber,"No blockNumber!?")
fs.writeFileSync("src/contracts/"+thisContract+".blocknumber.js","module.exports = \""+blockNumber+"\"");
let abi = fs.readFileSync(thisContract+"/"+thisContract+".abi").toString().trim()
fs.writeFileSync("src/contracts/"+thisContract+".abi.js","module.exports = "+abi);
let bytecode = fs.readFileSync(thisContract+"/"+thisContract+".bytecode").toString().trim()
fs.writeFileSync("src/contracts/"+thisContract+".bytecode.js","module.exports = \""+bytecode+"\"");
}
fs.writeFileSync("src/contracts/contracts.js","module.exports = "+JSON.stringify(module.exports.contracts));
module.exports.reload()
});
});
},
metamask:()=>{
describe('#transfer() ', function() {
it('should give metamask account some ether or tokens to test', async function() {
this.timeout(600000)
const result = await clevis("sendTo","0.1","0","0x2a906694D15Df38F59e76ED3a5735f8AAbccE9cb")
printTxResult(result)
//here is an example of running a funtion from within this object:
//module.exports.mintTo("Greens",0,"0x2a906694d15df38f59e76ed3a5735f8aabcce9cb",20)
//view more examples here: https://github.com/austintgriffith/galleass/blob/master/tests/galleass.js
});
});
},
broadcast:(accountIndex,outputString)=>{
describe('#broadcast() ', function() {
it('should broadcast a string and generate an event', async function() {
this.timeout(60000)
const result = await clevis("contract","broadcast","Broadcaster",accountIndex,outputString)
printTxResult(result)
let events = await clevis("contract","eventBroadcast","Broadcaster")
let lastEvent = events[events.length-1]
console.log(tab,lastEvent)
assert(lastEvent.returnValues.output == outputString,"output string is incorrect in the event or the event didn't fire")
});
});
},
////----------------------------------------------------------------------------///////////////////
add:(accountIndex)=>{
describe('#add() ', function() {
it('should call forward on proxy contract which should call add on example contract', async function() {
this.timeout(600000)
//var abi = require('ethereumjs-abi')
// need to have the ABI definition in JSON as per specification
//var testAbi = localContractAbi("TEst")
//var encoded = abi.encode(testAbi, "add()", [])
//var encoded = (abi.simpleEncode("balanceOf(address):(uint256)", "0x0000000000000000000000000000000000000000")).toString()
//console.log("Encoded:",encoded)
const result = await clevis("contract","forward","TEst",accountIndex,localContractAddress("Example"),"0","0x4f2be91f")
printTxResult(result)
//here is an example of running a funtion from within this object:
//module.exports.mintTo("Greens",0,"0x2a906694d15df38f59e76ed3a5735f8aabcce9cb",20)
//view more examples here: https://github.com/austintgriffith/galleass/blob/master/tests/galleass.js
});
});
},
////----------------------------------------------------------------------------///////////////////
full:()=>{
describe(bigHeader('COMPILE'), function() {
it('should compile all contracts', async function() {
this.timeout(6000000)
const result = await clevis("test","compile")
assert(result==0,"deploy ERRORS")
});
});
describe(bigHeader('FAST'), function() {
it('should run the fast test (everything after compile)', async function() {
this.timeout(6000000)
const result = await clevis("test","fast")
assert(result==0,"fast ERRORS")
});
});
},
fast:()=>{
describe(bigHeader('DEPLOY'), function() {
it('should deploy all contracts', async function() {
this.timeout(6000000)
const result = await clevis("test","deploy")
assert(result==0,"deploy ERRORS")
});
});
describe(bigHeader('METAMASK'), function() {
it('should deploy all contracts', async function() {
this.timeout(6000000)
const result = await clevis("test","metamask")
assert(result==0,"metamask ERRORS")
});
});
describe(bigHeader('PUBLISH'), function() {
it('should publish all contracts', async function() {
this.timeout(6000000)
const result = await clevis("test","publish")
assert(result==0,"publish ERRORS")
});
});
},
}
checkContractDeployment = async (contract)=>{
const localAddress = localContractAddress(contract)
const address = await clevis("contract","getContract","Example",web3.utils.fromAscii(contract))
console.log(tab,contract.blue+" contract address is "+(localAddress+"").magenta+" deployed as: "+(address+"").magenta)
assert(localAddress==address,contract.red+" isn't deployed correctly!?")
return address
}
/*
makeSureContractHasTokens = async (contract,contractAddress,token)=>{
const TokenBalance = await clevis("contract","balanceOf",token,contractAddress)
console.log(tab,contract.magenta+" has "+TokenBalance+" "+token)
assert(TokenBalance>0,contract.red+" doesn't have any "+token.red)
}
view more examples here: https://github.com/austintgriffith/galleass/blob/master/tests/galleass.js
*/