diff --git a/src/lib/i18n/ITranslation.ts b/src/lib/i18n/ITranslation.ts index 34482f1..cd25ba4 100644 --- a/src/lib/i18n/ITranslation.ts +++ b/src/lib/i18n/ITranslation.ts @@ -74,6 +74,7 @@ export interface ITranslation { NETWORK_FEE: string; NETWORK: string; NOT_AUTHORIZED_TO_TRANSACT: string; + NO_TRUSTLINE: string; OFFER_ID: string; OPERATION_ACCOUNT_MERGE: string; OPERATION_ACCOUNT_TRUST: string; diff --git a/src/lib/i18n/languages/english.json b/src/lib/i18n/languages/english.json index c86035d..52c392b 100644 --- a/src/lib/i18n/languages/english.json +++ b/src/lib/i18n/languages/english.json @@ -74,6 +74,7 @@ "NETWORK_FEE": "Network Fee:", "NETWORK": "Network", "NOT_AUTHORIZED_TO_TRANSACT": "Authorization: The account is not authorized to transact with the asset", + "NO_TRUSTLINE": "The recipient hasn't established a trustline with the asset.", "OFFER_ID": "Offer ID:", "OPERATION_ACCOUNT_MERGE": "Account Merge", "OPERATION_ACCOUNT_TRUST": "Trust", diff --git a/src/lib/i18n/languages/spanish.json b/src/lib/i18n/languages/spanish.json index c878f67..81afb22 100644 --- a/src/lib/i18n/languages/spanish.json +++ b/src/lib/i18n/languages/spanish.json @@ -74,6 +74,7 @@ "NETWORK_FEE": "Comisión de la red:", "NETWORK": "Red", "NOT_AUTHORIZED_TO_TRANSACT": "Autorización: La cuenta no está autorizada para realizar transacciones con el activo", + "NO_TRUSTLINE": "El destinatario no estableció una línea de confianza con el activo.", "OFFER_ID": "ID de la Oferta:", "OPERATION_ACCOUNT_MERGE": "Combinar cuentas", "OPERATION_ACCOUNT_TRUST": "Establecer línea de confianza", diff --git a/src/lib/stellar/Payment.ts b/src/lib/stellar/Payment.ts index d003564..8315088 100644 --- a/src/lib/stellar/Payment.ts +++ b/src/lib/stellar/Payment.ts @@ -3,6 +3,24 @@ import { Asset, BASE_FEE, Operation, TransactionBuilder } from 'stellar-sdk'; import { CURRENT_NETWORK_PASSPHRASE } from './StellarNetwork'; import { server } from './utils'; +export async function checkTrustline(receiver: string, assetCode: string, issuer: string) { + const account = await server.loadAccount(receiver); + + for (const balance of account.balances) { + if (assetCode === 'native') { + if ('asset_type' in balance && balance.asset_type === 'native') { + return true; + } + } else if ('asset_code' in balance && 'asset_issuer' in balance) { + if (balance.asset_code === assetCode && balance.asset_issuer === issuer) { + return true; + } + } + } + + return false; +} + export async function createPaymentTransaction( publicKey: string, receiver: string, diff --git a/src/routes/payment/Payment.svelte b/src/routes/payment/Payment.svelte index e69487f..c90ce3c 100644 --- a/src/routes/payment/Payment.svelte +++ b/src/routes/payment/Payment.svelte @@ -4,7 +4,7 @@ import Bridge, { SimpleSignerPageType } from '../../lib/bridge/Bridge'; import { setMinimumPopUpSize } from '../../lib/components/helpers/popUpSizeHelper'; import type { WalletConnectService } from '../../lib/service/walletConnect'; - import { createPaymentTransaction } from '../../lib/stellar/Payment'; + import { checkTrustline, createPaymentTransaction } from '../../lib/stellar/Payment'; import { CURRENT_STELLAR_NETWORK } from '../../lib/stellar/StellarNetwork'; import { server } from '../../lib/stellar/utils'; import LocalStorage from '../../lib/storage/storage'; @@ -39,14 +39,24 @@ if (urlParams) { ({ receiver, amount, assetCode, issuer } = urlParams); + checkTrustlineAndSetMessage(); } else { bridge.addPaymentMessageHandler((message) => { ({ receiver, amount, assetCode, issuer } = message); + checkTrustlineAndSetMessage(); }); } let isPaymentInProgress = false; let paymentResultMessage = ''; + let trustlineMessage = ''; + + async function checkTrustlineAndSetMessage() { + const hasTrustline = await checkTrustline(receiver, assetCode, issuer); + if (!hasTrustline) { + trustlineMessage = $language.NO_TRUSTLINE; + } + } async function handlePayment() { if (isPaymentInProgress) return; @@ -102,10 +112,14 @@ {:else}
- {#if !receiver || !amount || !assetCode || !issuer} + {#if !receiver || !amount || !assetCode || !issuer || trustlineMessage === $language.NO_TRUSTLINE}

{$language.ERROR}

-

{$language.ERROR_MISSING_RECEIVER_DATA}

+

+ {trustlineMessage === $language.NO_TRUSTLINE + ? trustlineMessage + : $language.ERROR_MISSING_RECEIVER_DATA} +

{:else} @@ -119,7 +133,7 @@
{$language.YOU_ARE_PAYING} {amount} - {assetCode === 'native' ? 'XLM' : { assetCode }} + {assetCode === 'native' ? 'XLM' : assetCode} {$language.TO_THE_ACCOUNT}
{receiver}.