diff --git a/README.md b/README.md index 55e204d..9b9fcd5 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ await swapHandler.settleIncoming(serializedSettlementTx, secret) /** * 5. Await confirmation of settled HTLC * - * This is especially relevant for EUR contracts, as they may take a + * This is especially relevant for EUR and CRC contracts, as they may take a * few minutes to confirm after they were settled. * * The optional `onUpdate` callback receives the transaction object: diff --git a/src/EuroAssetAdapter.ts b/src/FiatAssetAdapter.ts similarity index 94% rename from src/EuroAssetAdapter.ts rename to src/FiatAssetAdapter.ts index af41075..7eb19a6 100644 --- a/src/EuroAssetAdapter.ts +++ b/src/FiatAssetAdapter.ts @@ -1,5 +1,5 @@ import { Htlc as OasisHtlc, HtlcStatus, SettlementInstruction, SettlementStatus } from '@nimiq/oasis-api'; -import { AssetAdapter, SwapAsset } from './IAssetAdapter'; +import { AssetAdapter, FiatSwapAsset } from './IAssetAdapter'; export type HtlcDetails = OasisHtlc; @@ -8,11 +8,11 @@ export interface OasisClient { settleHtlc(id: string, secret: string, settlementJWS: string, authorizationToken?: string): Promise; } -export class EuroAssetAdapter implements AssetAdapter { +export class FiatAssetAdapter implements AssetAdapter { private cancelCallback: ((reason: Error) => void) | null = null; private stopped = false; - constructor(public client: OasisClient) {} + constructor(public client: OasisClient) { } private async findTransaction( id: string, @@ -82,7 +82,7 @@ export class EuroAssetAdapter implements AssetAdapter { // eslint-disable-next-line class-methods-use-this public async fundHtlc(): Promise { - throw new Error('Method "fundHtlc" not available for EUR HTLCs'); + throw new Error('Method "fundHtlc" not available for EUR/CRC HTLCs'); } public async awaitHtlcSettlement(id: string): Promise> { @@ -103,7 +103,7 @@ export class EuroAssetAdapter implements AssetAdapter { hash: string, authorizationToken?: string, ): Promise { - if (this.stopped) throw new Error('EuroAssetAdapter called while stopped'); + if (this.stopped) throw new Error('FiatAssetAdapter called while stopped'); const jwsBody = settlementJWS.split('.')[1]; // JWS is encoded as Base64Url diff --git a/src/IAssetAdapter.ts b/src/IAssetAdapter.ts index 151b47f..4ce8981 100644 --- a/src/IAssetAdapter.ts +++ b/src/IAssetAdapter.ts @@ -11,9 +11,9 @@ import { Web3Client, } from './UsdcAssetAdapter'; import { - HtlcDetails as EuroHtlcDetails, + HtlcDetails, OasisClient, -} from './EuroAssetAdapter'; +} from './FiatAssetAdapter'; export enum SwapAsset { NIM = 'NIM', @@ -21,13 +21,17 @@ export enum SwapAsset { USDC = 'USDC', USDC_MATIC = 'USDC_MATIC', EUR = 'EUR', + CRC = 'CRC', } +export type FiatSwapAsset = SwapAsset.EUR | SwapAsset.CRC; + export type Transaction = TAsset extends SwapAsset.NIM ? NimiqTransactionDetails : TAsset extends SwapAsset.BTC ? BitcoinTransactionDetails : TAsset extends SwapAsset.USDC | SwapAsset.USDC_MATIC ? UsdcTransactionDetails - : TAsset extends SwapAsset.EUR ? EuroHtlcDetails + : TAsset extends SwapAsset.EUR ? HtlcDetails + : TAsset extends SwapAsset.CRC ? HtlcDetails : never; export type Client = @@ -35,6 +39,7 @@ export type Client = : TAsset extends SwapAsset.BTC ? BitcoinClient : TAsset extends SwapAsset.USDC | SwapAsset.USDC_MATIC ? Web3Client : TAsset extends SwapAsset.EUR ? OasisClient + : TAsset extends SwapAsset.CRC ? OasisClient : never; export interface AssetAdapter { diff --git a/src/SwapHandler.ts b/src/SwapHandler.ts index c36c41f..a0a2314 100644 --- a/src/SwapHandler.ts +++ b/src/SwapHandler.ts @@ -1,9 +1,9 @@ -import { AssetAdapter, SwapAsset } from './IAssetAdapter'; -import type { Transaction, Client } from './IAssetAdapter'; +import { SwapAsset } from './IAssetAdapter'; +import type { Transaction, Client, AssetAdapter } from './IAssetAdapter'; import { NimiqAssetAdapter } from './NimiqAssetAdapter'; import { BitcoinAssetAdapter } from './BitcoinAssetAdapter'; import { UsdcAssetAdapter } from './UsdcAssetAdapter'; -import { EuroAssetAdapter } from './EuroAssetAdapter'; +import { FiatAssetAdapter } from './FiatAssetAdapter'; // Re-export to centralize exports export { SwapAsset, Client, Transaction }; @@ -49,7 +49,9 @@ export class SwapHandler case SwapAsset.USDC_MATIC: return new UsdcAssetAdapter(client as Client) as AssetAdapter; case SwapAsset.EUR: - return new EuroAssetAdapter(client as Client) as AssetAdapter; + return new FiatAssetAdapter(client as Client) as AssetAdapter; + case SwapAsset.CRC: + return new FiatAssetAdapter(client as Client) as AssetAdapter; default: throw new Error(`Unsupported asset: ${asset}`); }