Skip to content

Instantly share code, notes, and snippets.

@mujuni88
Created February 26, 2022 22:11
Show Gist options
  • Save mujuni88/8f875277d1728f5a3e0cc3fcabf14fb2 to your computer and use it in GitHub Desktop.
Save mujuni88/8f875277d1728f5a3e0cc3fcabf14fb2 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.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "hardhat/console.sol";
contract EscrowAgent {
address public agent;
enum Stages {
NONE,
IN_PROGRESS,
COMPLETED
}
struct Service {
uint amount;
Stages stage;
}
mapping(address => Service) public deposits;
constructor(address _owner){
agent = _owner;
}
modifier isAgent() {
console.log("AGENT & SENDER %s => %s", agent, msg.sender);
require(agent == msg.sender, "Only agent allowed call this method");
_;
}
modifier hasDeposit(address _receiver) {
require(deposits[_receiver].stage != Stages.NONE, "No deposits have been made");
_;
}
modifier canComplete(address _receiver) {
require(deposits[_receiver].stage == Stages.IN_PROGRESS, "Can not complete a service that was never started");
_;
}
modifier canWithdaw(address _receiver) {
require(deposits[_receiver].stage == Stages.COMPLETED, "Can not withdraw until the service is completed");
_;
}
function deposit(address _receiver) payable public isAgent {
Service storage service = deposits[_receiver];
service.amount += msg.value;
service.stage = Stages.IN_PROGRESS;
}
function completed(address _receiver) public isAgent canComplete(_receiver) {
deposits[_receiver].stage = Stages.COMPLETED;
}
function withdraw(address payable _receiver) public isAgent canWithdaw(_receiver) {
Service storage service = deposits[_receiver];
_receiver.transfer(service.amount);
service.amount = 0;
service.stage = Stages.NONE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment