@nimiq/fastspot-api
Advanced tools
Comparing version 1.9.0 to 1.10.0
@@ -1,2 +0,2 @@ | ||
import { RequestAsset, SwapAsset, Estimate, PreSwap, ContractWithEstimate, Swap, Limits, UserLimits, AssetList, ReferralCodes } from './types'; | ||
import { AssetList, ContractWithEstimate, Estimate, Limits, PreSwap, ReferralCodes, RequestAsset, Swap, SwapAsset, UserLimits } from './types'; | ||
export declare function init(url: string, key: string, options?: Partial<{ | ||
@@ -11,3 +11,3 @@ referral?: ReferralCodes; | ||
export declare function confirmSwap(swap: PreSwap, redeem: { | ||
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC; | ||
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT; | ||
address: string; | ||
@@ -23,3 +23,3 @@ } | { | ||
}, refund?: { | ||
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC; | ||
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT; | ||
address: string; | ||
@@ -26,0 +26,0 @@ } | { |
@@ -11,3 +11,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { SwapAsset, } from './types'; | ||
import { validateRequestPairs, convertFromData, convertToData, convertContract, convertSwap, convertLimits, convertUserLimits, coinsToUnits, } from './helpers'; | ||
import { coinsToUnits, convertContract, convertFromData, convertLimits, convertSwap, convertToData, convertUserLimits, validateRequestPairs, } from './helpers'; | ||
let API_URL; | ||
@@ -92,9 +92,24 @@ let API_KEY; | ||
} | ||
// Using a switch statement for TS safety | ||
let beneficiary; | ||
switch (redeem.asset) { | ||
case SwapAsset.EUR: | ||
beneficiary = { | ||
[redeem.asset]: Object.assign({ kty: redeem.kty, crv: redeem.crv, x: redeem.x }, (redeem.y ? { y: redeem.y } : {})), | ||
}; | ||
break; | ||
case SwapAsset.BTC_LN: | ||
beneficiary = {}; | ||
break; | ||
case SwapAsset.NIM: | ||
case SwapAsset.BTC: | ||
case SwapAsset.USDC: | ||
case SwapAsset.USDC_MATIC: | ||
case SwapAsset.USDT: | ||
beneficiary = { [redeem.asset]: redeem.address }; | ||
break; | ||
} | ||
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 === SwapAsset.BTC_LN | ||
? {} | ||
: { [redeem.asset]: redeem.address } }, (refund ? { refund: { [refund.asset]: 'address' in refund ? refund.address : '' } } : {})), (uid ? { uid } : {})), | ||
body: Object.assign(Object.assign({ confirm: true, beneficiary }, (refund ? { refund: { [refund.asset]: 'address' in refund ? refund.address : '' } } : {})), (uid ? { uid } : {})), | ||
}); | ||
@@ -157,3 +172,3 @@ return convertSwap(result); | ||
name: record.name, | ||
feePerUnit: coinsToUnits(record.symbol, record.feePerUnit, { treatUsdcAsMatic: true }), | ||
feePerUnit: coinsToUnits(record.symbol, record.feePerUnit, { treatPolygonTokenAsMatic: true }), | ||
limits: { | ||
@@ -160,0 +175,0 @@ minimum: record.limits && record.limits.minimum |
@@ -1,5 +0,5 @@ | ||
import { RequestAsset, SwapAsset, ReferenceAsset, PriceData, FastspotPrice, FastspotContract, FastspotPreSwap, FastspotSwap, FastspotLimits, FastspotUserLimits, Contract, PreSwap, Swap, Limits, UserLimits } from './types'; | ||
import { Contract, FastspotContract, FastspotLimits, FastspotPreSwap, FastspotPrice, FastspotSwap, FastspotUserLimits, Limits, PreSwap, PriceData, ReferenceAsset, RequestAsset, Swap, SwapAsset, UserLimits } from './types'; | ||
export declare function coinsToUnits(asset: SwapAsset | ReferenceAsset, value: string | number, options?: Partial<{ | ||
roundUp: boolean; | ||
treatUsdcAsMatic: boolean; | ||
treatPolygonTokenAsMatic: boolean; | ||
}>): number; | ||
@@ -6,0 +6,0 @@ export declare function convertFromData(from: FastspotPrice): PriceData; |
@@ -1,6 +0,7 @@ | ||
import { SwapAsset, Precision, } from './types'; | ||
import { Precision, SwapAsset, } from './types'; | ||
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 || asset === SwapAsset.USDC_MATIC) && options.treatUsdcAsMatic) | ||
// Some fees for USDC/T are provided in MATIC, and must be converted accordingly | ||
const isPolygonToken = asset === SwapAsset.USDC || asset === SwapAsset.USDC_MATIC || asset === SwapAsset.USDT; | ||
if (isPolygonToken && options.treatPolygonTokenAsMatic) | ||
decimals = 18; | ||
@@ -19,11 +20,21 @@ if (typeof decimals === 'undefined') | ||
const asset = from.symbol; | ||
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 }) }); | ||
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, | ||
treatPolygonTokenAsMatic: 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, { 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 }) }); | ||
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, | ||
treatPolygonTokenAsMatic: true, | ||
}), | ||
} | ||
: {})), { serviceNetworkFee: coinsToUnits(asset, to.fundingNetworkFee.total, { roundUp: true }), serviceEscrowFee: coinsToUnits(asset, to.operatingNetworkFee.total, { roundUp: true }) }); | ||
} | ||
@@ -50,6 +61,9 @@ export function convertContract(contract) { | ||
case SwapAsset.USDC_MATIC: | ||
case SwapAsset.USDT: | ||
htlc = { | ||
address: contract.id.substring(0, 2) === '0x' ? contract.id : `0x${contract.id}`, | ||
contract: contract.intermediary.address, | ||
data: contract.intermediary.data, | ||
contract: contract.intermediary | ||
.address, | ||
data: contract.intermediary | ||
.data, | ||
}; | ||
@@ -63,3 +77,4 @@ break; | ||
break; | ||
default: throw new Error(`Invalid asset ${contract.asset}`); | ||
default: | ||
throw new Error(`Invalid asset ${contract.asset}`); | ||
} | ||
@@ -72,3 +87,4 @@ return { | ||
? JSON.stringify(contract.recipient) | ||
: contract.recipient.address, | ||
: contract | ||
.recipient.address, | ||
amount: coinsToUnits(contract.asset, contract.amount), | ||
@@ -86,3 +102,3 @@ timeout: contract.timeout, | ||
id: swap.id, | ||
expires: Math.floor(swap.expires), | ||
expires: Math.floor(swap.expires), // `result.expires` can be a float timestamp | ||
from: convertFromData(inputObject), | ||
@@ -89,0 +105,0 @@ to: convertToData(outputObject), |
@@ -0,2 +1,2 @@ | ||
export * from './api'; | ||
export * from './types'; | ||
export * from './api'; |
@@ -0,2 +1,2 @@ | ||
export * from './api'; | ||
export * from './types'; | ||
export * from './api'; |
@@ -7,2 +7,3 @@ export declare enum SwapAsset { | ||
USDC_MATIC = "USDC_MATIC", | ||
USDT = "USDT",// Alternatively USDT_MATIC | ||
EUR = "EUR" | ||
@@ -13,3 +14,3 @@ } | ||
} | ||
export declare type ReferralCodes = { | ||
export type ReferralCodes = { | ||
partnerCode: string; | ||
@@ -24,2 +25,3 @@ refCode?: string; | ||
readonly USDC_MATIC: 6; | ||
readonly USDT: 6; | ||
readonly EUR: 2; | ||
@@ -47,3 +49,3 @@ readonly USD: 2; | ||
} | ||
export declare type FastspotAsset = { | ||
export type FastspotAsset = { | ||
symbol: SwapAsset; | ||
@@ -68,3 +70,3 @@ name: string; | ||
}; | ||
export declare type FastspotFee = { | ||
export type FastspotFee = { | ||
perUnit?: string; | ||
@@ -74,3 +76,3 @@ total: string; | ||
}; | ||
export declare type FastspotPrice = { | ||
export type FastspotPrice = { | ||
symbol: SwapAsset; | ||
@@ -83,3 +85,3 @@ name: string; | ||
}; | ||
export declare type FastspotEstimate = { | ||
export type FastspotEstimate = { | ||
from: FastspotPrice[]; | ||
@@ -90,3 +92,3 @@ to: FastspotPrice[]; | ||
}; | ||
export declare type FastspotContract<T extends SwapAsset> = { | ||
export type FastspotContract<T extends SwapAsset> = { | ||
asset: T; | ||
@@ -122,3 +124,3 @@ refund?: { | ||
request: string; | ||
} : T extends SwapAsset.USDC | SwapAsset.USDC_MATIC ? { | ||
} : T extends SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT ? { | ||
address: string; | ||
@@ -130,7 +132,7 @@ data?: string; | ||
}; | ||
export declare type FastspotContractWithEstimate<T extends SwapAsset> = { | ||
export type FastspotContractWithEstimate<T extends SwapAsset> = { | ||
contract: FastspotContract<T>; | ||
info: FastspotEstimate; | ||
}; | ||
export declare type FastspotPreSwap = { | ||
export type FastspotPreSwap = { | ||
id: string; | ||
@@ -141,3 +143,3 @@ status: SwapStatus; | ||
}; | ||
export declare type FastspotSwap = FastspotPreSwap & { | ||
export type FastspotSwap = FastspotPreSwap & { | ||
hash: string; | ||
@@ -147,3 +149,3 @@ secret?: string; | ||
}; | ||
export declare type FastspotLimits<T extends SwapAsset> = { | ||
export type FastspotLimits<T extends SwapAsset> = { | ||
asset: T; | ||
@@ -164,3 +166,3 @@ daily: string; | ||
}; | ||
export declare type FastspotUserLimits = { | ||
export type FastspotUserLimits = { | ||
asset: ReferenceAsset; | ||
@@ -174,4 +176,4 @@ daily: string; | ||
}; | ||
export declare type FastspotResult = FastspotAsset[] | FastspotEstimate[] | FastspotSwap | FastspotContractWithEstimate<SwapAsset> | FastspotLimits<SwapAsset> | FastspotUserLimits; | ||
export declare type FastspotError = { | ||
export type FastspotResult = FastspotAsset[] | FastspotEstimate[] | FastspotSwap | FastspotContractWithEstimate<SwapAsset> | FastspotLimits<SwapAsset> | FastspotUserLimits; | ||
export type FastspotError = { | ||
status: number; | ||
@@ -182,3 +184,3 @@ type: string; | ||
}; | ||
export declare type Asset = { | ||
export type Asset = { | ||
asset: SwapAsset; | ||
@@ -192,6 +194,6 @@ name: string; | ||
}; | ||
export declare type AssetList = { | ||
export type AssetList = { | ||
[asset in SwapAsset]: Asset; | ||
}; | ||
export declare type RequestAsset<K extends SwapAsset> = { | ||
export type RequestAsset<K extends SwapAsset> = { | ||
[P in K]: (Record<P, number> & Partial<Record<Exclude<K, P>, never>>) extends infer O ? { | ||
@@ -201,3 +203,3 @@ [Q in keyof O]: O[Q]; | ||
}[K]; | ||
export declare type PriceData = { | ||
export type PriceData = { | ||
asset: SwapAsset; | ||
@@ -210,3 +212,3 @@ amount: number; | ||
}; | ||
export declare type Estimate = { | ||
export type Estimate = { | ||
from: PriceData; | ||
@@ -217,3 +219,3 @@ to: PriceData; | ||
}; | ||
export declare type NimHtlcDetails = { | ||
export type NimHtlcDetails = { | ||
address: string; | ||
@@ -223,13 +225,13 @@ timeoutBlock: number; | ||
}; | ||
export declare type BtcHtlcDetails = { | ||
export type BtcHtlcDetails = { | ||
address: string; | ||
script: string; | ||
}; | ||
export declare type BtcLnHtlcDetails = { | ||
export type BtcLnHtlcDetails = { | ||
nodeId: string; | ||
}; | ||
export declare type EurHtlcDetails = { | ||
export type EurHtlcDetails = { | ||
address: string; | ||
}; | ||
export declare type UsdcHtlcDetails = { | ||
export type Erc20HtlcDetails = { | ||
address: string; | ||
@@ -239,4 +241,4 @@ contract: string; | ||
}; | ||
export declare type HtlcDetails = NimHtlcDetails | BtcHtlcDetails | BtcLnHtlcDetails | UsdcHtlcDetails | EurHtlcDetails; | ||
export declare type Contract<T extends SwapAsset> = { | ||
export type HtlcDetails = NimHtlcDetails | BtcHtlcDetails | BtcLnHtlcDetails | Erc20HtlcDetails | EurHtlcDetails; | ||
export type Contract<T extends SwapAsset> = { | ||
id: string; | ||
@@ -250,8 +252,8 @@ asset: T; | ||
status: ContractStatus; | ||
htlc: T extends SwapAsset.NIM ? NimHtlcDetails : T extends SwapAsset.BTC ? BtcHtlcDetails : T extends SwapAsset.BTC_LN ? BtcLnHtlcDetails : T extends SwapAsset.USDC | SwapAsset.USDC_MATIC ? UsdcHtlcDetails : T extends SwapAsset.EUR ? EurHtlcDetails : never; | ||
htlc: T extends SwapAsset.NIM ? NimHtlcDetails : T extends SwapAsset.BTC ? BtcHtlcDetails : T extends SwapAsset.BTC_LN ? BtcLnHtlcDetails : T extends SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT ? Erc20HtlcDetails : T extends SwapAsset.EUR ? EurHtlcDetails : never; | ||
}; | ||
export declare type ContractWithEstimate<T extends SwapAsset> = Estimate & { | ||
export type ContractWithEstimate<T extends SwapAsset> = Estimate & { | ||
contract: Contract<T>; | ||
}; | ||
export declare type PreSwap = Estimate & { | ||
export type PreSwap = Estimate & { | ||
id: string; | ||
@@ -261,3 +263,3 @@ expires: number; | ||
}; | ||
export declare type Swap = PreSwap & { | ||
export type Swap = PreSwap & { | ||
hash: string; | ||
@@ -267,3 +269,3 @@ secret?: string; | ||
}; | ||
export declare type Limits<T extends SwapAsset> = { | ||
export type Limits<T extends SwapAsset> = { | ||
asset: T; | ||
@@ -286,3 +288,3 @@ daily: number; | ||
}; | ||
export declare type UserLimits = { | ||
export type UserLimits = { | ||
asset: ReferenceAsset; | ||
@@ -289,0 +291,0 @@ daily: number; |
@@ -8,2 +8,3 @@ export var SwapAsset; | ||
SwapAsset["USDC_MATIC"] = "USDC_MATIC"; | ||
SwapAsset["USDT"] = "USDT"; | ||
SwapAsset["EUR"] = "EUR"; | ||
@@ -21,2 +22,3 @@ })(SwapAsset || (SwapAsset = {})); | ||
[SwapAsset.USDC_MATIC]: 6, | ||
[SwapAsset.USDT]: 6, | ||
[SwapAsset.EUR]: 2, | ||
@@ -23,0 +25,0 @@ [ReferenceAsset.USD]: 2, |
{ | ||
"name": "@nimiq/fastspot-api", | ||
"version": "1.9.0", | ||
"version": "1.10.0", | ||
"description": "Typescript library to interact with the Fastspot API", | ||
@@ -14,7 +14,11 @@ "repository": "git@github.com:nimiq/fastspot-api.git", | ||
"scripts": { | ||
"prepare": "tsc" | ||
"build": "tsc", | ||
"fmt": "dprint fmt", | ||
"lint": "oxlint" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^4.6.3" | ||
"dprint": "^0.47.5", | ||
"oxlint": "^0.10.3", | ||
"typescript": "^5.6.3" | ||
} | ||
} |
39370
737
3