Skip to content

Commit

Permalink
Add prettier to format solidity code (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
lakonema2000 authored Jun 1, 2024
1 parent ab98268 commit 8daa22b
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 80 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Code Format Check

on:
push:
branches:
- main
pull_request:

jobs:
prettier-check:
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '14' # You can specify any version of Node.js that matches your project requirements

- name: Cache Node.js modules
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install dependencies
run: npm install

- name: Run Prettier check
run: npm run prettier:check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Compiler files
cache/
out/
node_modules/

# Ignores development broadcast logs
!/broadcast
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib/
out/
16 changes: 12 additions & 4 deletions git_hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
#!/bin/sh

# Get the current branch name
branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

# Make sure branch != main branch

# Block commits to the main branch
if [ "$branch" = "main" ]; then
echo "You are on the main branch. Commit blocked."
exit 1
fi

exit 0
# Run Prettier and add all changed files to the commit
npm run prettier:check
if [ $? -ne 0 ]; then
echo "Prettier found issues that need to be resolved."
exit 1
fi

# Make sure branch name starts will allowed prefixes
git add -A

# Allowed branch prefixes
PREFIXES="feat/ fix/ doc/ chore/ refactor/ test/"

# Check if branch name starts with an allowed prefix
for prefix in $PREFIXES; do
case $branch in
$prefix*)
Expand All @@ -23,5 +30,6 @@ for prefix in $PREFIXES; do
esac
done

# If none of the prefixes match, block the commit
echo "Commit blocked: Branch name must start with one of the following prefixes: $PREFIXES"
exit 1
78 changes: 78 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "nova-vault",
"version": "1.0.0",
"description": "## introduction",
"main": "index.js",
"directories": {
"lib": "lib",
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"prettier:check": "prettier --plugin=prettier-plugin-solidity --check \"src/**/*.sol\" \"test/**/*.sol\" \"script/**/*.sol\"",
"prettier:write": "prettier --plugin=prettier-plugin-solidity --write \"src/**/*.sol\" \"test/**/*.sol\" \"script/**/*.sol\""
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"prettier": "^3.2.5",
"prettier-plugin-solidity": "^1.3.1"
}
}
37 changes: 20 additions & 17 deletions src/NovaAdapterBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,49 @@ pragma solidity ^0.8.13;
import {IERC20} from "./interfaces/IERC20.sol";

abstract contract NovaAdapterBase {

error TransferFailed(
address sender,
address recipient,
uint256 amount
);
error TransferFailed(address sender, address recipient, uint256 amount);

address immutable sDAI;
address immutable asset;

constructor(
address _asset,
address _sDAI
) {
constructor(address _asset, address _sDAI) {
asset = _asset;
sDAI = _sDAI;
}

function deposit(uint256 assets) external returns (bool, uint256) {
bool successFirst = IERC20(asset).transferFrom(msg.sender, address(this), assets);
bool successFirst = IERC20(asset).transferFrom(
msg.sender,
address(this),
assets
);

(, int256 sDaiOut) = _swap(int256(assets), true);
uint256 sDaiToTransfer = uint256(-sDaiOut);
bool successSecond = IERC20(sDAI).transfer(msg.sender, sDaiToTransfer);

if(!successFirst || !successSecond){
if (!successFirst || !successSecond) {
revert TransferFailed(msg.sender, address(this), assets);
}

return (true, sDaiToTransfer);
}

function withdraw(uint256 shares) external returns (bool, uint256) {
bool successFirst = IERC20(sDAI).transferFrom(msg.sender, address(this), shares);
bool successFirst = IERC20(sDAI).transferFrom(
msg.sender,
address(this),
shares
);

(int256 assets, ) = _swap(int256(shares), false);
uint256 assetsToTransfer = uint256(-assets);
bool successSecond = IERC20(asset).transfer(msg.sender, assetsToTransfer);
bool successSecond = IERC20(asset).transfer(
msg.sender,
assetsToTransfer
);

if(!successFirst || !successSecond){
if (!successFirst || !successSecond) {
revert TransferFailed(msg.sender, address(this), assetsToTransfer);
}

Expand All @@ -54,7 +57,7 @@ abstract contract NovaAdapterBase {
* @notice Performs a swap operation between the stable asset and sDAI.
* @dev This function interacts with the pool to execute the swap.
* @param amount The amount to be swapped.
* @param fromStableTosDai A boolean indicating the direction of the swap.
* @param fromStableTosDai A boolean indicating the direction of the swap.
* - `true` for swapping from the stable asset to sDAI.
* - `false` for swapping from sDAI to the stable asset.
* @return amount0 The amount of token0 involved in the swap.
Expand All @@ -64,4 +67,4 @@ abstract contract NovaAdapterBase {
int256 amount,
bool fromStableTosDai
) internal virtual returns (int256, int256);
}
}
7 changes: 3 additions & 4 deletions src/NovaAdapterVelo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {IERC20} from "./interfaces/IERC20.sol";
import {NovaAdapterBase} from "./NovaAdapterBase.sol";

contract NovaAdapterVelo is NovaAdapterBase {

bool private isStableFirst;
address immutable veloToken0;
address immutable veloToken1;
Expand Down Expand Up @@ -38,10 +37,10 @@ contract NovaAdapterVelo is NovaAdapterBase {
) external {
require(msg.sender == address(veloPool), "Caller is not VelodromePool");

if (amount0Delta > 0){
if (amount0Delta > 0) {
IERC20(veloToken0).transfer(msg.sender, uint256(amount0Delta));
}
if (amount1Delta > 0){
if (amount1Delta > 0) {
IERC20(veloToken1).transfer(msg.sender, uint256(amount1Delta));
}
}
Expand All @@ -53,7 +52,7 @@ contract NovaAdapterVelo is NovaAdapterBase {
(uint160 sqrtPriceX96, , , , , ) = veloPool.slot0();
uint160 num = fromStableTosDai ? 95 : 105;
int256 sign = isStableFirst ? int256(1) : int256(-1);

(int256 amount0, int256 amount1) = veloPool.swap(
address(this),
fromStableTosDai,
Expand Down
40 changes: 22 additions & 18 deletions src/NovaVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ contract NovaVault is INovaVault {
}
}

function _approveAdapter(
address stable,
address adapter
) internal {
function _approveAdapter(address stable, address adapter) internal {
require(stable != address(0), Errors.INVALID_ADDRESS);

require(
Expand All @@ -45,7 +42,7 @@ contract NovaVault is INovaVault {
);

address underlyingAsset = INovaAdapterBase(adapter).getAsset();

require(
underlyingAsset == stable,
Errors.INVALID_STABLE_TO_ADAPTER_MAPPING
Expand All @@ -55,20 +52,24 @@ contract NovaVault is INovaVault {
emit AdapterApproval(stable, adapter);
}

function deposit(address stable, uint256 assets, uint16 referral) external returns (bool, uint256) {
function deposit(
address stable,
uint256 assets,
uint16 referral
) external returns (bool, uint256) {
address adapter = _novaAdapters[stable];
require(
adapter != address(0),
Errors.NO_ADAPTER_APPROVED
);
require(adapter != address(0), Errors.NO_ADAPTER_APPROVED);

IERC20(stable).transferFrom(msg.sender, address(this), assets);
IERC20(stable).approve(adapter, assets);

(bool success, bytes memory data) = adapter.call(
abi.encodeWithSignature("deposit(uint256)", assets)
);
(bool successDeposit, uint256 sDaiAmount) = abi.decode(data, (bool, uint256));
(bool successDeposit, uint256 sDaiAmount) = abi.decode(
data,
(bool, uint256)
);
require(success && successDeposit, "Deposit failed");

IERC20(sDAI).transfer(msg.sender, sDaiAmount);
Expand All @@ -78,25 +79,28 @@ contract NovaVault is INovaVault {
return (true, sDaiAmount);
}

function withdraw(address stable, uint256 shares) external returns (bool, uint256) {
function withdraw(
address stable,
uint256 shares
) external returns (bool, uint256) {
address adapter = _novaAdapters[stable];
require(
adapter != address(0),
Errors.NO_ADAPTER_APPROVED
);
require(adapter != address(0), Errors.NO_ADAPTER_APPROVED);

IERC20(sDAI).transferFrom(msg.sender, address(this), shares);
IERC20(sDAI).approve(adapter, shares);

(bool success, bytes memory data) = adapter.call(
abi.encodeWithSignature("withdraw(uint256)", shares)
);
(bool successWithdraw, uint256 assetsAmount) = abi.decode(data, (bool, uint256));
(bool successWithdraw, uint256 assetsAmount) = abi.decode(
data,
(bool, uint256)
);

require(success && successWithdraw, "Withdraw failed");

IERC20(stable).transfer(msg.sender, assetsAmount);

return (true, assetsAmount);
}
}
}
Loading

0 comments on commit 8daa22b

Please sign in to comment.