Repository navigation
Expand file tree
/
Copy pathmainContract.js
More file actions
512 lines (441 loc) · 16.3 KB
/
Copy pathmainContract.js
File metadata and controls
512 lines (441 loc) · 16.3 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/**
*
* iZ³ | IZZZIO blockchain - https://izzz.io
*
* iZ³ BigNet Master contract
* Provides main token and platform functionality
* Process payable transaction
* Process resources rental
* Backend for C2C ordering
*
*/
/**
* Token emission amount
* @type {number}
*/
const EMISSION = 9999999999;
/**
* Token full name
* @type {string}
*/
const TOKEN_NAME = 'IZZZIO main token';
/**
* Token ticker
* @type {string}
*/
const TICKER = 'iZ3';
/**
* Address of main contract owner
* @type {string}
*/
const CONTRACT_OWNER = '-----BEGIN RSA PUBLIC KEY-----\n' +
'MIIBCgKCAQEApSJ2Lm6h26vHgiqB4VcyOZE+meRB6Jaow6Z+6cBn43fvcM57l8O2DfFTgo9R\n' +
'4AUavuFJU8bekhcCWYC53RErumjHBrWVviGDOxRALfev8fOU6V+hm9E7FGiW5RXMew5729lt\n' +
'rOxsyrayUtBLsd6BAEO5n/AtAI08Et403X/UX/7N/9zKu+F2E/fi1VlvJS07TtgPoRuT9vx6\n' +
'ol7B0OcqGU0lIe84TasfX4pN9RIZe3+O8idBTg9aHbtuD2qSSQ9x1jpcD4wOtb+FhgBJ3dOI\n' +
'eIC3eapWvK4HFtAtX1uIyod3LruSVssrNtLEqWOgD5MwOlv1KWAR0ZDZ3cYNL8Of8QIDAQAB\n' +
'-----END RSA PUBLIC KEY-----\n';
//Real net config
//const CONTRACT_OWNER = 'izM1Tr1nhKaeDMqUaZjHqaWzjZmCndnUhML';
/**
* Minimal zero-cost resources
* @type {{timeLimit: number, callLimit: number, ram: number}}
*/
const MINIMAL_RESOURCES = {
ram: 8, //MB
timeLimit: 500, //Milliseconds
callLimit: 1, //per minute
};
/**
* Maximum possible resources
* @type {number}
*/
const MAX_RESOURCES_COST = 120; //Tokens
/**
* Default resources price
* @type {{timeLimit: number, callLimit: number, ram: number}}
*/
const RESOURCES_PRICE = {
ram: 2,
timeLimit: 100,
callLimit: 1
};
/**
* C2C Fee
* @type {number}
*/
const C2C_FEE = 0.001;
/**
* C2C Fee transfer address
* @type {string}
*/
const FEE_ADDRESS = CONTRACT_OWNER;
/**
* Max size of adding contract
* @type {number}
*/
const MAX_CONTRACT_LENGTH = 10 * 1024 * 1024;
/**
* Main IZZZIO token contract
*/
class mainToken extends TokenContract {
/**
* Contract info
* @return {{owner: string, ticker: string, name: string}}
*/
get contract() {
return {
name: TOKEN_NAME,
ticker: TICKER,
owner: CONTRACT_OWNER,
emission: EMISSION,
c2cFee: C2C_FEE,
type: 'token',
};
}
/**
* Initialization and emission
*/
init() {
super.init(EMISSION);
/**
* Resource rents info
* @type {KeyValue}
*/
this._resourceRents = new KeyValue('resourceRents');
this._c2cOrders = new BlockchainMap('c2cOrders');
this._resourcePrice = new BlockchainMap('resourcePrice');
this._ResourcesCostChange = new Event('ResourcesCostChange', 'string', 'string');
this._maxContractLength = new KeyValue('maxContractLength');
this._MaxContractLengthChange = new Event('MaxContractLengthChange', 'string', 'number');
if (contracts.isDeploy()) {
this._resourcePrice['ram'] = RESOURCES_PRICE.ram;
this._resourcePrice['callLimit'] = RESOURCES_PRICE.callLimit;
this._resourcePrice['timeLimit'] = RESOURCES_PRICE.timeLimit;
this._ResourcesCostChange.emit('initial',this.getCurrentResources());
this._maxContractLength.put('maxContractLength', MAX_CONTRACT_LENGTH);
this._MaxContractLengthChange.emit('initial', this.getCurrentMaxContractLength());
}
}
/**
* Used whe payable method is called from the other contract
* @param {string} contractAddress contract whose method is called
* @param {number} txValue sending amounts
* @param {string} methodName
* @param {array} args method arguments
*/
processPayableTransaction(contractAddress, txValue, methodName, args) {
const state = global.getState();
assert.false(contractAddress === state.contractAddress, 'You can\'t call payment method in token contract');
contractAddress = String(contractAddress);
txValue = String(txValue);
const oldBalance = this.balanceOf(contractAddress);
this._sendToContract(contractAddress, txValue);
global.contracts.callDelayedMethodDeploy(contractAddress, methodName, args, {
type: 'pay',
amount: txValue,
balance: this.balanceOf(contractAddress),
oldBalance: oldBalance,
ticker: this.contract.ticker,
contractName: this.contract.name
});
}
/**
* Private method, used for sending ZZZ tokens
* @param {string} contractAddress contract whose method is called
* @param {number} txValue sending amounts
*/
_sendToContract(contractAddress, txValue) {
assert.true(this.checkContractAddress(contractAddress), 'Invalid address');
this.transfer(contractAddress, txValue);
}
/**
* Checks address type actuality
* @param {string} address contract address
*/
checkContractAddress(address) {
let addr = parseFloat(address);
return !isNaN(addr) && isFinite(address) && addr % 1 === 0;
}
/**
* Creates C2C order
* @param {string} sellerAddress
* @param {*} args
* @return {*}
*/
processC2CBuyRequest(sellerAddress, args) {
assert.true(this.checkContractAddress(sellerAddress), 'Invalid address');
assert.true(contracts.isChild(), 'This method can be called only from other contract');
const addressFrom = contracts.caller();
sellerAddress = String(sellerAddress);
const price = new BigNumber(global.contracts.callMethodDeploy(sellerAddress, 'getPrice', [args]));
assert.false((new BigNumber(this.balanceOf(addressFrom))).lt(price), 'Insufficient funds for contract buy');
const orderId = this._generateOrderId(sellerAddress, addressFrom, args);
assert.false(this._c2cOrders[orderId], 'You already have same order');
this._c2cOrders[orderId] = {
buyerAddress: addressFrom,
args: args,
price: price.toFixed(),
result: false,
};
contracts.callDelayedMethodDeploy(sellerAddress, 'processC2COrder', [addressFrom, orderId, args]);
return orderId;
}
/**
* Process C2C order
* @param {string} orderId
* @param {*} resultData
*/
processC2CBuyResponse(orderId, resultData) {
assert.true(contracts.isChild(), 'This method can be called only from other contract');
assert.true(this._c2cOrders[orderId] !== null && this._c2cOrders[orderId].result === false, 'Order not found or already finished');
const order = this._c2cOrders[orderId];
order.buyerAddress = String(order.buyerAddress);
const sellerAddress = String(contracts.caller());
assert.true(this._generateOrderId(sellerAddress, order.buyerAddress, order.args) === orderId, "Order id validity checking error");
const price = new BigNumber(order.price);
assert.false((new BigNumber(this.balanceOf(order.buyerAddress))).lt(price), 'Insufficient funds for contract buy');
//Saving result
order.result = resultData;
this._c2cOrders[orderId] = order;
//Take price
this._transferFromTo(order.buyerAddress, sellerAddress, price.toFixed());
//Take comission
const fee = price.times(C2C_FEE);
this._transferFromTo(sellerAddress, FEE_ADDRESS, fee.toFixed());
contracts.callDelayedMethodDeploy(order.buyerAddress, 'processC2COrderResult', [resultData, orderId, sellerAddress]);
}
/**
* Get order result
* @param {string} orderId
* @return {*}
*/
getC2CBuyResult(orderId) {
assert.true(contracts.isChild(), 'This method can be called only from other contract');
assert.true(this._c2cOrders[orderId] !== null, 'Order not found or already finished');
assert.true(this._c2cOrders[orderId].result !== false, 'Order not ready yet');
const order = this._c2cOrders[orderId];
order.buyerAddress = String(order.buyerAddress);
assert.true(order.buyerAddress === String(contracts.caller()), 'Access denied for this orderId');
return JSON.stringify(order.result);
}
/**
* Override transfer method
* @param from
* @param to
* @param amount
* @private
*/
_transferFromTo(from, to, amount) {
this._wallets.transfer(from, to, amount);
this._TransferEvent.emit(from, to, new BigNumber(amount));
}
/**
* Generate order Id by params
* @param seller
* @param buyer
* @param args
* @return {*}
* @private
*/
_generateOrderId(seller, buyer, args) {
return crypto.hash(seller + '_' + buyer + '_' + args.toString());
}
/**
* Process new contract deployment
*/
processDeploy() {
const state = global.getState();
const contractAddress = state.contractAddress;
const sender = state.deployState.from;
const resourceRent = state.deployState.resourceRent;
assert.true(Number(resourceRent) >= 0, 'Good plan');
assert.true(Number(resourceRent) <= MAX_RESOURCES_COST, 'You can\'t rent more than possible for ' + MAX_RESOURCES_COST + ' tokens');
if(Number(resourceRent) !== 0) {
//Transfer rent payment for system account
this._wallets.transfer(sender, CONTRACT_OWNER, resourceRent);
}
//Saving rent information
this._resourceRents.put(String(contractAddress), resourceRent);
}
/**
* Returns calculated resources
* @param {string} amount
* @return {{callLimit: number, timeLimit: number, ram: number}}
*/
calculateResources(amount) {
amount = Math.abs(Number(amount));
if(amount > MAX_RESOURCES_COST) {
amount = MAX_RESOURCES_COST;
}
let ram = this._resourcePrice['ram'] * amount;
let timeLimit = this._resourcePrice['timeLimit'] * amount;
let callLimit = this._resourcePrice['callLimit'] * amount;
ram = (ram < MINIMAL_RESOURCES.ram) ? MINIMAL_RESOURCES.ram : ram;
timeLimit = (timeLimit < MINIMAL_RESOURCES.timeLimit) ? MINIMAL_RESOURCES.timeLimit : timeLimit;
callLimit = (callLimit < MINIMAL_RESOURCES.callLimit) ? MINIMAL_RESOURCES.callLimit : callLimit;
return {ram: Math.round(ram), timeLimit: Math.round(timeLimit), callLimit: Math.round(callLimit)};
}
/**
* Returns calculated resources as JSON
* @param {string} amount
* @return JSON {{callLimit: number, timeLimit: number, ram: number}}
*/
getCalculatedResources(amount) {
return JSON.stringify(this.calculateResources(amount));
}
/**
* Returns calculated contracts limits
* @param address
*/
checkContractLimits(address) {
let resourcesAmount = this._resourceRents.get(String(address));
if(!resourcesAmount) {
return false;
}
return JSON.stringify(this.calculateResources(resourcesAmount));
}
/**
* accepting new resources cost after voting
* @param amount
*/
_acceptNewResources(newCost) {
this._resourcePrice['ram'] = newCost.ram;
this._resourcePrice['timeLimit'] = newCost.timeLimit;
this._resourcePrice['callLimit'] = newCost.callLimit;
}
/**
* accepting new contract size after voting
* @param newValue
*/
_acceptNewMaxContractLength(newValue) {
this._maxContractLength.put('maxContractLength', +newValue);
}
/**
* get results og voting contract by its address
* @param voteContractAddress
* @returns {any}
* @private
*/
_getResultsOfVoting(voteContractAddress) {
return JSON.parse(contracts.callMethodDeploy(voteContractAddress, 'getResultsOfVoting',[]));
}
/**
* Process results of change contract cost voting
* @param voteContractAddress
* @returns {number} result of processing: 0 - voting isn't started, 1 - voting hasn't been ended yet, 2 - old variant of cost wins, 3 - new variant of cost wins and accepted, 4 - old var of max contract size wins, 5 new var of max contract size wins and accepted
*/
processResults(voteContractAddress) {
const voteResults = this._getResultsOfVoting(voteContractAddress);
switch (voteResults.state) {
case 'waiting':
return 0;
break;
case 'started':
return 1;
break;
case 'ended':
let winner = this._findMaxVariantIndex(voteResults.results);
if (typeof JSON.parse(winner.index) === 'object'){
// if wins the same variant as we have now then do nothing
const curResourses = this.getCurrentResources();
if (winner.index === curResourses) {
this._ResourcesCostChange.emit('not change by voting',curResourses);
return 2;
} else {
this._acceptNewResources(JSON.parse(winner.index));
this._ResourcesCostChange.emit('change by voting', curResourses);
return 3;
}
} else {
const curMaxLen = this.getCurrentMaxContractLength();
if ( +winner.index === curMaxLen) {
this._MaxContractLengthChange.emit('not change by voting', curMaxLen);
return 4;
} else {
this._acceptNewMaxContractLength(JSON.parse(winner.index));
this._MaxContractLengthChange.emit('change by voting', curMaxLen);
return 5;
}
}
}
}
/**
* find max element and returns winner's index and value
* @param map
* @returns {{index: string, value: number}}
* @private
*/
_findMaxVariantIndex(map) {
let max = {
index:'',
value: -1,
};
for (let ind in map) {
if(map.hasOwnProperty(ind)) {
if (max.value <= map[ind]) {
max.index = ind;
max.value = map[ind];
}
}
}
return max;
}
/**
* get current resources
* @returns {string}
*/
getCurrentResources() {
return JSON.stringify({
ram: this._resourcePrice['ram'],
timeLimit: this._resourcePrice['timeLimit'],
callLimit: this._resourcePrice['callLimit']
});
}
/**
* get current max contract size
*/
getCurrentMaxContractLength() {
return Number(this._maxContractLength.get('maxContractLength'));
}
/**
* returns resources as string
* @param obj
* @returns {string}
*/
resourcesObjectToString(obj) {
return `ram:${obj.ram}, time limit:${obj.timeLimit}, call limit:${obj.callLimit}`
}
/**
* starts voting contract by address to decide if we need new resouces price or not
* @param voteContractAddress address of voting contract
* @param newVariant multiplier for new resources cost (new = old * multiplier)
*/
startVotingForChangeResourcesPrice(voteContractAddress, newVariant) {
let newCost = JSON.stringify(this.calculateResources(newVariant));
let oldCost = this.getCurrentResources();
contracts.callMethodDeploy(voteContractAddress, 'startVoting',[newCost, oldCost]);
return JSON.stringify([newCost, oldCost]);
}
/**
* starts voting contract by address to decide if we need newMaxContractLength or not
* @param voteContractAddress address of voting contract
* @param newVariant multiplier for new resources cost (new = old * multiplier)
*/
startVotingForChangeMaxContractLength(voteContractAddress, newVariant) {
let newVal = newVariant;
let oldVal = this.getCurrentMaxContractLength();
contracts.callMethodDeploy(voteContractAddress, 'startVoting',[newVal, oldVal]);
return JSON.stringify([newVal, oldVal]);
}
/**
* TODO: Remove in release
*/
test() {
console.log('TEST CALL');
console.log(contracts.callMethodDeploy('5', 'balanceOf', [CONTRACT_OWNER]));
console.log(contracts.callMethodDeploy('5', 'checkContractLimits', ['6']));
console.log('RESULT GRANTED');
}
}
global.registerContract(mainToken);