/protocol

Solidity Smart Contracts for the dYdX Margin Trading Protocol

Primary LanguageJavaScriptApache License 2.0Apache-2.0

Coverage Status License NPM Slack

Source code for Ethereum Smart Contracts used by the dYdX Margin Trading Protocol

Whitepaper

Short & Leveraged Long Tokens Whitepaper

Development

Install

npm install

Compile

npm run compile

Test

npm test

Lint

Lint the javascript files (tests, deploy scripts)

npm run lint

Lint the solidity files (all smart contracts)

npm run solint

Lint the solidity files (custom dYdX linter)

npm run dydxlint

Architecture

Contracts

Base Protocol

Margin.sol

Contains business logic for margin trading. All external functions for margin trading are in this contract.

Proxy.sol

Used to transfer user funds. Users set token allowance for the proxy authorizing it to transfer their funds. Only allows authorized contracts to transfer funds.

Vault.sol

Holds all token funds. Is authorized to transfer user funds via the Proxy. Allows authorized contracts to withdraw funds.

Second Layer

ZeroExExchangeWrapper.sol

Allows positions to be opened or closed using 0x orders. Wraps the 0x Exchange Contract in a standard interface usable by Margin.

ERC20Short.sol

Allows short positions to be tokenized as ERC20 tokens. Ownership of a short token grants ownership of a proportional piece of the backing position.

ERC20Long.sol

Allows leveraged long positions to be tokenized as ERC20 tokens. Ownership of a leveraged long token grants ownership of a proportional piece of the backing position.

ERC721Position.sol

Allows margin positions to be represented as ERC721 tokens.

ERC721MarginLoan.sol

Allows loans to be represented as ERC721 tokens.

DutchAuctionCloser.sol

Allows margin positions to be automatically close via a dutch auction.

SharedLoan.sol

Allows multiple lenders to share in a loan position together.

Read more about our smart contract architecture here