Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
build/contracts
configuration
contracts
formatter
ganache-docker
installed_contracts/0xV2-contracts/contracts
migrations
test
.gitignore
README.md
docker-compose.yml
package-lock.json
package.json
ropsten.js
truffle.js

README.md

0x

Formatter

The formatter expects a valid ZeroEx signed order and will create a makerValues object to be used as input to initialize a paradigm-connect Order.
The formatter is the default export of the package for javascript use.

import zeroExFormatter from 'paradigm-0x-contracts';
Ropsten

Deployed SubContract Address: 0x0afd97c4548d6a5db854d6b1b4b18138327f944c
Sample transaction passing though the OrderGateway

0xV2 Transaction Full Cycle Example (Ropsten)

Prerequisites

To get started, you will need an ethereum address with ether on the ROPSTEN network. ROPSTEN ether can be obtained diretly though MetaMask.

  1. Point MetaMask to the Ropsten network.
  2. Click Deposit
  3. Click Get Ether under the Test Facet label.
  4. Request ether from the faucet.
Example Test Token Addresses:

After you get some test ether you may generate some balance on the following test tokens by sending ether to their address. These test tokens are specially designed to automatically return all ether immediately.

  • DIGM: 0xdbffce76e8ab7b64b8d4400778bf514b92facb66
  • RAIN: 0x842e71cdd25d4b279eb3058a2fb9960b0f10d910
  • SPARK: 0xb2be0b836e7705f6dd8b8f3fc14502072c44c18c
  • FIRE: 0xb71479cc1ff8ed31e9d18bdb7f004964a56fdb89
  • GLOW: 0xbbbb6cca5029690fc56c2af4f5c18c78885a7174
  • HORSE: 0x7022f9e5b700325327dbdefb66fa51865032c093
  • TRUCK: 0xdfbb1f7d9a93bd47857c8362c9497b54aca529e2
  • GOLF: 0x452669b5e77a83779ccd869d7426f06174fffe98
  • BASE: 0x18218faa00502f4ead664f1b77fa905265a3f1f9

Javascript Requirements

The required Javascript is hosted online here, but you may replicate by modifying the following:

import Web3 from 'web3';

import { Dharma, BigNumber } from '@dharmaprotocol/dharma.js';
import {
  generatePseudoRandomSalt,
  assetDataUtils,
  ContractWrappers,
  signatureUtils,
  orderHashUtils
} from '0x.js';
import { MetamaskSubprovider } from '@0x/subproviders';
import Paradigm from 'paradigm-connect';
import dharmaFormatter from 'paradigm-dharma-contracts';
import zeroExFormatter from 'paradigm-0x-contracts';

global.generatePseudoRandomSalt = generatePseudoRandomSalt;
global.assetDataUtils = assetDataUtils;
global.ContractWrappers = ContractWrappers;
global.signatureUtils = signatureUtils;
global.orderHashUtils = orderHashUtils;
global.Dharma = Dharma;
global.BigNumber = BigNumber;
global.Paradigm = Paradigm;
global.dharmaFormatter = dharmaFormatter;
global.zeroExFormatter = zeroExFormatter;
global.MetamaskSubprovider = MetamaskSubprovider;

(async () => {
  global.web3 = new Web3(web3.currentProvider);
  console.log(web3.version);
  console.log(Paradigm.version);
})();

Order Creation

First prepare the Javascript console with the following code:

const paradigm = new Paradigm({ provider: web3.currentProvider });
const makerAddress = await web3.eth.getCoinbase();
const subContract = "0x0afd97c4548d6a5db854d6b1b4b18138327f944c";
const subProvider = new MetamaskSubprovider(web3.currentProvider);
const zeroExContracts = new ContractWrappers(subProvider, { 
    networkId: await web3.eth.net.getId() 
});
const EXCHANGE_ADDRESS = zeroExContracts.exchange.address;

Select tokens to be used for the transaction. This example is using TKA and TKB from the list above:

const token1 = "0x842e71cdd25d4b279eb3058a2fb9960b0f10d910";
const token2 = "0xb71479cc1ff8ed31e9d18bdb7f004964a56fdb89";

Next provide the 0x proxy with an allowance to transfer The desired token on your behalf (This gives an unlimited approval so it only needs to be done onces per user address per token):

  await zeroExContracts.erc20Token.setUnlimitedProxyAllowanceAsync(token1, makerAddress);

Now create and sign an order with the 0x library:

const zeroExOrder = {
  makerAddress,
  takerAddress: paradigm.utils.NULL_ADDRESS,  //Customize these freely
  feeRecipientAddress: paradigm.utils.NULL_ADDRESS,  //Customize these freely
  senderAddress: paradigm.utils.NULL_ADDRESS,  //Customize these freely
  makerAssetAmount: new BigNumber(100),  //Customize these freely
  takerAssetAmount: new BigNumber(100),  //Customize these freely
  makerFee: new BigNumber(0),  //Customize these freely
  takerFee: new BigNumber(0),  //Customize these freely
  expirationTimeSeconds: new BigNumber(
    (Date.now()/1000).toString() //Feel free to customize using the Date api but don't forget to convert to seconds! Ex: Date.parse('Jan 1 1019')/1000
  ), 
  salt: generatePseudoRandomSalt(),
  makerAssetData: assetDataUtils.encodeERC20AssetData(token1),
  takerAssetData: assetDataUtils.encodeERC20AssetData(token2),
  exchangeAddress: EXCHANGE_ADDRESS
};

const signedZeroExOrder = {
  ...zeroExOrder,
  signature: await signatureUtils.ecSignHashAsync(
  subProvider, orderHashUtils.getOrderHashHex(zeroExOrder), makerAddress, 'DEFAULT')
};

Last we will create a Paradigm order and submit the order to the OrderStream:

const makerValues = zeroExFormatter(signedZeroExOrder);
const order = new paradigm.Order({ maker: makerAddress, subContract, makerValues });
await order.prepareForPost();

await paradigm.orderStream.add(order);

Taking the Order

The following will setup the Javascript console to reactively take any orders that are received through the socket connection. The following code will need to be in place prior to an order being sent:

const taker = await web3.eth.getCoinbase();
const zeroExContracts = new ContractWrappers(subProvider, { 
  networkId: await web3.eth.net.getId() 
});
const tokenToOffer = "0xb71479cc1ff8ed31e9d18bdb7f004964a56fdb89";
const tokenAssetData = assetDataUtils.encodeERC20AssetData(tokenToOffer)

paradigm.orderStream.listen(async (message) => {
  console.log('Message received.');
  const json = JSON.parse(message.data);
  if(json.event === "order") {
    const order = new paradigm.Order(json.data);
    if(order.subContract === '0x0afd97c4548d6a5db854d6b1b4b18138327f944c' &&
     order.makerValues.takerAssetData === tokenAssetData) {
    console.log('Message contained order:', order.toJSON());

    await zeroExContracts.erc20Token.setUnlimitedAllowanceAsync(tokenToOffer, taker, order.subContract);

    const resp =  await order.take(taker, { taker, takerAssetAmount: new BigNumber(100) });
    console.log(resp)
    } else {
      console.log('Message contained order with wrong SubContract');
    }
  } else {
    console.log('Message is not an order.')
  }
});
You can’t perform that action at this time.