This repository has been archived by the owner on Jun 14, 2018. It is now read-only.
/
contracts.js
276 lines (239 loc) · 8.79 KB
/
contracts.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
var assert = require("chai").assert;
var Artifactor = require("../");
var contract = require("truffle-contract");
var Schema = require("truffle-contract-schema");
var temp = require("temp").track();
var path = require("path");
var solc = require("solc");
var fs = require("fs");
var requireNoCache = require("require-nocache")(module);
var TestRPC = require("ganache-cli");
var Web3 = require("web3");
describe("artifactor + require", function() {
var Example;
var accounts;
var abi;
var binary;
var network_id;
var artifactor;
var provider = TestRPC.provider();
var web3 = new Web3();
web3.setProvider(provider)
before(function(done) {
web3.version.getNetwork(function(err, id) {
if (err) return done(err);
network_id = id;
done();
});
});
before(function(done) {
this.timeout(10000);
// Compile first
var result = solc.compile(fs.readFileSync("./test/Example.sol", {encoding: "utf8"}), 1);
// Clean up after solidity. Only remove solidity's listener,
// which happens to be the first.
process.removeListener("uncaughtException", process.listeners("uncaughtException")[0]);
var compiled = Schema.normalize(result.contracts["Example"]);
abi = compiled.abi;
binary = compiled.bytecode;
// Setup
var dirPath = temp.mkdirSync({
dir: path.resolve("./"),
prefix: 'tmp-test-contract-'
});
var expected_filepath = path.join(dirPath, "Example.json");
artifactor = new Artifactor(dirPath);
artifactor.save({
contractName: "Example",
abi: abi,
binary: binary,
address: "0xe6e1652a0397e078f434d6dda181b218cfd42e01",
network_id: network_id
}).then(function() {
var json = requireNoCache(expected_filepath);
Example = contract(json);
Example.setProvider(provider);
}).then(done).catch(done);
});
before(function(done) {
web3.eth.getAccounts(function(err, accs) {
accounts = accs;
Example.defaults({
from: accounts[0]
});
done(err);
});
});
after(function(done) {
temp.cleanupSync();
done();
});
it("should set the transaction hash of contract instantiation", function() {
return Example.new(1, {gas: 3141592}).then(function(example) {
assert(example.transactionHash, "transactionHash should be non-empty");
});
});
it("should get and set values via methods and get values via .call", function(done) {
var example;
Example.new(1, {gas: 3141592}).then(function(instance) {
example = instance;
return example.value.call();
}).then(function(value) {
assert.equal(value.valueOf(), 1, "Starting value should be 1");
return example.setValue(5);
}).then(function(tx) {
return example.value.call();
}).then(function(value) {
assert.equal(value.valueOf(), 5, "Ending value should be five");
}).then(done).catch(done);
});
it("shouldn't synchronize constant functions", function(done) {
var example;
Example.new(5, {gas: 3141592}).then(function(instance) {
example = instance;
return example.getValue();
}).then(function(value) {
assert.equal(value.valueOf(), 5, "Value should have been retrieved without explicitly calling .call()");
}).then(done).catch(done);
});
it("should allow BigNumbers as input parameters, and not confuse them as transaction objects", function(done) {
// BigNumber passed on new()
var example = null;
Example.new(web3.toBigNumber(30), {gas: 3141592}).then(function(instance) {
example = instance;
return example.value.call();
}).then(function(value) {
assert.equal(value.valueOf(), 30, "Starting value should be 30");
// BigNumber passed in a transaction.
return example.setValue(web3.toBigNumber(25), {gas: 3141592});
}).then(function(tx) {
return example.value.call();
}).then(function(value) {
assert.equal(value.valueOf(), 25, "Ending value should be twenty-five");
// BigNumber passed in a call.
return example.parrot.call(web3.toBigNumber(865));
}).then(function(parrot_value) {
assert.equal(parrot_value.valueOf(), 865, "Parrotted value should equal 865")
}).then(done).catch(done);
});
it("should return transaction hash, logs and receipt when using synchronised transactions", function(done) {
var example = null;
Example.new(1, {gas: 3141592}).then(function(instance) {
example = instance;
return example.triggerEvent();
}).then(function(result) {
assert.isDefined(result.tx, "transaction hash wasn't returned");
assert.isDefined(result.logs, "synchronized transaction didn't return any logs");
assert.isDefined(result.receipt, "synchronized transaction didn't return a receipt");
assert.isOk(result.tx.length > 42, "Unexpected transaction hash"); // There has to be a better way to do this.
assert.equal(result.tx, result.receipt.transactionHash, "Transaction had different hash than receipt");
assert.equal(result.logs.length, 1, "logs array expected to be 1");
var log = result.logs[0];
assert.equal("ExampleEvent", log.event);
assert.equal(accounts[0], log.args._from);
assert.equal(8, log.args.num); // 8 is a magic number inside Example.sol
}).then(done).catch(done);
});
it("should trigger the fallback function when calling sendTransaction()", function() {
var example = null;
return Example.new(1, {gas: 3141592}).then(function(instance) {
example = instance;
return example.fallbackTriggered();
}).then(function(triggered) {
assert(triggered == false, "Fallback should not have been triggered yet");
return example.sendTransaction({
value: web3.toWei(1, "ether")
});
}).then(function(results) {
return new Promise(function(accept, reject) {
return web3.eth.getBalance(example.address, function(err, balance) {
if (err) return reject(err);
accept(balance);
});
});
}).then(function(balance) {
assert(balance == web3.toWei(1, "ether"));
});
});
it("should trigger the fallback function when calling send() (shorthand notation)", function() {
var example = null;
return Example.new(1, {gas: 3141592}).then(function(instance) {
example = instance;
return example.fallbackTriggered();
}).then(function(triggered) {
assert(triggered == false, "Fallback should not have been triggered yet");
return example.send(web3.toWei(1, "ether"));
}).then(function(results) {
return new Promise(function(accept, reject) {
return web3.eth.getBalance(example.address, function(err, balance) {
if (err) return reject(err);
accept(balance);
});
});
}).then(function(balance) {
assert(balance == web3.toWei(1, "ether"));
});
});
it("errors when setting an invalid provider", function(done) {
try {
Example.setProvider(null);
assert.fail("setProvider() should have thrown an error");
} catch (e) {
// Do nothing with the error.
}
done();
});
it("creates a network object when an address is set if no network specified", function(done) {
var NewExample = contract({
abi: abi,
unlinked_binary: binary
});
NewExample.setProvider(provider);
NewExample.defaults({
from: accounts[0]
});
assert.equal(NewExample.network_id, null);
NewExample.new(1, {gas: 3141592}).then(function(instance) {
// We have a network id in this case, with new(), since it was detected,
// but no further configuration.
assert.equal(NewExample.network_id, network_id);
assert.equal(NewExample.toJSON().networks[network_id], null);
NewExample.address = instance.address;
assert.equal(NewExample.toJSON().networks[network_id].address, instance.address);
done();
}).catch(done);
});
it("doesn't error when calling .links() or .events() with no network configuration", function(done) {
var event_abi = {
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "nameHash",
"type": "bytes32"
},
{
"indexed": true,
"name": "releaseHash",
"type": "bytes32"
}
],
"name": "PackageRelease",
"type": "event"
};
var MyContract = contract({
contractName: "MyContract",
abi: [
event_abi
],
binary: "0x12345678",
});
MyContract.setNetwork(5);
var expected_event_topic = web3.sha3("PackageRelease(bytes32,bytes32)");
// We want to make sure these don't throw when a network configuration doesn't exist.
// While we're at it, lets make sure we still get the event we expect.
assert.deepEqual(MyContract.links, {});
assert.deepEqual(MyContract.events[expected_event_topic], event_abi);
done();
});
});