Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into bitcoin-redeemer
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuba committed Apr 3, 2024
2 parents e9b5092 + 1e05e0e commit 8e779c2
Show file tree
Hide file tree
Showing 36 changed files with 4,278 additions and 178 deletions.
124 changes: 124 additions & 0 deletions .github/workflows/subgraph.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
name: Subgraph

on:
push:
branches:
- main
paths:
- "subgraph/**"
pull_request:

defaults:
run:
working-directory: ./subgraph

jobs:
subgraph-codegen:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up pnpm
uses: pnpm/action-setup@v3

- name: Set up Node
uses: actions/setup-node@v4
with:
node-version-file: "subgraph/.nvmrc"
cache: "pnpm"

- name: Install Dependencies
run: pnpm install --prefer-offline --frozen-lockfile

- name: Codegen
run: pnpm run codegen

- name: Upload Codegen Artifacts
uses: actions/upload-artifact@v4
with:
name: subgraph-codegen
path: |
subgraph/generated
if-no-files-found: error

subgraph-format:
needs: [subgraph-codegen]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up pnpm
uses: pnpm/action-setup@v3

- name: Set up Node
uses: actions/setup-node@v4
with:
node-version-file: "subgraph/.nvmrc"
cache: "pnpm"

- name: Download Codegen Artifacts
uses: actions/download-artifact@v4
with:
name: subgraph-codegen
path: subgraph/generated

- name: Install Dependencies
run: pnpm install --prefer-offline --frozen-lockfile

- name: Format
run: pnpm run format

subgraph-build:
needs: [subgraph-codegen]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up pnpm
uses: pnpm/action-setup@v3

- name: Set up Node
uses: actions/setup-node@v4
with:
node-version-file: "subgraph/.nvmrc"
cache: "pnpm"

- name: Download Codegen Artifacts
uses: actions/download-artifact@v4
with:
name: subgraph-codegen
path: subgraph/generated

- name: Install Dependencies
run: pnpm install --prefer-offline --frozen-lockfile

- name: Build
run: pnpm run build

subgraph-test:
needs: [subgraph-codegen]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up pnpm
uses: pnpm/action-setup@v3

- name: Set up Node
uses: actions/setup-node@v4
with:
node-version-file: "subgraph/.nvmrc"
cache: "pnpm"

- name: Download Codegen Artifacts
uses: actions/download-artifact@v4
with:
name: subgraph-codegen
path: subgraph/generated

- name: Install Dependencies
run: pnpm install --prefer-offline --frozen-lockfile

- name: Tests
run: pnpm run test

18 changes: 17 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
- id: root-lint
name: "lint root"
entry: /usr/bin/env bash -c "npm run format"
exclude: (^core/|^dapp/|^website/)
exclude: (^core/|^dapp/|^website/|^subgraph/)
language: script
description: "Checks code according to the package's linter configuration"
# Core
Expand Down Expand Up @@ -75,3 +75,19 @@ repos:
types_or: [json, yaml]
language: script
description: "Checks JSON/YAML code according to the package's linter configuration"
# Subgraph
- id: subgraph-lint-js
name: "lint subgraph ts/js"
entry: /usr/bin/env bash -c "npm --prefix ./subgraph/ run lint:js"
files: ^subgraph/
types_or: [ts, javascript]
language: script
description: "Checks TS/JS code according to the package's linter configuration"
- id: subgraph-lint-config
name: "lint subgraph json/yaml"
entry: /usr/bin/env bash -c "npm --prefix ./subgraph/ run lint:config"
files: ^subgraph/
types_or: [json, yaml]
language: script
description: "Checks JSON/YAML code according to the package's linter configuration"

1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ core/
dapp/
website/
sdk/
subgraph/

# Auto-generated files.
pnpm-lock.yaml
128 changes: 128 additions & 0 deletions core/contracts/lib/ERC4626Fees.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// SPDX-License-Identifier: MIT

// Inspired by https://docs.openzeppelin.com/contracts/5.x/erc4626#fees

pragma solidity ^0.8.21;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ERC4626Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC4626Upgradeable.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";

/// @dev ERC4626 vault with entry/exit fees expressed in https://en.wikipedia.org/wiki/Basis_point[basis point (bp)].
abstract contract ERC4626Fees is ERC4626Upgradeable {
using Math for uint256;

uint256 private constant _BASIS_POINT_SCALE = 1e4;

// === Overrides ===

/// @dev Preview taking an entry fee on deposit. See {IERC4626-previewDeposit}.
function previewDeposit(
uint256 assets
) public view virtual override returns (uint256) {
uint256 fee = _feeOnTotal(assets, _entryFeeBasisPoints());
return super.previewDeposit(assets - fee);
}

/// @dev Preview adding an entry fee on mint. See {IERC4626-previewMint}.
function previewMint(
uint256 shares
) public view virtual override returns (uint256) {
uint256 assets = super.previewMint(shares);
return assets + _feeOnRaw(assets, _entryFeeBasisPoints());
}

/// @dev Preview adding an exit fee on withdraw. See {IERC4626-previewWithdraw}.
function previewWithdraw(
uint256 assets
) public view virtual override returns (uint256) {
uint256 fee = _feeOnRaw(assets, _exitFeeBasisPoints());
return super.previewWithdraw(assets + fee);
}

/// @dev Preview taking an exit fee on redeem. See {IERC4626-previewRedeem}.
function previewRedeem(
uint256 shares
) public view virtual override returns (uint256) {
uint256 assets = super.previewRedeem(shares);
return assets - _feeOnTotal(assets, _exitFeeBasisPoints());
}

/// @dev Send entry fee to {_feeRecipient}. See {IERC4626-_deposit}.
function _deposit(
address caller,
address receiver,
uint256 assets,
uint256 shares
) internal virtual override {
uint256 fee = _feeOnTotal(assets, _entryFeeBasisPoints());
address recipient = _feeRecipient();

super._deposit(caller, receiver, assets, shares);

if (fee > 0 && recipient != address(this)) {
SafeERC20.safeTransfer(IERC20(asset()), recipient, fee);
}
}

/// @dev Send exit fee to {_exitFeeRecipient}. See {IERC4626-_deposit}.
function _withdraw(
address caller,
address receiver,
address owner,
uint256 assets,
uint256 shares
) internal virtual override {
uint256 fee = _feeOnRaw(assets, _exitFeeBasisPoints());
address recipient = _feeRecipient();

super._withdraw(caller, receiver, owner, assets, shares);

if (fee > 0 && recipient != address(this)) {
SafeERC20.safeTransfer(IERC20(asset()), recipient, fee);
}
}

// === Fee configuration ===

// slither-disable-next-line dead-code
function _entryFeeBasisPoints() internal view virtual returns (uint256);

// slither-disable-next-line dead-code
function _exitFeeBasisPoints() internal view virtual returns (uint256);

// slither-disable-next-line dead-code
function _feeRecipient() internal view virtual returns (address);

// === Fee operations ===

/// @dev Calculates the fees that should be added to an amount `assets`
/// that does not already include fees.
/// Used in {IERC4626-mint} and {IERC4626-withdraw} operations.
function _feeOnRaw(
uint256 assets,
uint256 feeBasisPoints
) private pure returns (uint256) {
return
assets.mulDiv(
feeBasisPoints,
_BASIS_POINT_SCALE,
Math.Rounding.Ceil
);
}

/// @dev Calculates the fee part of an amount `assets` that already includes fees.
/// Used in {IERC4626-deposit} and {IERC4626-redeem} operations.
function _feeOnTotal(
uint256 assets,
uint256 feeBasisPoints
) private pure returns (uint256) {
return
assets.mulDiv(
feeBasisPoints,
feeBasisPoints + _BASIS_POINT_SCALE,
Math.Rounding.Ceil
);
}
}
Loading

0 comments on commit 8e779c2

Please sign in to comment.