@nimiq/fastspot-api
Advanced tools
Comparing version 1.5.1 to 1.5.2
205
dist/api.js
@@ -0,1 +1,10 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import { SwapAsset, } from './types'; | ||
@@ -11,111 +20,117 @@ import { validateRequestPairs, convertFromData, convertToData, convertContract, convertSwap, convertLimits, convertUserLimits, coinsToUnits, } from './helpers'; | ||
} | ||
async function api(path, method, body) { | ||
if (!API_URL || !API_KEY) | ||
throw new Error('API URL and key not set, call init() first'); | ||
const response = await fetch(`${API_URL}${path}`, { | ||
method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-FAST-ApiKey': API_KEY, | ||
}, | ||
...(body ? { body: JSON.stringify(body) } : {}), | ||
function api(path, method, body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!API_URL || !API_KEY) | ||
throw new Error('API URL and key not set, call init() first'); | ||
const response = yield fetch(`${API_URL}${path}`, Object.assign({ method, headers: { | ||
'Content-Type': 'application/json', | ||
'X-FAST-ApiKey': API_KEY, | ||
} }, (body ? { body: JSON.stringify(body) } : {}))); | ||
if (!response.ok) { | ||
const error = yield response.json(); | ||
throw new Error(error.detail); | ||
} | ||
return response.json(); | ||
}); | ||
if (!response.ok) { | ||
const error = await response.json(); | ||
throw new Error(error.detail); | ||
} | ||
return response.json(); | ||
} | ||
export async function getEstimate(from, to) { | ||
validateRequestPairs(from, to); | ||
const result = await api('/estimates', 'POST', { | ||
from, | ||
to, | ||
includedFees: 'required', | ||
export function getEstimate(from, to) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
validateRequestPairs(from, to); | ||
const result = yield api('/estimates', 'POST', { | ||
from, | ||
to, | ||
includedFees: 'required', | ||
}); | ||
const inputObject = result[0].from[0]; | ||
const outputObject = result[0].to[0]; | ||
if (!inputObject || !outputObject) | ||
throw new Error('Insufficient market liquidity'); | ||
const estimate = { | ||
from: convertFromData(inputObject), | ||
to: convertToData(outputObject), | ||
serviceFeePercentage: parseFloat(result[0].serviceFeePercentage), | ||
}; | ||
return estimate; | ||
}); | ||
const inputObject = result[0].from[0]; | ||
const outputObject = result[0].to[0]; | ||
if (!inputObject || !outputObject) | ||
throw new Error('Insufficient market liquidity'); | ||
const estimate = { | ||
from: convertFromData(inputObject), | ||
to: convertToData(outputObject), | ||
serviceFeePercentage: parseFloat(result[0].serviceFeePercentage), | ||
}; | ||
return estimate; | ||
} | ||
export async function createSwap(from, to) { | ||
validateRequestPairs(from, to); | ||
const result = await api('/swaps', 'POST', { | ||
from, | ||
to, | ||
includedFees: 'required', | ||
export function createSwap(from, to) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
validateRequestPairs(from, to); | ||
const result = yield api('/swaps', 'POST', { | ||
from, | ||
to, | ||
includedFees: 'required', | ||
}); | ||
return convertSwap(result); | ||
}); | ||
return convertSwap(result); | ||
} | ||
export async function confirmSwap(swap, redeem, refund, uid) { | ||
const result = await api(`/swaps/${swap.id}`, 'POST', { | ||
confirm: true, | ||
beneficiary: redeem.asset === SwapAsset.EUR | ||
? { [redeem.asset]: { | ||
kty: redeem.kty, | ||
crv: redeem.crv, | ||
x: redeem.x, | ||
...(redeem.y ? { y: redeem.y } : {}), | ||
} } | ||
: { [redeem.asset]: redeem.address }, | ||
refund: { [refund.asset]: 'address' in refund ? refund.address : '' }, | ||
...(uid ? { uid } : {}), | ||
export function confirmSwap(swap, redeem, refund, uid) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield api(`/swaps/${swap.id}`, 'POST', Object.assign({ confirm: true, beneficiary: redeem.asset === SwapAsset.EUR | ||
? { [redeem.asset]: Object.assign({ kty: redeem.kty, crv: redeem.crv, x: redeem.x }, (redeem.y ? { y: redeem.y } : {})) } | ||
: { [redeem.asset]: redeem.address }, refund: { [refund.asset]: 'address' in refund ? refund.address : '' } }, (uid ? { uid } : {}))); | ||
return convertSwap(result); | ||
}); | ||
return convertSwap(result); | ||
} | ||
export async function getSwap(id) { | ||
const result = await api(`/swaps/${id}`, 'GET'); | ||
return convertSwap(result); | ||
export function getSwap(id) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield api(`/swaps/${id}`, 'GET'); | ||
return convertSwap(result); | ||
}); | ||
} | ||
export async function cancelSwap(swap) { | ||
const result = await api(`/swaps/${swap.id}`, 'DELETE'); | ||
return convertSwap(result); | ||
export function cancelSwap(swap) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield api(`/swaps/${swap.id}`, 'DELETE'); | ||
return convertSwap(result); | ||
}); | ||
} | ||
export async function getContract(asset, address) { | ||
const result = await api(`/contracts/${asset}/${address}`, 'GET'); | ||
return { | ||
contract: convertContract(result.contract), | ||
from: convertFromData(result.info.from[0]), | ||
to: convertToData(result.info.to[0]), | ||
serviceFeePercentage: parseFloat(result.info.serviceFeePercentage), | ||
}; | ||
export function getContract(asset, address) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield api(`/contracts/${asset}/${address}`, 'GET'); | ||
return { | ||
contract: convertContract(result.contract), | ||
from: convertFromData(result.info.from[0]), | ||
to: convertToData(result.info.to[0]), | ||
serviceFeePercentage: parseFloat(result.info.serviceFeePercentage), | ||
}; | ||
}); | ||
} | ||
export async function getLimits(asset, address) { | ||
const result = await api(`/limits/${asset}/${address}`, 'GET'); | ||
return convertLimits(result); | ||
export function getLimits(asset, address) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield api(`/limits/${asset}/${address}`, 'GET'); | ||
return convertLimits(result); | ||
}); | ||
} | ||
export async function getUserLimits(uid) { | ||
const result = await api(`/limits/${uid}`, 'GET'); | ||
return convertUserLimits(result); | ||
export function getUserLimits(uid) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield api(`/limits/${uid}`, 'GET'); | ||
return convertUserLimits(result); | ||
}); | ||
} | ||
export async function getAssets() { | ||
const result = await api('/assets', 'GET'); | ||
const records = {}; | ||
for (const record of result) { | ||
try { | ||
records[record.symbol] = { | ||
asset: record.symbol, | ||
name: record.name, | ||
feePerUnit: coinsToUnits(record.symbol, record.feePerUnit), | ||
limits: { | ||
minimum: record.limits && record.limits.minimum | ||
? coinsToUnits(record.symbol, record.limits.minimum) | ||
: 0, | ||
maximum: record.limits && record.limits.maximum | ||
? coinsToUnits(record.symbol, record.limits.maximum) | ||
: Infinity, | ||
}, | ||
}; | ||
export function getAssets() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield api('/assets', 'GET'); | ||
const records = {}; | ||
for (const record of result) { | ||
try { | ||
records[record.symbol] = { | ||
asset: record.symbol, | ||
name: record.name, | ||
feePerUnit: coinsToUnits(record.symbol, record.feePerUnit), | ||
limits: { | ||
minimum: record.limits && record.limits.minimum | ||
? coinsToUnits(record.symbol, record.limits.minimum) | ||
: 0, | ||
maximum: record.limits && record.limits.maximum | ||
? coinsToUnits(record.symbol, record.limits.maximum) | ||
: Infinity, | ||
}, | ||
}; | ||
} | ||
catch (error) { | ||
console.warn(error.message, record); // eslint-disable-line no-console | ||
} | ||
} | ||
catch (error) { | ||
console.warn(error.message, record); // eslint-disable-line no-console | ||
} | ||
} | ||
return records; | ||
return records; | ||
}); | ||
} |
@@ -29,27 +29,14 @@ import { SwapAsset, ReferenceAsset, } from './types'; | ||
const asset = SwapAsset[from.symbol]; | ||
return { | ||
asset, | ||
amount: coinsToUnits(asset, from.amount), | ||
fee: coinsToUnits(asset, from.fundingNetworkFee.total, true), | ||
...(from.fundingNetworkFee.perUnit ? { | ||
feePerUnit: coinsToUnits(asset, from.fundingNetworkFee.perUnit, true), | ||
} : {}), | ||
serviceNetworkFee: coinsToUnits(asset, from.finalizeNetworkFee.total, true), | ||
serviceEscrowFee: coinsToUnits(asset, from.operatingNetworkFee.total, true), | ||
}; | ||
return Object.assign(Object.assign({ asset, amount: coinsToUnits(asset, from.amount), fee: coinsToUnits(asset, from.fundingNetworkFee.total, true) }, (from.fundingNetworkFee.perUnit ? { | ||
feePerUnit: coinsToUnits(asset, from.fundingNetworkFee.perUnit, true), | ||
} : {})), { serviceNetworkFee: coinsToUnits(asset, from.finalizeNetworkFee.total, true), serviceEscrowFee: coinsToUnits(asset, from.operatingNetworkFee.total, true) }); | ||
} | ||
export function convertToData(to) { | ||
const asset = SwapAsset[to.symbol]; | ||
return { | ||
asset, | ||
amount: coinsToUnits(asset, to.amount), | ||
fee: coinsToUnits(asset, to.finalizeNetworkFee.total, true), | ||
...(to.finalizeNetworkFee.perUnit ? { | ||
feePerUnit: coinsToUnits(asset, to.finalizeNetworkFee.perUnit, true), | ||
} : {}), | ||
serviceNetworkFee: coinsToUnits(asset, to.fundingNetworkFee.total, true), | ||
serviceEscrowFee: coinsToUnits(asset, to.operatingNetworkFee.total, true), | ||
}; | ||
return Object.assign(Object.assign({ asset, amount: coinsToUnits(asset, to.amount), fee: coinsToUnits(asset, to.finalizeNetworkFee.total, true) }, (to.finalizeNetworkFee.perUnit ? { | ||
feePerUnit: coinsToUnits(asset, to.finalizeNetworkFee.perUnit, true), | ||
} : {})), { serviceNetworkFee: coinsToUnits(asset, to.fundingNetworkFee.total, true), serviceEscrowFee: coinsToUnits(asset, to.operatingNetworkFee.total, true) }); | ||
} | ||
export function convertContract(contract) { | ||
var _a; | ||
let htlc; | ||
@@ -79,3 +66,3 @@ switch (contract.asset) { | ||
asset: contract.asset, | ||
refundAddress: contract.refund?.address || '', | ||
refundAddress: ((_a = contract.refund) === null || _a === void 0 ? void 0 : _a.address) || '', | ||
redeemAddress: contract.asset === SwapAsset.EUR | ||
@@ -107,8 +94,3 @@ ? JSON.stringify(contract.recipient) | ||
} | ||
const fullSwap = { | ||
...preSwap, | ||
hash: swap.hash, | ||
...(swap.secret ? { secret: swap.secret } : {}), | ||
contracts, | ||
}; | ||
const fullSwap = Object.assign(Object.assign(Object.assign(Object.assign({}, preSwap), { hash: swap.hash }), (swap.secret ? { secret: swap.secret } : {})), { contracts }); | ||
return fullSwap; | ||
@@ -115,0 +97,0 @@ } |
{ | ||
"name": "@nimiq/fastspot-api", | ||
"version": "1.5.1", | ||
"version": "1.5.2", | ||
"description": "Typescript library to interact with the Fastspot API", | ||
@@ -5,0 +5,0 @@ "repository": "git@github.com:nimiq/fastspot-api.git", |
33000
568