Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: included AddTokenReview in component ManageTokensModal #1503

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
import { toastsError } from '$lib/stores/toasts.store';
import { saveCustomTokens } from '$icp/services/ic-custom-tokens.services';
import type { IcrcCustomToken } from '$icp/types/icrc-custom-token';
import { selectedNetwork } from '$lib/derived/network.derived';
import { ETHEREUM_NETWORK_ID, ICP_NETWORK_ID } from '$env/networks.env';
import AddTokenReview from '$eth/components/tokens/AddTokenReview.svelte';
import { isNullishOrEmpty } from '$lib/utils/input.utils';
import { addUserToken } from '$lib/api/backend.api';
import { selectedChainId, selectedEthereumNetwork } from '$eth/derived/network.derived';
import { erc20TokensStore } from '$eth/stores/erc20.store';
import { mapErc20Token } from '$eth/utils/erc20.utils';
import type { Erc20Metadata } from '$eth/types/erc20';
import type { Network } from '$lib/types/network';

const steps: WizardSteps = [
{
Expand Down Expand Up @@ -90,6 +100,69 @@
}
};

let erc20Metadata: Erc20Metadata | undefined;

const saveErc20Token = async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are already there, I would rather like to have this in a service or at least everything we can extract. Or are you planing to refactor that afterwards?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was planning to do it afterwrds actually, so that we can move step by step, it seems the best way to have smaller PRs

what do you think? I refactor first?

if (isNullishOrEmpty(erc20ContractAddress)) {
toastsError({
msg: { text: $i18n.tokens.error.invalid_contract_address }
});
return;
}

if (isNullish(erc20Metadata)) {
toastsError({
msg: { text: $i18n.tokens.error.no_metadata }
});
return;
}

if (isNullish($authStore.identity)) {
await nullishSignOut();
return;
}

modal.next();

try {
saveProgressStep = ProgressStepsAddToken.SAVE;

await addUserToken({
identity: $authStore.identity,
token: {
chain_id: $selectedChainId,
contract_address: erc20ContractAddress,
symbol: [],
decimals: [],
version: []
}
});

saveProgressStep = ProgressStepsAddToken.UPDATE_UI;

erc20TokensStore.add(
mapErc20Token({
address: erc20ContractAddress,
exchange: 'ethereum',
category: 'custom',
network: $selectedEthereumNetwork,
...erc20Metadata
})
);

saveProgressStep = ProgressStepsAddToken.DONE;

setTimeout(() => close(), 750);
} catch (err: unknown) {
toastsError({
msg: { text: $i18n.tokens.error.unexpected },
err
});

modal.back();
}
};

const close = () => {
modalStore.close();

Expand All @@ -98,6 +171,9 @@

let ledgerCanisterId = '';
let indexCanisterId = '';
let erc20ContractAddress = '';

let network: Network | undefined = $selectedNetwork;
</script>

<WizardModal
Expand All @@ -110,12 +186,21 @@
<svelte:fragment slot="title">{currentStep?.title ?? ''}</svelte:fragment>

{#if currentStep?.name === 'Review'}
<IcAddTokenReview
on:icBack={modal.back}
on:icSave={addToken}
{ledgerCanisterId}
{indexCanisterId}
/>
{#if network?.id === ICP_NETWORK_ID}
<IcAddTokenReview
on:icBack={modal.back}
on:icSave={addToken}
{ledgerCanisterId}
{indexCanisterId}
/>
{:else if network?.id === ETHEREUM_NETWORK_ID}
<AddTokenReview
on:icBack={modal.back}
on:icSave={saveErc20Token}
contractAddress={erc20ContractAddress}
bind:metadata={erc20Metadata}
/>
{/if}
{:else if currentStep?.name === 'Saving'}
<InProgressWizard progressStep={saveProgressStep} steps={addTokenSteps($i18n)} />
{:else if currentStep?.name === 'Import'}
Expand Down