Skip to content

Commit

Permalink
Add dprint for formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Oct 29, 2024
1 parent da2d938 commit bf82af2
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 70 deletions.
13 changes: 13 additions & 0 deletions dprint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"typescript": {
"indentWidth": 4,
"quoteStyle": "preferSingle",
"typeLiteral.separatorKind": "comma",
},
"excludes": [
"**/node_modules",
],
"plugins": [
"https://plugins.dprint.dev/typescript-0.93.1.wasm",
],
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
],
"scripts": {
"build": "tsc",
"fmt": "dprint fmt",
"fmt:check": "dprint fmt --check",
"lint": "oxlint",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"dprint": "^0.47.5",
"oxlint": "^0.10.3",
"typescript": "^5.6.3"
}
Expand Down
42 changes: 23 additions & 19 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
import {
FastspotResult,
FastspotError,
AssetList,
ContractWithEstimate,
Estimate,
FastspotAsset,
FastspotContractWithEstimate,
FastspotError,
FastspotEstimate,
FastspotLimits,
FastspotPreSwap,
FastspotContractWithEstimate,
FastspotResult,
FastspotSwap,
FastspotLimits,
FastspotUserLimits,
RequestAsset,
SwapAsset,
Estimate,
Limits,
PreSwap,
ContractWithEstimate,
ReferralCodes,
RequestAsset,
Swap,
Limits,
SwapAsset,
UserLimits,
AssetList,
ReferralCodes,
} from './types';

import {
validateRequestPairs,
convertFromData,
convertToData,
coinsToUnits,
convertContract,
convertSwap,
convertFromData,
convertLimits,
convertSwap,
convertToData,
convertUserLimits,
coinsToUnits,
validateRequestPairs,
} from './helpers';

let API_URL: string | undefined;
let API_KEY: string | undefined;
let REFERRAL: ReferralCodes | undefined;
let FETCH: typeof fetch;

export function init(url: string, key: string, options?: Partial<{ referral?: ReferralCodes, customFetch?: typeof fetch }>) {
export function init(
url: string,
key: string,
options?: Partial<{ referral?: ReferralCodes, customFetch?: typeof fetch }>,
) {
if (!url || !key) throw new Error('url and key must be provided');
API_URL = url;
API_KEY = key;
Expand All @@ -50,7 +54,7 @@ async function api(
options?: {
headers?: Record<string, string>,
body?: object,
}
},
): Promise<FastspotResult> {
if (!API_URL || !API_KEY) throw new Error('API URL and key not set, call init() first');

Expand Down Expand Up @@ -171,7 +175,7 @@ export async function confirmSwap(
crv: redeem.crv,
x: redeem.x,
...(redeem.y ? { y: redeem.y } : {}),
}
},
};
break;
case SwapAsset.BTC_LN:
Expand Down
50 changes: 31 additions & 19 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import {
RequestAsset,
SwapAsset,
ReferenceAsset,
Precision,
PriceData,
FastspotPrice,
Contract,
FastspotContract,
FastspotLimits,
FastspotPreSwap,
FastspotPrice,
FastspotSwap,
FastspotLimits,
FastspotUserLimits,
Contract,
HtlcDetails,
Limits,
Precision,
PreSwap,
PriceData,
ReferenceAsset,
RequestAsset,
Swap,
Limits,
SwapAsset,
UserLimits,
HtlcDetails,
} from './types';

export function coinsToUnits(asset: SwapAsset | ReferenceAsset, value: string | number, options: Partial<{
Expand All @@ -25,7 +25,7 @@ export function coinsToUnits(asset: SwapAsset | ReferenceAsset, value: string |
let decimals = Precision[asset] as number;

// Some fees for USDC are provided in MATIC, and must be converted accordingly
if ((asset === SwapAsset.USDC || asset === SwapAsset.USDC_MATIC) && options.treatUsdcAsMatic) decimals = 18
if ((asset === SwapAsset.USDC || asset === SwapAsset.USDC_MATIC) && options.treatUsdcAsMatic) decimals = 18;

if (typeof decimals === 'undefined') throw new Error(`Invalid asset ${asset}`);

Expand All @@ -46,9 +46,14 @@ export function convertFromData(from: FastspotPrice): PriceData {
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 }),
} : {}),
...(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 }),
};
Expand All @@ -60,9 +65,14 @@ export function convertToData(to: FastspotPrice): PriceData {
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 }),
} : {}),
...(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 }),
};
Expand Down Expand Up @@ -102,7 +112,8 @@ export function convertContract<T extends SwapAsset>(contract: FastspotContract<
// TODO: Parse clearing instructions if provided
};
break;
default: throw new Error(`Invalid asset ${contract.asset}`);
default:
throw new Error(`Invalid asset ${contract.asset}`);
}

return {
Expand All @@ -111,7 +122,8 @@ export function convertContract<T extends SwapAsset>(contract: FastspotContract<
refundAddress: contract.refund?.address || '',
redeemAddress: contract.asset === SwapAsset.EUR
? JSON.stringify((contract as FastspotContract<SwapAsset.EUR>).recipient)
: (contract as FastspotContract<SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC>).recipient.address,
: (contract as FastspotContract<SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC>)
.recipient.address,
amount: coinsToUnits(contract.asset, contract.amount),
timeout: contract.timeout,
direction: contract.direction,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './types';
export * from './api';
export * from './types';
67 changes: 36 additions & 31 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,37 +96,43 @@ export type FastspotContract<T extends SwapAsset> = {
asset: T,
refund?: { address: string } | null,
recipient: T extends SwapAsset.EUR ? {
kty: string,
crv: string,
x: string,
y?: string,
} : {
address: string,
},
kty: string,
crv: string,
x: string,
y?: string,
}
: {
address: string,
},
amount: number,
timeout: number,
direction: 'send' | 'receive',
status: ContractStatus,
id: string,
intermediary: T extends SwapAsset.NIM ? {
address: string,
timeoutBlock: number,
data: string,
} : T extends SwapAsset.BTC ? {
p2sh: string,
p2wsh: string,
scriptBytes: string,
} : T extends SwapAsset.BTC_LN ? {
nodeId: string,
amount: string,
hash: string,
request: string,
} : T extends SwapAsset.USDC | SwapAsset.USDC_MATIC ? {
address: string,
data?: string, // Only provided for 'send' direction
} : T extends SwapAsset.EUR ? {
contractId?: string,
} : never,
address: string,
timeoutBlock: number,
data: string,
}
: T extends SwapAsset.BTC ? {
p2sh: string,
p2wsh: string,
scriptBytes: string,
}
: T extends SwapAsset.BTC_LN ? {
nodeId: string,
amount: string,
hash: string,
request: string,
}
: T extends SwapAsset.USDC | SwapAsset.USDC_MATIC ? {
address: string,
data?: string, // Only provided for 'send' direction
}
: T extends SwapAsset.EUR ? {
contractId?: string,
}
: never,
};

export type FastspotContractWithEstimate<T extends SwapAsset> = {
Expand Down Expand Up @@ -172,10 +178,10 @@ export type FastspotUserLimits = {
monthlyRemaining: string,
swap: string,
current: string,
}
};

export type FastspotResult
= FastspotAsset[]
export type FastspotResult =
| FastspotAsset[]
| FastspotEstimate[]
| FastspotSwap
| FastspotContractWithEstimate<SwapAsset>
Expand Down Expand Up @@ -205,9 +211,8 @@ export type AssetList = { [asset in SwapAsset]: Asset };

// export type RequestAsset = Partial<Record<SwapAsset, number>>;
export type RequestAsset<K extends SwapAsset> = {
[P in K]: (Record<P, number> & Partial<Record<Exclude<K, P>, never>>) extends infer O
? { [Q in keyof O]: O[Q] }
: never
[P in K]: (Record<P, number> & Partial<Record<Exclude<K, P>, never>>) extends infer O ? { [Q in keyof O]: O[Q] }
: never;
}[K];

export type PriceData = {
Expand Down
Loading

0 comments on commit bf82af2

Please sign in to comment.