Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active May 15, 2021 20:29
Show Gist options
  • Save percybolmer/73e38eb4e559f9d82cb097a41b8c7af2 to your computer and use it in GitHub Desktop.
Save percybolmer/73e38eb4e559f9d82cb097a41b8c7af2 to your computer and use it in GitHub Desktop.
/**
* @notice getOwner just calls Ownables owner function.
* returns owner of the token
*
*/
function getOwner() external view returns (address) {
return owner();
}
/**
* @notice allowance is used view how much allowance an spender has
*/
function allowance(address owner, address spender) external view returns(uint256){
return _allowances[owner][spender];
}
/**
* @notice approve will use the senders address and allow the spender to use X amount of tokens on his behalf
*/
function approve(address spender, uint256 amount) external returns (bool) {
_approve(msg.sender, spender, amount);
return true;
}
/**
* @notice _approve is used to add a new Spender to a Owners account
*
* Events
* - {Approval}
*
* Requires
* - owner and spender cannot be zero address
*/
function _approve(address owner, address spender, uint256 amount) internal {
require(owner != address(0), "DevToken: approve cannot be done from zero address");
require(spender != address(0), "DevToken: approve cannot be to zero address");
// Set the allowance of the spender address at the Owner mapping over accounts to the amount
_allowances[owner][spender] = amount;
emit Approval(owner,spender,amount);
}
/**
* @notice transferFrom is uesd to transfer Tokens from a Accounts allowance
* Spender address should be the token holder
*
* Requires
* - The caller must have a allowance = or bigger than the amount spending
*/
function transferFrom(address spender, address recipient, uint256 amount) external returns(bool){
// Make sure spender is allowed the amount
require(_allowances[spender][msg.sender] >= amount, "DevToken: You cannot spend that much on this account");
// Transfer first
_transfer(spender, recipient, amount);
// Reduce current allowance so a user cannot respend
_approve(spender, msg.sender, _allowances[spender][msg.sender] - amount);
return true;
}
/**
* @notice increaseAllowance
* Adds allowance to a account from the function caller address
*/
function increaseAllowance(address spender, uint256 amount) public returns (bool) {
_approve(msg.sender, spender, _allowances[msg.sender][spender]+amount);
return true;
}
/**
* @notice decreaseAllowance
* Decrease the allowance on the account inputted from the caller address
*/
function decreaseAllowance(address spender, uint256 amount) public returns (bool) {
_approve(msg.sender, spender, _allowances[msg.sender][spender]-amount);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment