Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up code, factory, deployment scripts, more tests #7

Merged
merged 6 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
[submodule "lib/openzeppelin-contracts-upgradeable"]
path = lib/openzeppelin-contracts-upgradeable
url = https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
117 changes: 26 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Foundry Template [![Open in Gitpod][gitpod-badge]][gitpod] [![Github Actions][gha-badge]][gha] [![Foundry][foundry-badge]][foundry] [![License: MIT][license-badge]][license]
# TenderSwap - Unified Liquidity for Staked Assets 🌊 [![Open in Gitpod][gitpod-badge]][gitpod] [![Github Actions][gha-badge]][gha] [![Foundry][foundry-badge]][foundry] [![License: MIT][license-badge]][license]

[gitpod]: https://gitpod.io/#https://github.com/Tenderize/tenderswap
[gitpod-badge]: https://img.shields.io/badge/Gitpod-Open%20in%20Gitpod-FFB45B?logo=gitpod
Expand All @@ -9,81 +9,12 @@
[license]: https://opensource.org/licenses/MIT
[license-badge]: https://img.shields.io/badge/License-MIT-blue.svg

A Foundry-based template for developing Solidity smart contracts, with sensible defaults.
## Overview

## What's Inside
For an overview of the mechanics and implemented mathematics in TenderSwap, check following sources:

- [Forge](https://github.com/foundry-rs/foundry/blob/master/forge): compile, test, fuzz, format, and deploy smart
contracts
- [Forge Std](https://github.com/foundry-rs/forge-std): collection of helpful contracts and cheatcodes for testing
- [PRBTest](https://github.com/PaulRBerg/prb-test): modern collection of testing assertions and logging utilities
- [Prettier](https://github.com/prettier/prettier): code formatter for non-Solidity files
- [Solhint Community](https://github.com/solhint-community/solhint-community): linter for Solidity code

## Getting Started

Click the [`Use this template`](https://github.com/PaulRBerg/foundry-template/generate) button at the top of the page to
create a new repository with this repo as the initial state.

Or, if you prefer to install the template manually:

```sh
$ mkdir my-project
$ cd my-project
$ forge init --template PaulRBerg/foundry-template
$ pnpm install # install Solhint, Prettier, and other Node.js deps
```

If this is your first time with Foundry, check out the
[installation](https://github.com/foundry-rs/foundry#installation) instructions.

## Features

This template builds upon the frameworks and libraries mentioned above, so for details about their specific features,
please consult their respective documentation.

For example, if you're interested in exploring Foundry in more detail, you should look at the
[Foundry Book](https://book.getfoundry.sh/). In particular, you may be interested in reading the
[Writing Tests](https://book.getfoundry.sh/forge/writing-tests.html) tutorial.

### Sensible Defaults

This template comes with a set of sensible default configurations for you to use. These defaults can be found in the
following files:

```text
├── .editorconfig
├── .gitignore
├── .prettierignore
├── .prettierrc.yml
├── .solhint.json
├── foundry.toml
└── remappings.txt
```

### VSCode Integration

This template is IDE agnostic, but for the best user experience, you may want to use it in VSCode alongside Nomic
Foundation's [Solidity extension](https://marketplace.visualstudio.com/items?itemName=NomicFoundation.hardhat-solidity).

For guidance on how to integrate a Foundry project in VSCode, please refer to this
[guide](https://book.getfoundry.sh/config/vscode).

### GitHub Actions

This template comes with GitHub Actions pre-configured. Your contracts will be linted and tested on every push and pull
request made to the `main` branch.

You can edit the CI script in [.github/workflows/ci.yml](./.github/workflows/ci.yml).

## Writing Tests

To write a new test contract, you start by importing [PRBTest](https://github.com/PaulRBerg/prb-test) and inherit from
it in your test contract. PRBTest comes with a pre-instantiated [cheatcodes](https://book.getfoundry.sh/cheatcodes/)
environment accessible via the `vm` property. If you would like to view the logs in the terminal output you can add the
`-vvv` flag and use [console.log](https://book.getfoundry.sh/faq?highlight=console.log#how-do-i-use-consolelog).

This template comes with an example test contract [Foo.t.sol](./test/Foo.t.sol)
- [White Paper](https://whitepaper.tenderize.me/core-architecture/tenderswap)
- [Yellow Paper](https://whitepaper.tenderize.me/tenderswap/yellow-paper)

## Usage

Expand All @@ -94,77 +25,81 @@ This is a list of the most frequently needed commands.
Build the contracts:

```sh
$ forge build
forge build
```

### Clean

Delete the build artifacts and cache directories:

```sh
$ forge clean
forge clean
```

### Compile

Compile the contracts:

```sh
$ forge build
forge build
```

### Coverage

Get a test coverage report:

```sh
$ forge coverage
forge coverage
```

### Deploy

Deploy to Anvil:
Deployments and upgrades can through the `SwapFactory` contract for indexing purposes on the Tenderize Subgraph.

TenderSwap can also be deployed standlone following the same pattern as the `SwapFactory` contract.

### Format

Format the contracts:

```sh
$ forge script script/Deploy.s.sol --broadcast --fork-url http://localhost:8545
forge fmt
```

For this script to work, you need to have a `MNEMONIC` environment variable set to a valid
[BIP39 mnemonic](https://iancoleman.io/bip39/).

For instructions on how to deploy to a testnet or mainnet, check out the
[Solidity Scripting](https://book.getfoundry.sh/tutorials/solidity-scripting.html) tutorial.
or

### Format
```sh
yarn lint:sol
```

Format the contracts:
Format other files:

```sh
$ forge fmt
yarn prettier:write
```

### Gas Usage

Get a gas report:

```sh
$ forge test --gas-report
forge test --gas-report
```

### Lint

Lint the contracts:

```sh
$ pnpm lint
pnpm lint
```

### Test

Run the tests:

```sh
$ forge test
forge test
```

## Notes
Expand Down
4 changes: 2 additions & 2 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

[profile.default]
bytecode_hash = "none"
fuzz = { runs = 1_000 }
fuzz = { runs = 10_000 }
gas_reports = ["*"]
libs = ["lib"]
# optimizer = true (default)
optimizer_runs = 200
fs_permissions = [{ access = "read-write", path = "./" }]
solc = "0.8.19"
solc = "0.8.20"

[profile.ci]
verbosity = 4
Expand Down
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at dbb610
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"scripts": {
"clean": "rm -rf cache out",
"lint": "yarn lint:sol && yarn prettier:write",
"lint:sol": "yarn solhint {src,test}/**/*.sol",
"lint:sol": "forge fmt && yarn solhint src/**/*.sol",
"prettier:check": "prettier --check **/*.{json,md,yml} --ignore-path=.prettierignore",
"prettier:write": "prettier --write **/*.{json,md,yml} --ignore-path=.prettierignore"
}
Expand Down
2 changes: 1 addition & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ solmate/=lib/solmate/src/
@tenderize/stake/=lib/staking/src/
test/=test/
openzeppelin-contracts-upgradeable/=lib/staking/lib/openzeppelin-contracts-upgradeable/contracts/
openzeppelin-contracts/=lib/staking/lib/openzeppelin-contracts/contracts/
openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/
2 changes: 1 addition & 1 deletion script/Add_Liquidity.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.17;

import { Script, console2 } from "forge-std/Script.sol";
import { ERC20 } from "solmate/tokens/ERC20.sol";
import { TenderSwap, Config } from "@tenderize/swap/Swap.sol";
import { TenderSwap, ConstructorConfig } from "@tenderize/swap/Swap.sol";

contract Add_Liquidity is Script {
// Contracts are deployed deterministically.
Expand Down
29 changes: 29 additions & 0 deletions script/Factory_Deploy.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import { Script, console2 } from "forge-std/Script.sol";
import { SwapFactory } from "@tenderize/swap/Factory.sol";
import { ERC1967Proxy } from "openzeppelin-contracts/proxy/ERC1967/ERC1967Proxy.sol";

address constant FACTORY = address(0);

uint256 constant VERSION = 1;

contract Swap_Deploy is Script {
// Contracts are deployed deterministically.
// e.g. `foo = new Foo{salt: salt}(constructorArgs)`
// The presence of the salt argument tells forge to use https://github.com/Arachnid/deterministic-deployment-proxy
bytes32 constant SALT = bytes32(VERSION);

// Start broadcasting with private key from `.env` file
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");

function run() public {
vm.startBroadcast(deployerPrivateKey);
address fac = address(new SwapFactory{ salt: SALT }());
address proxy = address(new ERC1967Proxy{ salt: SALT }(fac, abi.encodeWithSelector(SwapFactory.initialize.selector)));
console2.log("SwapFactory deployed at: ", proxy);
console2.log("Implementation deployed at: ", fac);
vm.stopBroadcast();
}
}
2 changes: 1 addition & 1 deletion script/Stats.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.17;

import { Script, console2 } from "forge-std/Script.sol";
import { ERC20 } from "solmate/tokens/ERC20.sol";
import { TenderSwap, Config } from "@tenderize/swap/Swap.sol";
import { TenderSwap, ConstructorConfig } from "@tenderize/swap/Swap.sol";
import { UD60x18 } from "@prb/math/UD60x18.sol";
import { Tenderizer } from "@tenderize/stake/tenderizer/Tenderizer.sol";
import { StakingXYZ } from "lib/staking/test/helpers/StakingXYZ.sol";
Expand Down
20 changes: 14 additions & 6 deletions script/Swap_Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ pragma solidity ^0.8.17;

import { Script, console2 } from "forge-std/Script.sol";
import { ERC20 } from "solmate/tokens/ERC20.sol";
import { TenderSwap, Config } from "@tenderize/swap/Swap.sol";
import { TenderSwap, ConstructorConfig } from "@tenderize/swap/Swap.sol";
import { SwapFactory } from "@tenderize/swap/Factory.sol";
import { SD59x18 } from "@prb/math/SD59x18.sol";
import { ERC1967Proxy } from "openzeppelin-contracts/proxy/ERC1967/ERC1967Proxy.sol";

address constant FACTORY = address(0);

contract Swap_Deploy is Script {
// Contracts are deployed deterministically.
Expand All @@ -14,14 +19,17 @@ contract Swap_Deploy is Script {
// Start broadcasting with private key from `.env` file
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
address underlying = vm.envAddress("UNDERLYING");
address registry = vm.envAddress("REGISTRY");
address unlocks = vm.envAddress("UNLOCKS");
Config cfg = Config({ underlying: ERC20(underlying), registry: registry, unlocks: unlocks });
SD59x18 BASE_FEE = SD59x18.wrap(vm.envInt("BASE_FEE"));
SD59x18 K = SD59x18.wrap(vm.envInt("K"));

ConstructorConfig cfg = ConstructorConfig({ UNDERLYING: ERC20(underlying), BASE_FEE: BASE_FEE, K: K });

function run() public {
vm.startBroadcast(deployerPrivateKey);
TenderSwap swap = new TenderSwap{salt: salt}(cfg);
console2.log("TenderSwap deployed at: ", address(swap));
(address proxy, address implementation) = SwapFactory(FACTORY).deploy(cfg);
console2.log("Deployment for ", underlying);
console2.log("TenderSwap deployed at: ", proxy);
console2.log("Implementation deployed at: ", implementation);
vm.stopBroadcast();
}
}
68 changes: 68 additions & 0 deletions src/Factory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: MIT
//
// _____ _ _
// |_ _| | | (_)
// | | ___ _ __ __| | ___ _ __ _ _______
// | |/ _ \ '_ \ / _` |/ _ \ '__| |_ / _ \
// | | __/ | | | (_| | __/ | | |/ / __/
// \_/\___|_| |_|\__,_|\___|_| |_/___\___|
//
// Copyright (c) Tenderize Labs Ltd

pragma solidity ^0.8.20;

import { Owned } from "solmate/auth/Owned.sol";
import { ERC1967Proxy } from "openzeppelin-contracts/proxy/ERC1967/ERC1967Proxy.sol";
import { TenderSwap, ConstructorConfig } from "@tenderize/swap/Swap.sol";

import { OwnableUpgradeable } from "openzeppelin-contracts-upgradeable/access/OwnableUpgradeable.sol";
import { Initializable } from "openzeppelin-contracts-upgradeable/proxy/utils/Initializable.sol";
import { UUPSUpgradeable } from "openzeppelin-contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";

// Used for subgraph indexing and atomic deployments

contract SwapFactory is Initializable, UUPSUpgradeable, OwnableUpgradeable {
event SwapDeployed(address underlying, address swap, address implementation);
event SwapUpgraded(address underlying, address swap, address implementation);

mapping(address pool => uint256 v) public version;

function initialize() public initializer {
__Ownable_init();
__UUPSUpgradeable_init();
}

constructor() {
_disableInitializers();
}

function deploy(ConstructorConfig memory cfg) external onlyOwner returns (address proxy, address implementation) {
uint256 v = 1;
// Deploy the implementation
implementation = address(new TenderSwap{ salt: bytes32(v) }(cfg));
// deploy the contract
proxy = address(
new ERC1967Proxy{ salt: bytes32("tenderswap") }(implementation, abi.encodeWithSelector(TenderSwap.initialize.selector))
);

TenderSwap(proxy).transferOwnership(owner());
version[proxy] = v;
emit SwapDeployed(address(cfg.UNDERLYING), proxy, implementation);
}

function upgrade(ConstructorConfig memory cfg, address swapProxy) external onlyOwner returns (address implementation) {
if (TenderSwap(swapProxy).UNDERLYING() != cfg.UNDERLYING) {
revert("SwapFactory: UNDERLYING_MISMATCH");
}

uint256 v = ++version[swapProxy];

implementation = address(new TenderSwap{ salt: bytes32(v) }(cfg));

TenderSwap(swapProxy).upgradeTo(implementation);
}

///@dev required by the OZ UUPS module
// solhint-disable-next-line no-empty-blocks
function _authorizeUpgrade(address) internal override onlyOwner { }
}
2 changes: 1 addition & 1 deletion src/LPToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ contract LPToken is ERC20 {
}

function _encodeSymbol(string memory _symbol) internal pure returns (string memory) {
return string.concat("tSWAP", " ", _symbol);
return string.concat("ts", _symbol);
}
}
Loading
Loading