Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Commit

Permalink
docs(api): add example scripts for order, cancel and trade (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhustler committed Dec 13, 2018
1 parent 5464202 commit 455474c
Show file tree
Hide file tree
Showing 6 changed files with 1,410 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules/
yarn-error.log
11 changes: 11 additions & 0 deletions package.json
@@ -0,0 +1,11 @@
{
"name": "idex-api-docs",
"version": "1.0.0",
"description": "IDEX API - example scripts for creating request payloads",
"author": "AuroraDAO IDEX",
"dependencies": {
"ethereumjs-util": "^6.0.0",
"lodash": "^4.17.11",
"web3-utils": "^1.0.0-beta.37"
}
}
49 changes: 49 additions & 0 deletions scripts/generate-cancel-payload.js
@@ -0,0 +1,49 @@
const { soliditySha3 } = require('web3-utils');
const {
hashPersonalMessage,
bufferToHex,
toBuffer,
ecsign,
} = require('ethereumjs-util');
const { mapValues } = require('lodash');


/**
* Your wallet's address and private key
*/
const wallet = {
address: '0x...',
privateKey: '0x...',
};

/**
* https://api.idex.market/returnNextNonce?address=...
*/
const nonce = 123;

const orderHash = '0x...';


const args = {
orderHash,
nonce: nonce,
address: wallet.address,
};
const raw = soliditySha3(
{
t: 'uint256',
v: args.orderHash,
},
{
t: 'uint256',
v: args.nonce,
},
);
const salted = hashPersonalMessage(toBuffer(raw));
const vrs = mapValues(
ecsign(salted, toBuffer(wallet.privateKey)),
(value, key) => key === 'v' ? value : bufferToHex(value),
);

console.log('Cancel payload:');
console.log(JSON.stringify(Object.assign(args, vrs), null, 2));
84 changes: 84 additions & 0 deletions scripts/generate-order-payload.js
@@ -0,0 +1,84 @@
const { soliditySha3 } = require('web3-utils');
const {
hashPersonalMessage,
bufferToHex,
toBuffer,
ecsign,
} = require('ethereumjs-util');
const { mapValues } = require('lodash');

/**
* https://api.idex.market/returnContractAddress
*/
const idexContractAddress = '0x2a0c0dbecc7e4d658f48e01e3fa353f44050c208';

/**
* Your wallet's address and private key
*/
const wallet = {
address: '0x...',
privateKey: '0x...',
};

/**
* https://api.idex.market/returnNextNonce?address=...
*/
const nonce = 123;

const tokenBuy = '0x0000000000000000000000000000000000000000'; // ETH
const amountBuy = '150000000000000000'; // 0.15 ETH (IDEX's minimum)
const tokenSell = '0xcdcfc0f66c522fd086a1b725ea3c0eeb9f9e8814'; // AURA
const amountSell = '1000000000000000000000'; // 1000 coins


const args = {
tokenBuy,
amountBuy,
tokenSell,
amountSell,
address: wallet.address,
nonce,
expires: 100000,
};
const raw = soliditySha3(
{
t: 'address',
v: idexContractAddress,
},
{
t: 'address',
v: args.tokenBuy,
},
{
t: 'uint256',
v: args.amountBuy,
},
{
t: 'address',
v: args.tokenSell,
},
{
t: 'uint256',
v: args.amountSell,
},
{
t: 'uint256',
v: args.expires,
},
{
t: 'uint256',
v: args.nonce,
},
{
t: 'address',
v: args.address,
},
);
const salted = hashPersonalMessage(toBuffer(raw));
const vrs = mapValues(
ecsign(salted, toBuffer(wallet.privateKey)),
(value, key) => key === 'v' ? value : bufferToHex(value),
);

console.log('Order payload:');
console.log(JSON.stringify(Object.assign(args, vrs), null, 2));
86 changes: 86 additions & 0 deletions scripts/generate-trade-payload.js
@@ -0,0 +1,86 @@
const { soliditySha3 } = require('web3-utils');
const {
hashPersonalMessage,
bufferToHex,
toBuffer,
ecsign,
} = require('ethereumjs-util');
const { mapValues } = require('lodash');


/**
* Your wallet's address and private key
*/
const wallet = {
address: '0x...',
privateKey: '0x...',
};

/**
* https://api.idex.market/returnNextNonce?address=...
*/
const nonce = 123;

/**
* Copy and paste an order object from any of the order endpoints
*/
const openOrder = {
"price": "0.00019293",
"amount": "9999.9999999999996543",
"total": "1.9293",
"orderHash": "0x...",
"params": {
"tokenBuy": "0x0000000000000000000000000000000000000000",
"buySymbol": "ETH",
"buyPrecision": 18,
"amountBuy": "1929300000000000000",
"tokenSell": "0xcdcfc0f66c522fd086a1b725ea3c0eeb9f9e8814",
"sellSymbol": "AURA",
"sellPrecision": 18,
"amountSell": "9999999999999999654300",
"expires": 10000,
"nonce": 648943716,
"user": "0x...",
},
};

/**
* If not filling the order in full, specify the amount here.
* Expressed in the currency you are paying with (tokenBuy/amountBuy).
*/
const purchaseAmount = openOrder.params.amountBuy;
// const purchaseAmount = '150000000000000000';


const args = {
orderHash: openOrder.orderHash,
amount: purchaseAmount,
nonce,
address: wallet.address,
};
const raw = soliditySha3(
{
t: 'uint256',
v: args.orderHash,
},
{
t: 'uint256',
v: args.amount,
},
{
t: 'address',
v: args.address,
},
{
t: 'uint256',
v: args.nonce,
},
);
const salted = hashPersonalMessage(toBuffer(raw));
const vrs = mapValues(
ecsign(salted, toBuffer(wallet.privateKey)),
(value, key) => key === 'v' ? value : bufferToHex(value),
);

console.log('Trade payload:');
console.log(JSON.stringify(Object.assign(args, vrs), null, 2));

0 comments on commit 455474c

Please sign in to comment.