Skip to content

Instantly share code, notes, and snippets.

@santosh79
Created June 29, 2018 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santosh79/fface6904b15955525a4e5973ac3ac0b to your computer and use it in GitHub Desktop.
Save santosh79/fface6904b15955525a4e5973ac3ac0b to your computer and use it in GitHub Desktop.
Escrow Solidity Contract
pragma solidity ^0.4.21;
// This is a simple smart
contract Escrow {
mapping(address => uint256) depositorMappings;
mapping(address => uint256) recipientMappings;
mapping(address => address) depositorToRecipientMappings;
mapping(address => address) recipientToDepositorMappings;
mapping(address => uint) depositorBlockMappings;
uint256 constant oneDayInBlocks = 3 * 60 * 24; // around 3 blocks a minute
uint256 constant twoDaysInBlocks = 2 * oneDayInBlocks;
//
// PUBLIC FUNCTIONS:
//
// This function kicks off an Escrow.
// The depositor will need to specify the address of the
// recipient. The `msg.value` contains the amount for the recipient.
function startEscrow(address recipient) payable public {
depositorMappings[msg.sender] = msg.value;
recipientMappings[recipient] = msg.value;
depositorBlockMappings[msg.sender] = block.number;
depositorToRecipientMappings[msg.sender] = recipient;
recipientToDepositorMappings[recipient] = msg.sender;
}
// This function reclaimDepositorFunds allows a depositor to reclaim
// funds they had deposited if they are in the valid window.
function reclaimDepositorFunds() public {
address depositor = msg.sender;
require(inDepositorWindow(depositor), "No funds to reclaim");
uint256 amount = 0;
amount = depositorMappings[depositor];
depositor.transfer(amount);
// cleanup entries
address recipient = depositorToRecipientMappings[msg.sender];
cleanupEntries(depositor, recipient);
}
// This function claimRecipientFunds allows a recipient to claim
// funds if they are in a valid window
function claimRecipientFunds() public {
address recipient = msg.sender;
require(inRecipientWindow(recipient), "No funds to claim");
uint256 amount = 0;
amount = recipientMappings[recipient];
recipient.transfer(amount);
// cleanup entries
address depositor = recipientToDepositorMappings[recipient];
cleanupEntries(depositor, recipient);
}
// This function getDepositorBalance allows a depositor of funds
// to examine his/her deposits as long as they are in the window.
function getDepositorBalance(address depositor) public view returns (uint256) {
if ((depositorMappings[depositor] > 0) && inDepositorWindow(depositor)) {
return depositorMappings[depositor];
}
return 0;
}
// This function getRecipientBalance allows a recipient of funds
// to examine his/her deposits as long as they are in the window.
function getRecipientBalance(address recipient) public view returns (uint256) {
if ((recipientMappings[recipient] > 0) && inRecipientWindow(recipient)) {
return recipientMappings[recipient];
}
return 0;
}
//
// PRIVATE FUNCTIONS
//
function cleanupEntries(address depositor, address recipient) private {
delete depositorMappings[depositor];
delete recipientMappings[recipient];
delete depositorBlockMappings[depositor];
}
function inDepositorWindow(address depositor) private view returns (bool) {
uint256 diff = 0;
diff = block.number - depositorBlockMappings[depositor];
return (depositorMappings[depositor] > 0) &&
((diff < oneDayInBlocks) || (diff > twoDaysInBlocks));
}
function inRecipientWindow(address recipient) private view returns (bool) {
uint256 diff = 0;
address depositor = recipientToDepositorMappings[recipient];
diff = block.number - depositorBlockMappings[depositor];
return (recipientMappings[recipient] > 0) &&
((diff >= oneDayInBlocks) && (diff <= twoDaysInBlocks));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment