Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
deuch13 committed Aug 21, 2024
1 parent b09a1a2 commit 2060c1e
Show file tree
Hide file tree
Showing 7 changed files with 11,278 additions and 2,564 deletions.
13,413 changes: 10,897 additions & 2,516 deletions web-wallet/package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions web-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@
"@dusk-network/dusk-wallet-js": "0.4.2",
"@floating-ui/dom": "1.6.5",
"@mdi/js": "7.4.47",
"@walletconnect/web3wallet": "^1.14.0",
"@wagmi/connectors": "5.1.6",
"@wagmi/core": "2.13.4",
"@web3modal/wagmi": "5.1.0",
"bip39": "3.1.0",
"css-doodle": "0.39.2",
"lamb": "0.61.1",
"qr-scanner": "1.4.2",
"qrcode": "1.5.3",
"svelte-persisted-store": "0.11.0"
"svelte-persisted-store": "0.11.0",
"viem": "2.19.8"
},
"devDependencies": {
"@dusk-network/eslint-config": "3.1.0",
Expand Down
116 changes: 72 additions & 44 deletions web-wallet/src/lib/containers/MigrateContract/MigrateContract.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,40 @@
import { createCurrencyFormatter } from "$lib/dusk/currency";
import { logo } from "$lib/dusk/icons";
import { calculateAdaptiveCharCount, middleEllipsis } from "$lib/dusk/string";
import { onMount } from "svelte";
import { onDestroy, onMount } from "svelte";
import { settingsStore } from "$lib/stores";
const ERC_TOKEN = "ERC-20";
const BEP_TOKEN = "BEP-20";
import { account, accountBalance, modal, walletDisconnect, switchWalletChain } from "$lib/migration/walletConnection";
import { mainnet, bsc } from 'viem/chains';
import { writable } from "svelte/store";
const tokens = {
eth: {
name: "ERC-20",
chainId: mainnet.id
},
bnb: {
name: "BEP-20",
chainId: bsc.id
}
}
const options = [
{ disabled: false, label: ERC_TOKEN, value: ERC_TOKEN },
{ disabled: false, label: BEP_TOKEN, value: BEP_TOKEN },
{ disabled: false, label: tokens.eth.name, value: tokens.eth.name },
{ disabled: false, label: tokens.bnb.name, value: tokens.bnb.name },
];
const web3ModalEvent = writable();
const web3ActiveNetwork = writable();
/** @type {String} */
let selected = ERC_TOKEN;
let selected = tokens.eth.name;
/** @type {Boolean} */
const migrationInProgress = false;
/** @type {Boolean} */
let isWalletConnected = false;
/** @type {String} */
let connectedWalletAddress =
"6rmam3FisEHih84oJMEsmi2GGVetDPmcjw7H4heeVbm79DwZUPvPJTBeFriLx85Cy2Q2cGisz32BeSYX99kNXjD";
/** @type {Number} */
let connectedWalletBalance = 10;
let connectedWalletBalance = 1;
/** @type {Number | undefined} */
let amount;
Expand Down Expand Up @@ -79,11 +87,46 @@
: false;
$: ({ language } = $settingsStore);
$: duskFormatter = createCurrencyFormatter(language, "DUSK", 9);
$: if (selected) {
resetMigrationIfConnected();
$:if($web3ModalEvent?.event === "CONNECT_SUCCESS") {
switchNetwork();
accountBalance().then((balance) => {
connectedWalletBalance = Number(balance.value)
});
}
function onNetworkChange(e) {
if(e.target.value === tokens.bnb.name){
switchWalletChain(bsc.id);
} else {
switchWalletChain(mainnet.id);
}
}
/** @param {string} tokenName */
function getChainIdByName(tokenName) {
for (const key in tokens) {
if (tokens[key].name === tokenName) {
return tokens[key].chainId;
}
}
return null;
}
function switchNetwork(){
let chainId = getChainIdByName(selected);
if($account.chainId !== chainId) {
switchWalletChain(chainId);
}
}
onMount(() => {
modal.subscribeEvents((e) => {
web3ModalEvent.set(e.data);
});
amountInput = document.querySelector(".migrate__input-field");
const resizeObserver = new ResizeObserver((entries) => {
Expand All @@ -97,23 +140,9 @@
return () => resizeObserver.disconnect();
});
/**
* @param {Number} ms
*/
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function resetMigrationIfConnected() {
if (isWalletConnected) {
isWalletConnected = false;
connectedWalletAddress = "";
connectedWalletBalance = 10;
amount = undefined;
isMigrationInitialized = false;
isMigrationBeingApproved = false;
}
}
onDestroy(async ()=> {
await walletDisconnect();
})
</script>
<article class="migrate">
Expand Down Expand Up @@ -151,12 +180,12 @@
<div class="migrate__token">
<p class="migrate__token-header">From:</p>
<ExclusiveChoice {options} bind:value={selected} />
{#if isWalletConnected}
<ExclusiveChoice {options} bind:value={selected} on:change={(e)=>onNetworkChange(e)}/>
{#if $account.isConnected}
<p class="migrate__token-header">Connected Wallet:</p>
<p class="migrate__token-address">
{middleEllipsis(
connectedWalletAddress,
$account.address,
calculateAdaptiveCharCount(screenWidth)
)}
</p>
Expand All @@ -169,11 +198,11 @@
{/if}
</div>
{#if isWalletConnected}
{#if $account.isConnected}
<div class="migrate__amount">
<div class="migrate__amount-header">
<div class="migrate__amount-token">
{#if selected === ERC_TOKEN}
{#if selected === tokens.eth.name}
<AppImage
src={darkMode ? "/eth_dusk_light.svg" : "/eth_dusk.svg"}
alt="Ethereum Dusk"
Expand Down Expand Up @@ -218,11 +247,11 @@
</div>
{/if}
{#if isWalletConnected && !isAmountValid && typeof amount === "number"}
{#if $account.isConnected && !isAmountValid && typeof amount === "number"}
<div class="migrate__amount-notice">Not enough balance</div>
{/if}
{#if isWalletConnected && isAmountValid && isMigrationInitialized}
{#if $account.isConnected && isAmountValid && isMigrationInitialized}
<div class="migrate__information">
<div class="migrate__information-header">
<p class="migrate__information-time">
Expand Down Expand Up @@ -256,7 +285,6 @@
nextButton={{
action: async () => {
isMigrationBeingApproved = true;
await delay(10000);
},
disabled: isMigrationBeingApproved,
icon: null,
Expand Down Expand Up @@ -324,12 +352,12 @@
</div>
{/if}
{#if !isWalletConnected}
{#if !$account.isConnected}
<Button
icon={{ path: mdiWalletOutline }}
text={`CONNECT TO ${selected === ERC_TOKEN ? "ETHEREUM" : "BSC"}`}
text={`CONNECT TO ${selected === tokens.eth.name ? "ETHEREUM" : "BSC"}`}
on:click={() => {
isWalletConnected = true;
modal.open();
}}
/>
{:else if !isMigrationInitialized}
Expand Down
157 changes: 157 additions & 0 deletions web-wallet/src/lib/migration/abi/erc_bep_duskABI.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
[
{
"constant": false,
"inputs": [
{
"name": "spender",
"type": "address"
},
{
"name": "value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "spender",
"type": "address"
},
{
"name": "addedValue",
"type": "uint256"
}
],
"name": "increaseAllowance",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "to",
"type": "address"
},
{
"name": "value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "owner",
"type": "address"
},
{
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": true,
"name": "spender",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
}
]
Loading

0 comments on commit 2060c1e

Please sign in to comment.