@nimiq/fastspot-api
Advanced tools
Comparing version 1.6.1 to 1.7.0
@@ -8,3 +8,3 @@ import { RequestAsset, SwapAsset, Estimate, PreSwap, ContractWithEstimate, Swap, Limits, UserLimits, AssetList, ReferralCodes } from './types'; | ||
export declare function confirmSwap(swap: PreSwap, redeem: { | ||
asset: SwapAsset.NIM | SwapAsset.BTC; | ||
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC; | ||
address: string; | ||
@@ -18,12 +18,12 @@ } | { | ||
}, refund?: { | ||
asset: SwapAsset.NIM | SwapAsset.BTC; | ||
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC; | ||
address: string; | ||
} | { | ||
asset: SwapAsset.EUR; | ||
}, uid?: string): Promise<Swap>; | ||
}, uid?: string, kycToken?: string, oasisPrepareToken?: string): Promise<Swap>; | ||
export declare function getSwap(id: string): Promise<PreSwap | Swap>; | ||
export declare function cancelSwap(swap: PreSwap): Promise<PreSwap>; | ||
export declare function getContract<T extends SwapAsset>(asset: T, address: string): Promise<ContractWithEstimate<T>>; | ||
export declare function getLimits<T extends SwapAsset>(asset: T, address: string): Promise<Limits<T>>; | ||
export declare function getUserLimits(uid: string): Promise<UserLimits>; | ||
export declare function getLimits<T extends SwapAsset>(asset: T, address: string, kycUid?: string): Promise<Limits<T>>; | ||
export declare function getUserLimits(uid: string, kycUid?: string): Promise<UserLimits>; | ||
export declare function getAssets(): Promise<AssetList>; |
@@ -22,7 +22,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
function api(path, method, body, headers) { | ||
function api(path, method, options) { | ||
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: Object.assign({ 'Content-Type': 'application/json', 'X-FAST-ApiKey': API_KEY }, headers) }, (body ? { body: JSON.stringify(body) } : {}))); | ||
const response = yield fetch(`${API_URL}${path}`, Object.assign({ method, headers: Object.assign({ 'Content-Type': 'application/json', 'X-FAST-ApiKey': API_KEY }, options === null || options === void 0 ? void 0 : options.headers) }, ((options === null || options === void 0 ? void 0 : options.body) ? { body: JSON.stringify(options.body) } : {}))); | ||
if (!response.ok) { | ||
@@ -39,5 +39,7 @@ const error = yield response.json(); | ||
const result = yield api('/estimates', 'POST', { | ||
from, | ||
to, | ||
includedFees: 'required', | ||
body: { | ||
from, | ||
to, | ||
includedFees: 'required', | ||
}, | ||
}); | ||
@@ -67,14 +69,30 @@ const inputObject = result[0].from[0]; | ||
const result = yield api('/swaps', 'POST', { | ||
from, | ||
to, | ||
includedFees: 'required', | ||
}, headers); | ||
headers, | ||
body: { | ||
from, | ||
to, | ||
includedFees: 'required', | ||
}, | ||
}); | ||
return convertSwap(result); | ||
}); | ||
} | ||
export function confirmSwap(swap, redeem, refund, uid) { | ||
export function confirmSwap(swap, redeem, refund, uid, kycToken, oasisPrepareToken) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield api(`/swaps/${swap.id}`, 'POST', Object.assign(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: { [refund.asset]: 'address' in refund ? refund.address : '' } } : {})), (uid ? { uid } : {}))); | ||
const headers = {}; | ||
if (kycToken) { | ||
if (!uid) | ||
throw new Error('UID is required when using kycToken'); | ||
headers['X-S3-KYC-Token'] = kycToken; | ||
headers['X-S3-KYC-UID'] = uid; | ||
if (oasisPrepareToken) { | ||
headers['X-OASIS-Prepare-Token'] = oasisPrepareToken; | ||
} | ||
} | ||
const result = yield api(`/swaps/${swap.id}`, 'POST', { | ||
headers, | ||
body: Object.assign(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: { [refund.asset]: 'address' in refund ? refund.address : '' } } : {})), (uid ? { uid } : {})), | ||
}); | ||
return convertSwap(result); | ||
@@ -107,11 +125,19 @@ }); | ||
} | ||
export function getLimits(asset, address) { | ||
export function getLimits(asset, address, kycUid) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield api(`/limits/${asset}/${address}`, 'GET'); | ||
const headers = {}; | ||
if (kycUid) { | ||
headers['X-S3-KYC-UID'] = kycUid; | ||
} | ||
const result = yield api(`/limits/${asset}/${address}`, 'GET', { headers }); | ||
return convertLimits(result); | ||
}); | ||
} | ||
export function getUserLimits(uid) { | ||
export function getUserLimits(uid, kycUid) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield api(`/limits/${uid}`, 'GET'); | ||
const headers = {}; | ||
if (kycUid) { | ||
headers['X-S3-KYC-UID'] = kycUid; | ||
} | ||
const result = yield api(`/limits/${uid}`, 'GET', { headers }); | ||
return convertUserLimits(result); | ||
@@ -129,10 +155,10 @@ }); | ||
name: record.name, | ||
feePerUnit: coinsToUnits(record.symbol, record.feePerUnit), | ||
feePerUnit: coinsToUnits(record.symbol, record.feePerUnit, { treatUsdcAsMatic: true }), | ||
limits: { | ||
minimum: record.limits && record.limits.minimum | ||
? coinsToUnits(record.symbol, record.limits.minimum) | ||
: 0, | ||
: undefined, | ||
maximum: record.limits && record.limits.maximum | ||
? coinsToUnits(record.symbol, record.limits.maximum) | ||
: Infinity, | ||
: undefined, | ||
}, | ||
@@ -139,0 +165,0 @@ }; |
import { RequestAsset, SwapAsset, ReferenceAsset, PriceData, FastspotPrice, FastspotContract, FastspotPreSwap, FastspotSwap, FastspotLimits, FastspotUserLimits, Contract, PreSwap, Swap, Limits, UserLimits } from './types'; | ||
export declare function coinsToUnits(asset: SwapAsset | ReferenceAsset, value: string | number, roundUp?: boolean): number; | ||
export declare function coinsToUnits(asset: SwapAsset | ReferenceAsset, value: string | number, options?: Partial<{ | ||
roundUp: boolean; | ||
treatUsdcAsMatic: boolean; | ||
}>): number; | ||
export declare function convertFromData(from: FastspotPrice): PriceData; | ||
@@ -4,0 +7,0 @@ export declare function convertToData(to: FastspotPrice): PriceData; |
import { SwapAsset, Precision, } from './types'; | ||
export function coinsToUnits(asset, value, roundUp = false) { | ||
export function coinsToUnits(asset, value, options = {}) { | ||
let decimals = Precision[asset]; | ||
// Some fees for USDC are provided in MATIC, and must be converted accordingly | ||
if (asset === SwapAsset.USDC && options.treatUsdcAsMatic) | ||
decimals = 18; | ||
if (typeof decimals === 'undefined') | ||
throw new Error(`Invalid asset ${asset}`); | ||
const parts = value.toString().split('.'); | ||
parts[1] = (parts[1] || '').substr(0, decimals + 1).padEnd(decimals + 1, '0'); | ||
parts[1] = (parts[1] || '').substring(0, decimals + 1).padEnd(decimals + 1, '0'); | ||
const units = parseInt(parts.join(''), 10) / 10; | ||
if (roundUp) { | ||
if (options.roundUp) { | ||
return Math.ceil(units); | ||
@@ -16,11 +19,11 @@ } | ||
const asset = from.symbol; | ||
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) }); | ||
return Object.assign(Object.assign({ asset, amount: coinsToUnits(asset, from.amount), fee: coinsToUnits(asset, from.fundingNetworkFee.total, { roundUp: true }) }, (from.fundingNetworkFee.perUnit ? { | ||
feePerUnit: coinsToUnits(asset, from.fundingNetworkFee.perUnit, { roundUp: true, treatUsdcAsMatic: true }), | ||
} : {})), { serviceNetworkFee: coinsToUnits(asset, from.finalizeNetworkFee.total, { roundUp: true }), serviceEscrowFee: coinsToUnits(asset, from.operatingNetworkFee.total, { roundUp: true }) }); | ||
} | ||
export function convertToData(to) { | ||
const asset = to.symbol; | ||
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) }); | ||
return Object.assign(Object.assign({ asset, amount: coinsToUnits(asset, to.amount), fee: coinsToUnits(asset, to.finalizeNetworkFee.total, { roundUp: true }) }, (to.finalizeNetworkFee.perUnit ? { | ||
feePerUnit: coinsToUnits(asset, to.finalizeNetworkFee.perUnit, { roundUp: true, treatUsdcAsMatic: true }), | ||
} : {})), { serviceNetworkFee: coinsToUnits(asset, to.fundingNetworkFee.total, { roundUp: true }), serviceEscrowFee: coinsToUnits(asset, to.operatingNetworkFee.total, { roundUp: true }) }); | ||
} | ||
@@ -32,7 +35,3 @@ export function convertContract(contract) { | ||
case SwapAsset.NIM: | ||
htlc = { | ||
address: contract.intermediary.address, | ||
timeoutBlock: contract.intermediary.timeoutBlock, | ||
data: contract.intermediary.data, | ||
}; | ||
htlc = Object.assign({}, contract.intermediary); | ||
break; | ||
@@ -45,2 +44,9 @@ case SwapAsset.BTC: | ||
break; | ||
case SwapAsset.USDC: | ||
htlc = { | ||
address: contract.id.substring(0, 2) === '0x' ? contract.id : `0x${contract.id}`, | ||
contract: contract.intermediary.address, | ||
data: contract.intermediary.data, | ||
}; | ||
break; | ||
case SwapAsset.EUR: | ||
@@ -47,0 +53,0 @@ htlc = { |
export declare enum SwapAsset { | ||
NIM = "NIM", | ||
BTC = "BTC", | ||
USDC = "USDC", | ||
EUR = "EUR" | ||
@@ -16,2 +17,3 @@ } | ||
readonly BTC: 8; | ||
readonly USDC: 6; | ||
readonly EUR: 2; | ||
@@ -104,2 +106,5 @@ readonly USD: 2; | ||
scriptBytes: string; | ||
} : T extends SwapAsset.USDC ? { | ||
address: string; | ||
data?: string; | ||
} : T extends SwapAsset.EUR ? { | ||
@@ -161,4 +166,4 @@ contractId?: string; | ||
limits: { | ||
minimum: number; | ||
maximum: number; | ||
minimum?: number; | ||
maximum?: number; | ||
}; | ||
@@ -200,3 +205,8 @@ }; | ||
}; | ||
export declare type HtlcDetails = NimHtlcDetails | BtcHtlcDetails | EurHtlcDetails; | ||
export declare type UsdcHtlcDetails = { | ||
address: string; | ||
contract: string; | ||
data?: string; | ||
}; | ||
export declare type HtlcDetails = NimHtlcDetails | BtcHtlcDetails | UsdcHtlcDetails | EurHtlcDetails; | ||
export declare type Contract<T extends SwapAsset> = { | ||
@@ -210,3 +220,3 @@ asset: T; | ||
status: ContractStatus; | ||
htlc: T extends SwapAsset.NIM ? NimHtlcDetails : T extends SwapAsset.BTC ? BtcHtlcDetails : T extends SwapAsset.EUR ? EurHtlcDetails : never; | ||
htlc: T extends SwapAsset.NIM ? NimHtlcDetails : T extends SwapAsset.BTC ? BtcHtlcDetails : T extends SwapAsset.USDC ? UsdcHtlcDetails : T extends SwapAsset.EUR ? EurHtlcDetails : never; | ||
}; | ||
@@ -213,0 +223,0 @@ export declare type ContractWithEstimate<T extends SwapAsset> = Estimate & { |
@@ -5,2 +5,3 @@ export var SwapAsset; | ||
SwapAsset["BTC"] = "BTC"; | ||
SwapAsset["USDC"] = "USDC"; | ||
SwapAsset["EUR"] = "EUR"; | ||
@@ -15,2 +16,3 @@ })(SwapAsset || (SwapAsset = {})); | ||
[SwapAsset.BTC]: 8, | ||
[SwapAsset.USDC]: 6, | ||
[SwapAsset.EUR]: 2, | ||
@@ -17,0 +19,0 @@ [ReferenceAsset.USD]: 2, |
{ | ||
"name": "@nimiq/fastspot-api", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "Typescript library to interact with the Fastspot API", | ||
@@ -5,0 +5,0 @@ "repository": "git@github.com:nimiq/fastspot-api.git", |
37328
669