-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c71891
commit b6ef3e1
Showing
52 changed files
with
1,773 additions
and
68 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
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
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
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 loadBalances = (): 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 loadExchanges = 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 loadBalances(); | ||
await loadExchanges(); | ||
}; | ||
</script> | ||
|
||
<SwapButtonWithModal open={onOpenSwap} isOpen={$modalSwap}> | ||
<SwapModal on:nnsClose /> | ||
</SwapButtonWithModal> |
18 changes: 18 additions & 0 deletions
18
src/frontend/src/lib/components/swap/SwapAmountExchange.svelte
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,18 @@ | ||
<script lang="ts"> | ||
import { nonNullish } from '@dfinity/utils'; | ||
import IconArrowUpDown from '$lib/components/icons/lucide/IconArrowUpDown.svelte'; | ||
import type { OptionAmount } from '$lib/types/send'; | ||
import { formatUSD } from '$lib/utils/format.utils'; | ||
export let amount: OptionAmount; | ||
export let exchangeRate: number | undefined; | ||
let amountUSD: number | undefined; | ||
$: amountUSD = nonNullish(amount) && nonNullish(exchangeRate) ? Number(amount) * exchangeRate : 0; | ||
</script> | ||
|
||
<div class="flex items-center gap-1"> | ||
<IconArrowUpDown size="14" /> | ||
|
||
<span>{nonNullish(exchangeRate) ? formatUSD({ value: amountUSD }) : '-'}</span> | ||
</div> |
Oops, something went wrong.