Giter Site home page Giter Site logo

kenadia / protocol Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dydxprotocol/protocol_v1

0.0 2.0 0.0 2.6 MB

Solidity Smart Contracts for the dYdX Margin Trading Protocol

License: Apache License 2.0

JavaScript 53.76% Dockerfile 0.12% Solidity 44.55% Shell 0.15% Python 1.42%

protocol's Introduction

CI Coverage Status License NPM Docker Slack

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

Whitepaper

Short & Leveraged Long Tokens Whitepaper

Npm Package

The npm package contains the deployed addresses of the contracts, and also allows access to seed positions and orders on the docker test container

Install

npm install --save @dydxprotocol/protocol

Contracts

import { Margin as MarginContract } from '@dydxprotocol/protocol';
import truffleContract from 'truffle-contract';

async function openPosition(provider, networkId) {
  const Margin = truffleContract(MarginContract);

  Margin.setProvider(provider);
  Margin.setNetwork(networkId);

  const margin = await Margin.deployed();

  await margin.openPosition(...);
}

Seed Positions / Orders

Seed positions are available and already deployed on the docker container

import { seeds } from '@dydxprotocol/protocol';

const position = seeds.positions[2];

console.log(position.id);
console.log(position.isTokenized);

// Test 0x V1 orders. Maker already has balance and allowance set
const order = seeds.orders[1];

console.log(order.maker);

Snapshotting

When using the docker container, you can reset the evm to the default state. This can be useful when running automated test suites

import { resetEVM } from '@dydxprotocol/protocol';

await resetEVM(web3.currentProvider);

Docker Container

Docker container with a a deployed version of the protocol running on a ganache-cli node with network_id = 1212. Docker container versions correspond to npm versions of this package, so use the same version for both

docker pull dydxprotocol/protocol
docker run dydxprotocol/protocol

Docker Compose

# docker-compose.yml

version: '3'
services:
  protocol:
    image: dydxprotocol/protocol:latest
    ports:
      - 8545:8545

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.

TokenProxy.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 TokenProxy. Allows authorized contracts to withdraw funds.

Second Layer

ZeroExV1ExchangeWrapper.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

protocol's People

Contributors

antoniojuliano avatar brendanchou avatar jeremyschlatter avatar maxwolff avatar prettymuchbryce avatar richmcateer avatar wjmelements avatar

Watchers

 avatar  avatar

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.