Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USDT Swapping #188

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"@formatjs/intl-displaynames": "^3.3.4",
"@linusborg/vue-simple-portal": "^0.1.4",
"@nimiq/electrum-client": "https://github.com/nimiq/electrum-client#build",
"@nimiq/fastspot-api": "^1.8.0",
"@nimiq/hub-api": "^1.8.0",
"@nimiq/fastspot-api": "^1.10.2",
"@nimiq/hub-api": "^1.9.0",
"@nimiq/iqons": "^1.5.2",
"@nimiq/libswap": "^1.3.0",
"@nimiq/libswap": "^1.4.2",
"@nimiq/oasis-api": "^1.1.1",
"@nimiq/oasis-bank-list": "https://github.com/nimiq/oasis-bank-list#main",
"@nimiq/rpc": "^0.4.1",
Expand Down Expand Up @@ -59,7 +59,7 @@
"devDependencies": {
"@babel/plugin-proposal-optional-chaining": "^7.8.3",
"@fullhuman/postcss-purgecss": "^1.3.0",
"@nimiq/core-web": "1.5.8",
"@nimiq/core-web": "1.6.3",
"@types/iban": "^0.0.32",
"@types/promise.allsettled": "^1.0.3",
"@typescript-eslint/eslint-plugin": "^5.4.0",
Expand Down
2 changes: 1 addition & 1 deletion src/components/BtcTransactionListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<UsdcIcon v-else-if="swapData
&& (swapData.asset === SwapAsset.USDC || swapData.asset === SwapAsset.USDC_MATIC)"/>
<UsdtIcon v-else-if="swapData
&& (swapData.asset === SwapAsset.USDT)"/>
&& (swapData.asset === SwapAsset.USDT_MATIC)"/>
<BankIcon v-else-if="swapData && swapData.asset === SwapAsset.EUR"/>
<Avatar v-else :label="!isCancelledSwap ? peerLabel || '' : ''"/>
<SwapSmallIcon/>
Expand Down
3 changes: 1 addition & 2 deletions src/components/TransactionListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
<BitcoinIcon v-else-if="swapData && swapData.asset === SwapAsset.BTC"/>
<UsdcIcon v-else-if="swapData
&& (swapData.asset === SwapAsset.USDC || swapData.asset === SwapAsset.USDC_MATIC)"/>
<UsdtIcon v-else-if="swapData
&& (swapData.asset === SwapAsset.USDT)"/>
<UsdtIcon v-else-if="swapData && swapData.asset === SwapAsset.USDT_MATIC"/>
<BankIcon v-else-if="swapData && swapData.asset === SwapAsset.EUR"/>
<Identicon v-else :address="peerAddress" />
<div v-if="isCashlink" class="cashlink-or-swap"><CashlinkXSmallIcon/></div>
Expand Down
6 changes: 2 additions & 4 deletions src/components/layouts/AccountOverview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
v-if="$config.fastspot.enabled
&& activeAccountInfo.type !== AccountType.LEDGER
&& hasPolygonAddresses && $config.polygon.enabled
&& stablecoin === CryptoCurrency.USDC
&& (
nimAccountBalance > 0
|| (stablecoin === CryptoCurrency.USDC
Expand All @@ -104,7 +103,7 @@
$event,
`${SwapAsset.NIM}-${stablecoin === CryptoCurrency.USDC
? SwapAsset.USDC_MATIC
: SwapAsset.USDT}`
: SwapAsset.USDT_MATIC}`
)"
@focus="nimUsdcSwapTooltip$ && nimUsdcSwapTooltip$.show()"
@blur="nimUsdcSwapTooltip$ && nimUsdcSwapTooltip$.hide()"
Expand Down Expand Up @@ -156,7 +155,6 @@
&& activeAccountInfo.type !== AccountType.LEDGER
&& hasBitcoinAddresses && $config.enableBitcoin
&& hasPolygonAddresses && $config.polygon.enabled
&& stablecoin === CryptoCurrency.USDC
&& (
btcAccountBalance > 0
|| (stablecoin === CryptoCurrency.USDC
Expand All @@ -176,7 +174,7 @@
$event,
`${SwapAsset.BTC}-${stablecoin === CryptoCurrency.USDC
? SwapAsset.USDC_MATIC
: SwapAsset.USDT}`
: SwapAsset.USDT_MATIC}`
)"
@focus="btcUsdcSwapTooltip$ && btcUsdcSwapTooltip$.show()"
@blur="btcUsdcSwapTooltip$ && btcUsdcSwapTooltip$.hide()"
Expand Down
6 changes: 3 additions & 3 deletions src/components/layouts/Sidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ export default defineComponent({
? stablecoin.value === CryptoCurrency.USDC
// For USDC, only native USDC is supported for swapping.
? store.accountUsdcBalance.value
// : stablecoin.value === CryptoCurrency.USDT
// ? store.accountUsdtBridgedBalance.value
: 0
: stablecoin.value === CryptoCurrency.USDT
? store.accountUsdtBridgedBalance.value
: 0
: store.accountBalance.value;
}),
);
Expand Down
36 changes: 30 additions & 6 deletions src/components/modals/BtcTransactionModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<i18n v-else-if="swapData && isIncoming" path="Swap from {address}" :tag="false">
<template v-if="swapData.asset === SwapAsset.NIM
|| swapData.asset === SwapAsset.USDC
|| swapData.asset === SwapAsset.USDC_MATIC" v-slot:address>
|| swapData.asset === SwapAsset.USDC_MATIC
|| swapData.asset === SwapAsset.USDT_MATIC" v-slot:address>
<label>{{ peerLabel || peerAddresses[0].substring(0, 9) }}</label>
</template>

Expand All @@ -26,7 +27,8 @@
<i18n v-else-if="swapData" path="Swap to {address}" :tag="false">
<template v-if="swapData.asset === SwapAsset.NIM
|| swapData.asset === SwapAsset.USDC
|| swapData.asset === SwapAsset.USDC_MATIC" v-slot:address>
|| swapData.asset === SwapAsset.USDC_MATIC
|| swapData.asset === SwapAsset.USDT_MATIC" v-slot:address>
<label>{{ peerLabel || peerAddresses[0].substring(0, 9) }}</label>
</template>

Expand Down Expand Up @@ -92,8 +94,7 @@
:address="peerAddresses[0]"/>
<UsdcIcon v-else-if="swapData
&& (swapData.asset === SwapAsset.USDC || swapData.asset === SwapAsset.USDC_MATIC)"/>
<UsdtIcon v-else-if="swapData
&& (swapData.asset === SwapAsset.USDT)"/>
<UsdtIcon v-else-if="swapData && swapData.asset === SwapAsset.USDT_MATIC"/>
<BankIcon v-else-if="swapData && swapData.asset === SwapAsset.EUR"/>
<Avatar v-else :label="!isCancelledSwap ? peerLabel || '' : ''"/>
<SwapMediumIcon/>
Expand Down Expand Up @@ -147,8 +148,7 @@
:address="peerAddresses[0]"/>
<UsdcIcon v-else-if="swapData
&& (swapData.asset === SwapAsset.USDC || swapData.asset === SwapAsset.USDC_MATIC)"/>
<UsdtIcon v-else-if="swapData
&& (swapData.asset === SwapAsset.USDT)"/>
<UsdtIcon v-else-if="swapData && swapData.asset === SwapAsset.USDT_MATIC"/>
<BankIcon v-else-if="swapData && swapData.asset === SwapAsset.EUR"/>
<Avatar v-else :label="!isCancelledSwap ? peerLabel || '' : ''"/>
<SwapMediumIcon/>
Expand Down Expand Up @@ -265,6 +265,20 @@
class="swapped-amount"
value-mask/>
</button>
<button v-if="swapData.asset === SwapAsset.USDT_MATIC && swapTransaction"
class="swap-other-side reset flex-row" :class="{'incoming': !isIncoming}"
@click="$router.replace(`/usdt-transaction/${swapTransaction.transactionHash}`)"
>
<div class="icon">
<GroundedArrowUpIcon v-if="isIncoming"/>
<GroundedArrowDownIcon v-else/>
</div>
<Amount
:amount="swapTransaction.value"
:currency="assetToCurrency(swapData.asset)"
class="swapped-amount"
value-mask/>
</button>
<div v-else-if="swapData.asset === SwapAsset.EUR"
class="swap-other-side flex-row" :class="{'incoming': !isIncoming}">
<div class="icon">
Expand Down Expand Up @@ -357,6 +371,7 @@ import { explorerTxLink } from '../../lib/ExplorerUtils';
import { assetToCurrency } from '../../lib/swap/utils/Assets';
import TransactionDetailOasisPayoutStatus from '../TransactionDetailOasisPayoutStatus.vue';
import { useUsdcTransactionsStore, Transaction as UsdcTransaction } from '../../stores/UsdcTransactions';
import { useUsdtTransactionsStore, Transaction as UsdtTransaction } from '../../stores/UsdtTransactions';
import { useBtcTransactionInfo } from '../../composables/useBtcTransactionInfo';
import UsdcIcon from '../icons/UsdcIcon.vue';
import UsdtIcon from '../icons/UsdtIcon.vue';
Expand Down Expand Up @@ -421,6 +436,10 @@ export default defineComponent({
return useUsdcTransactionsStore().state.transactions[swapData.value.transactionHash] || null;
}

if (swapData.value.asset === SwapAsset.USDT_MATIC) {
return useUsdtTransactionsStore().state.transactions[swapData.value.transactionHash] || null;
}

return null;
});

Expand Down Expand Up @@ -468,6 +487,11 @@ export default defineComponent({
return isIncoming.value ? [swapTx.sender] : [swapTx.recipient];
}

if (swapData.value.asset === SwapAsset.USDT_MATIC && swapTransaction.value) {
const swapTx = swapTransaction.value as UsdtTransaction;
return isIncoming.value ? [swapTx.sender] : [swapTx.recipient];
}

if (swapData.value.asset === SwapAsset.EUR) return [swapData.value.iban || ''];
}

Expand Down
7 changes: 5 additions & 2 deletions src/components/modals/BuyCryptoModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ import {
cancelSwap,
getSwap,
Swap,
Contract,
} from '@nimiq/fastspot-api';
import {
getHtlc,
Expand Down Expand Up @@ -741,7 +742,8 @@ export default defineComponent({
let oasisHtlc: OasisHtlc;
try {
// TODO: Retry getting the HTLC if first time fails
oasisHtlc = await getHtlc(confirmedSwap.contracts[SwapAsset.EUR]!.htlc.address);
const contract = confirmedSwap.contracts[SwapAsset.EUR] as Contract<SwapAsset.EUR>;
oasisHtlc = await getHtlc(contract.htlc.address);
if (oasisHtlc.status !== HtlcStatus.PENDING) {
throw new Error(`UNEXPECTED: OASIS HTLC is not 'pending' but '${oasisHtlc.status}'`);
}
Expand Down Expand Up @@ -883,7 +885,8 @@ export default defineComponent({
function onPaid() {
if (!swap.value!.fundingInstructions || swap.value!.fundingInstructions.type !== 'sepa') {
// We are in a test environment
sandboxMockClearHtlc(swap.value!.contracts.EUR!.htlc.address);
const contract = swap.value!.contracts[SwapAsset.EUR] as Contract<SwapAsset.EUR>;
sandboxMockClearHtlc(contract.htlc.address);
}

if (!swap.value!.stateEnteredAt) {
Expand Down
4 changes: 3 additions & 1 deletion src/components/modals/SellCryptoModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ import {
createSwap,
cancelSwap,
getSwap,
Contract,
} from '@nimiq/fastspot-api';
import {
getHtlc,
Expand Down Expand Up @@ -789,7 +790,8 @@ export default defineComponent({
});

// Fetch OASIS HTLC to get clearing instructions
const oasisHtlc = await getHtlc(confirmedSwap.contracts[SwapAsset.EUR]!.htlc.address);
const contract = confirmedSwap.contracts[SwapAsset.EUR] as Contract<SwapAsset.EUR>;
const oasisHtlc = await getHtlc(contract.htlc.address);
if (oasisHtlc.status !== HtlcStatus.PENDING && oasisHtlc.status !== HtlcStatus.CLEARED) {
const error = new Error(`UNEXPECTED: OASIS HTLC is not 'pending'/'cleared' but '${oasisHtlc.status}'`);
if (config.reportToSentry) captureException(error);
Expand Down
40 changes: 34 additions & 6 deletions src/components/modals/TransactionModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<i18n v-else-if="swapData && isIncoming" path="Swap from {address}" :tag="false">
<template v-if="swapData.asset === SwapAsset.BTC
|| swapData.asset === SwapAsset.USDC
|| swapData.asset === SwapAsset.USDC_MATIC" v-slot:address>
|| swapData.asset === SwapAsset.USDC_MATIC
|| swapData.asset === SwapAsset.USDT_MATIC" v-slot:address>
<label>{{ peerLabel || peerAddress.substring(0, 9) }}</label>
</template>

Expand All @@ -23,7 +24,8 @@
<i18n v-else-if="swapData" path="Swap to {address}" :tag="false">
<template v-if="swapData.asset === SwapAsset.BTC
|| swapData.asset === SwapAsset.USDC
|| swapData.asset === SwapAsset.USDC_MATIC" v-slot:address>
|| swapData.asset === SwapAsset.USDC_MATIC
|| swapData.asset === SwapAsset.USDT_MATIC" v-slot:address>
<label>{{ peerLabel || peerAddress.substring(0, 9) }}</label>
</template>

Expand Down Expand Up @@ -106,8 +108,7 @@
<BitcoinIcon v-if="swapData && swapData.asset === SwapAsset.BTC"/>
<UsdcIcon v-else-if="swapData
&& (swapData.asset === SwapAsset.USDC || swapData.asset === SwapAsset.USDC_MATIC)"/>
<UsdtIcon v-else-if="swapData
&& (swapData.asset === SwapAsset.USDT)"/>
<UsdtIcon v-else-if="swapData && swapData.asset === SwapAsset.USDT_MATIC"/>
<BankIcon v-else-if="swapData && swapData.asset === SwapAsset.EUR"/>
<Identicon v-else :address="peerAddress"/>
<div v-if="isCashlink" class="cashlink-or-swap"><CashlinkSmallIcon/></div>
Expand Down Expand Up @@ -150,8 +151,7 @@
<BitcoinIcon v-else-if="swapData && swapData.asset === SwapAsset.BTC"/>
<UsdcIcon v-else-if="swapData
&& (swapData.asset === SwapAsset.USDC || swapData.asset === SwapAsset.USDC_MATIC)"/>
<UsdtIcon v-else-if="swapData
&& (swapData.asset === SwapAsset.USDT)"/>
<UsdtIcon v-else-if="swapData && swapData.asset === SwapAsset.USDT_MATIC"/>
<BankIcon v-else-if="swapData && swapData.asset === SwapAsset.EUR"/>
<Identicon v-else :address="peerAddress"/>
<div v-if="isCashlink" class="cashlink-or-swap"><CashlinkSmallIcon/></div>
Expand Down Expand Up @@ -269,6 +269,20 @@
class="swapped-amount"
value-mask/>
</button>
<button v-if="swapData.asset === SwapAsset.USDT_MATIC && swapTransaction"
class="swap-other-side reset flex-row" :class="{'incoming': !isIncoming}"
@click="$router.replace(`/usdt-transaction/${swapTransaction.transactionHash}`)"
>
<div class="icon">
<GroundedArrowUpIcon v-if="isIncoming"/>
<GroundedArrowDownIcon v-else/>
</div>
<Amount
:amount="swapTransaction.value"
:currency="assetToCurrency(swapData.asset)"
class="swapped-amount"
value-mask/>
</button>
<div v-else-if="swapData.asset === SwapAsset.EUR"
class="swap-other-side flex-row" :class="{'incoming': !isIncoming}"
>
Expand Down Expand Up @@ -363,6 +377,7 @@ import { manageCashlink, refundSwap } from '../../hub';
import { SwapNimData } from '../../stores/Swaps';
import { useBtcTransactionsStore, Transaction as BtcTransaction } from '../../stores/BtcTransactions';
import { useUsdcTransactionsStore, Transaction as UsdcTransaction } from '../../stores/UsdcTransactions';
import { useUsdtTransactionsStore, Transaction as UsdtTransaction } from '../../stores/UsdtTransactions';
import { sendTransaction } from '../../network';
import { useAccountStore, AccountType } from '../../stores/Account';
import { explorerTxLink } from '../../lib/ExplorerUtils';
Expand Down Expand Up @@ -476,6 +491,12 @@ export default defineComponent({
return usdcTx;
}

if (swapData.value.asset === SwapAsset.USDT_MATIC) {
const usdtTx = useUsdtTransactionsStore().state.transactions[swapData.value.transactionHash];
if (!usdtTx) return null;
return usdtTx;
}

return null;
});

Expand Down Expand Up @@ -532,6 +553,13 @@ export default defineComponent({
: ''; // we don't know the peer address
}

if (swapData.value.asset === SwapAsset.USDT_MATIC) {
const swapTx = swapTransaction.value as UsdtTransaction | null;
return swapTx
? isIncoming.value ? swapTx.sender : swapTx.recipient
: ''; // we don't know the peer address
}

if (swapData.value.asset === SwapAsset.EUR) {
return swapData.value.iban || '';
}
Expand Down
5 changes: 3 additions & 2 deletions src/components/modals/UsdcTransactionModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ import GroundedArrowDownIcon from '../icons/GroundedArrowDownIcon.vue';
import Avatar from '../Avatar.vue';
import InteractiveShortAddress from '../InteractiveShortAddress.vue';
import TransactionDetailOasisPayoutStatus from '../TransactionDetailOasisPayoutStatus.vue';
import { SwapUsdcData } from '../../stores/Swaps';
import { SwapErc20Data } from '../../stores/Swaps';
import { useTransactionsStore, Transaction as NimTransaction } from '../../stores/Transactions';
import { useBtcTransactionsStore, Transaction as BtcTransaction } from '../../stores/BtcTransactions';
import { isProxyData, ProxyType } from '../../lib/ProxyDetection';
Expand Down Expand Up @@ -468,7 +468,7 @@ export default defineComponent({
);

async function refundHtlc() {
const htlcDetails = (swapInfo.value?.in as SwapUsdcData | undefined)?.htlc;
const htlcDetails = (swapInfo.value?.in as SwapErc20Data | undefined)?.htlc;
if (!htlcDetails) {
alert('Unexpected: unknown HTLC refund details'); // eslint-disable-line no-alert
return;
Expand Down Expand Up @@ -539,6 +539,7 @@ export default defineComponent({
: SwapAsset.USDC,
...relayRequest,
amount: transaction.value.value - fee.toNumber(),
token: transaction.value.token || config.polygon.usdc_bridged.tokenContract,
},
};

Expand Down
Loading