Skip to content

Commit 5b41d8f

Browse files
committed
Add support for USDT
1 parent d3c33ed commit 5b41d8f

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/api.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export async function createSwap(
134134
export async function confirmSwap(
135135
swap: PreSwap,
136136
redeem: {
137-
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC,
137+
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT,
138138
address: string,
139139
} | {
140140
asset: SwapAsset.EUR,
@@ -146,7 +146,7 @@ export async function confirmSwap(
146146
asset: SwapAsset.BTC_LN,
147147
},
148148
refund?: {
149-
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC,
149+
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT,
150150
address: string,
151151
} | {
152152
asset: SwapAsset.EUR,
@@ -185,6 +185,7 @@ export async function confirmSwap(
185185
case SwapAsset.BTC:
186186
case SwapAsset.USDC:
187187
case SwapAsset.USDC_MATIC:
188+
case SwapAsset.USDT:
188189
beneficiary = { [redeem.asset]: redeem.address };
189190
break;
190191
}
@@ -249,7 +250,7 @@ export async function getAssets(): Promise<AssetList> {
249250
records[record.symbol] = {
250251
asset: record.symbol,
251252
name: record.name,
252-
feePerUnit: coinsToUnits(record.symbol, record.feePerUnit, { treatUsdcAsMatic: true }),
253+
feePerUnit: coinsToUnits(record.symbol, record.feePerUnit, { treatPolygonTokenAsMatic: true }),
253254
limits: {
254255
minimum: record.limits && record.limits.minimum
255256
? coinsToUnits(record.symbol, record.limits.minimum)

src/helpers.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ import {
2020

2121
export function coinsToUnits(asset: SwapAsset | ReferenceAsset, value: string | number, options: Partial<{
2222
roundUp: boolean,
23-
treatUsdcAsMatic: boolean,
23+
treatPolygonTokenAsMatic: boolean,
2424
}> = {}): number {
2525
let decimals = Precision[asset] as number;
2626

27-
// Some fees for USDC are provided in MATIC, and must be converted accordingly
28-
if ((asset === SwapAsset.USDC || asset === SwapAsset.USDC_MATIC) && options.treatUsdcAsMatic) decimals = 18;
27+
// Some fees for USDC/T are provided in MATIC, and must be converted accordingly
28+
const isPolygonToken = asset === SwapAsset.USDC || asset === SwapAsset.USDC_MATIC || asset === SwapAsset.USDT;
29+
if (isPolygonToken && options.treatPolygonTokenAsMatic) decimals = 18;
2930

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

@@ -50,7 +51,7 @@ export function convertFromData(from: FastspotPrice): PriceData {
5051
? {
5152
feePerUnit: coinsToUnits(asset, from.fundingNetworkFee.perUnit, {
5253
roundUp: true,
53-
treatUsdcAsMatic: true,
54+
treatPolygonTokenAsMatic: true,
5455
}),
5556
}
5657
: {}),
@@ -69,7 +70,7 @@ export function convertToData(to: FastspotPrice): PriceData {
6970
? {
7071
feePerUnit: coinsToUnits(asset, to.finalizeNetworkFee.perUnit, {
7172
roundUp: true,
72-
treatUsdcAsMatic: true,
73+
treatPolygonTokenAsMatic: true,
7374
}),
7475
}
7576
: {}),
@@ -100,10 +101,15 @@ export function convertContract<T extends SwapAsset>(contract: FastspotContract<
100101
break;
101102
case SwapAsset.USDC:
102103
case SwapAsset.USDC_MATIC:
104+
case SwapAsset.USDT:
103105
htlc = {
104106
address: contract.id.substring(0, 2) === '0x' ? contract.id : `0x${contract.id}`,
105-
contract: (contract as FastspotContract<SwapAsset.USDC | SwapAsset.USDC_MATIC>).intermediary.address,
106-
data: (contract as FastspotContract<SwapAsset.USDC | SwapAsset.USDC_MATIC>).intermediary.data,
107+
contract:
108+
(contract as FastspotContract<SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT>).intermediary
109+
.address,
110+
data:
111+
(contract as FastspotContract<SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT>).intermediary
112+
.data,
107113
};
108114
break;
109115
case SwapAsset.EUR:
@@ -122,7 +128,9 @@ export function convertContract<T extends SwapAsset>(contract: FastspotContract<
122128
refundAddress: contract.refund?.address || '',
123129
redeemAddress: contract.asset === SwapAsset.EUR
124130
? JSON.stringify((contract as FastspotContract<SwapAsset.EUR>).recipient)
125-
: (contract as FastspotContract<SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC>)
131+
: (contract as FastspotContract<
132+
SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT
133+
>)
126134
.recipient.address,
127135
amount: coinsToUnits(contract.asset, contract.amount),
128136
timeout: contract.timeout,

src/types.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export enum SwapAsset {
44
BTC_LN = 'BTC_LN',
55
USDC = 'USDC',
66
USDC_MATIC = 'USDC_MATIC',
7+
USDT = 'USDT', // Alternatively USDT_MATIC
78
EUR = 'EUR',
89
}
910

@@ -22,6 +23,7 @@ export const Precision = {
2223
[SwapAsset.BTC_LN]: 8,
2324
[SwapAsset.USDC]: 6,
2425
[SwapAsset.USDC_MATIC]: 6,
26+
[SwapAsset.USDT]: 6,
2527
[SwapAsset.EUR]: 2,
2628
[ReferenceAsset.USD]: 2,
2729
} as const;
@@ -125,7 +127,7 @@ export type FastspotContract<T extends SwapAsset> = {
125127
hash: string,
126128
request: string,
127129
}
128-
: T extends SwapAsset.USDC | SwapAsset.USDC_MATIC ? {
130+
: T extends SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT ? {
129131
address: string,
130132
data?: string, // Only provided for 'send' direction
131133
}
@@ -250,13 +252,13 @@ export type EurHtlcDetails = {
250252
address: string,
251253
};
252254

253-
export type UsdcHtlcDetails = {
255+
export type Erc20HtlcDetails = {
254256
address: string,
255257
contract: string,
256258
data?: string,
257259
};
258260

259-
export type HtlcDetails = NimHtlcDetails | BtcHtlcDetails | BtcLnHtlcDetails | UsdcHtlcDetails | EurHtlcDetails;
261+
export type HtlcDetails = NimHtlcDetails | BtcHtlcDetails | BtcLnHtlcDetails | Erc20HtlcDetails | EurHtlcDetails;
260262

261263
export type Contract<T extends SwapAsset> = {
262264
id: string,
@@ -270,7 +272,7 @@ export type Contract<T extends SwapAsset> = {
270272
htlc: T extends SwapAsset.NIM ? NimHtlcDetails
271273
: T extends SwapAsset.BTC ? BtcHtlcDetails
272274
: T extends SwapAsset.BTC_LN ? BtcLnHtlcDetails
273-
: T extends SwapAsset.USDC | SwapAsset.USDC_MATIC ? UsdcHtlcDetails
275+
: T extends SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT ? Erc20HtlcDetails
274276
: T extends SwapAsset.EUR ? EurHtlcDetails
275277
: never,
276278
};

0 commit comments

Comments
 (0)