Code Monkey home page Code Monkey logo

mstable-contracts's Introduction


CI Coverage Status Discord npm version


This repo contains all contracts and tests relevant to the core mStable protocol. mStable is a protocol built to make stablecoins easy, robust and profitable.

mStable Assets (mAssets) are tokens that allow minting and redemption for underlying Basket Assets (bAssets) of the same peg (i.e. USD, BTC, Gold). The InvariantValidator applies both min and max weights to these bAssets, and enforces penalties and bonuses when minting with these assets to provide low slippage swaps. This Invariant applies progressive penalties and bonuses on either end of the weight scale - having the result of having a large area of low slippage. bAssets are integrated with lending protocols (initially Aave, Compound) to generate interest which is accrued in mAsset terms. mAssets can be deposited to earn native interest through their respective Savings Contract, just like you would with a savings account. bAssets within an mAsset can also be swapped with low slippage (provided they remain within their validator limits), with a swapFee credited additionally to Savers.

Core mAsset contracts utilise OpenZeppelin's InitializableAdminUpgradeabilityProxy to facilitate future upgrades, fixes or feature additions. The upgrades are proposed by the mStable Governors (with current governor address stored in the Nexus - the system kernel) and executed via the DelayedProxyAdmin. Both changes to the governor, and contract upgrades have a one week delay built in to execution. This allows mStable users a one week opt out window if they do not agree with the given change.

mStable rewards those who contribute to its utility and growth - for more information see MTA.


๐Ÿ  https://mstable.org
๐Ÿ“€ https://app.mstable.org
๐Ÿ“„ https://docs.mstable.org


Bug bounty

Found a bug? Claim a reward from our open Bug Bounty by reporting it to mStable (following the responsible disclosure policy)




Branches

  • master contains complete, tested and audited contract code, generally on mainnet
  • beta is for the pre-release code, generally on ropsten

Artifacts

We publish the contract artifacts to an npm package called @mstable/protocol. You can browse them via unpkg.com.


Dev notes

Prerequisites

Installing dependencies

$ yarn

Testing

Tests are written with Hardhat, Ethers, Waffle & Typescript, using Typechain to generate typings for all contracts. Tests are executed using hardhat in hardhats evm.

$ yarn test

Suite

Key folders:

  • /contracts/z_mocks: All mocks used throughout the test suite
  • /security: Scripts used to run static analysis tools like Slither and Securify
  • /tasks: Hardhat tasks that run operational reports and transactions.
  • /test: Unit tests in folders corresponding to contracts/xx
  • /test-utils: Core util files used throughout the test framework
    • /machines: Mock contract machines for creating configurable instances of the contracts
  • /types: TS Types used throughout the suite
    • /generated: Output from Typechain; strongly-typed, Ethers-flavoured contract interfaces

Coverage

Solidity-coverage is used to run coverage analysis on test suite.

This produces reports that are visible in the /coverage folder, and navigatable/uploadable. Ultimately they are used as a reference that there is some sort of adequate cover, although they will not be a source of truth for a robust test framework. Reports publically available on coveralls.

NB: solidity-coverage runs with solc optimizer=false (see discussion)

CI

Codebase rules are enforced through a passing GitHub Actions (workflow configs are in .github/workflows). These rules are:

  • Linting of both the contracts (through Solium) and TS files (ESLint)
  • Passing unit test suite
  • Maintaining high unit testing coverage

Code formatting

  • Solidity imports deconstructed as import { xxx } from "../xxx.sol"
  • Solidity commented as per NatSpec format
  • Internal function ordering from high > low order

Command Line Interface

Hardhat Tasks are used for command line interactions with the mStable contracts. The tasks can be found in the tasks folder.

A separate Hardhat config file tasks.config.ts is used for task config. This inherits from the main Hardhat config file hardhat.config.ts. This avoids circular dependencies when the repository needs to be compiled before the Typechain artifacts have been generated. This means the --config tasks.config.ts Hardhat option needs to be used to run the mStable tasks.

Config your network. If you are just using readonly tasks like mBTC-snap you don't need to have a signer with Ether in it so the default Hardhat test account is ok to use. For safety, the mainnet config is not committed to the repository to avoid accidentally running tasks against mainnet.

mainnet: {
    url: process.env.NODE_URL || "",
    accounts: {
        mnemonic: "test test test test test test test test test test test junk",
    },
},

Never commit mainnet private keys, mnemonics or provider URLs to the repository.

Examples of using the Hardhat tasks

# List all Hardhat tasks
hh --config tasks.config.ts

# Set the provider url
export NODE_URL=https://mainnet.infura.io/v3/yourApiKey

# To run the mBTC-snap task against mainnet
yarn task mBTC-snap --network mainnet

mstable-contracts's People

Contributors

alsco77 avatar chrisjgf avatar dependabot[bot] avatar dimsome avatar doncesarts avatar jameslefrere avatar jchittoda avatar lovrobiljeskovic avatar naddison36 avatar paulrberg avatar shapeshed avatar stefanionescu avatar stobiewan avatar tanliwei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mstable-contracts's Issues

Factory contract to support permissionless fPool creation

Is your feature request related to a problem? Please describe.
Users wish to deploy their own feeder pools permissionlessly

Describe the solution you'd like
Factory contract with configurable parameters to deploy feeder pools, and test cases to support this

TS error: Cannot find namespace 'Truffle'

I'm getting the following error when deploying the contracts with yarn migrate to a local network with ganache:

yarn run v1.22.4
$ truffle migrate --network development --reset

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.



Starting migrations...
======================
> Network name:    'development'
> Network id:      5777
> Block gas limit: 6721975 (0x6691b7)


1_main.js
=========

/home/gus/Projects/mstable/mStable-contracts/node_modules/ts-node/dist/index.js:175
            throw error;
            ^

TSError: โจฏ Unable to compile TypeScript:
migrations/src/1_initial_migration.ts:9:20 - error TS2503: Cannot find namespace 'Truffle'.

9         artifacts: Truffle.Artifacts;
                     ~~~~~~~
migrations/src/1_initial_migration.ts:2:22 - error TS6053: File '/home/gus/Projects/mstable/mStable-contracts/types/generated/index.d.ts' not found.

2 /// <reference path="../../types/generated/index.d.ts" />
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
migrations/src/1_initial_migration.ts:3:22 - error TS6053: File '/home/gus/Projects/mstable/mStable-contracts/types/generated/types.d.ts' not found.

3 /// <reference path="../../types/generated/types.d.ts" />
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    at createTSError (/home/gus/Projects/mstable/mStable-contracts/node_modules/ts-node/src/index.ts:245:12)
    at reportTSError (/home/gus/Projects/mstable/mStable-contracts/node_modules/ts-node/src/index.ts:249:19)
    at getOutput (/home/gus/Projects/mstable/mStable-contracts/node_modules/ts-node/src/index.ts:357:34)
    at Object.compile (/home/gus/Projects/mstable/mStable-contracts/node_modules/ts-node/src/index.ts:415:32)
    at Module.m._compile (/home/gus/Projects/mstable/mStable-contracts/node_modules/ts-node/src/index.ts:493:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Object.require.extensions.<computed> [as .ts] (/home/gus/Projects/mstable/mStable-contracts/node_modules/ts-node/src/index.ts:496:12)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Module.require (internal/modules/cjs/loader.js:1026:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at require (/home/gus/Projects/mstable/mStable-contracts/node_modules/truffle/build/webpack:/packages/require/require.js:54:1)
    at /home/gus/Projects/mstable/mStable-contracts/migrations/1_main.js:4:26
    at Script.runInContext (vm.js:131:20)
    at Script.runInNewContext (vm.js:137:17)
    at Object.file (/home/gus/Projects/mstable/mStable-contracts/node_modules/truffle/build/webpack:/packages/require/require.js:92:1)
    at Migration._load (/home/gus/Projects/mstable/mStable-contracts/node_modules/truffle/build/webpack:/packages/migrate/migration.js:43:1)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at Migration.run (/home/gus/Projects/mstable/mStable-contracts/node_modules/truffle/build/webpack:/packages/migrate/migration.js:167:1)
    at Object.runMigrations (/home/gus/Projects/mstable/mStable-contracts/node_modules/truffle/build/webpack:/packages/migrate/index.js:148:1)
    at Object.runFrom (/home/gus/Projects/mstable/mStable-contracts/node_modules/truffle/build/webpack:/packages/migrate/index.js:110:1)
    at Object.runAll (/home/gus/Projects/mstable/mStable-contracts/node_modules/truffle/build/webpack:/packages/migrate/index.js:114:1)
    at Object.run (/home/gus/Projects/mstable/mStable-contracts/node_modules/truffle/build/webpack:/packages/migrate/index.js:79:1)
    at runMigrations (/home/gus/Projects/mstable/mStable-contracts/node_modules/truffle/build/webpack:/packages/core/lib/commands/migrate.js:253:1)
    at /home/gus/Projects/mstable/mStable-contracts/node_modules/truffle/build/webpack:/packages/core/lib/commands/migrate.js:218:1
Truffle v5.1.22 (core: 5.1.22)
Node v12.18.1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I'm executing the instructions presented on Readme, and wasn't able to find any truffle or typescript issues regarding it not finding the namespace. Also, the types generated in the initial migration are not found, am I missing something?

Thanks in advance

Port Liquidator contracts from `master` to `master-v2`

Is your feature request related to a problem? Please describe.
Recently this repo was ported from solc 0.5.16 to 0.8.x, and truffle/web3.js to hardhat/ethers.js.

There are a number of contracts that have not yet been migrated, and will be used in future product releases.

Describe the solution you'd like
This issue proposes that the Liquidator.sol is ported from master to master-v2. This includes:

  • porting of the solidity file and all dependencies
  • refactoring to bring code in line with solc 0.8 (e.g. removal of SafeMath)
  • porting of relevant test cases to new repo (TestSavingsManager.spec.ts)

Port IncentivisedVotingLockup contracts from `master` to `master-v2`

Is your feature request related to a problem? Please describe.
Recently this repo was ported from solc 0.5.16 to 0.8.x, and truffle/web3.js to hardhat/ethers.js.

There are a number of contracts that have not yet been migrated, and will be used in future product releases.

Describe the solution you'd like
This issue proposes that the IncentivisedVotingLockup.sol is ported from master to master-v2. This includes:

  • porting of the solidity file and all dependencies
  • refactoring to bring code in line with solc 0.8 (e.g. removal of SafeMath)
  • porting of relevant test cases to new repo (TestSavingsManager.spec.ts)

Unliquidator

MIP 26 vote was passed. We should stop liquidating COMP and stkAave rewards. A contract to claim and send the rewards to the Treasury is specified in https://mips.mstable.org/MIPS/mip-26.html

  • To add test coverage to Unliquidator Smart Contract
  • Deploy the Unliquidator to mainnet
  • Protocol DAO propose new Liquidator module in the Nexus to new Unliquidator contract
  • After a week Protocol DAO accepts new Liquidator module
  • Each integration contract approves the new Unliquidator contract
    • Aave mUSD used by USDT, sUSD and DAI
    • Compound mUSD used by USDC
    • Aave mBTC used by WBTC
    • Aave BUSD Feeder Pool
    • Aave RAI Feeder Pool
  • Execute first claim for each integration contract

Clean up the solidity compilation warnings shown at compile time

Is your feature request related to a problem? Please describe.
Run yarn compile and you will see a LOT of yellow warning text show up. Some of these are very very minor, some can potentially come into a larger issue.

Describe the solution you'd like
Resolve all of these warnings by modifying the solidity code, or add rules to the compiler to dismiss them, such that we have no warnings left when compilation happens.

Additional context
There is no real reason to receive warnings for the contracts in z_mocks since they are unused - these can simply be turned off.

Make a generic mAsset deployment script to facilitate mETH deployment

Is your feature request related to a problem? Please describe.
mStable plan to introduce a number of mAssets and feeder pools this year. Both of these deploy scripts are specific and should be made generic to support n deployments.

Describe the solution you'd like
Fork the existing deployMBTC hardhat task and make a generic deployment script for mAssets

Extra context
At a simple level, most of the "mBTC specific" things in the current deploy script are superficial. Only the mainnet deploy and re-deployment scripts have specific info in them - they may benefit from additional arguments to use for the pools deployment.

Deploy on rinkeby

I guess this issue is more for the backlog, but it would be nice to have an instance running on the rinkeby test network.

Clean up the typescript errors and linting

Is your feature request related to a problem? Please describe.
There are a number of typescript errors dotted around the codebase, for example in test-utils/math.ts (or most of the test-utils folder)

Describe the solution you'd like
Clean out the test and test-utils folders, as well as any root level files, that have:

  • typescript linting errors
  • unused dependencies
  • etc

KeeperDAO Coordination facility for mStable AMM (0 Fees Swap for KeeperDAO keepers)

Is your feature request related to a problem? Please describe.
Allow KeeperDAO keepers to arbitrage the AMM (mAsset). In order to give KeeperDAO an edge and to allow them to reach arbitrage price first, set fees to 0 for KeeperDAO keepers. Only whitelisted keepers can call the special function to bypass the fee.

Describe the solution you'd like

  • Requires upgrading contracts
  • adjust swap, redeem, mint, or add one function to figure out the path callable only for Keeper to set swapFee = 0 and pass it to the Logic (Library Contract).
  • Add function to set a keeper
  • Add modifier/or check in the function to check for keeper
    MassetLogic library to allow accept the swapFee from the calling function

Relevant contracts:
mStable-contracts/Masset.sol at master ยท mstable/mStable-contracts ยท GitHub mStable-contracts/MassetLogic.sol at master ยท mstable/mStable-contracts ยท GitHub

Additional context
More info: https://forum.mstable.org/t/rfc-explore-a-keeperdao-coordination-facility-for-mstable-amm/798/9

Pass Criteria

  • Working contracts
  • Unit tests
  • Fork tests including the upgrade of contracts (Ethereum mainnet) and function testing.

SAVE Connector for depositing collateral on Creams Iron Bank

Is your feature request related to a problem? Please describe.
The mAssets in mStable SAVE contracts (SavingsContract.sol) are deposited to produce imAssets. This collateral can be recycled to produce additional yield by adding an IConnector to the contract. Currently there is no active IConnector on mUSD SAVE.

Currently there exists a market on Cream Finance's Iron Bank (https://creamdotfinance.medium.com/introducing-the-iron-bank-bab9417c9a) that could act as a lending market for mUSD. In addition, there is an active proposal for mUSD to be listed on Aave as a market.

Describe the solution you'd like

The solution:

  • A contract implementing IConnector interface for mUSD that deposits the collateral to Creams Iron Bank (https://v1.yearn.finance/lending) and tracks the interest generated
  • Test cases for this contract

Additional context
There are a number of properties that must be fulfilled here. See the IConnector interface for more details, or the _poke function in SavingsContract.sol, or take a look at some of the example connectors in z_mocks/savings.

Port SavingsManager contracts from `master` to `master-v2`

Is your feature request related to a problem? Please describe.
Recently this repo was ported from solc 0.5.16 to 0.8.x, and truffle/web3.js to hardhat/ethers.js.

There are a number of contracts that have not yet been migrated, and will be used in future product releases.

Describe the solution you'd like
This issue proposes that the SavingsManager.sol is ported from master to master-v2. This includes:

  • porting of the solidity file and all dependencies
  • refactoring to bring code in line with solc 0.8 (e.g. removal of SafeMath)
  • porting of relevant test cases to new repo (TestSavingsManager.spec.ts)

Add a StakingRewards contract that wraps existing

Is your feature request related to a problem? Please describe.
It would be good to be able to have double incentives on mStable. Consider the scenario in which Protocol X is incentivising liquidity of a given token Y (through a contract like StakingRewards.sol), and mStable also wish to incentivise this token Y. It would be good to wrap X's rewards contract with a contract Z, to offer both tokens in one.

Describe the solution you'd like
A contract written that forks from the existing StakingRewards.sol, and facilitates the ability to wrap another rewards contract. Each time a user deposits or withdraws (i.e. in updateReward) all tokens that were accrued in the underlying contract would be collected and then factored into the reward distribution.

For example, if 100 tokens were received from X, you would need to then distribute this amount between the participants in Z between now and the time of the previous collection. This would introduce a second layer of accounting in the contract.

Port StakingRewards contracts from `master` to `master-v2`

Is your feature request related to a problem? Please describe.
Recently this repo was ported from solc 0.5.16 to 0.8.x, and truffle/web3.js to hardhat/ethers.js.

There are a number of contracts that have not yet been migrated, and will be used in future product releases.

Describe the solution you'd like
This issue proposes that the StakingRewards.sol is ported from master to master-v2. This includes:

  • porting of the solidity file and all dependencies
  • refactoring to bring code in line with solc 0.8 (e.g. removal of SafeMath)
  • porting of relevant test cases to new repo (TestSavingsManager.spec.ts)

How can we help?

How can we help?

I suggest adding issues to the repo for devs that could be interested to help on the project and tag the one that are good for beginners with good first issue

Port Nexus contracts from `master` to `master-v2`

Is your feature request related to a problem? Please describe.
Recently this repo was ported from solc 0.5.16 to 0.8.x, and truffle/web3.js to hardhat/ethers.js.

There are a number of contracts that have not yet been migrated, and will be used in future product releases.

Describe the solution you'd like
This issue proposes that the Nexus.sol is ported from master to master-v2. This includes:

  • porting of the solidity file and all dependencies
  • refactoring to bring code in line with solc 0.8 (e.g. removal of SafeMath)
  • porting of relevant test cases to new repo (TestSavingsManager.spec.ts)

Port RewardsDistributor contracts from `master` to `master-v2`

Is your feature request related to a problem? Please describe.
Recently this repo was ported from solc 0.5.16 to 0.8.x, and truffle/web3.js to hardhat/ethers.js.

There are a number of contracts that have not yet been migrated, and will be used in future product releases.

Describe the solution you'd like
This issue proposes that the RewardsDistributor.sol is ported from master to master-v2. This includes:

  • porting of the solidity file and all dependencies
  • refactoring to bring code in line with solc 0.8 (e.g. removal of SafeMath)
  • porting of relevant test cases to new repo (TestSavingsManager.spec.ts)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.