@exodus/models
Advanced tools
Comparing version 9.0.0 to 10.0.0
@@ -28,3 +28,3 @@ "use strict"; | ||
const ac = new AddressSet(); | ||
for (let addr of vals) { | ||
for (const addr of vals) { | ||
ac._data.set(addr.address, addr); | ||
@@ -36,3 +36,3 @@ } | ||
(0, _minimalisticAssert.default)(_address.default.isAddress(address), 'Must be an instance of an Address.'); | ||
let ac = new AddressSet(); | ||
const ac = new AddressSet(); | ||
ac._data = new Map(this._data); | ||
@@ -52,5 +52,5 @@ ac._data.set(address.address, address); | ||
remove(address) { | ||
let addrObj = this.get(address); | ||
const addrObj = this.get(address); | ||
if (!addrObj) return this; | ||
let ac = new AddressSet(); | ||
const ac = new AddressSet(); | ||
ac._data = new Map(this._data); | ||
@@ -75,5 +75,5 @@ ac._data.delete(addrObj.address); | ||
union(otherSet) { | ||
let arr1 = [...this]; | ||
let arr2 = [...otherSet]; | ||
let both = [...arr1, ...arr2]; | ||
const arr1 = [...this]; | ||
const arr2 = [...otherSet]; | ||
const both = [...arr1, ...arr2]; | ||
return AddressSet.fromArray(both); | ||
@@ -80,0 +80,0 @@ } |
@@ -29,3 +29,3 @@ "use strict"; | ||
if (typeof json === 'string') json = JSON.parse(json); | ||
return Address.create(json.address, json.meta ? json.meta : json.path); | ||
return Address.create(json.address, json.meta || json.path); | ||
} | ||
@@ -32,0 +32,0 @@ get pathArray() { |
@@ -78,2 +78,14 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "currenciesForAsset", { | ||
enumerable: true, | ||
get: function () { | ||
return _utils.currenciesForAsset; | ||
} | ||
}); | ||
Object.defineProperty(exports, "ensureCurrencies", { | ||
enumerable: true, | ||
get: function () { | ||
return _utils.ensureCurrencies; | ||
} | ||
}); | ||
Object.defineProperty(exports, "normalizeTxJSON", { | ||
@@ -80,0 +92,0 @@ enumerable: true, |
@@ -53,3 +53,3 @@ "use strict"; | ||
if (!items) return this; | ||
for (let item of items) { | ||
for (const item of items) { | ||
if (this.get(item)) { | ||
@@ -79,3 +79,3 @@ console.log(`OrderSet#add() already has #{item.orderId}`); | ||
if (this._sortOrder.length !== otherSet._sortOrder.length) return false; | ||
let len = this._sortOrder.length; | ||
const len = this._sortOrder.length; | ||
for (let i = 0; i < len; ++i) { | ||
@@ -111,5 +111,5 @@ if (this._sortOrder[i] !== otherSet._sortOrder[i]) return false; | ||
union(otherSet) { | ||
let arr1 = [...this]; | ||
let arr2 = [...otherSet]; | ||
let both = [...arr1, ...arr2]; | ||
const arr1 = [...this]; | ||
const arr2 = [...otherSet]; | ||
const both = [...arr1, ...arr2]; | ||
return OrderSet.fromArray(both); | ||
@@ -116,0 +116,0 @@ } |
@@ -53,4 +53,4 @@ "use strict"; | ||
(0, _minimalisticAssert.default)(typeof this.orderId === 'string', `orderId must be a string, received: ${this.orderId}`); | ||
if (typeof props.fromAmount !== 'undefined' && !(0, _currency.isNumberUnit)(props.fromAmount)) throw new Error('fromAmount must be of type: NumberUnit'); | ||
if (typeof props.toAmount !== 'undefined' && !(0, _currency.isNumberUnit)(props.toAmount)) throw new Error('toAmount must be of type: NumberUnit'); | ||
if (props.fromAmount !== undefined && !(0, _currency.isNumberUnit)(props.fromAmount)) throw new Error('fromAmount must be of type: NumberUnit'); | ||
if (props.toAmount !== undefined && !(0, _currency.isNumberUnit)(props.toAmount)) throw new Error('toAmount must be of type: NumberUnit'); | ||
this.status = props.status || ''; | ||
@@ -94,3 +94,3 @@ if (props.errorDetails) this.errorDetails = props.errorDetails; | ||
toJSON() { | ||
let obj = { | ||
const obj = { | ||
...this, | ||
@@ -106,3 +106,3 @@ date: this.date.toISOString() | ||
console.log('Order: calling deprecated function order.toJSONLegacy(), use orderToJSONLegacy() instead'); | ||
let obj = { | ||
const obj = { | ||
...this, | ||
@@ -109,0 +109,0 @@ date: this.date.toISOString() |
@@ -40,3 +40,3 @@ "use strict"; | ||
(0, _minimalisticAssert.default)(order && order instanceof _.default, 'toJSONLegacy: requires an Order'); | ||
let obj = { | ||
const obj = { | ||
...order, | ||
@@ -43,0 +43,0 @@ date: order.date.toISOString() |
@@ -22,3 +22,3 @@ "use strict"; | ||
toJSON() { | ||
let obj = { | ||
const obj = { | ||
...this | ||
@@ -28,3 +28,3 @@ }; | ||
obj.sends = {}; | ||
for (let [key, value] of Object.entries(this.sends)) { | ||
for (const [key, value] of Object.entries(this.sends)) { | ||
obj.sends[key] = { | ||
@@ -31,0 +31,0 @@ ...value |
@@ -41,3 +41,3 @@ "use strict"; | ||
} | ||
static fromArray(arr = [], opts = {}) { | ||
static fromArray(arr = []) { | ||
if (!Array.isArray(arr)) { | ||
@@ -47,3 +47,4 @@ console.warn(`Unexpected non-array value:`, arr); | ||
} | ||
const txs = new Map(arr.map(tx => [tx.txId, tx instanceof _tx.default ? tx : _tx.default.fromJSON(tx, opts)])); | ||
arr = arr.map(tx => tx instanceof _tx.default ? tx : _tx.default.fromJSON(tx)); | ||
const txs = new Map(arr.map(tx => [tx.txId, tx])); | ||
return createTxSet({ | ||
@@ -70,3 +71,3 @@ txs | ||
delete(tx) { | ||
let txObj = this.get(tx); | ||
const txObj = this.get(tx); | ||
if (!txObj) return this; | ||
@@ -73,0 +74,0 @@ const txs = new Map(this._txs); |
@@ -9,6 +9,6 @@ "use strict"; | ||
var _proxyFreeze = _interopRequireDefault(require("proxy-freeze")); | ||
var _assets2 = _interopRequireDefault(require("@exodus/assets")); | ||
var _lodash = require("lodash"); | ||
var _currency = require("@exodus/currency"); | ||
var _addressSet = _interopRequireDefault(require("../address-set")); | ||
var _utils = require("./utils"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -38,5 +38,7 @@ const FACTORY_SYMBOL = Symbol('Tx'); | ||
token, | ||
addresses | ||
addresses, | ||
currencies | ||
} = json; | ||
(0, _minimalisticAssert.default)(initSymbol === FACTORY_SYMBOL, 'Tx: please use Tx.fromJSON().'); | ||
(0, _minimalisticAssert.default)(!(0, _lodash.isEmpty)(currencies), 'Tx: `currencies` is required'); | ||
(0, _minimalisticAssert.default)(!from || Array.isArray(from), 'Tx: `from` has to be an array.'); | ||
@@ -46,18 +48,17 @@ (0, _minimalisticAssert.default)(!to || (0, _lodash.isString)(to), 'Tx: `to` has to be a string.'); | ||
(0, _minimalisticAssert.default)(!addresses || Array.isArray(addresses) || addresses instanceof _addressSet.default, 'Tx: unknown `addresses` type'); | ||
(0, _minimalisticAssert.default)(!coinAmount || coinName, 'Tx: coinName is required with coinAmount'); | ||
(0, _minimalisticAssert.default)(!feeAmount || feeCoinName, 'Tx: feeCoinName is required with feeAmount'); | ||
this.currencies = (0, _lodash.mapValues)(currencies, currency => currency instanceof _currency.UnitType ? currency : _currency.UnitType.create(currency)); | ||
this.txId = txId; | ||
this.error = error || null; | ||
this.date = new Date(date); | ||
this.date = date ? new Date(date) : new Date(); | ||
this.confirmations = typeof confirmations === 'number' ? confirmations : -1; | ||
if (typeof dropped !== 'undefined') this.dropped = !!dropped && this.confirmations <= 0; | ||
if (dropped !== undefined) this.dropped = !!dropped && this.confirmations <= 0; | ||
if (coinAmount) { | ||
this.coinAmount = coinAmount; | ||
if (coinName) { | ||
this.coinName = coinName; | ||
} | ||
this.coinAmount = parseCurrency(coinAmount, this.currencies[coinName]); | ||
this.coinName = coinName; | ||
} | ||
if (feeAmount) { | ||
this.feeAmount = feeAmount; | ||
if (feeCoinName) { | ||
this.feeCoinName = feeCoinName; | ||
} | ||
this.feeAmount = parseCurrency(feeAmount, this.currencies[feeCoinName]); | ||
this.feeCoinName = feeCoinName; | ||
} | ||
@@ -73,41 +74,8 @@ this.selfSend = selfSend || false; | ||
this.addresses = Array.isArray(addresses) ? _addressSet.default.fromArray(addresses) : addresses instanceof _addressSet.default ? addresses : _addressSet.default.EMPTY; | ||
this.version = 1; | ||
} | ||
static fromJSON(jsonOrString, opts = {}) { | ||
static fromJSON(jsonOrString) { | ||
const json = typeof jsonOrString === 'string' ? JSON.parse(jsonOrString) : jsonOrString; | ||
const assets = opts.assets || _assets2.default; | ||
const updates = {}; | ||
if (json.coinAmount) { | ||
if (json.coinName) { | ||
updates.coinAmount = parseCurrency(json.coinAmount, assets[json.coinName].currency); | ||
} else if ((0, _currency.isNumberUnit)(json.coinAmount)) { | ||
console.log('WARNING: Tx.fromJSON(): no `coinName`.', json.coinAmount.toDefaultString({ | ||
unit: true | ||
})); | ||
} else { | ||
updates.coinAmount = undefined; | ||
console.log('WARNING: Tx.fromJSON(): unrecognized `coinAmount`.', json.coinAmount); | ||
} | ||
} | ||
if (json.feeAmount) { | ||
if (json.feeCoinName) { | ||
updates.feeAmount = parseCurrency(json.feeAmount, assets[json.feeCoinName].currency); | ||
} else if (json.coinName) { | ||
const feeAssetName = assets[json.coinName].feeAsset.name; | ||
updates.feeAmount = parseCurrency(json.feeAmount, assets[feeAssetName].currency); | ||
updates.feeCoinName = feeAssetName; | ||
console.log('WARNING: Tx.fromJSON(): using `coinName` to derive `feeCoinName`.', json.coinName); | ||
} else if ((0, _currency.isNumberUnit)(json.feeAmount)) { | ||
console.log('WARNING: Tx.fromJSON(): no `feeCoinName` or `coinName`.', json.feeAmount.toDefaultString({ | ||
unit: true | ||
})); | ||
} else { | ||
updates.feeAmount = undefined; | ||
console.log('WARNING: Tx.fromJSON(): unrecognized `feeAmount`', json.feeAmount); | ||
} | ||
} | ||
const tx = new Tx({ | ||
json: { | ||
...json, | ||
...updates | ||
}, | ||
json, | ||
initSymbol: FACTORY_SYMBOL | ||
@@ -144,2 +112,3 @@ }); | ||
if (obj.tokens.length === 0) delete obj.tokens; | ||
obj.currencies = (0, _lodash.mapValues)(this.currencies, _utils.unitTypeToJSON); | ||
return obj; | ||
@@ -152,2 +121,3 @@ } | ||
const current = this.toJSON(); | ||
fields = (0, _lodash.omit)(fields, ['version']); | ||
const isNoop = Object.keys(fields).every(field => (0, _lodash.isEqual)(current[field], fields[field])); | ||
@@ -154,0 +124,0 @@ if (isNoop) { |
@@ -6,6 +6,12 @@ "use strict"; | ||
}); | ||
exports.currenciesForAsset = currenciesForAsset; | ||
exports.ensureCurrencies = void 0; | ||
exports.normalizeTxJSON = normalizeTxJSON; | ||
exports.normalizeTxsJSON = normalizeTxsJSON; | ||
exports.unitTypeToJSON = void 0; | ||
var _minimalisticAssert = _interopRequireDefault(require("minimalistic-assert")); | ||
var _lodash = require("lodash"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const unitTypeToJSON = ut => (0, _lodash.mapValues)(ut.units, u => u.power); | ||
exports.unitTypeToJSON = unitTypeToJSON; | ||
function normalizeTxJSON({ | ||
@@ -16,10 +22,18 @@ json, | ||
(0, _minimalisticAssert.default)(json, 'normalizeTxJSON: `json` object is required'); | ||
if (json.version !== undefined) return json; | ||
(0, _minimalisticAssert.default)(asset, 'normalizeTxJSON: `asset` object is required'); | ||
const updated = { | ||
...json, | ||
version: 1 | ||
}; | ||
if (json.feeAmount && !json.feeCoinName) { | ||
return { | ||
...json, | ||
feeCoinName: asset.feeAsset.name | ||
}; | ||
updated.feeCoinName = asset.feeAsset.name; | ||
} | ||
return json; | ||
if (!json.currencies) { | ||
updated.currencies = (0, _lodash.mapValues)(currenciesForAsset(asset), unitTypeToJSON); | ||
} | ||
return { | ||
...json, | ||
...updated | ||
}; | ||
} | ||
@@ -38,2 +52,19 @@ function normalizeTxsJSON({ | ||
}); | ||
} | ||
const ensureCurrencies = (json, assets) => { | ||
if (!(0, _lodash.isEmpty)(json.currencies)) return json; | ||
(0, _minimalisticAssert.default)(assets, "ensureCurrencies: expected `assets` when `json` doesn't contain `currencies`"); | ||
(0, _minimalisticAssert.default)(json.coinName, 'ensureCurrencies: expected `json.coinName`'); | ||
(0, _minimalisticAssert.default)(assets[json.coinName], `ensureCurrencies: asset not found for ${json.coinName}`); | ||
return { | ||
...json, | ||
currencies: currenciesForAsset(assets[json.coinName]) | ||
}; | ||
}; | ||
exports.ensureCurrencies = ensureCurrencies; | ||
function assetsForAsset(asset) { | ||
return Object.fromEntries((0, _lodash.uniqBy)([asset, asset.baseAsset, asset.feeAsset], asset => asset.name).map(asset => [asset.name, asset])); | ||
} | ||
function currenciesForAsset(asset) { | ||
return (0, _lodash.mapValues)(assetsForAsset(asset), asset => asset.currency); | ||
} |
@@ -25,4 +25,4 @@ "use strict"; | ||
_defineProperty(this, _Symbol$iterator, function* symbolIterator() { | ||
let arr = this.toArray(); | ||
for (let val of arr) yield val; | ||
const arr = this.toArray(); | ||
for (const val of arr) yield val; | ||
}); | ||
@@ -39,3 +39,3 @@ this.currency = currency; | ||
} = {}) { | ||
let currency = options.currency || pickCurrencyFromArray(utxoArray); | ||
const currency = options.currency || pickCurrencyFromArray(utxoArray); | ||
if (currency == null && utxoArray == null) console.warn('UtxoCollection.fromArray(): both currency and utxoArray are null.');else if (currency == null && utxoArray.length === 0) { | ||
@@ -45,3 +45,3 @@ console.warn('UtxoCollection.fromArray(): currency is null and utxoArray has zero items.'); | ||
utxoArray = (0, _lodash.uniqBy)(utxoArray, utxo => `${utxo.txId}:${utxo.vout}`); | ||
let utxos = utxoArray.map(utxo => { | ||
const utxos = utxoArray.map(utxo => { | ||
(0, _minimalisticAssert.default)(utxo.address, 'utxo.address must be provided'); | ||
@@ -65,8 +65,8 @@ (0, _minimalisticAssert.default)(utxo.address instanceof _address.default || addressMap, 'Must pass addressMap if utxo.address is not of type Address'); | ||
if (typeof json === 'string') json = JSON.parse(json); | ||
let utxoData = {}; | ||
const utxoData = {}; | ||
Object.keys(json).forEach(address => { | ||
let addr = new _address.default(json[address].address, { | ||
const addr = new _address.default(json[address].address, { | ||
path: json[address].path | ||
}); | ||
let sortedUtxos = (0, _lodash.sortBy)(json[address].utxos, utxo => currency.parse(utxo.value).toDefaultNumber()); | ||
const sortedUtxos = (0, _lodash.sortBy)(json[address].utxos, utxo => currency.parse(utxo.value).toDefaultNumber()); | ||
utxoData[address] = sortedUtxos.map(utxo => ({ | ||
@@ -78,3 +78,3 @@ ...utxo, | ||
}); | ||
let col = new UtxoCollection({ | ||
const col = new UtxoCollection({ | ||
currency | ||
@@ -95,5 +95,5 @@ }); | ||
const txMap = col => { | ||
let map = {}; | ||
const map = {}; | ||
col.toArray().forEach(utxo => { | ||
let key = `${utxo.txId}/${utxo.vout}`; | ||
const key = `${utxo.txId}/${utxo.vout}`; | ||
map[key] = utxo; | ||
@@ -103,5 +103,5 @@ }); | ||
}; | ||
let thisMap = txMap(this); | ||
let otherMap = txMap(utxoCol); | ||
let retUtxos = []; | ||
const thisMap = txMap(this); | ||
const otherMap = txMap(utxoCol); | ||
const retUtxos = []; | ||
Object.keys(thisMap).forEach(key => { | ||
@@ -127,4 +127,4 @@ if (!otherMap[key]) retUtxos.push(thisMap[key]); | ||
getAddressesForTxId(txId) { | ||
let arrUtxos = this.toArray().filter(utxo => utxo.txId === txId); | ||
let arrAddrs = arrUtxos.map(utxo => utxo.address); | ||
const arrUtxos = this.toArray().filter(utxo => utxo.txId === txId); | ||
const arrAddrs = arrUtxos.map(utxo => utxo.address); | ||
return _addressSet.default.fromArray(arrAddrs); | ||
@@ -150,3 +150,3 @@ } | ||
let currentUtxos = this; | ||
for (let address of utxoCol.addresses) { | ||
for (const address of utxoCol.addresses) { | ||
currentUtxos = currentUtxos.setAddressUtxos(address, utxoCol.getAddressUtxos(address)); | ||
@@ -157,7 +157,7 @@ } | ||
removeAddressUtxos(address) { | ||
let newData = { | ||
const newData = { | ||
...this._data | ||
}; | ||
delete newData[address.toString()]; | ||
let col = new UtxoCollection({ | ||
const col = new UtxoCollection({ | ||
...this | ||
@@ -177,7 +177,7 @@ }); | ||
if (utxos.size === 0) return [selected, remaining]; | ||
let arrUtxos = minimumAmount.defaultUnit.unitName === 'BTC' ? (0, _lodash.sortBy)(utxos.toArray(), utxo => !utxo.confirmations, utxo => utxo.value.toDefaultNumber()) : (0, _lodash.sortBy)(utxos.toArray(), utxo => utxo.value.toDefaultNumber()); | ||
let selectedUtxos = []; | ||
let currency = arrUtxos[0].value.unitType; | ||
const arrUtxos = minimumAmount.defaultUnit.unitName === 'BTC' ? (0, _lodash.sortBy)(utxos.toArray(), utxo => !utxo.confirmations, utxo => utxo.value.toDefaultNumber()) : (0, _lodash.sortBy)(utxos.toArray(), utxo => utxo.value.toDefaultNumber()); | ||
const selectedUtxos = []; | ||
const currency = arrUtxos[0].value.unitType; | ||
let sum = currency.ZERO; | ||
for (let utxo of arrUtxos) { | ||
for (const utxo of arrUtxos) { | ||
selectedUtxos.push(utxo); | ||
@@ -245,7 +245,7 @@ sum = sum.add(utxo.value); | ||
if (utxos.size === 0) return this.removeAddressUtxos(address); | ||
let newUtxos = { | ||
const newUtxos = { | ||
...this._data, | ||
[address.toString()]: utxos.toArray() | ||
}; | ||
let col = new UtxoCollection({ | ||
const col = new UtxoCollection({ | ||
...this | ||
@@ -263,3 +263,3 @@ }); | ||
toJSON() { | ||
let jsonMap = {}; | ||
const jsonMap = {}; | ||
[...this.addresses].forEach(addr => { | ||
@@ -284,4 +284,4 @@ const address = addr.toString(); | ||
union(utxos) { | ||
let arr1 = this.toArray(); | ||
let arr2 = utxos.toArray(); | ||
const arr1 = this.toArray(); | ||
const arr2 = utxos.toArray(); | ||
return UtxoCollection.fromArray([...arr1, ...arr2], { | ||
@@ -341,5 +341,5 @@ ...this | ||
if (!Array.isArray(utxos)) return; | ||
for (let utxo of utxos) { | ||
for (const utxo of utxos) { | ||
if (typeof utxo.value === 'object' && utxo.value.unitType) return utxo.value.unitType; | ||
} | ||
} |
{ | ||
"name": "@exodus/models", | ||
"version": "9.0.0", | ||
"version": "10.0.0", | ||
"description": "Exodus models", | ||
@@ -35,3 +35,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "fa4657928633ef68e7392f524e2a4144ab12ddcf" | ||
"gitHead": "fddef337a8f8935e851d0baddb429870a04859c5" | ||
} |
75258
1999
25