-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): implement Swap component and use it in Actions (#4174)
# Motivation This is the last step of the Swap feature - Swap component + its usage.
- Loading branch information
1 parent
2109a8a
commit 3cebd36
Showing
3 changed files
with
110 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
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,99 @@ | ||
<script lang="ts"> | ||
import { isNullish, nonNullish } from '@dfinity/utils'; | ||
import { BigNumber } from '@ethersproject/bignumber'; | ||
import { setContext } from 'svelte'; | ||
import { ICRC_CK_TOKENS_LEDGER_CANISTER_IDS } from '$env/networks/networks.icrc.env'; | ||
import type { Erc20ContractAddress, Erc20Token } from '$eth/types/erc20'; | ||
import { balance } from '$icp/api/icrc-ledger.api'; | ||
import type { LedgerCanisterIdText } from '$icp/types/canister'; | ||
import type { IcCkToken } from '$icp/types/ic-token'; | ||
import SwapButtonWithModal from '$lib/components/swap/SwapButtonWithModal.svelte'; | ||
import SwapModal from '$lib/components/swap/SwapModal.svelte'; | ||
import { allDisabledIcrcTokens } from '$lib/derived/all-tokens.derived'; | ||
import { authIdentity } from '$lib/derived/auth.derived'; | ||
import { modalSwap } from '$lib/derived/modal.derived'; | ||
import { nullishSignOut } from '$lib/services/auth.services'; | ||
import { exchangeRateERC20ToUsd, exchangeRateICRCToUsd } from '$lib/services/exchange.services'; | ||
import { balancesStore } from '$lib/stores/balances.store'; | ||
import { exchangeStore } from '$lib/stores/exchange.store'; | ||
import { modalStore } from '$lib/stores/modal.store'; | ||
import { | ||
initSwapAmountsStore, | ||
SWAP_AMOUNTS_CONTEXT_KEY, | ||
type SwapAmountsContext | ||
} from '$lib/stores/swap-amounts.store'; | ||
setContext<SwapAmountsContext>(SWAP_AMOUNTS_CONTEXT_KEY, { | ||
store: initSwapAmountsStore() | ||
}); | ||
const onOpenSwap = async (tokenId: symbol) => { | ||
if (isNullish($authIdentity)) { | ||
await nullishSignOut(); | ||
return; | ||
} | ||
modalStore.openSwap(tokenId); | ||
const loadDisabledIcrcTokensBalances = (): Promise<void[]> => | ||
Promise.all( | ||
$allDisabledIcrcTokens.map(async ({ ledgerCanisterId, id }) => { | ||
const icrcTokenBalance = await balance({ | ||
identity: $authIdentity, | ||
owner: $authIdentity.getPrincipal(), | ||
ledgerCanisterId | ||
}); | ||
balancesStore.set({ | ||
tokenId: id, | ||
data: { | ||
data: BigNumber.from(icrcTokenBalance), | ||
certified: true | ||
} | ||
}); | ||
}) | ||
); | ||
const loadDisabledIcrcTokensExchanges = async (): Promise<void> => { | ||
const [currentErc20Prices, currentIcrcPrices] = await Promise.all([ | ||
exchangeRateERC20ToUsd( | ||
$allDisabledIcrcTokens.reduce<Erc20ContractAddress[]>((acc, token) => { | ||
const twinTokenAddress = ( | ||
(token as Partial<IcCkToken>).twinToken as Erc20Token | undefined | ||
)?.address; | ||
return nonNullish(twinTokenAddress) | ||
? [ | ||
...acc, | ||
{ | ||
address: twinTokenAddress | ||
} | ||
] | ||
: acc; | ||
}, []) | ||
), | ||
exchangeRateICRCToUsd( | ||
$allDisabledIcrcTokens.reduce<LedgerCanisterIdText[]>( | ||
(acc, { ledgerCanisterId }) => | ||
!ICRC_CK_TOKENS_LEDGER_CANISTER_IDS.includes(ledgerCanisterId) | ||
? [...acc, ledgerCanisterId] | ||
: acc, | ||
[] | ||
) | ||
) | ||
]); | ||
exchangeStore.set([ | ||
...(nonNullish(currentErc20Prices) ? [currentErc20Prices] : []), | ||
...(nonNullish(currentIcrcPrices) ? [currentIcrcPrices] : []) | ||
]); | ||
}; | ||
await loadDisabledIcrcTokensBalances(); | ||
await loadDisabledIcrcTokensExchanges(); | ||
}; | ||
</script> | ||
|
||
<SwapButtonWithModal open={onOpenSwap} isOpen={$modalSwap}> | ||
<SwapModal on:nnsClose /> | ||
</SwapButtonWithModal> |
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