Skip to content

Commit

Permalink
working vault inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
lorbke committed Nov 16, 2024
1 parent 0b10efc commit 0a67098
Show file tree
Hide file tree
Showing 5 changed files with 801 additions and 49 deletions.
16 changes: 14 additions & 2 deletions packages/foundry/contracts/HumanOracleWithVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ pragma solidity ^0.8.28;

import {IWorldID} from "../lib/world-id-onchain-template/contracts/src/interfaces/IWorldID.sol";
import {ByteHasher} from "./ByteHasher.sol";
import {Permit2Vault} from "./Permit2Vault.sol";
import {IERC20} from "../interfaces/IERC20.sol";
// import "forge-std/console.sol";

contract HumanOracleWithVault {
contract HumanOracleWithVault is Permit2Vault {

using ByteHasher for bytes;

Expand Down Expand Up @@ -116,7 +118,7 @@ contract HumanOracleWithVault {
// === Constructor ====
// ====================

constructor(address _worldIdAddr, uint256 _groupId, string memory _appId, string memory _action) {
constructor(address _worldIdAddr, uint256 _groupId, string memory _appId, string memory _action, address _permit, address _owner) Permit2Vault(_permit, _owner) {
worldId = IWorldID(_worldIdAddr);
groupId = _groupId;
externalNullifierHash = abi.encodePacked(abi.encodePacked(_appId).hashToField(), _action).hashToField();
Expand Down Expand Up @@ -254,6 +256,16 @@ contract HumanOracleWithVault {
return getStakeResolvedUserAmount(userAddr, voteId);
}

// function depositERC20(
// IERC20 token,
// uint256 amount,
// uint256 nonce,
// uint256 deadline,
// bytes calldata signature
// ) public {
// Permit2Vault.depositERC20(token, amount, nonce, deadline, signature);
// }

// internal

// stake related
Expand Down
14 changes: 11 additions & 3 deletions packages/foundry/contracts/Permit2Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.28;

import "../interfaces/IPermit2.sol";
import "../interfaces/IERC20.sol";
import "../lib/openzeppelin-contracts/contracts/access/Ownable.sol";

// Trivial vault that allows users to deposit ERC20 tokens then claim them later.
contract Permit2Vault is Ownable {
Expand All @@ -11,11 +12,12 @@ contract Permit2Vault is Ownable {
IPermit2 public immutable PERMIT2;
// User -> token -> deposit balance
mapping (address => mapping (IERC20 => uint256)) public tokenBalancesByUser;
uint256 public totalBalance;

constructor(IPermit2 permit_) {
PERMIT2 = permit_;
constructor(address _permit, address _owner) Ownable(_owner) {
PERMIT2 = IPermit2(_permit);
}

// Prevents reentrancy attacks via tokens with callback mechanisms.
modifier nonReentrant() {
require(!_reentrancyGuard, 'no reentrancy');
Expand All @@ -35,6 +37,7 @@ contract Permit2Vault is Ownable {
) external nonReentrant {
// Credit the caller.
tokenBalancesByUser[msg.sender][token] += amount;
totalBalance += amount;
// Transfer tokens from the caller to ourselves.
PERMIT2.permitTransferFrom(
// The permit message. Spender will be inferred as the caller (us).
Expand Down Expand Up @@ -64,6 +67,7 @@ contract Permit2Vault is Ownable {
// Return ERC20 tokens deposited by the caller.
function withdrawERC20(IERC20 token, uint256 amount) external nonReentrant {
tokenBalancesByUser[msg.sender][token] -= amount;
totalBalance -= amount;
// TODO: In production, use an ERC20 compatibility library to
// execute thie transfer to support non-compliant tokens.
token.transfer(msg.sender, amount);
Expand All @@ -77,4 +81,8 @@ contract Permit2Vault is Ownable {
permissions[i] = IPermit2.TokenPermissions({ token: tokens[i], amount: amounts[i] });
}
}

function rescueTokens(IERC20 token, address recipient) onlyOwner() external {
token.transfer(recipient, totalBalance);
}
}
Loading

0 comments on commit 0a67098

Please sign in to comment.