Skip to content

Commit

Permalink
AcceptETH test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
awtsPrasad committed Nov 15, 2018
1 parent e79cb0c commit c6773bd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 48 deletions.
4 changes: 2 additions & 2 deletions contracts/AcceptETH.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import "../zeppelin-solidity/contracts/ownership/Ownable.sol";

contract AcceptETH is Ownable{

event SendEvent(address, uint);
event SendEvent(address _sender, uint _value);

function acceptETH() external payable {
function acceptEther() external payable {
require(msg.value > 0, "Incorrect package amount");
owner.transfer(msg.value);
emit SendEvent(msg.sender, msg.value);
Expand Down
30 changes: 24 additions & 6 deletions test/AcceptETH.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,37 @@
const AcceptETH = artifacts.require("AcceptETH.sol");
const ORSToken = artifacts.require("ORSToken.sol");


// const web3 = require('web3');
const BN = web3.BigNumber;
const { expect } = require("chai").use(require("chai-bignumber")(BN));
const { rejectTx, rejectDeploy } = require("./helpers/common");


contract("AcceptETH", ([owner, holder, trustee, recipient, anyone]) => {

const deployToken = cap => {
return ORSToken.new(cap, { from: owner });
};
describe("Accept Ethers as payment", () => {

let acceptETHContract;

before('setup contract for each test', async function () {
acceptETHContract = await AcceptETH.new({ from: owner })
})

describe("transfer ETH", () => {
it('Ether transferred successfully', async function () {
var ownerBalance = await web3.eth.getBalance(owner); // because you get a BigNumber
await acceptETHContract.acceptEther({ from: anyone, value: 1000000000000000000 });
expect(await web3.eth.getBalance(owner)).to.be.bignumber.equal(ownerBalance.plus(new BN(1000000000000000000)));
})

})

describe("transfer ETH without sufficient balance", () => {

describe("AcceptETH", () => {

it('forbids transfer', async function () {
var holderBalance = await web3.eth.getBalance(holder).toString(10); // because you get a BigNumber
await rejectTx(acceptETHContract.acceptEther({ from: holder, value: holderBalance }));
})
});
})
})
1 change: 0 additions & 1 deletion test/AcceptORS.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ contract("AcceptORS", ([owner, holder, trustee, recipient, anyone]) => {
await token.finishMinting({ from: owner });
});


describe('transfer tokens', () => {

it("permits to transfer from", async () => {
Expand Down
78 changes: 39 additions & 39 deletions test/helpers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ module.exports = (() => {

// Some simple functions to translate durations to seconds.
const duration = (() => {
const secs = n => n;
const mins = n => n * secs(60);
const secs = n => n;
const mins = n => n * secs(60);
const hours = n => n * mins(60);
const days = n => n * hours(24);
const days = n => n * hours(24);
const weeks = n => n * days(7);
const years = n => n * days(365);

return {secs, seconds: secs, mins, minutes: mins, hours, days, weeks, years};
return { secs, seconds: secs, mins, minutes: mins, hours, days, weeks, years };
})();

// Some simple functions to translate currencies to wei.
const currency = (() => {
const $ = (n, m) => (new web3.BigNumber(n)).mul(m).trunc();

const wei = n => $(n, 1e0);
const ada = n => $(n, 1e3);
const babbage = n => $(n, 1e6);
const gwei = n => $(n, 1e9);
const szabo = n => $(n, 1e12);
const finney = n => $(n, 1e15);
const ether = n => $(n, 1e18);
const wei = n => $(n, 1e0);
const ada = n => $(n, 1e3);
const babbage = n => $(n, 1e6);
const gwei = n => $(n, 1e9);
const szabo = n => $(n, 1e12);
const finney = n => $(n, 1e15);
const ether = n => $(n, 1e18);
const einstein = n => $(n, 1e21);

return {wei, ada, babbage, gwei, szabo, finney, mether: finney, ether, einstein};
return { wei, ada, babbage, gwei, szabo, finney, mether: finney, ether, einstein };
})();

// Logging colors.
Expand All @@ -42,9 +42,9 @@ module.exports = (() => {

const log = message => {
console.log(" ".repeat(8)
+ COLOR_CYAN + "→ "
+ COLOR_GRAY + message
+ COLOR_RESET);
+ COLOR_CYAN + "→ "
+ COLOR_GRAY + message
+ COLOR_RESET);
};

// Try to execute a transaction and log its gas usage to console.
Expand All @@ -62,8 +62,8 @@ module.exports = (() => {
let tx = await promise;

log(message + (tx.hasOwnProperty("receipt")
? ": " + tx.receipt.gasUsed
: " unknown due to missing receipt"));
? ": " + tx.receipt.gasUsed
: " unknown due to missing receipt"));

return tx;
}
Expand All @@ -85,11 +85,10 @@ module.exports = (() => {

if (tx.hasOwnProperty("receipt")) {
let receipt = tx.receipt;

// Unfortunately, all cases where seen in the wild.
if (receipt.status === 0
|| receipt.status === "0x"
|| receipt.status === "0x0") {
|| receipt.status === "0x"
|| receipt.status === "0x0") {
return; // post-Byzantium rejection
}

Expand All @@ -116,13 +115,14 @@ module.exports = (() => {
}
catch (error) {
let message = error.toString().toLowerCase();

// That's ugly, older pre-Byzantium TestRPC just throws.
// Nevertheless, post-Byzantium Ganache throws, too.
if (message.includes("invalid opcode")
|| message.includes("invalid jump")
|| message.includes("sender account not recognized")
|| message.includes("vm exception while processing transaction: revert")) {
|| message.includes("invalid jump")
|| message.includes("sender account not recognized")
|| message.includes("sender doesn't have enough funds to send tx")
|| message.includes("vm exception while processing transaction: revert")) {
return; // pre-Byzantium rejection
}

Expand All @@ -143,7 +143,7 @@ module.exports = (() => {
let message = error.toString().toLowerCase();

if (message.includes("the contract code couldn't be stored")
|| message.includes("vm exception while processing transaction: revert")) {
|| message.includes("vm exception while processing transaction: revert")) {
return;
}

Expand All @@ -155,20 +155,20 @@ module.exports = (() => {

const increaseTime = secs =>
new Promise((resolve, reject) => {
web3.currentProvider.sendAsync(
{jsonrpc: "2.0", method: "evm_increaseTime", params: [secs], id: now()},
error => {
if (error) { reject(error); }
else {
web3.currentProvider.sendAsync(
{jsonrpc: "2.0", method: "evm_mine", id: now() + 1},
(error, result) => {
if (error) { reject(error); }
else { resolve(result); }
});
}
});
});
web3.currentProvider.sendAsync(
{ jsonrpc: "2.0", method: "evm_increaseTime", params: [secs], id: now() },
error => {
if (error) { reject(error); }
else {
web3.currentProvider.sendAsync(
{ jsonrpc: "2.0", method: "evm_mine", id: now() + 1 },
(error, result) => {
if (error) { reject(error); }
else { resolve(result); }
});
}
});
});

// Create a random address.
const randomAddr = () => {
Expand Down

0 comments on commit c6773bd

Please sign in to comment.