Skip to content

Instantly share code, notes, and snippets.

View CinnamonGrrrl's full-sized avatar

CinnamonGrrrl

View GitHub Profile
// contracts/Clock.sol
pragma solidity ^0.4.17;
contract Clock {
uint private timestamp;
function getNow() public view returns (uint) {
if (timestamp > 0) {
return timestamp;
}
FundingTest
✓ testSettingAnOwnerDuringCreation (61ms)
✓ testSettingAnOwnerOfDeployedContract (65ms)
✓ testAcceptingDonations (97ms)
✓ testTrackingDonatorsBalance (61ms)
Contract: Funding
✓ sets an owner (51ms)
✓ accepts donations (96ms)
✓ keeps track of donator balance (134ms)
contract Funding {
uint public raised;
address public owner;
mapping(address => uint) public balances;
function Funding() public {
owner = msg.sender;
}
function donate() public payable {
it("keeps track of donator balance", async () => {
const funding = await Funding.new();
await funding.donate({ from: firstAccount, value: 5 * FINNEY });
await funding.donate({ from: secondAccount, value: 15 * FINNEY });
await funding.donate({ from: secondAccount, value: 3 * FINNEY });
assert.equal(await funding.balances.call(firstAccount), 5 * FINNEY);
assert.equal(await funding.balances.call(secondAccount), 18 * FINNEY);
});
FundingTest
✓ testSettingAnOwnerDuringCreation (68ms)
✓ testSettingAnOwnerOfDeployedContract (63ms)
✓ testAcceptingDonations (80ms)
Contract: Funding
✓ sets an owner (56ms)
✓ accepts donations (96ms)
function testTrackingDonatorsBalance() public {
Funding funding = new Funding();
funding.donate.value(5 finney)();
funding.donate.value(15 finney)();
Assert.equal(funding.balances(this), 20 finney);
}
contract Funding {
uint public raised;
address public owner;
function Funding() public {
owner = msg.sender;
}
function donate() public payable {
raised += msg.value;
const FINNEY = 10**15;
contract("Funding", accounts => {
const [firstAccount, secondAccount] = accounts;
it("accepts donations", async () => {
const funding = await Funding.new();
await funding.donate({ from: firstAccount, value: 10 * FINNEY });
await funding.donate({ from: secondAccount, value: 20 * FINNEY });
assert.equal(await funding.raised.call(), 30 * FINNEY);
contract FundingTest {
uint public initialBalance = 10 ether;
function testAcceptingDonations() public {
Funding funding = new Funding();
Assert.equal(funding.raised(), 0);
funding.donate.value(10 finney)();
funding.donate.value(20 finney)();
Assert.equal(funding.raised(), 30 finney);
}
FundingTest
✓ testSettingAnOwnerDuringCreation (70ms)
✓ testSettingAnOwnerOfDeployedContract (63ms)
Contract: Funding
✓ sets an owner (62ms)
3 passing (744ms)