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 d3c33ed commit 5b41d8f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
7 changes: 4 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export async function createSwap(
export async function confirmSwap(
swap: PreSwap,
redeem: {
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC,
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT,
address: string,
} | {
asset: SwapAsset.EUR,
Expand All @@ -146,7 +146,7 @@ export async function confirmSwap(
asset: SwapAsset.BTC_LN,
},
refund?: {
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC,
asset: SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT,
address: string,
} | {
asset: SwapAsset.EUR,
Expand Down Expand Up @@ -185,6 +185,7 @@ export async function confirmSwap(
case SwapAsset.BTC:
case SwapAsset.USDC:
case SwapAsset.USDC_MATIC:
case SwapAsset.USDT:
beneficiary = { [redeem.asset]: redeem.address };
break;
}
Expand Down Expand Up @@ -249,7 +250,7 @@ export async function getAssets(): Promise<AssetList> {
records[record.symbol] = {
asset: record.symbol,
name: record.name,
feePerUnit: coinsToUnits(record.symbol, record.feePerUnit, { treatUsdcAsMatic: true }),
feePerUnit: coinsToUnits(record.symbol, record.feePerUnit, { treatPolygonTokenAsMatic: true }),
limits: {
minimum: record.limits && record.limits.minimum
? coinsToUnits(record.symbol, record.limits.minimum)
Expand Down
24 changes: 16 additions & 8 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import {

export function coinsToUnits(asset: SwapAsset | ReferenceAsset, value: string | number, options: Partial<{
roundUp: boolean,
treatUsdcAsMatic: boolean,
treatPolygonTokenAsMatic: boolean,
}> = {}): number {
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;
// Some fees for USDC/T are provided in MATIC, and must be converted accordingly
const isPolygonToken = asset === SwapAsset.USDC || asset === SwapAsset.USDC_MATIC || asset === SwapAsset.USDT;
if (isPolygonToken && options.treatPolygonTokenAsMatic) decimals = 18;

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

Expand All @@ -50,7 +51,7 @@ export function convertFromData(from: FastspotPrice): PriceData {
? {
feePerUnit: coinsToUnits(asset, from.fundingNetworkFee.perUnit, {
roundUp: true,
treatUsdcAsMatic: true,
treatPolygonTokenAsMatic: true,
}),
}
: {}),
Expand All @@ -69,7 +70,7 @@ export function convertToData(to: FastspotPrice): PriceData {
? {
feePerUnit: coinsToUnits(asset, to.finalizeNetworkFee.perUnit, {
roundUp: true,
treatUsdcAsMatic: true,
treatPolygonTokenAsMatic: true,
}),
}
: {}),
Expand Down Expand Up @@ -100,10 +101,15 @@ export function convertContract<T extends SwapAsset>(contract: FastspotContract<
break;
case SwapAsset.USDC:
case SwapAsset.USDC_MATIC:
case SwapAsset.USDT:
htlc = {
address: contract.id.substring(0, 2) === '0x' ? contract.id : `0x${contract.id}`,
contract: (contract as FastspotContract<SwapAsset.USDC | SwapAsset.USDC_MATIC>).intermediary.address,
data: (contract as FastspotContract<SwapAsset.USDC | SwapAsset.USDC_MATIC>).intermediary.data,
contract:
(contract as FastspotContract<SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT>).intermediary
.address,
data:
(contract as FastspotContract<SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT>).intermediary
.data,
};
break;
case SwapAsset.EUR:
Expand All @@ -122,7 +128,9 @@ export function convertContract<T extends SwapAsset>(contract: FastspotContract<
refundAddress: contract.refund?.address || '',
redeemAddress: contract.asset === SwapAsset.EUR
? JSON.stringify((contract as FastspotContract<SwapAsset.EUR>).recipient)
: (contract as FastspotContract<SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC>)
: (contract as FastspotContract<
SwapAsset.NIM | SwapAsset.BTC | SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT
>)
.recipient.address,
amount: coinsToUnits(contract.asset, contract.amount),
timeout: contract.timeout,
Expand Down
10 changes: 6 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum SwapAsset {
BTC_LN = 'BTC_LN',
USDC = 'USDC',
USDC_MATIC = 'USDC_MATIC',
USDT = 'USDT', // Alternatively USDT_MATIC
EUR = 'EUR',
}

Expand All @@ -22,6 +23,7 @@ export const Precision = {
[SwapAsset.BTC_LN]: 8,
[SwapAsset.USDC]: 6,
[SwapAsset.USDC_MATIC]: 6,
[SwapAsset.USDT]: 6,
[SwapAsset.EUR]: 2,
[ReferenceAsset.USD]: 2,
} as const;
Expand Down Expand Up @@ -125,7 +127,7 @@ export type FastspotContract<T extends SwapAsset> = {
hash: string,
request: string,
}
: T extends SwapAsset.USDC | SwapAsset.USDC_MATIC ? {
: T extends SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT ? {
address: string,
data?: string, // Only provided for 'send' direction
}
Expand Down Expand Up @@ -250,13 +252,13 @@ export type EurHtlcDetails = {
address: string,
};

export type UsdcHtlcDetails = {
export type Erc20HtlcDetails = {
address: string,
contract: string,
data?: string,
};

export type HtlcDetails = NimHtlcDetails | BtcHtlcDetails | BtcLnHtlcDetails | UsdcHtlcDetails | EurHtlcDetails;
export type HtlcDetails = NimHtlcDetails | BtcHtlcDetails | BtcLnHtlcDetails | Erc20HtlcDetails | EurHtlcDetails;

export type Contract<T extends SwapAsset> = {
id: string,
Expand All @@ -270,7 +272,7 @@ export type Contract<T extends SwapAsset> = {
htlc: T extends SwapAsset.NIM ? NimHtlcDetails
: T extends SwapAsset.BTC ? BtcHtlcDetails
: T extends SwapAsset.BTC_LN ? BtcLnHtlcDetails
: T extends SwapAsset.USDC | SwapAsset.USDC_MATIC ? UsdcHtlcDetails
: T extends SwapAsset.USDC | SwapAsset.USDC_MATIC | SwapAsset.USDT ? Erc20HtlcDetails
: T extends SwapAsset.EUR ? EurHtlcDetails
: never,
};
Expand Down

0 comments on commit 5b41d8f

Please sign in to comment.