Smart contract design for gas refund

by Amxx   Last Updated November 12, 2018 15:28 PM - source

I'm designing a proxy smartcontract that will be filled by users so that anyone has an incentive to can perform a transaction useful to the user. I won't go into the detail of "usefulness" here.

My contract is a proxy that calls a function XXX(data). So far I've got this:

contract Broker
    using SafeMathOZ for uint256;
    Target                      public target;
    mapping(address => uint256) public m_balance;
    mapping(address => uint256) public m_reward;

    constructor(address _target) public {
        target = Target(_target);

    function () public payable {
        m_balance[msg.sender] = m_balance[msg.sender].add(msg.value);

    function deposit() public payable {
        m_balance[msg.sender] = m_balance[msg.sender].add(msg.value);

    function depositFor(address _account) public payable {
        m_balance[_account] = m_balance[_account].add(msg.value);

    function withdraw(uint256 _amount) public {
        m_balance[msg.sender] = m_balance[msg.sender].sub(_amount);

    function setReward(uint256 _reward) public {
        m_reward[msg.sender] = _reward;

    function XXX(Data data) public returns (bytes32) {
        uint256 gasBefore = gasleft();
        bytes32 result = target.XXX(data);
        address payer = data.requester;
        uint256 price = tx.gasprice * (87000 + gasBefore - gasleft()) + m_reward[payer];
        m_balance[payer] = m_balance[payer].sub(price);
        return result;

It works as expected and rewards the sender as I expect. What I am worried about is the possibility of a message sender to provide a very high gas price, thus draining the user balance and "giving" it to the miner :/

Any idea on how to solve this issue? Is there a better way then having the user specify a "maximum gas cost"?

Related Questions

Upgradeable smart contracts

Updated October 01, 2017 15:28 PM

Updating content o af smart contract

Updated July 10, 2018 22:28 PM

Find word in string solidity

Updated April 05, 2019 11:28 AM

Reason for check of msg.value > 0

Updated April 30, 2019 14:28 PM