Created
August 22, 2021 17:43
-
-
Save livingrock7/f9ce0c953af8e47cc525c80663625f70 to your computer and use it in GitHub Desktop.
Dapp Gas Tank Draft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.6.2; | |
pragma experimental ABIEncoderV2; | |
/* | |
* @dev Provides information about the current execution context, including the | |
* sender of the transaction and its data. While these are generally available | |
* via msg.sender and msg.data, they should not be accessed in such a direct | |
* manner, since when dealing with GSN meta-transactions the account sending and | |
* paying for execution may not be the actual sender (as far as an application | |
* is concerned). | |
* | |
* This contract is only required for intermediate, library-like contracts. | |
*/ | |
contract Context { | |
// Empty internal constructor, to prevent people from mistakenly deploying | |
// an instance of this contract, which should be used via inheritance. | |
constructor () internal { } | |
function _msgSender() internal view virtual returns (address payable) { | |
return msg.sender; | |
} | |
function _msgData() internal view virtual returns (bytes memory) { | |
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691 | |
return msg.data; | |
} | |
} | |
/** | |
* @dev Contract module which provides a basic access control mechanism, where | |
* there is an account (an owner) that can be granted exclusive access to | |
* specific functions. | |
* | |
* This module is used through inheritance. It will make available the modifier | |
* `onlyOwner`, which can be applied to your functions to restrict their use to | |
* the owner. | |
*/ | |
contract Ownable is Context { | |
address private _owner; | |
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); | |
/** | |
* @dev Initializes the contract setting the deployer as the initial owner. | |
*/ | |
constructor () internal { | |
address msgSender = _msgSender(); | |
_owner = msgSender; | |
emit OwnershipTransferred(address(0), msgSender); | |
} | |
/** | |
* @dev Returns the address of the current owner. | |
*/ | |
function owner() public view returns (address) { | |
return _owner; | |
} | |
/** | |
* @dev Throws if called by any account other than the owner. | |
*/ | |
modifier onlyOwner() { | |
require(isOwner(), "Ownable: caller is not the owner"); | |
_; | |
} | |
/** | |
* @dev Returns true if the caller is the current owner. | |
*/ | |
function isOwner() public view returns (bool) { | |
return _msgSender() == _owner; | |
} | |
/** | |
* @dev Leaves the contract without owner. It will not be possible to call | |
* `onlyOwner` functions anymore. Can only be called by the current owner. | |
* | |
* NOTE: Renouncing ownership will leave the contract without an owner, | |
* thereby removing any functionality that is only available to the owner. | |
*/ | |
function renounceOwnership() public virtual onlyOwner { | |
emit OwnershipTransferred(_owner, address(0)); | |
_owner = address(0); | |
} | |
/** | |
* @dev Transfers ownership of the contract to a new account (`newOwner`). | |
* Can only be called by the current owner. | |
*/ | |
function transferOwnership(address newOwner) public virtual onlyOwner { | |
_transferOwnership(newOwner); | |
} | |
/** | |
* @dev Transfers ownership of the contract to a new account (`newOwner`). | |
*/ | |
function _transferOwnership(address newOwner) internal virtual { | |
require(newOwner != address(0), "Ownable: new owner is the zero address"); | |
emit OwnershipTransferred(_owner, newOwner); | |
_owner = newOwner; | |
} | |
} | |
/* | |
* @author livingrock (Biconomy) | |
* @title Dapp Deposit Gas Tank Contract | |
* @notice Handles customers deposits | |
*/ | |
contract DappGasTank is Ownable { | |
// Every depositor has a unique identifier. Helps with off-chain tracking. | |
mapping(address => uint256) public depositors; | |
uint256 public uniqueDepositors; | |
address payable public masterAccount; | |
uint256 public minDeposit=1e18; | |
//maintain balances for each funding key | |
mapping(uint256=>uint256) public dappBalances; | |
//do we need to maintain depositor addresses? | |
event Deposit(address indexed sender, uint256 amount, uint256 indexed fundingKey); // fundingKey or index | |
event Withdraw(address indexed actor, uint256 amount, address indexed receiver); | |
event MasterAccountChanged(address indexed account, address indexed actor); | |
function setMinDeposit(uint256 _newMinDeposit) external onlyOwner{ | |
minDeposit = _newMinDeposit; | |
} | |
function setMasterAccount(address payable _newAccount) external onlyOwner{ | |
masterAccount = _newAccount; | |
emit MasterAccountChanged(_newAccount, msg.sender); | |
} | |
//Should we allow Dapps to withdraw their funds? | |
//event WithdrawnByDApp | |
//Should owner be able to pull the funds OR we directly transfer to main account with each transaction | |
//TODO: smart contract conditions on deposit value? must be > outstanding and minimum amount | |
/** | |
* @param _fundingKey Associate funds with this funding key. | |
* Supply a deposit for a specified funding key. | |
* Caution: The funding key must be an your identifier generated from biconomy dashboard | |
*/ | |
function depositFor(uint256 _fundingKey) public payable { | |
require(msg.value > 0, "No value provided to depositFor."); | |
require(msg.value > minDeposit, "Must be grater than minimum deposit for this network"); | |
uint256 index = getDepositorIndex(msg.sender); | |
//Immediately transfers it to masterAccount | |
//review the need. this doesnt go with allowed withdrawals by Dapps | |
masterAccount.transfer(msg.value); | |
dappBalances[_fundingKey] = dappBalances[_fundingKey] + msg.value; | |
emit Deposit(msg.sender, msg.value, _fundingKey); | |
} | |
/** | |
* @param _depositor Depositor address | |
* Sets the depositors index if necessary. | |
*/ | |
function getDepositorIndex(address _depositor) internal returns(uint256) { | |
if(depositors[_depositor] == 0) { | |
uniqueDepositors = uniqueDepositors + 1; | |
depositors[_depositor] = uniqueDepositors; | |
} | |
return depositors[_depositor]; | |
} | |
/* | |
* It is only intended for external users who want to deposit via a wallet. | |
*/ | |
receive() external payable { | |
require(msg.value > 0, "No value provided to fallback."); | |
require(tx.origin == msg.sender, "Only EOA can deposit directly."); | |
uint256 index = getDepositorIndex(msg.sender); | |
emit Deposit(msg.sender, msg.value, index); | |
} | |
/** | |
* Internal function for sending funds. | |
*/ | |
function withdraw(uint256 _amount) public onlyOwner { | |
masterAccount.transfer(_amount); | |
emit Withdraw(msg.sender, _amount, masterAccount); | |
} | |
/* | |
option for Dapps to withdraw from their deposited funds | |
needs to maintain more data structures for this | |
If they are one of the depositors, and map depositor index with funding key | |
*/ | |
/*function withdrawFor(uint256 _amount, uint256 _fundingKey) public { | |
}*/ | |
//should we have migrate funds feature | |
//moving all or specific funds to another gas tank | |
} | |
/** | |
* @dev Wrappers over Solidity's arithmetic operations with added overflow | |
* checks. | |
* | |
* Arithmetic operations in Solidity wrap on overflow. This can easily result | |
* in bugs, because programmers usually assume that an overflow raises an | |
* error, which is the standard behavior in high level programming languages. | |
* `SafeMath` restores this intuition by reverting the transaction when an | |
* operation overflows. | |
* | |
* Using this library instead of the unchecked operations eliminates an entire | |
* class of bugs, so it's recommended to use it always. | |
*/ | |
library SafeMath { | |
/** | |
* @dev Returns the addition of two unsigned integers, reverting on | |
* overflow. | |
* | |
* Counterpart to Solidity's `+` operator. | |
* | |
* Requirements: | |
* - Addition cannot overflow. | |
*/ | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
require(c >= a, "SafeMath: addition overflow"); | |
return c; | |
} | |
/** | |
* @dev Returns the subtraction of two unsigned integers, reverting on | |
* overflow (when the result is negative). | |
* | |
* Counterpart to Solidity's `-` operator. | |
* | |
* Requirements: | |
* - Subtraction cannot overflow. | |
*/ | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
return sub(a, b, "SafeMath: subtraction overflow"); | |
} | |
/** | |
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on | |
* overflow (when the result is negative). | |
* | |
* Counterpart to Solidity's `-` operator. | |
* | |
* Requirements: | |
* - Subtraction cannot overflow. | |
* | |
* _Available since v2.4.0._ | |
*/ | |
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
require(b <= a, errorMessage); | |
uint256 c = a - b; | |
return c; | |
} | |
/** | |
* @dev Returns the multiplication of two unsigned integers, reverting on | |
* overflow. | |
* | |
* Counterpart to Solidity's `*` operator. | |
* | |
* Requirements: | |
* - Multiplication cannot overflow. | |
*/ | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the | |
// benefit is lost if 'b' is also tested. | |
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 | |
if (a == 0) { | |
return 0; | |
} | |
uint256 c = a * b; | |
require(c / a == b, "SafeMath: multiplication overflow"); | |
return c; | |
} | |
/** | |
* @dev Returns the integer division of two unsigned integers. Reverts on | |
* division by zero. The result is rounded towards zero. | |
* | |
* Counterpart to Solidity's `/` operator. Note: this function uses a | |
* `revert` opcode (which leaves remaining gas untouched) while Solidity | |
* uses an invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* - The divisor cannot be zero. | |
*/ | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
return div(a, b, "SafeMath: division by zero"); | |
} | |
/** | |
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on | |
* division by zero. The result is rounded towards zero. | |
* | |
* Counterpart to Solidity's `/` operator. Note: this function uses a | |
* `revert` opcode (which leaves remaining gas untouched) while Solidity | |
* uses an invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* - The divisor cannot be zero. | |
* | |
* _Available since v2.4.0._ | |
*/ | |
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
// Solidity only automatically asserts when dividing by 0 | |
require(b > 0, errorMessage); | |
uint256 c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
/** | |
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
* Reverts when dividing by zero. | |
* | |
* Counterpart to Solidity's `%` operator. This function uses a `revert` | |
* opcode (which leaves remaining gas untouched) while Solidity uses an | |
* invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* - The divisor cannot be zero. | |
*/ | |
function mod(uint256 a, uint256 b) internal pure returns (uint256) { | |
return mod(a, b, "SafeMath: modulo by zero"); | |
} | |
/** | |
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
* Reverts with custom message when dividing by zero. | |
* | |
* Counterpart to Solidity's `%` operator. This function uses a `revert` | |
* opcode (which leaves remaining gas untouched) while Solidity uses an | |
* invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* - The divisor cannot be zero. | |
* | |
* _Available since v2.4.0._ | |
*/ | |
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
require(b != 0, errorMessage); | |
return a % b; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let's use solidity 8.0.0 so that we don't need to use SafeMath library.
At line 151, what is the index variable used for?
Min deposit should be configured at fundingKey level. A default value should be used if minDeposit is not set for that fundingKey
What is the use of depositors mapping and how does it help in offchain tracking?