Source code for Ethereum Smart Contracts used by the dYdX Margin Trading Protocol
Short & Leveraged Long Tokens Whitepaper
The npm package contains the deployed addresses of the contracts, and also allows access to seed positions and orders on the docker test container
git clone https://github.com/amis-erc20/dydx
npm install --save @dydxprotocol/protocol
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 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);
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 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.yml
version: '3'
services:
protocol:
image: dydxprotocol/protocol:latest
ports:
- 8545:8545
npm install
npm run compile
npm test
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
Contains business logic for margin trading. All external functions for margin trading are in this contract.
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.
Holds all token funds. Is authorized to transfer user funds via the TokenProxy. Allows authorized contracts to withdraw funds.
Allows positions to be opened or closed using 0x orders. Wraps the 0x Exchange Contract in a standard interface usable by Margin.
Allows short positions to be tokenized as ERC20 tokens. Ownership of a short token grants ownership of a proportional piece of the backing position.
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.
Allows margin positions to be represented as ERC721 tokens.
Allows loans to be represented as ERC721 tokens.
Allows margin positions to be automatically close via a dutch auction.
Allows multiple lenders to share in a loan position together.
Read more about our smart contract architecture here