• This repository has been archived on 27/Sep/2022
  • Stars
    star
    200
  • Rank 195,296 (Top 4 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[DEPRECATED] Solidity Smart Contracts for the dYdX V1 Margin Trading Protocol

CI Coverage Status License NPM Docker Slack

DEPRECATED see our new protocol here

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

More Repositories

1

liquidator

DEPRECATED [๐Ÿค–๐Ÿ’ฐ Bot to automatically liquidate dYdX accounts]
TypeScript
468
star
2

solo

dYdX V2 Margin Trading Protocol
TypeScript
418
star
3

dydx-v3-python

Python client for dYdX (API v3)
Python
306
star
4

v4-chain

dydx v4 protocol
Go
221
star
5

perpetual

Ethereum Smart Contracts and TypeScript library used for the dYdX Perpetual Contracts
TypeScript
170
star
6

dydx-python

dYdX Python Client for Limit Orders
Python
108
star
7

v3-client

TypeScript client for dYdX (v3 API).
TypeScript
108
star
8

v4-web

TypeScript
74
star
9

exchange-wrappers

๐Ÿ’ฑ๐ŸฌCollection of exchange wrapper contracts used by the dYdX Protocol
Solidity
70
star
10

v4-clients

JavaScript
61
star
11

starkex-lib

Cryptographic functions for dYdX (v3 API)
TypeScript
25
star
12

dydx.js

[DEPRECATED] TypeScript library for interacting with the dYdX V1 protocol
TypeScript
22
star
13

starkex-eth

library for interacting and parsing logs for the starkware smart contracts
TypeScript
22
star
14

v4-abacus

JavaScript
14
star
15

v4-documentation

MDX
11
star
16

v4-infrastructure

HCL
8
star
17

walletdirect

Direct communication between dApps and Wallets with deep links
Swift
7
star
18

v4-testnets

Shell
4
star
19

starkex-peripheral

Contracts for interacting with StarkEx-contracts
TypeScript
4
star
20

node-service-base-dev

Shared dev dependencies, development configuration, and utilities for Node libraries and services.
JavaScript
4
star
21

leverage-intuition

Jupyter Notebook
3
star
22

v4-localization

TypeScript
2
star
23

grpc-stream-client

Example Client for dYdX Full Node gRPC Streams
Python
2
star
24

v4-native-android

JavaScript
2
star
25

go-systemCheck

Go
1
star
26

systemCheck

TypeScript
1
star
27

py-systemCheck

Python
1
star
28

v4-latency-scripts

Python scripts that measure the latency of the dYdX chain software
Python
1
star
29

cartera-ios

Swift
1
star
30

v4-native-ios

JavaScript
1
star