Skip to content

Commit

Permalink
feat: refactor EuroAssetAdapter to allow other fiat currencies
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Jun 5, 2024
1 parent 83121ef commit 7c44fe9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions src/EuroAssetAdapter.ts → src/FiatAssetAdapter.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -8,11 +8,11 @@ export interface OasisClient {
settleHtlc(id: string, secret: string, settlementJWS: string, authorizationToken?: string): Promise<HtlcDetails>;
}

export class EuroAssetAdapter implements AssetAdapter<SwapAsset.EUR> {
export class FiatAssetAdapter<Asset extends FiatSwapAsset> implements AssetAdapter<Asset> {
private cancelCallback: ((reason: Error) => void) | null = null;
private stopped = false;

constructor(public client: OasisClient) {}
constructor(public client: OasisClient) { }

private async findTransaction(
id: string,
Expand Down Expand Up @@ -82,7 +82,7 @@ export class EuroAssetAdapter implements AssetAdapter<SwapAsset.EUR> {

// eslint-disable-next-line class-methods-use-this
public async fundHtlc(): Promise<HtlcDetails> {
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<OasisHtlc<HtlcStatus.SETTLED>> {
Expand All @@ -103,7 +103,7 @@ export class EuroAssetAdapter implements AssetAdapter<SwapAsset.EUR> {
hash: string,
authorizationToken?: string,
): Promise<HtlcDetails> {
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
Expand Down
11 changes: 8 additions & 3 deletions src/IAssetAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,35 @@ import {
Web3Client,
} from './UsdcAssetAdapter';
import {
HtlcDetails as EuroHtlcDetails,
HtlcDetails,
OasisClient,
} from './EuroAssetAdapter';
} from './FiatAssetAdapter';

export enum SwapAsset {
NIM = 'NIM',
BTC = 'BTC',
USDC = 'USDC',
USDC_MATIC = 'USDC_MATIC',
EUR = 'EUR',
CRC = 'CRC',
}

export type FiatSwapAsset = SwapAsset.EUR | SwapAsset.CRC;

export type Transaction<TAsset extends SwapAsset> =
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<TAsset extends SwapAsset> =
TAsset extends SwapAsset.NIM ? NimiqClient
: 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<TAsset extends SwapAsset> {
Expand Down
10 changes: 6 additions & 4 deletions src/SwapHandler.ts
Original file line number Diff line number Diff line change
@@ -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 };
Expand Down Expand Up @@ -49,7 +49,9 @@ export class SwapHandler<FromAsset extends SwapAsset, ToAsset extends SwapAsset>
case SwapAsset.USDC_MATIC:
return new UsdcAssetAdapter(client as Client<SwapAsset.USDC | SwapAsset.USDC_MATIC>) as AssetAdapter<SwapAsset>;
case SwapAsset.EUR:
return new EuroAssetAdapter(client as Client<SwapAsset.EUR>) as AssetAdapter<SwapAsset>;
return new FiatAssetAdapter(client as Client<SwapAsset.EUR>) as AssetAdapter<SwapAsset>;
case SwapAsset.CRC:
return new FiatAssetAdapter(client as Client<SwapAsset.CRC>) as AssetAdapter<SwapAsset>;
default:
throw new Error(`Unsupported asset: ${asset}`);
}
Expand Down

0 comments on commit 7c44fe9

Please sign in to comment.