From 8805b8f939cf925305c28855e81469526c8197a4 Mon Sep 17 00:00:00 2001 From: Gonza Montiel Date: Mon, 7 Aug 2023 14:53:47 +0200 Subject: [PATCH 1/7] use stellar asset code instead of code:issuer for displaying in dialogs --- src/helpers/spacewalk.ts | 15 +++++++++++---- src/pages/bridge/TransferDialog.tsx | 14 +++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/helpers/spacewalk.ts b/src/helpers/spacewalk.ts index 47daf20e..2e90c8e8 100644 --- a/src/helpers/spacewalk.ts +++ b/src/helpers/spacewalk.ts @@ -1,12 +1,12 @@ -import bs58 from 'bs58'; -import { H256 } from '@polkadot/types/interfaces'; import { ApiPromise } from '@polkadot/api'; +import { U8aFixed } from '@polkadot/types-codec'; +import { H256 } from '@polkadot/types/interfaces'; import { SpacewalkPrimitivesCurrencyId } from '@polkadot/types/lookup'; +import bs58 from 'bs58'; import { DateTime } from 'luxon'; import { Asset, Keypair } from 'stellar-sdk'; -import { convertRawHexKeyToPublicKey } from './stellar'; import { TenantName } from '../models/Tenant'; -import { U8aFixed } from '@polkadot/types-codec'; +import { convertRawHexKeyToPublicKey } from './stellar'; // Convert a hex string to an ASCII string function hex_to_ascii(hexString: string, leading0x = true) { @@ -128,6 +128,13 @@ export function currencyToString(currency: SpacewalkPrimitivesCurrencyId, tenant } } +export function currencyToStellarAssetCode(currency: SpacewalkPrimitivesCurrencyId, tenant: TenantName = TenantName.Pendulum) { + if (!currency.isStellar) { + throw Error('Not an Stellar asset'); + } + return currencyToString(currency,tenant)?.split(':')[0]; +} + function tryConvertCodeToAscii(code: U8aFixed) { const ascii = hex_to_ascii(code.toHex()); if (ascii !== ascii.trim()) { diff --git a/src/pages/bridge/TransferDialog.tsx b/src/pages/bridge/TransferDialog.tsx index 03b23703..60f85be2 100644 --- a/src/pages/bridge/TransferDialog.tsx +++ b/src/pages/bridge/TransferDialog.tsx @@ -12,7 +12,7 @@ import { CloseButton } from '../../components/CloseButton'; import { CopyableAddress } from '../../components/PublicKey'; import TransferCountdown from '../../components/TransferCountdown'; import { nativeToDecimal } from '../../helpers/parseNumbers'; -import { calculateDeadline, currencyToString, deriveShortenedRequestId } from '../../helpers/spacewalk'; +import { calculateDeadline, currencyToStellarAssetCode, deriveShortenedRequestId } from '../../helpers/spacewalk'; import { convertRawHexKeyToPublicKey } from '../../helpers/stellar'; import { toTitle } from '../../helpers/string'; import { useSecurityPallet } from '../../hooks/spacewalk/security'; @@ -141,7 +141,7 @@ interface TransferDialogProps { export function CompletedTransferDialog(props: TransferDialogProps) { const { transfer, visible, onClose } = props; - const stellarAsset = currencyToString(transfer.original.asset); + const stellarAsset = currencyToStellarAssetCode(transfer.original.asset); const content = ( <>
{`You have received ${transfer.amount} ${stellarAsset}`}
@@ -168,7 +168,7 @@ export function CompletedTransferDialog(props: TransferDialogProps) { export function CancelledTransferDialog(props: TransferDialogProps) { const { transfer, visible, onClose } = props; - const stellarAsset = currencyToString(transfer.original.asset); + const stellarAsset = currencyToStellarAssetCode(transfer.original.asset); const amountToSend = nativeToDecimal(transfer.original.amount.add(transfer.original.fee).toNumber()).toNumber(); const content = ( <> @@ -204,7 +204,7 @@ export function ReimbursedTransferDialog(props: TransferDialogProps) { const { transfer, visible, onClose } = props; const tenant = useGlobalState().tenantName; - const stellarAsset = currencyToString(transfer.original.asset); + const stellarAsset = currencyToStellarAssetCode(transfer.original.asset); const collateralAsset = transfer.original.vault.currencies.collateral; const content = ( @@ -213,7 +213,7 @@ export function ReimbursedTransferDialog(props: TransferDialogProps) { {'Your redeem request failed but you decided to burn ' + stellarAsset + ' in return for ' + - currencyToString(collateralAsset, tenant)} + currencyToStellarAssetCode(collateralAsset, tenant)}

{transfer.amount} {stellarAsset} @@ -237,7 +237,7 @@ export function ReimbursedTransferDialog(props: TransferDialogProps) { export function PendingTransferDialog(props: TransferDialogProps) { const { transfer, visible, onClose } = props; - const stellarAsset = currencyToString(transfer.original.asset); + const stellarAsset = currencyToStellarAssetCode(transfer.original.asset); const destinationStellarAddress = convertRawHexKeyToPublicKey(transfer.original.stellarAddress.toHex()).publicKey(); const amountToSend = nativeToDecimal(transfer.original.amount.add(transfer.original.fee).toNumber()); const { getActiveBlockNumber } = useSecurityPallet(); @@ -310,7 +310,7 @@ export function PendingTransferDialog(props: TransferDialogProps) { export function FailedTransferDialog(props: TransferDialogProps) { const { transfer, visible, onClose } = props; - const stellarAsset = currencyToString(transfer.original.asset); + const stellarAsset = currencyToStellarAssetCode(transfer.original.asset); const amountToSend = nativeToDecimal(transfer.original.amount.add(transfer.original.fee).toNumber()).toNumber(); const compensation = 0.05; const content = ( From de0fe699d2b70882829aff272d30a909bfe1491c Mon Sep 17 00:00:00 2001 From: Gonza Montiel Date: Tue, 8 Aug 2023 09:25:53 +0200 Subject: [PATCH 2/7] filter out transfers not owned by the connected account --- src/pages/bridge/Transfers.tsx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/pages/bridge/Transfers.tsx b/src/pages/bridge/Transfers.tsx index 8fcc4b41..0d110492 100644 --- a/src/pages/bridge/Transfers.tsx +++ b/src/pages/bridge/Transfers.tsx @@ -32,7 +32,7 @@ function Transfers(): JSX.Element { const { getIssueRequests } = useIssuePallet(); const { getRedeemRequests } = useRedeemPallet(); const { subscribeActiveBlockNumber } = useSecurityPallet(); - const { tenantName } = useGlobalState(); + const { tenantName, walletAccount } = useGlobalState(); const [currentTransfer, setCurrentTransfer] = useState(); const [activeBlockNumber, setActiveBlockNumber] = useState(0); const [data, setData] = useState(undefined); @@ -53,6 +53,10 @@ function Transfers(): JSX.Element { const entries: TTransfer[] = []; issueEntries.forEach((e) => { + if (!walletAccount || e.request.requester.eq(walletAccount?.address)) { + return; + } + const deadline = calculateDeadline( activeBlockNumber as number, e.request.opentime.toNumber(), @@ -60,7 +64,6 @@ function Transfers(): JSX.Element { ); const timedOut = deadline < DateTime.now(); - entries.push({ updated: estimateRequestCreationTime(activeBlockNumber as number, e.request.opentime.toNumber()), amount: nativeToDecimal(e.request.amount.toString()).toString(), @@ -73,6 +76,9 @@ function Transfers(): JSX.Element { }); redeemEntries.forEach((e) => { + if (!walletAccount || e.request.redeemer.eq(walletAccount?.address)) { + return; + } entries.push({ updated: estimateRequestCreationTime(activeBlockNumber as number, e.request.opentime.toNumber()), amount: nativeToDecimal(e.request.amount.toString()).toString(), @@ -87,7 +93,7 @@ function Transfers(): JSX.Element { return entries; }; fetchAllEntries().then((res) => setData(res)); - }, [activeBlockNumber, getIssueRequests, getRedeemRequests]); + }, [activeBlockNumber, walletAccount, getIssueRequests, getRedeemRequests]); const columns = useMemo(() => { const detailsColumn = detailsColumnCreator(setCurrentTransfer); From 9724dbb6fe2be28e351fc2b89895a3be0248c01a Mon Sep 17 00:00:00 2001 From: Gonza Montiel Date: Tue, 8 Aug 2023 16:46:40 +0200 Subject: [PATCH 3/7] Update src/pages/bridge/Transfers.tsx Co-authored-by: Marcel Ebert --- src/pages/bridge/Transfers.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/bridge/Transfers.tsx b/src/pages/bridge/Transfers.tsx index 0d110492..84d9ad7d 100644 --- a/src/pages/bridge/Transfers.tsx +++ b/src/pages/bridge/Transfers.tsx @@ -53,7 +53,7 @@ function Transfers(): JSX.Element { const entries: TTransfer[] = []; issueEntries.forEach((e) => { - if (!walletAccount || e.request.requester.eq(walletAccount?.address)) { + if (!walletAccount || !e.request.requester.eq(walletAccount?.address)) { return; } From 1c029dc80867f330bba8f362380436f37f68f436 Mon Sep 17 00:00:00 2001 From: Gonza Montiel Date: Tue, 8 Aug 2023 16:46:46 +0200 Subject: [PATCH 4/7] Update src/pages/bridge/Transfers.tsx Co-authored-by: Marcel Ebert --- src/pages/bridge/Transfers.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/bridge/Transfers.tsx b/src/pages/bridge/Transfers.tsx index 84d9ad7d..4e9eaa66 100644 --- a/src/pages/bridge/Transfers.tsx +++ b/src/pages/bridge/Transfers.tsx @@ -76,7 +76,7 @@ function Transfers(): JSX.Element { }); redeemEntries.forEach((e) => { - if (!walletAccount || e.request.redeemer.eq(walletAccount?.address)) { + if (!walletAccount || !e.request.redeemer.eq(walletAccount?.address)) { return; } entries.push({ From a889593510be38814d81aa46b5c2c3acfdf42e15 Mon Sep 17 00:00:00 2001 From: Gonza Montiel Date: Tue, 8 Aug 2023 17:17:10 +0200 Subject: [PATCH 5/7] replace uses of currencyToStellarAssetCode() with existing function --- src/helpers/spacewalk.ts | 7 ------- src/pages/bridge/TransferDialog.tsx | 14 +++++++------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/helpers/spacewalk.ts b/src/helpers/spacewalk.ts index 2e90c8e8..776abe8a 100644 --- a/src/helpers/spacewalk.ts +++ b/src/helpers/spacewalk.ts @@ -128,13 +128,6 @@ export function currencyToString(currency: SpacewalkPrimitivesCurrencyId, tenant } } -export function currencyToStellarAssetCode(currency: SpacewalkPrimitivesCurrencyId, tenant: TenantName = TenantName.Pendulum) { - if (!currency.isStellar) { - throw Error('Not an Stellar asset'); - } - return currencyToString(currency,tenant)?.split(':')[0]; -} - function tryConvertCodeToAscii(code: U8aFixed) { const ascii = hex_to_ascii(code.toHex()); if (ascii !== ascii.trim()) { diff --git a/src/pages/bridge/TransferDialog.tsx b/src/pages/bridge/TransferDialog.tsx index 60f85be2..ace12d80 100644 --- a/src/pages/bridge/TransferDialog.tsx +++ b/src/pages/bridge/TransferDialog.tsx @@ -12,7 +12,7 @@ import { CloseButton } from '../../components/CloseButton'; import { CopyableAddress } from '../../components/PublicKey'; import TransferCountdown from '../../components/TransferCountdown'; import { nativeToDecimal } from '../../helpers/parseNumbers'; -import { calculateDeadline, currencyToStellarAssetCode, deriveShortenedRequestId } from '../../helpers/spacewalk'; +import { calculateDeadline, convertCurrencyToStellarAsset, deriveShortenedRequestId } from '../../helpers/spacewalk'; import { convertRawHexKeyToPublicKey } from '../../helpers/stellar'; import { toTitle } from '../../helpers/string'; import { useSecurityPallet } from '../../hooks/spacewalk/security'; @@ -141,7 +141,7 @@ interface TransferDialogProps { export function CompletedTransferDialog(props: TransferDialogProps) { const { transfer, visible, onClose } = props; - const stellarAsset = currencyToStellarAssetCode(transfer.original.asset); + const stellarAsset = convertCurrencyToStellarAsset(transfer.original.asset)?.getCode(); const content = ( <>
{`You have received ${transfer.amount} ${stellarAsset}`}
@@ -168,7 +168,7 @@ export function CompletedTransferDialog(props: TransferDialogProps) { export function CancelledTransferDialog(props: TransferDialogProps) { const { transfer, visible, onClose } = props; - const stellarAsset = currencyToStellarAssetCode(transfer.original.asset); + const stellarAsset = convertCurrencyToStellarAsset(transfer.original.asset)?.getCode(); const amountToSend = nativeToDecimal(transfer.original.amount.add(transfer.original.fee).toNumber()).toNumber(); const content = ( <> @@ -204,7 +204,7 @@ export function ReimbursedTransferDialog(props: TransferDialogProps) { const { transfer, visible, onClose } = props; const tenant = useGlobalState().tenantName; - const stellarAsset = currencyToStellarAssetCode(transfer.original.asset); + const stellarAsset = convertCurrencyToStellarAsset(transfer.original.asset)?.getCode(); const collateralAsset = transfer.original.vault.currencies.collateral; const content = ( @@ -213,7 +213,7 @@ export function ReimbursedTransferDialog(props: TransferDialogProps) { {'Your redeem request failed but you decided to burn ' + stellarAsset + ' in return for ' + - currencyToStellarAssetCode(collateralAsset, tenant)} + convertCurrencyToStellarAsset(collateralAsset)?.getCode()}

{transfer.amount} {stellarAsset} @@ -237,7 +237,7 @@ export function ReimbursedTransferDialog(props: TransferDialogProps) { export function PendingTransferDialog(props: TransferDialogProps) { const { transfer, visible, onClose } = props; - const stellarAsset = currencyToStellarAssetCode(transfer.original.asset); + const stellarAsset = convertCurrencyToStellarAsset(transfer.original.asset)?.getCode(); const destinationStellarAddress = convertRawHexKeyToPublicKey(transfer.original.stellarAddress.toHex()).publicKey(); const amountToSend = nativeToDecimal(transfer.original.amount.add(transfer.original.fee).toNumber()); const { getActiveBlockNumber } = useSecurityPallet(); @@ -310,7 +310,7 @@ export function PendingTransferDialog(props: TransferDialogProps) { export function FailedTransferDialog(props: TransferDialogProps) { const { transfer, visible, onClose } = props; - const stellarAsset = currencyToStellarAssetCode(transfer.original.asset); + const stellarAsset = convertCurrencyToStellarAsset(transfer.original.asset)?.getCode(); const amountToSend = nativeToDecimal(transfer.original.amount.add(transfer.original.fee).toNumber()).toNumber(); const compensation = 0.05; const content = ( From 0fe20f44d01b64db20eade8cbcf243eaf7582482 Mon Sep 17 00:00:00 2001 From: Gonza Montiel Date: Tue, 8 Aug 2023 17:17:39 +0200 Subject: [PATCH 6/7] turn Pending and timedout transfers into Cancelled (this should already work like this) --- src/pages/bridge/Transfers.tsx | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/pages/bridge/Transfers.tsx b/src/pages/bridge/Transfers.tsx index 4e9eaa66..de4f78ee 100644 --- a/src/pages/bridge/Transfers.tsx +++ b/src/pages/bridge/Transfers.tsx @@ -64,13 +64,14 @@ function Transfers(): JSX.Element { ); const timedOut = deadline < DateTime.now(); + const pending = e.request.status.type === 'Pending'; entries.push({ updated: estimateRequestCreationTime(activeBlockNumber as number, e.request.opentime.toNumber()), amount: nativeToDecimal(e.request.amount.toString()).toString(), asset: convertCurrencyToStellarAsset(e.request.asset)?.code, transactionId: e.id.toString(), type: TransferType.issue, - status: timedOut ? 'Cancelled' : e.request.status.type, + status: timedOut && pending ? 'Cancelled' : e.request.status.type, original: e.request, }); }); @@ -79,13 +80,23 @@ function Transfers(): JSX.Element { if (!walletAccount || !e.request.redeemer.eq(walletAccount?.address)) { return; } + + const deadline = calculateDeadline( + activeBlockNumber as number, + e.request.opentime.toNumber(), + e.request.period.toNumber(), + ); + + const timedOut = deadline < DateTime.now(); + const pending = e.request.status.type === 'Pending'; + entries.push({ updated: estimateRequestCreationTime(activeBlockNumber as number, e.request.opentime.toNumber()), amount: nativeToDecimal(e.request.amount.toString()).toString(), asset: convertCurrencyToStellarAsset(e.request.asset)?.code, transactionId: e.id.toString(), type: TransferType.redeem, - status: e.request.status.type, + status: timedOut && pending ? 'Cancelled' : e.request.status.type, original: e.request, }); }); From 62c36e6894ba7c18dc1541dd860d9563d035abb8 Mon Sep 17 00:00:00 2001 From: Gonza Montiel Date: Wed, 9 Aug 2023 16:49:51 +0200 Subject: [PATCH 7/7] amend timedout 'back to stellar' transfers with the proper status --- src/pages/bridge/Transfers.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/bridge/Transfers.tsx b/src/pages/bridge/Transfers.tsx index de4f78ee..6bc89184 100644 --- a/src/pages/bridge/Transfers.tsx +++ b/src/pages/bridge/Transfers.tsx @@ -96,7 +96,7 @@ function Transfers(): JSX.Element { asset: convertCurrencyToStellarAsset(e.request.asset)?.code, transactionId: e.id.toString(), type: TransferType.redeem, - status: timedOut && pending ? 'Cancelled' : e.request.status.type, + status: timedOut && pending ? 'Failed' : e.request.status.type, original: e.request, }); });