@dydxprotocol/protocol

中文文档
1.1.0发布于 2019/08/24Apache-2.0CommonJS近 30 天下载 2770

Ethereum Smart Contracts for the dYdX Margin Trading Protocol

原始使用文档
文档目录5 节
  1. Npm Package
  2. Docker Container
  3. Development
  4. Architecture
  5. Contracts

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

版本与发布记录

@dydxprotocol/protocol 共发布 71 个版本,首次发布于 2018/03/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2019/08/247 年前
1.0.202019/03/197 年前
1.0.192019/02/087 年前
1.0.182018/12/157 年前
1.0.172018/11/207 年前
1.0.162018/11/157 年前

关于 @dydxprotocol/protocol 的常见问题

@dydxprotocol/protocol 是什么?

Ethereum Smart Contracts for the dYdX Margin Trading Protocol

如何安装 @dydxprotocol/protocol?

在项目目录运行 npm install @dydxprotocol/protocol,或使用 pnpm add / yarn add / bun add。

@dydxprotocol/protocol 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/dydxprotocol__protocol 是否存在。

@dydxprotocol/protocol 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@dydxprotocol/protocol 可以商用吗?

@dydxprotocol/protocol 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@dydxprotocol/protocol 的下载量有多少?

根据 npm 官方下载统计,@dydxprotocol/protocol 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,770 次,日均约 92 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dydxprotocol/protocol 还在维护吗?

最新版本 1.1.0 发布于 2019/08/24(7 年前);至今共发布 71 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/03/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。