Skip to content

Instantly share code, notes, and snippets.

@samanshahmohamadi
Created August 25, 2019 14:45
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 samanshahmohamadi/7faed6c36b221147b74d54d466189c5e to your computer and use it in GitHub Desktop.
Save samanshahmohamadi/7faed6c36b221147b74d54d466189c5e 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.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity >=0.4.22 <0.6.0;
contract owned {
address public owner = msg.sender;
uint public creationTime = now;
modifier onlyOwner {
require(
msg.sender == owner,
"Access restricted."
);
_;
}
}
contract Paybox is owned {
struct Transaction {
bytes id; //payment id
bool paid; //is paidback to acceptor
address addr; //payment address
uint amount; //amount in wei
uint balance; //balance in wei
uint created; //created date
}
mapping(address => Transaction) public transactions;
function () external payable {
receive('accidental_payment',msg.sender, now);
}
function receive(bytes memory _id, address _addr, uint _created) onlyOwner public payable {
uint amount = msg.value;
require(msg.value > 0, "No value was sent with transaction");
require(_id.length != 0, "Id is required");
require(_addr != address(0), "Address is required");
Transaction memory trx = Transaction({
id: _id,
paid: false,
addr: _addr,
amount: amount,
balance: amount,
created: _created
});
transactions[_addr] = trx;
}
function send(address _from, address payable _to, uint _amount) onlyOwner public {
require(_amount > 0, "No value was sent with transaction");
require(_from != address(0), "FromAddress is required");
require(_to != address(0), "ToAddress is required");
Transaction storage txr = transactions[_from];
require(txr.balance >= _amount, "Transaction balance is not sufficient");
if (_amount == txr.amount)
txr.paid = true;
txr.balance -= _amount;
address(_to).transfer(_amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment