Skip to content

Commit

Permalink
adjust sample implementation to match interface
Browse files Browse the repository at this point in the history
  • Loading branch information
coburncoburn committed Jun 29, 2023
1 parent 861d031 commit b5efebe
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions EIPS/eip-7246.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ interface IERC7246{
*
* Emits a {Release} event.
*/
function release(address owner, uint amount);
function release(address owner, uint amount) external;
/**
* @dev Convenience function for reading the unencumbered balance of an address.
* Trivially implemented as `balanceOf(owner) - encumberedBalanceOf(owner)`
*/
function availableBalanceOf(address owner) external returns (uint);
function availableBalanceOf(address owner) public view returns (uint);
}
```

Expand All @@ -111,60 +111,53 @@ This can be implemented as an extension of any base [ERC-20](./eip-20.md) contra


``` solidity
// An erc-20 token that implements the encumber interface by blocking transfers.
pragma solidity ^0.8.0;
import {ERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import { IERC7246 } from "./IERC7246.sol";
contract EncumberableERC20 is ERC20 {
contract EncumberableERC20 is ERC20, IERC7246 {
// Owner -> Taker -> Amount that can be taken
mapping (address => mapping (address => uint)) public encumbrances;
// The encumbered balance of the token owner. encumberedBalance must not exceed balanceOf for a user
// Note this means rebasing tokens pose a risk of diminishing and violating this prototocol
mapping (address => uint) public encumberedBalance;
mapping (address => uint) public encumberedBalanceOf;
address public minter;
event Encumber(address indexed owner, address indexed taker, uint encumberedAmount);
event Release(address indexed owner, address indexed taker, uint releasedAmount);
constructor(string memory name, string memory symbol) ERC20(name, symbol) {
minter = msg.sender;
}
function mint(address recipient, uint amount) public virtual returns (bool) {
function mint(address recipient, uint amount) public {
require(msg.sender == minter, "only minter");
_mint(recipient, amount);
return true;
}
function encumber(address taker, uint amount) public virtual returns (bool) {
function encumber(address taker, uint amount) external {
_encumber(msg.sender, taker, amount);
return true;
}
function encumberFrom(address owner, address taker, uint amount) public virtual returns (bool) {
function encumberFrom(address owner, address taker, uint amount) external {
require(allowance(owner, msg.sender) >= amount);
_encumber(owner, taker, amount);
return true;
}
function release(address owner, uint amount) public virtual returns (bool) {
function release(address owner, uint amount) external {
_release(owner, msg.sender, amount);
return true;
}
// If bringing balance and encumbrances closer to equal, must check
function availableBalanceOf(address a) public view returns (uint) {
return (balanceOf(a) - encumberedBalance[a]);
return (balanceOf(a) - encumberedBalanceOf[a]);
}
function _encumber(address owner, address taker, uint amount) private {
require(availableBalanceOf(owner) >= amount, "insufficient balance");
encumbrances[owner][taker] += amount;
encumberedBalance[owner] += amount;
encumberedBalanceOf[owner] += amount;
emit Encumber(owner, taker, amount);
}
Expand All @@ -173,7 +166,7 @@ contract EncumberableERC20 is ERC20 {
amount = encumbrances[owner][taker];
}
encumbrances[owner][taker] -= amount;
encumberedBalance[owner] -= amount;
encumberedBalanceOf[owner] -= amount;
emit Release(owner, taker, amount);
}
Expand Down Expand Up @@ -212,7 +205,7 @@ contract EncumberableERC20 is ERC20 {
require(currentEncumbrance >= amount, "insufficient encumbrance");
uint newEncumbrance = currentEncumbrance - amount;
encumbrances[owner][taker] = newEncumbrance;
encumberedBalance[owner] -= amount;
encumberedBalanceOf[owner] -= amount;
}
}
```
Expand Down

0 comments on commit b5efebe

Please sign in to comment.