From daee98bb61974e1f76e6aaefb07ccc577da5e6e7 Mon Sep 17 00:00:00 2001 From: Alex Panturu Date: Tue, 27 Aug 2024 11:24:54 +0300 Subject: [PATCH] web-wallet: add token migration contract bindings --- web-wallet/CHANGELOG.md | 3 + web-wallet/README.md | 1 + .../ApproveMigration/ApproveMigration.svelte | 142 +++++++ .../ExecuteMigration/ExecuteMigration.svelte | 112 +++++ .../__snapshots__/GasControls.spec.js.snap | 53 +-- web-wallet/src/lib/components/index.js | 2 + .../MigrateContract/MigrateContract.svelte | 389 +++++++----------- .../containers/MigrateContract/tokenConfig.js | 33 ++ .../containers/MigrateContract/tokens.d.ts | 20 + .../src/lib/contracts/contract-descriptors.js | 15 + .../promise/__tests__/rejectAfter.spec.js | 30 ++ .../promise/__tests__/resolveAfter.spec.js | 30 ++ web-wallet/src/lib/dusk/promise/index.js | 2 + .../src/lib/dusk/promise/rejectAfter.js | 7 + .../src/lib/dusk/promise/resolveAfter.js | 7 + .../__tests__/createDataStore.spec.js | 351 ++++++++++++++++ .../lib/dusk/svelte-stores/createDataStore.js | 78 ++++ .../src/lib/dusk/svelte-stores/index.js | 1 + .../lib/dusk/svelte-stores/svelte-stores.d.ts | 10 + .../{erc_bep_duskABI.json => erc_bep_20.json} | 0 web-wallet/src/lib/migration/migration.js | 121 ++++++ .../src/lib/migration/walletConnection.js | 17 +- .../src/routes/(app)/dashboard/+page.svelte | 4 + .../__tests__/__snapshots__/page.spec.js.snap | 60 +++ .../(app)/dashboard/migrate/+page.svelte | 21 + web-wallet/vite-setup.js | 4 + web-wallet/vite.config.js | 2 + 27 files changed, 1223 insertions(+), 292 deletions(-) create mode 100644 web-wallet/src/lib/components/ApproveMigration/ApproveMigration.svelte create mode 100644 web-wallet/src/lib/components/ExecuteMigration/ExecuteMigration.svelte create mode 100644 web-wallet/src/lib/containers/MigrateContract/tokenConfig.js create mode 100644 web-wallet/src/lib/containers/MigrateContract/tokens.d.ts create mode 100644 web-wallet/src/lib/dusk/promise/__tests__/rejectAfter.spec.js create mode 100644 web-wallet/src/lib/dusk/promise/__tests__/resolveAfter.spec.js create mode 100644 web-wallet/src/lib/dusk/promise/index.js create mode 100644 web-wallet/src/lib/dusk/promise/rejectAfter.js create mode 100644 web-wallet/src/lib/dusk/promise/resolveAfter.js create mode 100644 web-wallet/src/lib/dusk/svelte-stores/__tests__/createDataStore.spec.js create mode 100644 web-wallet/src/lib/dusk/svelte-stores/createDataStore.js create mode 100644 web-wallet/src/lib/dusk/svelte-stores/index.js create mode 100644 web-wallet/src/lib/dusk/svelte-stores/svelte-stores.d.ts rename web-wallet/src/lib/migration/abi/{erc_bep_duskABI.json => erc_bep_20.json} (100%) create mode 100644 web-wallet/src/lib/migration/migration.js create mode 100644 web-wallet/src/routes/(app)/dashboard/migrate/+page.svelte diff --git a/web-wallet/CHANGELOG.md b/web-wallet/CHANGELOG.md index 91faa7eb2e..f25308d9ce 100644 --- a/web-wallet/CHANGELOG.md +++ b/web-wallet/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added gas settings validation on Unstake / Widthdraw Rewards flows [#2000] - Added allocation (shield/unshield) page and UI [#2196] +- Added token migration contract bindings [#2014] ### Changed @@ -242,6 +243,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#2071]: https://github.com/dusk-network/rusk/issues/2075 [#2118]: https://github.com/dusk-network/rusk/issues/2118 [#2118]: https://github.com/dusk-network/rusk/issues/2175 +[#2196]: https://github.com/dusk-network/rusk/issues/2196 +[#2014]: https://github.com/dusk-network/rusk/issues/2014 diff --git a/web-wallet/README.md b/web-wallet/README.md index fd86c74669..64a41810aa 100644 --- a/web-wallet/README.md +++ b/web-wallet/README.md @@ -34,6 +34,7 @@ N.B. the current `0.1.2` version of the library has no option to pick the networ # can be empty string, must start with a slash otherwise, must not end with a slash VITE_BASE_PATH="" VITE_CONTRACT_ALLOCATE_DISABLED=true +VITE_CONTRACT_MIGRATE_DISABLED=true VITE_CONTRACT_STAKE_DISABLED=false VITE_CONTRACT_TRANSFER_DISABLED=false VITE_CURRENT_NODE=${VITE_LOCAL_NODE} diff --git a/web-wallet/src/lib/components/ApproveMigration/ApproveMigration.svelte b/web-wallet/src/lib/components/ApproveMigration/ApproveMigration.svelte new file mode 100644 index 0000000000..7413e52bd9 --- /dev/null +++ b/web-wallet/src/lib/components/ApproveMigration/ApproveMigration.svelte @@ -0,0 +1,142 @@ + + + + +
+ {#if !isLoading && !error && !data} +
+

DUSK token migration requires two transactions:

+
    +
  1. + Approve: Authorize the migration contract to spend your DUSK tokens. +
  2. +
  3. Migrate: Transfer your DUSK tokens to the migration contract.
  4. +
+

+ Both steps must be completed for a successful migration.

Warning: Make sure your wallet has enough funds to pay for the gas. +

+
+ {:else if isLoading} +
+ + Approval in progress +
+ {:else if error} +
+ + An error occured during approval +
+ {/if} + +
+ + diff --git a/web-wallet/src/lib/components/ExecuteMigration/ExecuteMigration.svelte b/web-wallet/src/lib/components/ExecuteMigration/ExecuteMigration.svelte new file mode 100644 index 0000000000..4620f144c7 --- /dev/null +++ b/web-wallet/src/lib/components/ExecuteMigration/ExecuteMigration.svelte @@ -0,0 +1,112 @@ + + + + +
+ {#if !isLoading && !data && !error} +
+ + Migration Approved +
+ {:else if error} +
+ + Action has been rejected on the wallet connected. +
+ {:else if isLoading || data} +
+ + Migration has been submitted +
+ {/if} + {#if migrationHash && chain?.blockExplorers} +
+ Migration takes some minutes to complete. Your transaction is being + executed and you can check it here. +
+ {/if} + {#if isLoading || !data || error} +
+ + diff --git a/web-wallet/src/lib/components/__tests__/__snapshots__/GasControls.spec.js.snap b/web-wallet/src/lib/components/__tests__/__snapshots__/GasControls.spec.js.snap index b084dcf1d2..1e93f532a4 100644 --- a/web-wallet/src/lib/components/__tests__/__snapshots__/GasControls.spec.js.snap +++ b/web-wallet/src/lib/components/__tests__/__snapshots__/GasControls.spec.js.snap @@ -1,43 +1,22 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`GasControls > should render the \`GasControls\` component 1`] = ` - - + Price: (lux) + - - + + + `; diff --git a/web-wallet/src/lib/components/index.js b/web-wallet/src/lib/components/index.js index a4914716ab..6671565e6d 100644 --- a/web-wallet/src/lib/components/index.js +++ b/web-wallet/src/lib/components/index.js @@ -3,10 +3,12 @@ export { default as Allocate } from "./Allocate/Allocate.svelte"; export { default as AppAnchor } from "./AppAnchor/AppAnchor.svelte"; export { default as AppAnchorButton } from "./AppAnchorButton/AppAnchorButton.svelte"; export { default as AppImage } from "./AppImage/AppImage.svelte"; +export { default as ApproveMigration } from "./ApproveMigration/ApproveMigration.svelte"; export { default as Balance } from "./Balance/Balance.svelte"; export { default as ContractOperations } from "./ContractOperations/ContractOperations.svelte"; export { default as ContractStatusesList } from "./ContractStatusesList/ContractStatusesList.svelte"; export { default as DashboardNav } from "./DashboardNav/DashboardNav.svelte"; +export { default as ExecuteMigration } from "./ExecuteMigration/ExecuteMigration.svelte"; export { default as ExistingWalletNotice } from "./ExistingWalletNotice/ExistingWalletNotice.svelte"; export { default as GasControls } from "./GasControls/GasControls.svelte"; export { default as GasFee } from "./GasFee/GasFee.svelte"; diff --git a/web-wallet/src/lib/containers/MigrateContract/MigrateContract.svelte b/web-wallet/src/lib/containers/MigrateContract/MigrateContract.svelte index e1bf7b2727..116942c4d5 100644 --- a/web-wallet/src/lib/containers/MigrateContract/MigrateContract.svelte +++ b/web-wallet/src/lib/containers/MigrateContract/MigrateContract.svelte @@ -1,133 +1,138 @@
@@ -165,8 +174,8 @@ @@ -188,10 +197,10 @@

From:

- {#if isConnected} + {#if isConnected && address}

Connected Wallet:

{address @@ -199,24 +208,21 @@ : ""}

- Balance: {duskFormatter(connectedWalletBalance)} - {selected} DUSK + Balance: {connectedWalletBalance} {selectedChain} DUSK
{/if} - {#if isConnected} + {#if isConnected && connectedWalletBalance}
- {#if selected === tokens.eth.name} + {#if selectedChain === erc20.name} {:else} {/if} -

DUSK {selected}

+

DUSK {selectedChain}

@@ -249,127 +256,50 @@ required type="number" min={minAmount} - max={maxSpendable} - step="0.000000001" + max={Number(parseUnits(connectedWalletBalance, 0))} + step={minAmount} placeholder="Amount" + disabled={isInputDisabled} />
{/if} - {#if isConnected && !isAmountValid && typeof amount === "number"} -
Not enough balance
- {/if} - {#if isConnected && isAmountValid && isMigrationInitialized} -
-
-

- - Est. Time - - {estimatedTime} -

-

- - Total Gas Fee - - {gasFee} -

-
- - - { - isMigrationBeingApproved = true; - }, - disabled: isMigrationBeingApproved, - icon: null, - label: "APPROVE MIGRATION", - variant: "primary", +
+ + + {#if migrationStep === 0} + { + isInputDisabled = true; }} - > - {#if !isMigrationBeingApproved} -
-

DUSK token migration requires two transactions:

-
    -
  1. - Approve: Authorize the migration contract to spend your DUSK - tokens. -
  2. -
  3. - Migrate: Transfer your DUSK tokens to the migration contract. -
  4. -
-

- Both steps must be completed for a successful migration.

Warning: Make sure your wallet has enough funds to pay - for the gas. -

-
- {:else} -
- - Approval in progress -
- {/if} - - {}, - icon: null, - label: "EXECUTE MIGRATION", - variant: "primary", + on:errorApproval={() => { + isInputDisabled = false; }} - > -
- - Migration Approved -
-
- -
- - Migration in progress -
-
- Migration takes some minutes to complete. Your transaction is - being executed and you can check it here. -
-
- + {amount} + chainContract={tokens[network][selectedChain].contract} + migrationContract={tokens[network][selectedChain].migrationContract} + /> + {:else} + + {/if}
{/if} {#if !isConnected}
+