@hydro-protocol/hydro-client-js
Version:
Javascript SDK for the Hydro API
94 lines (93 loc) • 3.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var bignumber_js_1 = require("bignumber.js");
/**
* Data used by 0x to construct an order.
*
* See https://0xproject.com/wiki#Create,-Validate,-Fill-Order
*/
var OrderData = /** @class */ (function () {
function OrderData(json) {
this.trader = json.trader;
this.relayer = json.relayer;
this.baseToken = json.baseToken;
this.quoteToken = json.quoteToken;
this.baseTokenAmount = json.baseTokenAmount
? new bignumber_js_1.BigNumber(json.baseTokenAmount)
: new bignumber_js_1.BigNumber('0');
this.quoteTokenAmount = json.quoteTokenAmount
? new bignumber_js_1.BigNumber(json.quoteTokenAmount)
: new bignumber_js_1.BigNumber('0');
this.gasTokenAmount = json.gasTokenAmount
? new bignumber_js_1.BigNumber(json.gasTokenAmount)
: new bignumber_js_1.BigNumber('0');
this.data = json.data;
}
/* Methods to pull information from the compact data field */
/**
* Which version of Hydro was used to construct this order
*/
OrderData.prototype.getVersion = function () {
return this.sliceData(0, 1);
};
/**
* Whether this is a buy or sell order
*/
OrderData.prototype.getSide = function () {
var side = this.sliceData(1, 1);
return side === 0 ? 'buy' : 'sell';
};
/**
* Whether this is a limit or market order
*/
OrderData.prototype.getType = function () {
var type = this.sliceData(2, 1);
return type === 0 ? 'limit' : 'market';
};
/**
* Return a Date object representing when this order will expire
*/
OrderData.prototype.getExpiration = function () {
var expiration = this.sliceData(3, 5);
return new Date(expiration * 1000);
};
/**
* Return a BigNumber representing the rate for the fee the maker will pay
*/
OrderData.prototype.getMakerFeeRate = function () {
var makerFeeRate = this.sliceData(8, 2);
return new bignumber_js_1.BigNumber(makerFeeRate).div(100000);
};
/**
* Return a BigNumber representing the rate for the fee the taker will pay
*/
OrderData.prototype.getTakerFeeRate = function () {
var takerFeeRate = this.sliceData(10, 2);
return new bignumber_js_1.BigNumber(takerFeeRate).div(100000);
};
/**
* Return a BigNumber representing the rate for the rebate the maker will get
*/
OrderData.prototype.getMakerRebateRate = function () {
var makerRebateRate = this.sliceData(12, 2);
return new bignumber_js_1.BigNumber(makerRebateRate).div(100000);
};
/**
* Returns decimal representation of the data at a certain offset with a certain size.
* Offet will not include the prepended 0x, and both parameters will be defined by bytes, or a
* 2 digit hex representation. For example in the string 0x12345678, passing (0, 1) would get the
* hex value 12, convert it to decimal and return 18. Passing (1, 2) would get the hex value 3456
* and return 13398.
*
* @param offset The number of bytes to offset into the data field
* @param size The number of bytes to grab at that offset
* @return The decimal representation of the hex found
*/
OrderData.prototype.sliceData = function (offset, size) {
var additionalOffset = this.data.startsWith('0x') ? 2 : 0;
var hex = this.data.substr(offset * 2 + additionalOffset, size * 2);
return parseInt(hex, 16);
};
return OrderData;
}());
exports.OrderData = OrderData;