Code Monkey home page Code Monkey logo

rmm-core's Introduction

Primitive Replicating Market Maker

Twitter Follow Tests npm version

Core contracts of Primitive RMM protocol.

Bug Bounty

This repository has a $1,000,000 bug bounty through Immunefi. Details are on their website https://immunefi.com/bounty/primitive.

Documentation

The contract documentation is hosted here: Primitive Docs.

Testing

Compile contracts

yarn compile

Run typechain

yarn typechain

Run the tests

yarn test

Run the tests using --parallel flag

yarn test:fast

Notes

Running tests using the default yarn test will run it through hardhat, it takes approximately 10 minutes on a good CPU.

The testing environment is unique. Make sure that yarn typechain has been run first, or else there could be typescript compilation issues.

The test:fast script makes use of the parallel tag, which will take up a considerable amount of CPU power. It makes the tests run faster.

In the /test/shared/poolConfigs.ts file is an array of different curve parameters. Each of these pools will go through the entire test suite.

Note: When running tests with parallel, the swap tests will not be logged. If the tests are frozen, it means the swap tests are the last tests to be run.

Security

All audits are located in the audits/ folder.

rmm-core's People

Contributors

0xjepsen avatar alexangelj avatar clemlak avatar kinrezc avatar lint-action avatar rajivpo avatar sherlockprotocol avatar smonicas avatar transmissions11 avatar zachdt 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

rmm-core's Issues

Upgrade Hardhat and Solidity to >= 0.8.4

We should upgrade Hardhat and our contracts to >= 0.8.4, this will give us the opportunity to use new features, such as the new panic errors.
Once again, we need to be sure that our testing environment will be compatible with this new version.

Update `getEngine` mapping in factory

Right now, anyone would be able to grief a token pair by making the reverse incorrect pair, for example dai as the risky and eth as the stable asset. This would make a call to deploy an engine with eth as risky and dai as stable revert, because its already defined in the reverse mapping.

Time until expiry isn't being calculated

Pools are initialized with a time until expiry. Whenever a swap happens, we should be taking the current timestamp and calculating the new time until expiry. This value should be an input to the trading function, not the time until expiry value we initialize the pool with.

Time delta should be a state variable that is updated

Related to #97

When time changes, the curve translates. In order to translate, more tokens must be provided to the reserves, both risky and stable. To be able to do this, there must be some balance which can be drawn upon to update time delta.

One possible solution is to pool the fees, and draw from the fee pool when time delta tau is updated.

Implement Oracle

Engine contract needs to store reserve data so we can query it on-chain

[Test-env] Order of 'contractNames' matters, it shouldn't

In the switch case, if we havn't deployed the factory first, then we can't deploy the other contracts. There is an order we must deploy the contracts. This order is enforced only by the contract name strings in the array, so the position in the array matters to the deployment; but it shouldn't.

https://github.com/primitivefinance/primitive-v2-core/blob/863ee491c67a0126c751e4e1a83658c5375c5c8b/test/unit/primitiveEngine/effect/create.ts#L17

Reserve.update() function doesn't take a block timestamp as an argument

In the Reserve.sol library there is an internal function _blockTimestamp() to get the uint32 block timestamp. This is called within the library. We should move this and make it an argument in the update function, and have _blockTimestamp() implemented in the contract which is importing the library instead, like the PrimitiveEngine.sol contract

Refactor Functions in Engine

Most the Engine functions can most likely be improved. Whether that's improving gas by storing state vars in memory, adding comments, improving readability, or reducing code.

Export to libraries

A lot of the math, swap math, liquidity math, etc. can be exported to library contracts.

Implement Fees

Engine contract does not have any fee related code. This also means changes in the trading function math, which is gamma (1 - fee) multiplied by the deltaOut.

Base Engine

The Base Engine contract should have virtual functions which are implemented by the Engine contract; the common denominator.

Whats an Engine?

  • Its an AMM with some way to manipulate liquidity through a trading function.

Upgrade to TypeChain 5

TypeChain 5 comes with a caching feature that will speed up the compilation and testing. We need to upgrade from 4 to 5. However we have to be sure that this is not going to break our testing environment. My previous tests were not successful because of an issue when the deployed contracts were casted in the fixtures.

Mock erc20 contracts cannot have mock balances updated in Callbacks

We give some tokens to an engine and signer with:
erc20.mock.balanceOf(signer).returns(amount)

Then we call the create function on a target contract (which should be a TestCallee). TestCallee will then call engine.create().

In engine create, an amount of risky and stable amount is calculated to be the initializing reserve values. These reserve values are set, and now we need to get those reserve balances as actual tokens in the engine.

The engine stores the risky and stable balances.
The engine then calls the msg.sender (our TestCallee contract) using createCallback()
Then in the callback, TestCallee will pull tokens from the signer, and send them to the engine.
The Engine will then check the current risky and token balance against the balances saved in memory prior to the callback. If tokens were sent in during the callback, there would be a difference.

The problem with the mock contract, is we can't increment the engine balance of tokens during the callback with a mock

Liquidity Shares

Liquidity shares must be normalized so that 1 share = 1 covered call. This means the liquidity shares will scale down the reserves.

Prep for NPM

  • Separate periphery
  • Separate sim
  • Separate deployment environment

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.