diff --git a/contracts/AcceptETH.sol b/contracts/AcceptETH.sol index c4d5f80..5129ddd 100644 --- a/contracts/AcceptETH.sol +++ b/contracts/AcceptETH.sol @@ -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); diff --git a/test/AcceptETH.test.js b/test/AcceptETH.test.js index 0599199..4b19afb 100644 --- a/test/AcceptETH.test.js +++ b/test/AcceptETH.test.js @@ -3,7 +3,7 @@ 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"); @@ -11,11 +11,29 @@ 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 })); + }) + }); }) }) \ No newline at end of file diff --git a/test/AcceptORS.test.js b/test/AcceptORS.test.js index 71096a0..931f1b8 100644 --- a/test/AcceptORS.test.js +++ b/test/AcceptORS.test.js @@ -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 () => { diff --git a/test/helpers/common.js b/test/helpers/common.js index c3fc788..94b23d4 100644 --- a/test/helpers/common.js +++ b/test/helpers/common.js @@ -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. @@ -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. @@ -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; } @@ -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 } @@ -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 } @@ -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; } @@ -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 = () => {