Skip to content

Commit

Permalink
Rename contract to CryptoPunks2 for tests. Tests passing. Added some …
Browse files Browse the repository at this point in the history
…tests.
  • Loading branch information
megamattron committed Jun 19, 2017
1 parent 4109da8 commit 33c62f5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 20 deletions.
10 changes: 5 additions & 5 deletions contracts/CryptoPunks2.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pragma solidity ^0.4.8;
contract CryptoPunks {
contract CryptoPunks2 {

// You can use this hash to verify the image file containing all the punks
string public imageHash = "ac39af4793119ee46bbff351d8cb6b5f23da60222126add4268e261199a2921b";
Expand Down Expand Up @@ -44,7 +44,7 @@ contract CryptoPunks {
event PunkNoLongerForSale(uint indexed punkIndex);

/* Initializes contract with initial supply tokens to the creator of the contract */
function CryptoPunks() payable {
function CryptoPunks2() payable {
// balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
owner = msg.sender;
totalSupply = 10000; // Update total supply
Expand All @@ -63,7 +63,7 @@ contract CryptoPunks {
punksRemainingToAssign--;
Assign(to, punkIndex);
}

function allInitialOwnersAssigned() {
if (msg.sender != owner) throw;
allPunksAssigned = true;
Expand Down Expand Up @@ -138,5 +138,5 @@ contract CryptoPunks {
pendingWithdrawals[msg.sender] = 0;
msg.sender.transfer(amount);
}
}

}
2 changes: 2 additions & 0 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var ConvertLib = artifacts.require("./ConvertLib.sol");
var CryptoPunks = artifacts.require("./CryptoPunks.sol");
var CryptoPunks2 = artifacts.require("./CryptoPunks2.sol");

module.exports = function(deployer) {
deployer.deploy(ConvertLib);
deployer.link(ConvertLib, CryptoPunks);
deployer.deploy(CryptoPunks);
deployer.deploy(CryptoPunks2);
};
2 changes: 2 additions & 0 deletions test/cryptopunks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var CryptoPunks = artifacts.require("./CryptoPunks.sol");

/*
contract('CryptoPunks', function (accounts) {
it("should have 10000 punks available to assign", function () {
return CryptoPunks.deployed().then(function (instance) {
Expand Down Expand Up @@ -230,3 +231,4 @@ contract('CryptoPunks', function (accounts) {
});
});
});
*/
96 changes: 81 additions & 15 deletions test/cryptopunks2.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
var CryptoPunks = artifacts.require("./CryptoPunks2.sol");
var CryptoPunks2 = artifacts.require("./CryptoPunks2.sol");

contract('CryptoPunks', function (accounts) {
contract('CryptoPunks2', function (accounts) {
it("should have 10000 punks available to assign", function () {
return CryptoPunks.deployed().then(function (instance) {
return CryptoPunks2.deployed().then(function (instance) {
// console.log("Deployed");
return instance.punksRemainingToAssign.call();
}).then(function (balance) {
assert.equal(balance.valueOf(), 10000, "10000 punks not available.");
});
}),
it("should be able to reserve, buy and sell punks", function () {
it("should be able to complete the full buy and sell cycle", function () {
var contract;
var previousBalance;
var punksRemainingToAssign;
var NULL_ACCOUNT = "0x0000000000000000000000000000000000000000";

return CryptoPunks.deployed().then(function (instance) {
return CryptoPunks2.deployed().then(function (instance) {
// console.log("Deployed");
contract = instance;
return instance.setInitialOwner(accounts[0], 0);
Expand All @@ -27,13 +27,13 @@ contract('CryptoPunks', function (accounts) {
}).then(function (result) {
assert.equal(result.valueOf(), 9999, "Incorrect remaining punks.");
// assign another punk to 0
return instance.setInitialOwner(accounts[0], 1);
return contract.setInitialOwner(accounts[0], 1);
}).then(function () {
return contract.punkIndexToAddress(1);
}).then(function (address) {
assert.equal(accounts[0], address, "Making sure that 0 owns punk 1.");
// assign a punk to 2
return instance.setInitialOwner(accounts[2], 2);
return contract.setInitialOwner(accounts[2], 2);
}).then(function () {
// Try to transfer a punk from someone who doesn't own it
return contract.transferPunk(accounts[1], 1, {from: accounts[1]});
Expand Down Expand Up @@ -148,27 +148,93 @@ contract('CryptoPunks', function (accounts) {
console.log("Check that punk purchase price is available for withdrawal.");
return contract.pendingWithdrawals(accounts[1]);
}).then(function (balance) {
assert.equal(balance, 10000, "Account 0 balance incorrect.");
assert.equal(balance, 10000, "Account 1 balance incorrect.");
}).then(function () {
return web3.eth.getBalance(accounts[1]);
}).then(function (balance) {
previousBalance = balance;
console.log("Previous account 0 balance: "+balance);
console.log("Withdrawing balance for account 0 from contract.");
return contract.withdraw();
console.log("Previous account 1 balance: "+balance);
console.log("Withdrawing balance for account 1 from contract.");
return contract.withdraw({from: accounts[1]});
}).then(function () {
console.log("Checking new account balance after withdrawal.");
return web3.eth.getBalance(accounts[1]);
}).then(function (balance) {
console.log("Balance after withdrawal: " + balance);
var strPrevBalance = String(previousBalance);
var subPrevBalance = strPrevBalance.substr(strPrevBalance.length - 6, strPrevBalance.length);
var digitsToCompare = 8;
var subPrevBalance = strPrevBalance.substr(strPrevBalance.length - digitsToCompare, strPrevBalance.length);
var strBalance = String(balance);
var subCurrBalance = strBalance.substr(strBalance.length - 6, strBalance.length);
var subCurrBalance = strBalance.substr(strBalance.length - digitsToCompare, strBalance.length);
console.log("Comparing only least significant digits: "+subPrevBalance+" vs. "+subCurrBalance);
assert.equal(Number(subCurrBalance), Number(subPrevBalance) + 10000, "Account 0 balance incorrect after withdrawal.");
return assert.equal(Number(subCurrBalance), Number(subPrevBalance) + 10000, "Account 1 balance incorrect after withdrawal.");
})
// return contract.nextPunkIndexToAssign();
});
});
}),
it("should not be able to send bad number to setInitialOwner", function () {
var contract;
return CryptoPunks2.deployed().then(function (instance) {
contract = instance;
return instance.setInitialOwner(accounts[0], 10000);
}).then(function () {
// console.log("Bought punk.");
assert(false, "Was supposed to throw but didn't.");
}).catch(function (error) {
if (error.toString().indexOf("invalid opcode") != -1) {
// Expecting a throw here
// console.log("We were expecting a Solidity throw (aka an invalid JUMP), we got one. Test succeeded.");
} else {
// if the error is something else (e.g., the assert from previous promise), then we fail the test
assert(false, error.toString());
}
// Get account 0 to buy a punk with enough ether
// console.log("Buying punk 1001 with account 2 which should be allowed.");
// return contract.buyPunk(1001, {from: accounts[2], value: 10000});
})
}),
it("only owner can call setInitialOwner", function () {
var contract;
return CryptoPunks2.deployed().then(function (instance) {
contract = instance;
return instance.setInitialOwner(accounts[1], 10000);
}).then(function () {
// console.log("Bought punk.");
assert(false, "Was supposed to throw but didn't.");
}).catch(function (error) {
if (error.toString().indexOf("invalid opcode") != -1) {
// Expecting a throw here
// console.log("We were expecting a Solidity throw (aka an invalid JUMP), we got one. Test succeeded.");
} else {
// if the error is something else (e.g., the assert from previous promise), then we fail the test
assert(false, error.toString());
}
// Get account 0 to buy a punk with enough ether
// console.log("Buying punk 1001 with account 2 which should be allowed.");
// return contract.buyPunk(1001, {from: accounts[2], value: 10000});
})
}),
it("should not be able to call setInitialOwner after contract set to all initial assigned", function () {
var contract;
return CryptoPunks2.deployed().then(function (instance) {
contract = instance;
return contract.allInitialOwnersAssigned();
}).then(function () {
return contract.setInitialOwner(accounts[0], 0);
}).then(function () {
// console.log("Bought punk.");
assert(false, "Was supposed to throw but didn't.");
}).catch(function (error) {
if (error.toString().indexOf("invalid opcode") != -1) {
// Expecting a throw here
// console.log("We were expecting a Solidity throw (aka an invalid JUMP), we got one. Test succeeded.");
} else {
// if the error is something else (e.g., the assert from previous promise), then we fail the test
assert(false, error.toString());
}
// Get account 0 to buy a punk with enough ether
// console.log("Buying punk 1001 with account 2 which should be allowed.");
// return contract.buyPunk(1001, {from: accounts[2], value: 10000});
})
});
});

0 comments on commit 33c62f5

Please sign in to comment.