Last active
June 1, 2018 10:46
-
-
Save porobov/1ec910ea1989b879b0f933eb8fcdd847 to your computer and use it in GitHub Desktop.
Truffle tests for MillionEtherHomepage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract('My15', function(accounts) { | |
// SETTINGS | |
var CHAIN_OF_USERS = true; | |
var BUY_AND_SELL_BLOCKS = true; | |
var PAYOUT = true; | |
var IMAGES = true; | |
var ADMIN_FUNCTIONS = false; | |
var MANY_BLOCKS = false; | |
var EMERGENCY = false; | |
var SELFDESTRUCT = false; | |
var unit = 'finney' | |
var DELAY = 0; //seconds | |
const empty_address = '0x0000000000000000000000000000000000000000'; | |
const owner_account = accounts[0]; | |
const user_2 = accounts[1]; | |
const user_3 = accounts[2]; | |
const user_4 = accounts[3]; | |
const user_5 = accounts[4]; | |
const user_6 = accounts[5]; | |
const user_7 = accounts[6]; | |
const user_8 = accounts[7]; | |
const user_9 = accounts[8]; | |
function sleep(seconds) { | |
var currentTime = new Date().getTime(); | |
while (currentTime + seconds*1000 >= new Date().getTime()) { | |
} | |
} | |
function NewImageEventToConsole (event) { | |
var imageSourceUrl = event.args.imageSourceUrl.toString(); | |
var adUrl = event.args.adUrl.toString(); | |
var adText = event.args.adText.toString(); | |
console.log(" > NewImage: ", | |
event.args.ID.toNumber(), | |
event.args.fromX.toNumber(), | |
event.args.fromY.toNumber(), | |
event.args.toX.toNumber(), | |
event.args.toY.toNumber(), | |
imageSourceUrl.substring(0, 5) + "..." + imageSourceUrl.substring(imageSourceUrl.length-5), | |
adUrl.substring(0, 5) + "..." + adUrl.substring(adUrl.length-5), | |
adText.substring(0, 5) + "..." + adText.substring(adText.length-5)); | |
} | |
function NewAreaStatusToConsole (event) { | |
console.log(" > NewAreaStatus: ", | |
event.args.ID.toNumber(), | |
event.args.fromX.toNumber(), | |
event.args.fromY.toNumber(), | |
event.args.toX.toNumber(), | |
event.args.toY.toNumber(), | |
event.args.price.toNumber()); | |
} | |
function stateInfoToConsole (result) { | |
console.log( | |
"-------stateInfo:----------", | |
"\nnumUsers: ", result[0].toNumber(), | |
"\nblocksSold: ", result[1].toNumber(), | |
"\ntotalWeiInvested: ", web3.fromWei(result[2].toNumber(), unit), | |
"\nnumImages: ", result[3].toNumber(), | |
"\nsetting_imagePlacementPriceInWei: ", web3.fromWei(result[4].toNumber(), unit), | |
"\n---------------------------" | |
); | |
} | |
function userInfoToConsole (result) { | |
console.log( | |
"-------userInfo:----------", | |
"\nreferal: ", result[0].toString(), | |
"\nhandshakes: ", result[1].toNumber(), | |
"\nbalance: ", web3.fromWei(result[2].toNumber(), unit), | |
"\nactivationTime: ", result[3].toNumber(), | |
"\nbanned: ", result[4].toString(), | |
"\nrefunded: ", result[5].toString(), | |
"\ninvestments: ", web3.fromWei(result[6].toNumber(), unit), | |
"\n---------------------------" | |
); | |
} | |
function blockInfoToConsole (result) { | |
console.log("blockInfo:", | |
"\nlandlord: ", result[0].toString(), | |
"\nimageID: ", result[1].toNumber(), | |
"\nsellPrice: ", web3.fromWei(result[2].toNumber(), unit) | |
); | |
} | |
//******** INITIAL SETTINGS | |
/* | |
it("Initial settings should match", function() { | |
var my_15 = My15.deployed(); | |
return my_15.owner.call(). | |
then( | |
function(the_owner) { | |
assert.equal(the_owner, owner_account, "Owner doesn't match"); | |
return my_15.users.call(owner_account); | |
}).then( | |
function(num) { | |
assert.equal(num[1].toNumber(), 0, "Handshakes doesn't match!"); | |
assert.equal(num[0], owner_account, "Owner is not referral!"); | |
console.log("Activation time owner_account: ", num[3].toNumber()); | |
console.log("Handshakes: ", num[1].toNumber()); | |
}); | |
}); | |
*/ | |
/* | |
it("Check for overflow", function(done) { | |
var my_15 = My15.deployed(); | |
return my_15.getAreaPrice.call (1, 1, 100, 70) | |
.then(function(totalPrice){ | |
price_in_current_units = web3.fromWei(totalPrice, unit).toNumber(); | |
console.log(" - checking overflow for price: ", price_in_current_units); | |
assert.equal (price_in_current_units, 1, "wrong price!") | |
done(); | |
}).catch(done); | |
}); | |
*/ | |
// | |
if (BUY_AND_SELL_BLOCKS) { | |
it("Should let buy and sell blocks", function(done) { | |
var my_15 = My15.deployed(); | |
var newAreaStatus = my_15.NewAreaStatus({fromBlock: "latest"}); | |
var initialBalance = web3.eth.getBalance(user_3); | |
var contract_balance = 0; | |
var initialBlocks = 0; | |
var last_event; | |
var gas_cost; | |
//buy 4 blocks | |
return my_15.getStateInfo.call() | |
.then(function(result){ | |
initialBlocks = result[1].toNumber(); | |
}).then(function(){ | |
return my_15.buyBlocks (1,1,1,1,{from: user_3, value: web3.toWei(1, unit), gas: 4712388}) | |
}).then(function(tx_id){ | |
gas_used = web3.eth.getTransactionReceipt(tx_id).gasUsed; | |
var totalPayed = web3.eth.gasPrice.times(gas_used).plus(web3.toWei(1, unit)); | |
var withdrawnFromWallet = initialBalance.minus(web3.eth.getBalance(user_3)); | |
console.log(" - totalPayed: ", web3.fromWei(totalPayed, unit).toString(10)); | |
console.log(" - withdrawnFromWallet: ", web3.fromWei(withdrawnFromWallet, unit).toString(10)); | |
console.log(" - gas_used: ", gas_used); | |
}).then(function(){ | |
return my_15.buyBlocks (100,1,100,1,{from: user_3, value: web3.toWei(1, unit), gas: 4712388}); | |
}).then(function(){ | |
return my_15.buyBlocks (1,100,1,100,{from: user_3, value: web3.toWei(1, unit), gas: 4712388}); | |
}).then(function(){ | |
return my_15.buyBlocks (100,100,100,100,{from: user_3, value: web3.toWei(1, unit), gas: 4712388}); | |
//check events | |
}).then(function(){ | |
newAreaStatus.watch(function(error, result) { | |
// This will catch all Transfer events, regardless of how they originated. | |
if (error) { | |
console.log("This event throws an error! ", result.event); | |
newAreaStatus.stopWatching(); | |
done(error); } | |
NewAreaStatusToConsole(result); | |
newAreaStatus.stopWatching(); | |
}); | |
//check block info | |
}).then(function(){ | |
return my_15.getBlockInfo.call(1,1); | |
}).then(function(blockInfo){ | |
assert.equal(blockInfo[0], user_3, "Should be user_3!"); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(1,100); | |
}).then(function(blockInfo){ | |
assert.equal(blockInfo[0], user_3, "Should be user_3!"); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(100,1); | |
}).then(function(blockInfo){ | |
assert.equal(blockInfo[0], user_3, "Should be user_3!"); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(100,100); | |
}).then(function(blockInfo){ | |
assert.equal(blockInfo[0], user_3, "Should be user_3!"); | |
//check state info | |
}).then(function(){ | |
return my_15.getStateInfo.call(); | |
}).then(function(stateInfo){ | |
assert.equal(stateInfo[1].toNumber() - initialBlocks, 4, "Should be 4 more blocks sold!"); | |
//sell blocks | |
}).then(function(){ | |
return my_15.getUserInfo.call(user_3); | |
}).then(function(userInfo){ | |
initialBalance = web3.toWei(userInfo[2], "wei"); | |
}).then(function(){ | |
return my_15.sellBlocks (1,1,1,1,web3.toWei(2, unit),{from: user_3, gas: 4712388}) | |
}).then(function(){ | |
return my_15.sellBlocks (100,100,100,100,web3.toWei(2, unit),{from: user_3, gas: 4712388}) | |
}).then(function(){ | |
return my_15.buyBlocks (1,1,2,2,{from: user_6, value: web3.toWei(5, unit), gas: 4712388}); | |
}).then(function(){ | |
return my_15.buyBlocks (99,99,100,100,{from: user_6, value: web3.toWei(5, unit), gas: 4712388}); | |
//check ownership | |
}).then(function(){ | |
return my_15.getBlockInfo.call(1,1); | |
}).then(function(blockInfo){ | |
assert.equal(blockInfo[0], user_6, "Should be user_6!"); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(100,100); | |
}).then(function(blockInfo){ | |
assert.equal(blockInfo[0], user_6, "Should be user_6!"); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(2,2); | |
}).then(function(blockInfo){ | |
assert.equal(blockInfo[0], user_6, "Should be user_6!"); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(99,99); | |
}).then(function(blockInfo){ | |
assert.equal(blockInfo[0], user_6, "Should be user_6!"); | |
//check user_3 funds | |
}).then(function(){ | |
return my_15.getUserInfo.call(user_3); | |
}).then(function(userInfo){ | |
assert.equal(web3.fromWei(web3.toWei(userInfo[2], "wei").minus(initialBalance), unit).toNumber(), 4.75, "Should be 4.75 units!"); | |
done(); | |
}).catch(done); | |
}); | |
it("Should fail when one buys land which is not for sale", function(done) { | |
var my_15 = My15.deployed(); | |
var initial_blocks_sold = 0; | |
var initial_contract_balance = web3.eth.getBalance(my_15.address); | |
var throwed = false; | |
return my_15.getStateInfo.call() | |
.then(function(stateInfo){ | |
initial_blocks_sold = stateInfo[1].toNumber(); | |
}).then(function(){ | |
return my_15.buyBlocks (100,1,100,1,{from: user_6, value: web3.toWei(1, unit), gas: 4712388}); | |
}).catch(function(e) { | |
throwed = true; | |
//assert.equal(e.message.substring(43,55) , "invalid JUMP", "Should throw with invalid JUMP!"); | |
}).then(function(){ | |
//assert.equal(throwed, true, "Should raise exception"); | |
}).then(function(){ | |
return my_15.getStateInfo.call(); | |
}).then(function(stateInfo){ | |
assert.equal(stateInfo[1].toNumber() - initial_blocks_sold, 0, "Image ID should not increment!"); | |
assert.equal(initial_contract_balance - web3.eth.getBalance(my_15.address), 0, "Should be no changes in contract balance!"); | |
done(); | |
}).catch(done); | |
}); | |
} | |
/// | |
if (IMAGES) { | |
it("Should let place images", function(done) { | |
var my_15 = My15.deployed(); | |
var new_image = my_15.NewImage({fromBlock: "latest"}); | |
var image_1 = "http://i.imgur.com/3w5dNPn.jpg"; | |
var image_2 = "http://i.imgur.com/dRV0QGo.jpg"; | |
var image_3 = "http://i.imgur.com/r8vTZEc.jpg"; | |
return my_15.buyBlocks (1,10,5,12,{from: user_4, value: web3.toWei(15, unit), gas: 4712388}) | |
.then(function(tx_id){ | |
//console.log("gasUsed: ",(web3.eth.getTransactionReceipt(tx_id).gasUsed)); | |
//Place image 1 | |
}).then(function(){ | |
return my_15.placeImage (5, 12, 5, 12, image_1, 'http://www.россия.рф/', "> Текст на русском <",{from: user_4, gas: 4712388}) | |
}).then(function(tx_id){ | |
//console.log("logs: ",(web3.eth.getTransactionReceipt(tx_id).logs)); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(5,12); | |
}).then(function(blockInfo){ | |
return my_15.getImageInfo.call(blockInfo[1]); | |
}).then(function(imageInfo){ | |
assert.equal(imageInfo[4], image_1, "Should be image_1"); | |
//Place image 2 | |
}).then(function(){ | |
return my_15.placeImage (3, 11, 4, 12, image_2, image_2, image_2,{from: user_4, gas: 4712388}) | |
}).then(function(tx_id){ | |
//console.log("logs: ",(web3.eth.getTransactionReceipt(tx_id).logs)); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(3,11); | |
}).then(function(blockInfo){ | |
return my_15.getImageInfo.call(blockInfo[1]); | |
}).then(function(imageInfo){ | |
assert.equal(imageInfo[4], image_2, "Should be image_2"); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(4,12); | |
}).then(function(blockInfo){ | |
return my_15.getImageInfo.call(blockInfo[1]); | |
}).then(function(imageInfo){ | |
assert.equal(imageInfo[4], image_2, "Should be image_2"); | |
//Place image_3 | |
}).then(function(){ | |
return my_15.sellBlocks (1,10,5,11,web3.toWei(1, unit),{from: user_4, gas: 4712388}) | |
}).then(function(){ | |
return my_15.buyBlocks (1,10,5,11,{from: user_5, value: web3.toWei(10, unit), gas: 4712388}); | |
}).then(function(){ | |
return my_15.placeImage (1,10,3,11, image_3, "http://jinja.pocoo.org/docs/dev/templates/", image_3,{from: user_5, gas: 4712388}) | |
}).then(function(tx_id){ | |
//console.log("logs: ",(web3.eth.getTransactionReceipt(tx_id).logs)); | |
//Final check | |
}).then(function(){ | |
return my_15.getBlockInfo.call(1,10); | |
}).then(function(blockInfo){ | |
return my_15.getImageInfo.call(blockInfo[1]); | |
}).then(function(imageInfo){ | |
assert.equal(imageInfo[4], image_3, "Should be image_3"); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(3,11); | |
}).then(function(blockInfo){ | |
return my_15.getImageInfo.call(blockInfo[1]); | |
}).then(function(imageInfo){ | |
assert.equal(imageInfo[4], image_3, "Should be image_3"); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(4,12); | |
}).then(function(blockInfo){ | |
return my_15.getImageInfo.call(blockInfo[1]); | |
}).then(function(imageInfo){ | |
assert.equal(imageInfo[4], image_2, "Should be image_2"); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(5,12); | |
}).then(function(blockInfo){ | |
return my_15.getImageInfo.call(blockInfo[1]); | |
}).then(function(imageInfo){ | |
assert.equal(imageInfo[4], image_1, "Should be image_1"); | |
}).then(function(){ | |
new_image.watch(function(error, result) { | |
if (error) { | |
console.log("This event throws an error! ", result.event); | |
new_image.stopWatching(); | |
done(error); } | |
NewImageEventToConsole(result); | |
new_image.stopWatching(); | |
}); | |
done(); | |
}).catch(done);; | |
}); | |
it("Should fail to place image to other landlord's land", function(done) { | |
var my_15 = My15.deployed(); | |
var throwed = false; | |
var image_4 = "http://i.imgur.com/KqmazwD.jpg"; | |
var initial_num_images = 0; | |
return my_15.getStateInfo.call() | |
.then(function(stateInfo){ | |
initial_num_images = stateInfo[3].toNumber(); | |
}).then(function(){ | |
return my_15.placeImage (1,11,1,12, image_4, image_4, image_4,{from: user_5, gas: 4712388}) | |
}).catch(function(e) { | |
throwed = true; | |
console.log("place image to other landlord's land... catched " + e.message.substring(0,100)) | |
//assert.equal(e.message.substring(43,55) , "invalid JUMP", "Should throw with invalid JUMP!"); | |
}).then(function(){ | |
//assert.equal(throwed, true, "Should raise exception"); | |
}).then(function(){ | |
return my_15.getStateInfo.call(); | |
}).then(function(stateInfo){ | |
assert.equal(stateInfo[3].toNumber() - initial_num_images, 0, "Image ID should not increment!"); | |
done(); | |
}).catch(done); | |
}); | |
it("Should fail to place image to vacant land", function(done) { | |
var my_15 = My15.deployed(); | |
var throwed = false; | |
var image_4 = "img/color/10x10.jpg"; | |
var initial_num_images = 0; | |
return my_15.getStateInfo.call() | |
.then(function(stateInfo){ | |
initial_num_images = stateInfo[3].toNumber(); | |
}).then(function(){ | |
return my_15.placeImage (5,10,6,10, image_4, image_4, image_4,{from: user_5, gas: 4712388}) | |
}).catch(function(e) { | |
throwed = true; | |
console.log("place image to vacant land... catched " + e.message.substring(0,100)) | |
//assert.equal(e.message.substring(43,55) , "invalid JUMP", "Should throw with invalid JUMP!"); | |
}).then(function(){ | |
//assert.equal(throwed, true, "Should raise exception"); | |
}).then(function(){ | |
return my_15.getStateInfo.call(); | |
}).then(function(stateInfo){ | |
assert.equal(stateInfo[3].toNumber() - initial_num_images, 0, "Image ID should not increment!"); | |
done(); | |
}).catch(done); | |
}); | |
it("Should fail to place image for free", function(done) { | |
var my_15 = My15.deployed(); | |
var throwed = false; | |
var image_4 = "img/color/10x10.jpg"; | |
var initial_num_images = 0; | |
return my_15.getStateInfo.call() | |
.then(function(stateInfo){ | |
initial_num_images = stateInfo[3].toNumber(); | |
}).then(function(){ | |
//return my_15.adminContractSettings (3600, charity_user , 0, {from: owner_account, gas: 4712388}) | |
return my_15.adminContractSettings (3600, '0x0', web3.toWei(1, unit),{from: owner_account, gas: 4712388}) | |
}).then(function(){ | |
return my_15.placeImage (5,10,6,10, image_4, image_4, image_4,{from: user_5, gas: 4712388}) | |
}).catch(function(e) { | |
throwed = true; | |
console.log("place image for free... catched " + e.message.substring(0,100)) | |
//assert.equal(e.message.substring(43,55) , "invalid JUMP", "Should throw with invalid JUMP!"); | |
}).then(function(){ | |
//assert.equal(throwed, true, "Should raise exception"); | |
}).then(function(){ | |
return my_15.getStateInfo.call(); | |
}).then(function(stateInfo){ | |
assert.equal(stateInfo[3].toNumber() - initial_num_images, 0, "Image ID should not increment!"); | |
done(); | |
}).catch(done); | |
}); | |
it("Should charge for image placement when the price is set", function(done) { | |
var my_15 = My15.deployed(); | |
var image_6 = "img/color/10x10.jpg"; | |
var initial_user_balance = 0; | |
var initial_owner_balance = 0; | |
var price = web3.toWei(2, unit); | |
return my_15.getUserInfo.call(user_4) | |
.then(function(userInfo){ | |
initial_user_balance = web3.toWei(userInfo[2], 'wei'); | |
}).then(function(){ | |
return my_15.getUserInfo.call(owner_account); | |
}).then(function(userInfo){ | |
initial_owner_balance = web3.toWei(userInfo[2], 'wei'); | |
}).then(function(){ | |
return my_15.adminContractSettings (3600, '0x0', price,{from: owner_account, gas: 4712388}); | |
}).then(function(){ | |
return my_15.placeImage (2,12,2,12, image_6, image_6, image_6,{from: user_4, value: price, gas: 4712388}); | |
}).then(function(){ | |
return my_15.getUserInfo.call(user_4); | |
}).then(function(userInfo){ | |
assert.equal(web3.toWei(userInfo[2], 'wei').minus(initial_user_balance), 0, "Should be 0 units!"); | |
}).then(function(){ | |
return my_15.getUserInfo.call(owner_account); | |
}).then(function(userInfo){ | |
assert.equal(web3.toWei(userInfo[2], 'wei').minus(initial_owner_balance), price, "Owner should have recieved [price] units!"); | |
}).then(function(){ | |
my_15.adminContractSettings (3600, '0x0', 0 ,{from: owner_account, gas: 4712388}); | |
done(); | |
}).catch(done); | |
}); | |
} | |
if (ADMIN_FUNCTIONS) { | |
it("Shouldn't let usual user to use admin functions", function(done) { | |
var my_15 = My15.deployed(); | |
var throwed = false; | |
return my_15.adminContractSecurity ('0x0', false, true, false, {from: user_7, gas: 4712388}) | |
.catch(function(e){ | |
throwed = true; | |
//assert.equal(e.message.substring(43,55) , "invalid JUMP", "Should throw with invalid JUMP!"); | |
}).then(function(){ | |
//assert.equal(throwed, true, "Should raise exception"); | |
done(); | |
}).catch(done); | |
}); | |
it("Should let admin to stop contract and use blocked functions", function(done) { | |
var my_15 = My15.deployed(); | |
var throwed = false; | |
return my_15.adminContractSecurity ('0x0', false, true, false,{from: owner_account, gas: 4712388}) | |
.then(function(){ | |
return my_15.buyBlocks (10, 100, 10, 100,{from: user_7, value: web3.toWei(1, unit), gas: 4712388}) | |
}).catch(function(e){ | |
throwed = true; | |
console.log("let admin to stop contract and use blocked functions... catched " + e.message.substring(0,100)) | |
//assert.equal(e.message.substring(43,55) , "invalid JUMP", "Should throw with invalid JUMP!"); | |
}).then(function(){ | |
//assert.equal(throwed, true, "Should raise exception"); | |
return my_15.buyBlocks (10, 100, 10, 100,{from: owner_account, value: web3.toWei(1, unit), gas: 4712388}); | |
}).then(function(){ | |
return my_15.getBlockInfo.call(10, 100); | |
}).then(function(blockInfo){ | |
assert.equal(blockInfo[0], owner_account, "Should be owner_account!"); | |
}).then(function(){ | |
return my_15.sellBlocks (10, 100, 10, 100 ,web3.toWei(1, unit),{from: owner_account, gas: 4712388}); | |
}).then(function(){ | |
return my_15.adminContractSecurity ('0x0', false, false, false, {from: owner_account, gas: 4712388}); | |
}).then(function(){ | |
done(); | |
}).catch(done); | |
}); | |
it("Should let admin to freeze any user", function(done) { | |
var my_15 = My15.deployed(); | |
var throwed = false; | |
var image_1 = 'sdfa'; | |
var image_id = 0; | |
var violator = user_7; | |
return my_15.buyBlocks (10, 99, 10, 99,{from: violator, value: web3.toWei(1, unit), gas: 4712388}) | |
.then(function(){ | |
return my_15.getBlockInfo.call(10, 99); | |
}).then(function(blockInfo){ | |
assert.equal(blockInfo[0], violator, "Should be violator!"); | |
}).then(function(){ | |
return my_15.adminContractSecurity (violator, true, false, false, {from: owner_account, gas: 4712388}) | |
}).then(function(){ | |
return my_15.getUserInfo.call(violator); | |
}).then(function(userInfo){ | |
assert.equal(userInfo[4], true, "Should be frozen!"); | |
}).then(function(){ | |
return my_15.placeImage (10, 99, 10, 99, image_1, image_1, image_1,{from: violator, gas: 4712388}) | |
}).catch(function(e){ | |
throwed = true; | |
console.log("let admin to freeze any user... catched " + e.message.substring(0,100)) | |
//assert.equal(e.message.substring(43,55) , "invalid JUMP", "Should throw with invalid JUMP!"); | |
}).then(function(){ | |
//assert.equal(throwed, true, "Should raise exception"); | |
return my_15.getBlockInfo.call(10, 99); | |
}).then(function(blockInfo){ | |
assert.equal(blockInfo[1], 0, "Should be no image_ID!"); | |
}).then(function(){ | |
return my_15.adminContractSecurity (violator, false, false, false, {from: owner_account, gas: 4712388}) | |
}).then(function(){ | |
return my_15.getUserInfo.call(violator); | |
}).then(function(userInfo){ | |
assert.equal(userInfo[4], false, "Shouldn't be frozen now!"); | |
}).then(function(){ | |
return my_15.placeImage (10, 99, 10, 99, image_1, image_1, image_1,{from: violator, gas: 4712388}); | |
}).then(function(){ | |
return my_15.getStateInfo.call(); | |
}).then(function(stateInfo){ | |
image_id = stateInfo[3].toNumber(); | |
return my_15.getBlockInfo.call(10, 99); | |
}).then(function(blockInfo){ | |
assert.equal(blockInfo[1].toNumber(), image_id, "Should be rigth image_ID!"); | |
done(); | |
}).catch(done); | |
}); | |
it("Should let admin set delay", function(done) { | |
var my_15 = My15.deployed(); | |
return my_15.adminContractSettings (DELAY + 1 , '0x0', 0, {from: owner_account, gas: 4712388}) | |
.then(function(){ | |
return my_15.getStateInfo.call(); | |
}).then(function(stateInfo) { | |
assert.equal(stateInfo[6].toNumber(), DELAY + 1, "Should set new delay!"); | |
//return my_15.admin_setDelay (DELAY, {from: owner_account, gas: 4712388}); | |
return my_15.adminContractSettings (DELAY, '0x0', 0, {from: owner_account, gas: 4712388}); | |
}).then(function(){ | |
done(); | |
}).catch(done); | |
}); | |
it("Check charity calculations", function(done) { | |
var my_15 = My15.deployed(); | |
var initialCharityBal = 0; | |
var finalCharityBal; | |
var initialCharityUserBal = 0; | |
var buyer = user_3; | |
var charity_user = user_8; | |
// check charity balance (unassigned) | |
return my_15.charityBalance.call() | |
.then(function(balance) { | |
initialCharityBal = balance; | |
}).then(function(){ | |
return my_15.buyBlocks (10, 94, 10, 97,{from: buyer, value: web3.toWei(4, unit), gas: 4712388}); | |
}).then(function(){ | |
return my_15.charityBalance.call(); | |
}).then(function(balance) { | |
console.log(" - Charity - balance.minus(initialCharityBal): ", balance.minus(initialCharityBal).toNumber()); | |
assert.equal(balance.minus(initialCharityBal), web3.toWei(1, unit), "Charity balance should increase by 1 unit!" ); | |
finalCharityBal = balance; | |
return my_15.getUserInfo.call(charity_user); | |
// assign charity user | |
}).then(function(userInfo){ | |
initialCharityUserBal = userInfo[2]; | |
//no changes to charity should be done with this line | |
return my_15.adminContractSettings (3600, '0x0' , 0, {from: owner_account, gas: 4712388}) | |
}).then(function(){ | |
return my_15.adminContractSettings (3600, charity_user , 0, {from: owner_account, gas: 4712388}) | |
}).then(function(){ | |
return my_15.getUserInfo.call(charity_user); | |
}).then(function(userInfo){ | |
console.log(" - Charity - userInfo[2].minus(initialCharityUserBal): ", userInfo[2].minus(initialCharityUserBal).toNumber()); | |
console.log(" - Charity - finalCharityBal): ", finalCharityBal.toNumber()); | |
assert.equal(userInfo[2].minus(initialCharityUserBal).toNumber(), finalCharityBal.toNumber(), "Charity user balance should recieve all charity!" ) | |
initialCharityUserBal = userInfo[2]; | |
// check charity transactions with assigned user | |
}).then(function(){ | |
return my_15.buyBlocks (10, 90, 10, 93,{from: buyer, value: web3.toWei(4, unit), gas: 4712388}); | |
}).then(function(){ | |
return my_15.getUserInfo.call(charity_user); | |
}).then(function(userInfo){ | |
assert.equal(userInfo[2].minus(initialCharityUserBal), web3.toWei(1, unit), "Charity user balance should increase by 1 unit!" ) | |
done(); | |
}).catch(done); | |
}); | |
it("Check other INFO functions", function(done) { | |
var my_15 = My15.deployed(); | |
var user_id = 0; | |
var user_balance = 0; | |
return my_15.getUserInfo.call(user_7) | |
.then(function(userInfo) { | |
user_balance = userInfo[2]; | |
user_id = userInfo[5]; | |
}).then(function(){ | |
return my_15.getUserAddressByID.call(user_id); | |
}).then(function(user_address){ | |
assert.equal(user_address, user_7, "Sould retrieve user_7 address!"); | |
}).then(function(){ | |
//return my_15.getMyBalance.call ({from: user_7}); | |
return my_15.getMyInfo.call ({from: user_7}); | |
}).then(function(balance){ | |
assert.equal(balance[0].toNumber(), user_balance.toNumber(), "Sould retrieve user_7 balance!"); | |
done(); | |
}).catch(done); | |
}); | |
} | |
if (MANY_BLOCKS) { | |
it("Should let buy many blocks", function(done) { | |
var my_15 = My15.deployed(); | |
var initialBlocks = 0; | |
var gas_cost; | |
var big_image = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam"; | |
var big_image2 = "dquis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat"; | |
return my_15.getStateInfo.call() | |
.then(function(result){ | |
initialBlocks = result[1].toNumber(); | |
console.log(" - initialBlocks: ", initialBlocks); | |
}).then(function(){ | |
return my_15.buyBlocks (11,99,60,100,{from: user_2, value: web3.toWei(100, unit), gas: 4712388}); | |
//return my_15.buyBlocks (11,99,50,100,{from: user_2, value: web3.toWei(100, unit), gas: 4712388}); //buy 50 instead | |
}).then(function(tx_id){ | |
gas_used = web3.eth.getTransactionReceipt(tx_id).gasUsed; | |
console.log(" - buyBlocks gas_used: ", gas_used); | |
console.log(" - Many blocks - 100 bought"); | |
//gas estimates | |
/*}).then(function(){ | |
return my_15.placeImage (10,98,50,100, big_image, big_image, big_image,{from: user_2}); | |
}).then(function(tx_id){ | |
gas_used = web3.eth.getTransactionReceipt(tx_id).gasUsed; | |
console.log(" - placeImage gas_used: ", gas_used); | |
}).then(function(){ | |
return my_15.sellBlocks (10,98,50,100,web3.toWei(1, unit),{from: user_2}) | |
}).then(function(tx_id){ | |
gas_used = web3.eth.getTransactionReceipt(tx_id).gasUsed; | |
console.log(" - sellBlocks gas_used: ", gas_used); | |
}).then(function(){ | |
return my_15.buyBlocks (10,98,50,100,{from: user_3, value: web3.toWei(100, unit)}); | |
}).then(function(tx_id){ | |
gas_used = web3.eth.getTransactionReceipt(tx_id).gasUsed; | |
console.log(" - buyBlocks 2 gas_used: ", gas_used);*/ | |
}).then(function(){ | |
return my_15.buyBlocks (11,97,60,98,{from: user_2, value: web3.toWei(100, unit), gas: 4712388}); | |
}).then(function(){ | |
console.log(" - Many blocks - 200 bought"); | |
return my_15.buyBlocks (11,95,60,96,{from: user_2, value: web3.toWei(100, unit), gas: 4712388}); | |
}).then(function(){ | |
console.log(" - Many blocks - 300 bought"); | |
return my_15.buyBlocks (11,93,60,94,{from: user_2, value: web3.toWei(100, unit), gas: 4712388}); | |
}).then(function(){ | |
console.log(" - Many blocks - 400 bought"); | |
return my_15.buyBlocks (11,91,60,92,{from: user_2, value: web3.toWei(100, unit), gas: 4712388}); | |
}).then(function(){ | |
console.log(" - Many blocks - 500 bought"); | |
return my_15.buyBlocks (11,89,60,90,{from: user_2, value: web3.toWei(100, unit), gas: 4712388}); | |
}).then(function(){ | |
console.log(" - Many blocks - 600 bought"); | |
return my_15.buyBlocks (11,87,60,88,{from: user_2, value: web3.toWei(100, unit), gas: 4712388}); | |
}).then(function(){ | |
console.log(" - Many blocks - 700 bought"); | |
return my_15.buyBlocks (11,85,60,86,{from: user_2, value: web3.toWei(100, unit), gas: 4712388}); | |
}).then(function(){ | |
console.log(" - Many blocks - 800 bought"); | |
return my_15.buyBlocks (11,83,60,84,{from: user_2, value: web3.toWei(100, unit), gas: 4712388}); | |
}).then(function(){ | |
console.log(" - Many blocks - 900 bought"); | |
return my_15.buyBlocks (11,81,60,82,{from: user_2, value: web3.toWei(200, unit), gas: 4712388}); | |
}).then(function(){ | |
console.log(" - Many blocks - 1000 bought"); | |
return my_15.getStateInfo.call() | |
}).then(function(result){ | |
console.log(" - finalBlocks: ", result[1].toNumber()); | |
}).then(function(){ | |
return my_15.getAreaPrice.call (11, 80, 11, 80); | |
}).then(function(totalPrice){ | |
console.log(" - totalPrice: ", totalPrice); | |
assert.equal (totalPrice.toNumber(), web3.toWei(2, unit), "price should increase!") | |
done(); | |
}).catch(done); | |
}); | |
} | |
if (EMERGENCY) { | |
it("Emergency refund check", function(done) { | |
var my_15 = My15.deployed(); | |
var investor_initial_bal; | |
var investor = user_3; | |
var other_user = user_2; | |
var totalWeiInvested; | |
var availableForRefund; | |
var percent; | |
var price; | |
return my_15.getUserInfo.call(investor) | |
.then(function(userInfo) { | |
investor_initial_bal = web3.toWei(userInfo[7], 'wei'); | |
}).then(function(){ | |
return my_15.getAreaPrice.call(9, 97, 9, 100); | |
}).then(function(area_price){ | |
price = area_price; | |
}).then(function(){ | |
return my_15.buyBlocks (9, 97, 9, 100,{from: investor, value: price, gas: 4712388}) | |
}).then(function(){ | |
return my_15.getUserInfo.call(investor) | |
}).then(function(userInfo) { | |
console.log(' - price:', price.toNumber()); | |
console.log(' - investor_initial_bal:', investor_initial_bal); | |
console.log(' - investor_final_bal:', userInfo[7].toNumber()); | |
assert.equal(userInfo[7].minus(investor_initial_bal).toNumber(), price.toNumber(), "Investor balance should increase by price units!" ); | |
investor_initial_bal = web3.toWei(userInfo[7], 'wei'); | |
}).then(function(){ | |
return my_15.withdrawAll ({from: other_user, gas: 4712388}); | |
}).then(function(){ | |
return my_15.withdrawAll ({from: owner_account, gas: 4712388}); | |
}).then(function(){ | |
return my_15.adminContractSecurity ('0x0', false, true, false, {from: owner_account, gas: 4712388}) | |
}).then(function(){ | |
return my_15.adminContractSecurity ('0x0', false, true, true, {from: owner_account, gas: 4712388}) | |
}).then(function(){ | |
return my_15.getStateInfo.call(); | |
}).then(function(result){ | |
totalWeiInvested = web3.toWei(result[2], 'wei'); | |
percent = web3.eth.getBalance(my_15.address).times(100).dividedBy(totalWeiInvested).ceil(); | |
availableForRefund = investor_initial_bal.times(percent).ceil().dividedBy(100).ceil(); | |
}).then(function(){ | |
return my_15.emergencyRefund ({from: investor, gas: 4712388}); | |
}).then(function(){ | |
return my_15.getUserInfo.call(investor) | |
}).then(function(userInfo) { | |
assert.equal(investor_initial_bal.minus(userInfo[7]).toNumber(), availableForRefund.toNumber(), "Investor balance should decrease by right amount!" ); | |
done(); | |
}).catch(done); | |
}); | |
} | |
if (SELFDESTRUCT) { | |
it("Selfdestruct", function() { | |
var my_15 = My15.deployed(); | |
return my_15.admin_removeContract ({gas: 4712388}); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment