Skip to content

Commit

Permalink
Add support for USDT
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Oct 29, 2024
1 parent 4fde7f7 commit db6dd11
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/UsdcAssetAdapter.ts → src/Erc20AssetAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface Web3Client {
endBlock?: number;
}

export class UsdcAssetAdapter implements AssetAdapter<SwapAsset.USDC | SwapAsset.USDC_MATIC> {
export class Erc20AssetAdapter implements AssetAdapter<SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT> {
private cancelCallback: ((reason: Error) => void) | null = null;
private stopped = false;

Expand Down Expand Up @@ -137,7 +137,7 @@ export class UsdcAssetAdapter implements AssetAdapter<SwapAsset.USDC | SwapAsset
async (id, token, amount, recipient, hash, timeout, log) => {
if (amount.toNumber() !== value) {
console.warn(
`Found USDC HTLC, but amount does not match. Expected ${value}, found ${amount.toNumber()}`,
`Found ERC-20 HTLC, but amount does not match. Expected ${value}, found ${amount.toNumber()}`,
);
return false;
}
Expand All @@ -158,7 +158,7 @@ export class UsdcAssetAdapter implements AssetAdapter<SwapAsset.USDC | SwapAsset
}

public async fundHtlc(_serializedTx: string): Promise<Event<EventType.OPEN>> {
throw new Error('Method "fundHtlc" not available for USDC HTLCs');
throw new Error('Method "fundHtlc" not available for ERC-20 HTLCs');
}

public async awaitHtlcSettlement(htlcId: string): Promise<Event<EventType.REDEEM>> {
Expand All @@ -175,7 +175,7 @@ export class UsdcAssetAdapter implements AssetAdapter<SwapAsset.USDC | SwapAsset
_serializedTx: string,
_secret: string,
): Promise<Event<EventType.REDEEM>> {
throw new Error('Method "settleHtlc" not available for USDC HTLCs');
throw new Error('Method "settleHtlc" not available for ERC-20 HTLCs');
}

public async awaitSettlementConfirmation(htlcId: string): Promise<Event<EventType.REDEEM>> {
Expand Down
7 changes: 4 additions & 3 deletions src/IAssetAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { BitcoinClient, TransactionDetails as BitcoinTransactionDetails } from './BitcoinAssetAdapter';
import { GenericEvent as Erc20TransactionDetails, Web3Client } from './Erc20AssetAdapter';
import { HtlcDetails as EuroHtlcDetails, OasisClient } from './EuroAssetAdapter';
import { NimiqClient, TransactionDetails as NimiqTransactionDetails } from './NimiqAssetAdapter';
import { GenericEvent as UsdcTransactionDetails, Web3Client } from './UsdcAssetAdapter';

export enum SwapAsset {
NIM = 'NIM',
BTC = 'BTC',
USDC = 'USDC',
USDC_MATIC = 'USDC_MATIC',
USDT = 'USDT', // Alternatively USDT_MATIC
EUR = 'EUR',
}

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.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT ? Erc20TransactionDetails
: TAsset extends SwapAsset.EUR ? EuroHtlcDetails
: 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.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT ? Web3Client
: TAsset extends SwapAsset.EUR ? OasisClient
: never;

Expand Down
9 changes: 6 additions & 3 deletions src/SwapHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BitcoinAssetAdapter } from './BitcoinAssetAdapter';
import { Erc20AssetAdapter } from './Erc20AssetAdapter';
import { EuroAssetAdapter } from './EuroAssetAdapter';
import { AssetAdapter, SwapAsset } from './IAssetAdapter';
import type { Client, Transaction } from './IAssetAdapter';
import { NimiqAssetAdapter } from './NimiqAssetAdapter';
import { UsdcAssetAdapter } from './UsdcAssetAdapter';

// Re-export to centralize exports
export { Client, SwapAsset, Transaction };
Expand All @@ -13,7 +13,7 @@ export type Contract<TAsset extends SwapAsset> = {
address: string,
data: TAsset extends SwapAsset.NIM ? string : never,
script: TAsset extends SwapAsset.BTC ? string : never,
contract: TAsset extends SwapAsset.USDC | SwapAsset.USDC_MATIC ? string : never,
contract: TAsset extends SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT ? string : never,
},
};

Expand Down Expand Up @@ -47,7 +47,10 @@ export class SwapHandler<FromAsset extends SwapAsset, ToAsset extends SwapAsset>
return new BitcoinAssetAdapter(client as Client<SwapAsset.BTC>) as AssetAdapter<SwapAsset>;
case SwapAsset.USDC:
case SwapAsset.USDC_MATIC:
return new UsdcAssetAdapter(client as Client<SwapAsset.USDC | SwapAsset.USDC_MATIC>) as AssetAdapter<
case SwapAsset.USDT:
return new Erc20AssetAdapter(
client as Client<SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT>,
) as AssetAdapter<
SwapAsset
>;
case SwapAsset.EUR:
Expand Down

0 comments on commit db6dd11

Please sign in to comment.