From bf82af2e3f016eace4fa6d92d97f83cfcb1acf9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren?= Date: Tue, 29 Oct 2024 08:16:07 +0100 Subject: [PATCH] Add dprint for formatting --- dprint.json | 13 +++++++ package.json | 3 ++ src/api.ts | 42 ++++++++++++---------- src/helpers.ts | 50 +++++++++++++++++---------- src/index.ts | 2 +- src/types.ts | 67 ++++++++++++++++++----------------- yarn.lock | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 201 insertions(+), 70 deletions(-) create mode 100644 dprint.json diff --git a/dprint.json b/dprint.json new file mode 100644 index 0000000..2471db5 --- /dev/null +++ b/dprint.json @@ -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", + ], +} diff --git a/package.json b/package.json index 6dd7c57..41a8dbf 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/src/api.ts b/src/api.ts index 4255663..1d184af 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,34 +1,34 @@ 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; @@ -36,7 +36,11 @@ 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; @@ -50,7 +54,7 @@ async function api( options?: { headers?: Record, body?: object, - } + }, ): Promise { if (!API_URL || !API_KEY) throw new Error('API URL and key not set, call init() first'); @@ -171,7 +175,7 @@ export async function confirmSwap( crv: redeem.crv, x: redeem.x, ...(redeem.y ? { y: redeem.y } : {}), - } + }, }; break; case SwapAsset.BTC_LN: diff --git a/src/helpers.ts b/src/helpers.ts index 8cb3716..071ce11 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -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<{ @@ -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}`); @@ -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 }), }; @@ -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 }), }; @@ -102,7 +112,8 @@ export function convertContract(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 { @@ -111,7 +122,8 @@ export function convertContract(contract: FastspotContract< refundAddress: contract.refund?.address || '', redeemAddress: contract.asset === SwapAsset.EUR ? JSON.stringify((contract as FastspotContract).recipient) - : (contract as FastspotContract).recipient.address, + : (contract as FastspotContract) + .recipient.address, amount: coinsToUnits(contract.asset, contract.amount), timeout: contract.timeout, direction: contract.direction, diff --git a/src/index.ts b/src/index.ts index f3b202c..4d4b4e2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export * from './types'; export * from './api'; +export * from './types'; diff --git a/src/types.ts b/src/types.ts index e23332b..af7c8ef 100644 --- a/src/types.ts +++ b/src/types.ts @@ -96,37 +96,43 @@ export type FastspotContract = { 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 = { @@ -172,10 +178,10 @@ export type FastspotUserLimits = { monthlyRemaining: string, swap: string, current: string, -} +}; -export type FastspotResult - = FastspotAsset[] +export type FastspotResult = + | FastspotAsset[] | FastspotEstimate[] | FastspotSwap | FastspotContractWithEstimate @@ -205,9 +211,8 @@ export type AssetList = { [asset in SwapAsset]: Asset }; // export type RequestAsset = Partial>; export type RequestAsset = { - [P in K]: (Record & Partial, never>>) extends infer O - ? { [Q in keyof O]: O[Q] } - : never + [P in K]: (Record & Partial, never>>) extends infer O ? { [Q in keyof O]: O[Q] } + : never; }[K]; export type PriceData = { diff --git a/yarn.lock b/yarn.lock index d13f043..318336f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,6 +2,100 @@ # yarn lockfile v1 +"@dprint/darwin-arm64@0.47.5": + version "0.47.5" + resolved "https://registry.yarnpkg.com/@dprint/darwin-arm64/-/darwin-arm64-0.47.5.tgz#74b2628d8d52c322fe2b7a00c48fe34ce3ed3f65" + integrity sha512-aVa3F//dkvEeNA7DCSlVcLxB0CV6zXpfbJZ/xsd+xgbayCXFuFr7qt0M6T4WP3gkQn5D7Zu8/pbXfRXQXo9qlQ== + +"@dprint/darwin-x64@0.47.5": + version "0.47.5" + resolved "https://registry.yarnpkg.com/@dprint/darwin-x64/-/darwin-x64-0.47.5.tgz#ee6f8e5fc8e5f88e18b7566f00f11638478c089e" + integrity sha512-84lmSLM/idIQ4UBkBHU1chP0WTldRjzLOEN22/XbdB1JGOIVN1pJIIU0lsmVWXaNI4SvGfty+thhGn73SSlQwA== + +"@dprint/linux-arm64-glibc@0.47.5": + version "0.47.5" + resolved "https://registry.yarnpkg.com/@dprint/linux-arm64-glibc/-/linux-arm64-glibc-0.47.5.tgz#6171dc88fd12d808e1176b065a05a336703c9fa9" + integrity sha512-Zk7Ut9Trgl2ssGWx0u3YegnRQFXivKaK1fPEimg/uMwdaLtWFGvNs6DACAJk34d883zmDkTQvllqY1kc78CeBg== + +"@dprint/linux-arm64-musl@0.47.5": + version "0.47.5" + resolved "https://registry.yarnpkg.com/@dprint/linux-arm64-musl/-/linux-arm64-musl-0.47.5.tgz#17420c69c90dd5f50b840c182b26a396449554b6" + integrity sha512-KmCu1yX5+/2MbT9n0iAgSK1gc6sQBcDayq8QRO7TRSs+gTDAZ/yQXHkhLdlk5fWsTR1mDQPVRG+2nAjHDhk8EA== + +"@dprint/linux-x64-glibc@0.47.5": + version "0.47.5" + resolved "https://registry.yarnpkg.com/@dprint/linux-x64-glibc/-/linux-x64-glibc-0.47.5.tgz#92db42f8ae2a50c4f5adda4570eec2015a31bf15" + integrity sha512-oBwENMikvcM+eT6JdliMIM+TOiV4VuBJGK+AN1sTOW45VeiYvmzGPOQwCxVeFq4MnZkMfrycC/PAY3C7Vcuh6w== + +"@dprint/linux-x64-musl@0.47.5": + version "0.47.5" + resolved "https://registry.yarnpkg.com/@dprint/linux-x64-musl/-/linux-x64-musl-0.47.5.tgz#a302ba7548ad09481edf10e1f0abdfb261589d4e" + integrity sha512-B1IGyaP0k25JDhqmR/UpvgyNtnclBoXV7ZNQbvygehBkTeC69afwzpUxjQ2pKj2F9bl1Rby//fhsAFOg60PzsA== + +"@dprint/win32-arm64@0.47.5": + version "0.47.5" + resolved "https://registry.yarnpkg.com/@dprint/win32-arm64/-/win32-arm64-0.47.5.tgz#5616fe5143a4f632c40e945e71dca1c28cb9953c" + integrity sha512-tKSPwGWsKc+QAdsx6UQav9AY8WXm+B5Mx23ujliJJMRss6Dnlmg17NjbAnSBSqXSrfqaMeQx6d4gujPpOS3F9A== + +"@dprint/win32-x64@0.47.5": + version "0.47.5" + resolved "https://registry.yarnpkg.com/@dprint/win32-x64/-/win32-x64-0.47.5.tgz#56f66778e088481f0f3235a1f00ab49b045fdd3b" + integrity sha512-ljbrGv5rDR00ziBFY6V+qLhtLHm2dsjgiFG9OU7kr3vHEj4eN31nwxU5W2mh0eMoRk7IbcJ5ahTJDLgoYdvfgw== + +"@oxlint/darwin-arm64@0.10.3": + version "0.10.3" + resolved "https://registry.yarnpkg.com/@oxlint/darwin-arm64/-/darwin-arm64-0.10.3.tgz#4ab8fe00731d1e333d40ea094091debb7b541e62" + integrity sha512-1zcWSqr9DmhbSrV1SEZRywp81CNq8ZidIcFGB8tGUMnlBNYiHnxyHHh2GIZsr1C2exmkBUGXruM1P8eQ9BWGuQ== + +"@oxlint/darwin-x64@0.10.3": + version "0.10.3" + resolved "https://registry.yarnpkg.com/@oxlint/darwin-x64/-/darwin-x64-0.10.3.tgz#fa90ecfccfe4adf75e9857d0f29a5751010e409d" + integrity sha512-lPAXCwYb+wZgjWAMhtPjGn7n1h8HvTulj6XCRyIgUu/aTGDRtnsae9IPXsJ9LJwYu5wyzkcWow0nhlIxkDaZ+w== + +"@oxlint/linux-arm64-gnu@0.10.3": + version "0.10.3" + resolved "https://registry.yarnpkg.com/@oxlint/linux-arm64-gnu/-/linux-arm64-gnu-0.10.3.tgz#7a5acb67412925cfa2a612ba1304c228e2606609" + integrity sha512-OmzpNVRwhjuI/RtoG4ROrwxRICTLoBB3ua4/Mh2iC3RelOog2xhlAAJHenKtzgQXNmebWof/0EckFpMfID7fMw== + +"@oxlint/linux-arm64-musl@0.10.3": + version "0.10.3" + resolved "https://registry.yarnpkg.com/@oxlint/linux-arm64-musl/-/linux-arm64-musl-0.10.3.tgz#153b10e6698e6fd9b73a3a524f24104845a7fb81" + integrity sha512-QQzlIW6rKOoGZK19gz/kbAbIFROJEV6ER0smcoBA7FzwsQ/ji796qx3TdITelgJwJt5x+OG5ZHztIjyzC871ZQ== + +"@oxlint/linux-x64-gnu@0.10.3": + version "0.10.3" + resolved "https://registry.yarnpkg.com/@oxlint/linux-x64-gnu/-/linux-x64-gnu-0.10.3.tgz#78a272ab9f26d06b452af2ad224f40886628377a" + integrity sha512-m4ZUKsQNXxXmHUYBEaO7KN83DPVsLTq8KaRO4qj0Cs1RgD8O3YYCCDewVe8x/9yskQrCCTP4mL0G4IZgDJ3Gpg== + +"@oxlint/linux-x64-musl@0.10.3": + version "0.10.3" + resolved "https://registry.yarnpkg.com/@oxlint/linux-x64-musl/-/linux-x64-musl-0.10.3.tgz#0b19efe13425364dc1c6735cc7b75850b43a4cf2" + integrity sha512-XKvAUYxE9bHjvWE4wmOlIdaVQN7pzZAH3Rx1EFg0MfGBVjyDm+uf0P3so+gx2PowqvBnBneL9tth2AS3Bcjl6g== + +"@oxlint/win32-arm64@0.10.3": + version "0.10.3" + resolved "https://registry.yarnpkg.com/@oxlint/win32-arm64/-/win32-arm64-0.10.3.tgz#8ed9dc0eaaf17c0c3b637b9878255be6b7e584fc" + integrity sha512-8xhYVvnwrveMHyXAgQAaDjJWFij4mZP6jtMlBIjk6pZSrjUzX+9o7Wh1zgH6kYApmcPDAzomYJLSYvyF5PzluA== + +"@oxlint/win32-x64@0.10.3": + version "0.10.3" + resolved "https://registry.yarnpkg.com/@oxlint/win32-x64/-/win32-x64-0.10.3.tgz#16993ddeb1e121486d9c34a499275aff96e2c1da" + integrity sha512-dQiHruLd42aock1WHp5BFvRvaz+Pe436dQkcmlCVmInhmOjRpDEVKPUUWamrImV6S8VM63bn4Jp60vpprjJQdQ== + +dprint@^0.47.5: + version "0.47.5" + resolved "https://registry.yarnpkg.com/dprint/-/dprint-0.47.5.tgz#6986cb09590e983a11b8a1a2a1f00a2671c6cece" + integrity sha512-EAP3OLYZXiW66HKMlhu6Gu0o7mzBVTWyMyuAAgT7dBtMX+W+pPJmIwyRUnTRQNyyFO4S7bAaa21rzIgo97Bg9A== + optionalDependencies: + "@dprint/darwin-arm64" "0.47.5" + "@dprint/darwin-x64" "0.47.5" + "@dprint/linux-arm64-glibc" "0.47.5" + "@dprint/linux-arm64-musl" "0.47.5" + "@dprint/linux-x64-glibc" "0.47.5" + "@dprint/linux-x64-musl" "0.47.5" + "@dprint/win32-arm64" "0.47.5" + "@dprint/win32-x64" "0.47.5" + oxlint@^0.10.3: version "0.10.3" resolved "https://registry.yarnpkg.com/oxlint/-/oxlint-0.10.3.tgz#e3592a879243b33459f56f5e5116ef3c293f85ea"