-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
apps/wallet-dashboard/components/Popup/Popups/MigratePopup.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import React from 'react'; | ||
import { VirtualList } from '@/components'; | ||
import { | ||
useCurrentAccount, | ||
useIotaClientContext, | ||
useSignAndExecuteTransaction, | ||
} from '@iota/dapp-kit'; | ||
import { getNetwork, IotaObjectData } from '@iota/iota-sdk/client'; | ||
import { useMigrationTransaction } from '@/hooks/useMigrationTransaction'; | ||
import { Button } from '@iota/apps-ui-kit'; | ||
import { useNotifications } from '@/hooks'; | ||
import { NotificationType } from '@/stores/notificationStore'; | ||
|
||
interface MigratePopupProps { | ||
stardustOutputObjects: IotaObjectData[]; | ||
closePopup: () => void; | ||
onSuccess?: (digest: string) => void; | ||
} | ||
|
||
function MigratePopup({ | ||
stardustOutputObjects, | ||
closePopup, | ||
onSuccess, | ||
}: MigratePopupProps): JSX.Element { | ||
const account = useCurrentAccount(); | ||
const { addNotification } = useNotifications(); | ||
const { data: migrateData } = useMigrationTransaction( | ||
stardustOutputObjects, | ||
account?.address || '', | ||
); | ||
const { network } = useIotaClientContext(); | ||
const { explorer } = getNetwork(network); | ||
const { mutateAsync: signAndExecuteTransaction, isPending } = useSignAndExecuteTransaction(); | ||
|
||
async function handleMigrate(): Promise<void> { | ||
if (!migrateData) return; | ||
signAndExecuteTransaction( | ||
{ | ||
transaction: migrateData.transaction, | ||
}, | ||
{ | ||
onSuccess: (tx) => { | ||
if (onSuccess) { | ||
onSuccess(tx.digest); | ||
} | ||
}, | ||
}, | ||
) | ||
.then(() => { | ||
closePopup(); | ||
addNotification('Migration transaction has been sent'); | ||
}) | ||
.catch(() => { | ||
addNotification('Migration transaction was not sent', NotificationType.Error); | ||
}); | ||
} | ||
|
||
const virtualItem = (asset: IotaObjectData): JSX.Element => ( | ||
<a href={`${explorer}/object/${asset.objectId}`} target="_blank" rel="noreferrer"> | ||
{asset.objectId} | ||
</a> | ||
); | ||
return ( | ||
<div className="flex min-w-[300px] flex-col gap-2"> | ||
<div className="flex flex-col"> | ||
<h1>Migratable Outputs: {stardustOutputObjects.length}</h1> | ||
<VirtualList | ||
items={stardustOutputObjects} | ||
estimateSize={() => 30} | ||
render={virtualItem} | ||
/> | ||
</div> | ||
<p>Gas Fees: {migrateData?.gasBudget?.toString() || '--'}</p> | ||
<Button text="Migrate" disabled={isPending} onClick={handleMigrate} /> | ||
</div> | ||
); | ||
} | ||
|
||
export default MigratePopup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useIotaClient } from '@iota/dapp-kit'; | ||
import { IotaObjectData } from '@iota/iota-sdk/client'; | ||
import { Transaction } from '@iota/iota-sdk/transactions'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
export function useMigrationTransaction( | ||
stardustOutputObjects: IotaObjectData[], | ||
senderAddress: string, | ||
) { | ||
const client = useIotaClient(); | ||
return useQuery({ | ||
// eslint-disable-next-line @tanstack/query/exhaustive-deps | ||
queryKey: ['migration-transaction', senderAddress], | ||
queryFn: async () => { | ||
const transaction = new Transaction(); | ||
transaction.setSender(senderAddress); | ||
await transaction.build({ client }); | ||
return transaction; | ||
}, | ||
enabled: !!stardustOutputObjects && !!senderAddress, | ||
gcTime: 0, | ||
select: (transaction) => { | ||
return { | ||
transaction, | ||
gasBudget: transaction.getData().gasData.budget, | ||
}; | ||
}, | ||
}); | ||
} |