Skip to content

Commit

Permalink
Merge pull request #3130 from dusk-network/feature-3129
Browse files Browse the repository at this point in the history
web-wallet: Upgrade Migration Feature to Use Reown AppKit
  • Loading branch information
nortonandreev authored Dec 10, 2024
2 parents 35f4195 + f109a8c commit c65f155
Show file tree
Hide file tree
Showing 7 changed files with 2,327 additions and 1,611 deletions.
2 changes: 2 additions & 0 deletions web-wallet/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Update Transactions list design [#1922]
- Reword "Staking" header to "Stake" [#3113]
- Upgrade Migration Feature to Use Reown AppKit [#3129]

### Removed

Expand Down Expand Up @@ -422,6 +423,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#3098]: https://github.com/dusk-network/rusk/issues/3098
[#3099]: https://github.com/dusk-network/rusk/issues/3099
[#3113]: https://github.com/dusk-network/rusk/issues/3113
[#3129]: https://github.com/dusk-network/rusk/issues/3129

<!-- VERSIONS -->

Expand Down
3,864 changes: 2,284 additions & 1,580 deletions web-wallet/package-lock.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions web-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@
"dependencies": {
"@floating-ui/dom": "1.6.5",
"@mdi/js": "7.4.47",
"@wagmi/connectors": "5.1.6",
"@wagmi/core": "2.13.4",
"@web3modal/wagmi": "5.1.0",
"@reown/appkit": "1.5.3",
"@reown/appkit-adapter-wagmi": "1.5.3",
"bip39": "3.1.0",
"css-doodle": "0.39.2",
"dexie": "4.0.8",
"lamb": "0.61.1",
"qr-scanner": "1.4.2",
"qrcode": "1.5.3",
"viem": "2.19.8",
"web-streams-polyfill": "4.0.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
}
}
/** Emits the switchChain event to the thrid-party wallet when the ExclusiveChoice UI is interacted with */
/** Emits the switchChain event to the third-party wallet when the ExclusiveChoice UI is interacted with */
// @ts-ignore
async function onChainSwitch(e) {
if (!isConnected) {
Expand Down Expand Up @@ -295,7 +295,9 @@
</div>
<Textbox
className="migrate__input-field"
className="migrate__input-field {!isAmountValid
? 'migrate__input-field--invalid'
: ''}"
bind:value={amount}
required
type="text"
Expand Down Expand Up @@ -442,4 +444,8 @@
:global(.dusk-textbox.migrate__input-field) {
background-color: var(--non-button-control-bg-color);
}
:global(.dusk-textbox.migrate__input-field--invalid) {
color: var(--error-color);
}
</style>
13 changes: 5 additions & 8 deletions web-wallet/src/lib/migration/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ export const allowance = async (
migrationContract
) => {
try {
const balance = /** @type {bigint} */ (
return /** @type {bigint} */ (
await readContract(wagmiConfig, {
abi: ERC20Abi,
address: stableCoinAddress,
args: [userAddress, migrationContract],
functionName: "allowance",
})
);
return balance;
} catch (e) {
const errorMessage =
e instanceof Error
? `An error occurred while checking the spender ${e.message}`
? `An error occurred while checking the spender: ${e.message}`
: "An unexpected error occurred while checking the spender";
throw new Error(errorMessage);
}
Expand All @@ -56,7 +55,7 @@ export const approve = async (migrationContract, stableCoinAddress, value) => {
} catch (e) {
const errorMessage =
e instanceof Error
? `An error occurred while approving the stable coin ${e.message}`
? `An error occurred while approving the stable coin: ${e.message}`
: "An unexpected error occurred while approving the stable coin";
throw new Error(errorMessage);
}
Expand All @@ -72,24 +71,22 @@ export const approve = async (migrationContract, stableCoinAddress, value) => {
*/
export const getBalanceOfCoin = async (userAddress, stableCoinAddress) => {
try {
const balance = /** @type {bigint} */ (
return /** @type {bigint} */ (
await readContract(wagmiConfig, {
abi: ERC20Abi,
address: stableCoinAddress,
args: [userAddress],
functionName: "balanceOf",
})
);
return balance;
} catch (e) {
const errorMessage =
e instanceof Error
? `An error occurred while checking the spender ${e.message}`
? `An error occurred while checking the spender: ${e.message}`
: "An unknown error occurred while checking the spender";
throw new Error(errorMessage);
}
};

/**
* Migrates the approved amount to the given account
*
Expand Down
40 changes: 25 additions & 15 deletions web-wallet/src/lib/migration/walletConnection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { createWeb3Modal, defaultWagmiConfig } from "@web3modal/wagmi";
import { createAppKit } from "@reown/appkit";
import { WagmiAdapter } from "@reown/appkit-adapter-wagmi";
// eslint-disable-next-line import/no-unresolved
import { bsc, mainnet, sepolia } from "@reown/appkit/networks";
import {
disconnect,
getAccount,
Expand All @@ -7,7 +10,6 @@ import {
watchAccount,
} from "@wagmi/core";
import { readable } from "svelte/store";
import { bsc, mainnet, sepolia } from "viem/chains";

// Required project metadata
const projectId = "b5303e1c8374b100fbb7f181884fef28";
Expand All @@ -18,26 +20,31 @@ const metadata = {
url: "https://127.0.0.1:5173/dashboard/",
};

/** @type {[import("viem").Chain, import("viem").Chain, import("viem").Chain]} */
const chains = [sepolia, bsc, mainnet];
/** @typedef {import("@reown/appkit/networks").AppKitNetwork} AppKitNetwork */
/** @type {[AppKitNetwork, ...AppKitNetwork[]]} */
const networks = [sepolia, bsc, mainnet];

export const wagmiConfig = defaultWagmiConfig({
auth: { email: false },
chains,
metadata,
const wagmiAdapter = new WagmiAdapter({
networks,
projectId,
});

export const wagmiConfig = wagmiAdapter.wagmiConfig;

reconnect(wagmiConfig);

// Create the Web3 modal with the WAGMI config
export const modal = createWeb3Modal({
allowUnsupportedChain: false,
enableAnalytics: false,
enableOnramp: false,
enableSwaps: false,
export const modal = createAppKit({
adapters: [wagmiAdapter],
features: {
analytics: false,
onramp: false,
swaps: false,
},
metadata,
networks,
projectId,
themeMode: "dark",
wagmiConfig,
});

// Svelte store to track the current account, and update if a new account is set
Expand All @@ -53,6 +60,9 @@ export const account = readable(getAccount(wagmiConfig), (set) => {

/** @param {*} address */
export const accountBalance = (address) =>
getBalance(wagmiConfig, { address: address, blockTag: "latest" });
getBalance(wagmiConfig, {
address: address,
blockTag: "latest",
});

export const walletDisconnect = () => disconnect(wagmiConfig);
3 changes: 1 addition & 2 deletions web-wallet/vite-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import "fake-indexeddb/auto";
import { IntersectionObserver } from "./src/lib/dusk/mocks";

// Mocking wallet connection modules
vi.mock("@wagmi/core");
vi.mock("@web3modal/wagmi");
vi.mock("@reown/appkit");

vi.mock("./src/lib/vendor/w3sper.js/src/protocol-driver/mod", async () => ({
...(await import("./src/__mocks__/ProtocolDriver.js")),
Expand Down

0 comments on commit c65f155

Please sign in to comment.