Skip to content

Instantly share code, notes, and snippets.

@collinsrj
Created September 18, 2019 13:27
Show Gist options
  • Save collinsrj/e663e61b364dc321b385f246f96fcb5a to your computer and use it in GitHub Desktop.
Save collinsrj/e663e61b364dc321b385f246f96fcb5a to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.26+commit.4563c3fc.js&optimize=false&gist=
pragma solidity >=0.4.1 <0.6.0;
contract Escrow {
uint balance;
address public buyer;
address public seller;
address private escrow;
uint private start;
bool buyerOk;
bool sellerOk;
constructor (address buyer_address, address seller_address) public {
// this is the constructor function that runs ONCE upon initialization
buyer = buyer_address;
seller = seller_address;
escrow = msg.sender;
start = now; //now is an alias for block.timestamp, not really "now"
}
function accept() public {
if (msg.sender == buyer){
buyerOk = true;
}
if (msg.sender == seller){
sellerOk = true;
}
if (buyerOk && sellerOk){
payBalance();
} else if (buyerOk && !sellerOk && now > start + 30 days) {
// Freeze 30 days before release to buyer. The customer has to remember to call this method after freeze period.
selfdestruct(buyer);
}
}
function payBalance() private {
// send seller the balance
require(seller.send(balance));
balance = 0;
}
function deposit() public payable {
if (msg.sender == buyer) {
balance += msg.value;
}
}
function cancel() public {
if (msg.sender == buyer){
buyerOk = false;
} else if (msg.sender == seller){
sellerOk = false;
}
// if both buyer and seller would like to cancel, money is returned to buyer
if (!buyerOk && !sellerOk){
selfdestruct(buyer);
}
}
function kill() public {
if (msg.sender == escrow) {
selfdestruct(buyer);
}
}
}
pragma solidity >=0.4.22 <0.6.0;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "./Escrow.sol";
contract escrow_test {
Escrow escrow;
address buyerAddress = 0xa978c937cB8A7fC57407B6B5C45741C579Ca3A11;
address sellerAddress = 0x4B0897b0513fdC7C541B6d9D7E929C4e5364D2dB;
function beforeAll () public {
escrow = new Escrow(buyerAddress, sellerAddress);
}
function checkAccept () public{
escrow.accept();
}
}
pragma solidity >=0.4.0 <0.6.0;
import "remix_tests.sol"; // this import is automatically injected by Remix.
// file name has to end with '_test.sol'
contract test_1 {
function beforeAll() public {
// here should instantiate tested contract
Assert.equal(uint(4), uint(3), "error in before all function");
}
function check1() public {
// use 'Assert' to test the contract
Assert.equal(uint(2), uint(1), "error message");
Assert.equal(uint(2), uint(2), "error message");
}
function check2() public view returns (bool) {
// use the return value (true or false) to test the contract
return true;
}
}
contract test_2 {
function beforeAll() public {
// here should instantiate tested contract
Assert.equal(uint(4), uint(3), "error in before all function");
}
function check1() public {
// use 'Assert' to test the contract
Assert.equal(uint(2), uint(1), "error message");
Assert.equal(uint(2), uint(2), "error message");
}
function check2() public view returns (bool) {
// use the return value (true or false) to test the contract
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment