npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dydxprotocol/protocol

v1.1.0

Published

Ethereum Smart Contracts for the dYdX Margin Trading Protocol

Downloads

55

Readme

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