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: add evm network recipients #2401

Merged
merged 19 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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 @@ -48,6 +48,7 @@
const sendFlowType = token.id === BASE_TOKEN_ID ? SendFlowType.BaseCoinTransfer : SendFlowType.TokenTransfer
setSendFlowParameters({
type: sendFlowType,
sourceNetworkId: token.networkId,
[sendFlowType]: {
token,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
function onSendClick(): void {
setSendFlowParameters({
type: SendFlowType.NftTransfer,
sourceNetworkId: nft.networkId,
nft,
recipient: undefined,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
<script lang="ts">
import { Alert } from '@bloomwalletio/ui'
import { PopupTemplate } from '@components'
import { selectedAccountIndex } from '@core/account/stores'
import { ContactManager } from '@core/contact/classes'
import { localize } from '@core/i18n'
import { canAccountMakeEvmTransaction } from '@core/layer-2/actions'
import {
IEvmNetwork,
NetworkId,
getActiveNetworkId,
isEvmNetwork,
getEvmNetwork,
NetworkType,
getIscChains,
getL1Network,
getNetwork,
isEvmNetwork,
} from '@core/network'
import { NftStandard } from '@core/nfts'
import { visibleActiveAccounts } from '@core/profile/stores'
import {
SendFlowType,
sendFlowParameters,
updateSendFlowParameters,
SubjectType,
Subject,
getNetworkIdFromSendFlowParameters,
} from '@core/wallet'
import { TokenStandard } from '@core/token'
import { SendFlowType, Subject, SubjectType, sendFlowParameters, updateSendFlowParameters } from '@core/wallet'
import { closePopup } from '@desktop/auxiliary/popup'
import features from '@features/features'
import { INetworkRecipientSelectorOption, NetworkRecipientSelector } from '@ui'
import { onMount } from 'svelte'
import { sendFlowRouter } from '../send-flow.router'
import { PopupTemplate } from '@components'
import { getTokenStandardFromSendFlowParameters } from '@core/wallet/utils'
import { TokenStandard } from '@core/token'
import { canAccountMakeEvmTransaction } from '@core/layer-2/actions'
import { NftStandard } from '@core/nfts'

let selector: NetworkRecipientSelector
let selectorOptions: INetworkRecipientSelectorOption[] = []
Expand All @@ -49,27 +41,28 @@
return
}

const originNetworkId = getNetworkIdFromSendFlowParameters($sendFlowParameters)
if (originNetworkId && isEvmNetwork(originNetworkId)) {
hasInsufficientFunds = !(await canAccountMakeEvmTransaction(
$selectedAccountIndex,
originNetworkId,
$sendFlowParameters.type
))
const { sourceNetworkId, type } = $sendFlowParameters
if (sourceNetworkId && isEvmNetwork(sourceNetworkId)) {
hasInsufficientFunds = !(await canAccountMakeEvmTransaction($selectedAccountIndex, sourceNetworkId, type))
} else {
hasInsufficientFunds = false
}
}

function getAssetName(): string | undefined {
if ($sendFlowParameters?.type === SendFlowType.BaseCoinTransfer) {
return $sendFlowParameters.baseCoinTransfer?.token?.metadata?.name
} else if ($sendFlowParameters?.type === SendFlowType.TokenTransfer) {
return $sendFlowParameters.tokenTransfer?.token?.metadata?.name
} else if ($sendFlowParameters?.type === SendFlowType.NftTransfer) {
return $sendFlowParameters.nft?.name
} else {
return ''
switch ($sendFlowParameters?.type) {
case SendFlowType.BaseCoinTransfer: {
return $sendFlowParameters.baseCoinTransfer?.token?.metadata?.name
}
case SendFlowType.TokenTransfer: {
return $sendFlowParameters.tokenTransfer?.token?.metadata?.name
}
case SendFlowType.NftTransfer: {
return $sendFlowParameters.nft?.name
}
default: {
return ''
}
}
}

Expand All @@ -81,9 +74,7 @@
function setInitialNetworkAndRecipient(): void {
selectedIndex = $sendFlowParameters?.destinationNetworkId
? selectorOptions.findIndex((option) => option.networkId === $sendFlowParameters?.destinationNetworkId)
: selectorOptions.findIndex(
(option) => option.networkId === getNetworkIdFromSendFlowParameters($sendFlowParameters)
) ?? 0
: selectorOptions.findIndex((option) => option.networkId === $sendFlowParameters?.sourceNetworkId) ?? 0

selectorOptions[selectedIndex] = {
...selectorOptions[selectedIndex],
Expand Down Expand Up @@ -125,7 +116,7 @@
}
}

function getLayer2AccountRecipients(coinType: number, accountIndexToExclude?: number): Subject[] {
function getEvmAddressAccountRecipients(coinType: number, accountIndexToExclude?: number): Subject[] {
return $visibleActiveAccounts
.filter(
(account) => account.index !== accountIndexToExclude && account.evmAddresses?.[coinType] !== undefined
Expand All @@ -140,63 +131,54 @@
)
}

function getRecipientOptionFromChain(
function getRecipientOptionForEvmNetwork(
evmNetwork: IEvmNetwork,
accountIndexToExclude?: number
): INetworkRecipientSelectorOption {
return {
networkId: evmNetwork.id,
name: evmNetwork.name,
recipients: [
...getLayer2AccountRecipients(evmNetwork.coinType, accountIndexToExclude),
...getEvmAddressAccountRecipients(evmNetwork.coinType, accountIndexToExclude),
...getContactRecipientsForNetwork(evmNetwork.id),
],
}
}

function getRecipientOptions(): INetworkRecipientSelectorOption[] {
if (!$sendFlowParameters) {
const sourceNetworkId = $sendFlowParameters?.sourceNetworkId
if (!$sendFlowParameters || !sourceNetworkId) {
return []
}

const layer1Network = getLayer1RecipientOption($selectedAccountIndex)
if (!features?.network?.layer2?.enabled) {
return [layer1Network]
const sourceNetwork = getNetwork(sourceNetworkId)

switch (sourceNetwork?.type) {
case NetworkType.Evm: {
return [getRecipientOptionForEvmNetwork(sourceNetwork, $selectedAccountIndex)]
}
case NetworkType.Isc: {
const canUnwrap =
$sendFlowParameters?.type === SendFlowType.BaseCoinTransfer ||
($sendFlowParameters?.type === SendFlowType.TokenTransfer &&
$sendFlowParameters?.tokenTransfer?.token?.standard === TokenStandard.Irc30) ||
($sendFlowParameters?.type === SendFlowType.NftTransfer &&
$sendFlowParameters?.nft?.standard === NftStandard.Irc27)
return [
getRecipientOptionForEvmNetwork(sourceNetwork, $selectedAccountIndex),
...(features.wallet.assets.unwrapToken.enabled && canUnwrap ? [getLayer1RecipientOption()] : []),
]
}
case NetworkType.Stardust: {
const l2Chains = features?.network?.layer2?.enabled ? getIscChains() : []
return [
getLayer1RecipientOption($selectedAccountIndex),
...l2Chains.map((iscChain) => getRecipientOptionForEvmNetwork(iscChain)),
]
}
default:
return []
}

const assetStandard = getTokenStandardFromSendFlowParameters($sendFlowParameters)
const sourceNetworkId = getNetworkIdFromSendFlowParameters($sendFlowParameters)
const sourceChain = sourceNetworkId ? getEvmNetwork(sourceNetworkId) : undefined

let networkRecipientOptions: INetworkRecipientSelectorOption[] = []

switch (assetStandard) {
case NftStandard.Irc27:
case TokenStandard.Irc30:
case TokenStandard.BaseToken:
if (sourceNetworkId === getActiveNetworkId()) {
// if we are on layer 1
networkRecipientOptions = [
layer1Network,
...getIscChains().map((iscChain) => getRecipientOptionFromChain(iscChain)),
]
} else if (sourceChain) {
// if we are on layer 2
networkRecipientOptions = [
...(features.wallet.assets.unwrapToken.enabled ? [getLayer1RecipientOption()] : []),
getRecipientOptionFromChain(sourceChain, $selectedAccountIndex),
]
}
break
case TokenStandard.Erc20:
case NftStandard.Erc721:
if (sourceChain) {
networkRecipientOptions = [getRecipientOptionFromChain(sourceChain, $selectedAccountIndex)]
}
break
}

return networkRecipientOptions
}

function onContinueClick(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<script lang="ts">
import { showNotification } from '@auxiliary/notification'
import { Spinner } from '@bloomwalletio/ui'
import { PopupTemplate } from '@components'
import { getSelectedAccount, selectedAccount } from '@core/account/stores'
import { handleError } from '@core/error/handlers'
import { localize } from '@core/i18n'
import { IEvmNetwork, getEvmNetwork, isEvmNetwork } from '@core/network'
import { setGasFee } from '@core/layer-2/actions'
import { LedgerAppName, ledgerPreparedOutput } from '@core/ledger'
import { IEvmNetwork, getNetwork, isEvmNetwork } from '@core/network'
import { checkActiveProfileAuth } from '@core/profile/actions'
import { getActiveProfileId, getIsActiveLedgerProfile } from '@core/profile/stores'
import { truncateString } from '@core/utils'
import { SendFlowParameters, SubjectType } from '@core/wallet'
import {
Expand All @@ -15,17 +20,12 @@
signEvmTransaction,
} from '@core/wallet/actions'
import { sendFlowParameters } from '@core/wallet/stores'
import { getNetworkIdFromSendFlowParameters, validateSendConfirmation } from '@core/wallet/utils'
import { validateSendConfirmation } from '@core/wallet/utils'
import { closePopup, modifyPopupState } from '@desktop/auxiliary/popup'
import { onMount } from 'svelte'
import { sendFlowRouter } from '../send-flow.router'
import { EvmTransactionSummary, StardustToEvmTransactionSummary, StardustTransactionSummary } from './components'
import { TransactionSummaryProps } from './types'
import { setGasFee } from '@core/layer-2/actions'
import { showNotification } from '@auxiliary/notification'
import { checkActiveProfileAuth } from '@core/profile/actions'
import { LedgerAppName, ledgerPreparedOutput } from '@core/ledger'
import { getActiveProfileId, getIsActiveLedgerProfile } from '@core/profile/stores'

export let transactionSummaryProps: TransactionSummaryProps
let { _onMount, preparedOutput, preparedTransaction } = transactionSummaryProps ?? {}
Expand Down Expand Up @@ -55,9 +55,9 @@
? recipient.account.name
: truncateString(recipient?.address, 6, 6)

const networkId = getNetworkIdFromSendFlowParameters(sendFlowParameters)
if (isEvmNetwork(networkId)) {
evmNetwork = getEvmNetwork(networkId)
const { sourceNetworkId } = sendFlowParameters
if (sourceNetworkId && isEvmNetwork(sourceNetworkId)) {
evmNetwork = getNetwork(sourceNetworkId) as IEvmNetwork
preparedTransaction = await createEvmTransactionFromSendFlowParameters(
sendFlowParameters,
evmNetwork,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function parseSendTransactionOperation(searchParams: URLSearchParams): SendFlowP
destinationNetworkId: networkId,
...(baseCoinTransfer && { baseCoinTransfer }),
...(tokenTransfer && { tokenTransfer }),
sourceNetworkId: networkId,
...(address && { recipient: { type: SubjectType.Address, address } }),
...(metadata && { metadata }),
...(tag && { tag }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function parseSendFormOperation(searchParams: URLSearchParams): SendFlowParamete
type,
...(baseCoinTransfer && { baseCoinTransfer }),
...(tokenTransfer && { tokenTransfer }),
sourceNetworkId: networkId,
...(address && { recipient }),
...(metadata && { metadata }),
...(tag && { tag }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async function claimShimmerRewardsForShimmerClaimingAccount(
},
rawAmount: BigInt(rawAmount),
},
sourceNetworkId: SupportedNetworkId.Shimmer,
destinationNetworkId: SupportedNetworkId.Shimmer,
}
setSendFlowParameters(sendFlowParameters)
Expand Down
14 changes: 4 additions & 10 deletions packages/shared/src/lib/core/layer-2/actions/setGasFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ import { handleError } from '@core/error/handlers'
import { getGasFeeForLayer1ToLayer2Transaction } from '@core/layer-2/actions'
import { calculateMaxGasFeeFromTransactionData } from '@core/layer-2/utils'
import { getEvmNetwork, isEvmNetwork } from '@core/network'
import {
SendFlowParameters,
getNetworkIdFromSendFlowParameters,
createEvmTransactionFromSendFlowParameters,
updateSendFlowParameters,
} from '@core/wallet'
import { SendFlowParameters, createEvmTransactionFromSendFlowParameters, updateSendFlowParameters } from '@core/wallet'

export async function setGasFee(sendFlowParams: SendFlowParameters, account: IAccountState): Promise<void> {
try {
if (sendFlowParams.gasFee) {
return
}

const sourceNetworkId = getNetworkIdFromSendFlowParameters(sendFlowParams)
if (!sourceNetworkId || !sendFlowParams.destinationNetworkId) {
if (!sendFlowParams.sourceNetworkId || !sendFlowParams.destinationNetworkId) {
throw new Error('Networks are not set in send flow parameters!')
}

let gasFee: bigint | undefined
if (isEvmNetwork(sourceNetworkId)) {
const evmNetwork = getEvmNetwork(sourceNetworkId)
if (isEvmNetwork(sendFlowParams.sourceNetworkId)) {
const evmNetwork = getEvmNetwork(sendFlowParams.sourceNetworkId)
if (!evmNetwork) {
throw new Error('Chain is undefined!')
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { IIscChain, IIscChainConfiguration, IIscChainMetadata } from '../interfaces'
import { Converter } from '@core/utils'
import { IAccountState } from '@core/account/interfaces'
import { ITokenBalance } from '@core/token/interfaces'
import { fetchIscAssetsForAccount } from '@core/layer-2/utils'
import { getActiveProfileId } from '@core/profile/stores'
import { NetworkType } from '@core/network/enums'
import { getActiveProfileId } from '@core/profile/stores'
import { ITokenBalance } from '@core/token/interfaces'
import { Converter } from '@core/utils'
import { IIscChain, IIscChainConfiguration, IIscChainMetadata } from '../interfaces'
import { EvmNetwork } from './evm-network.class'

export class IscChain extends EvmNetwork implements IIscChain {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ function setSendParametersForClaiming(activity: StardustActivity): void {
token,
}
}
params.sourceNetworkId = token.networkId
setSendFlowParameters(params)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { IAccountState } from '@core/account/interfaces'
import { EvmTransactionData } from '@core/layer-2/types'
import { IEvmNetwork } from '@core/network/interfaces'
import { isEvmNetwork, isStardustNetwork } from '@core/network/utils'
import { getNetworkIdFromSendFlowParameters } from '@core/wallet/utils'
import { SendFlowParameters } from '../../types'
import { createEvmToEvmTransaction } from './createEvmToEvmTransaction'
import { createEvmToStardustTransaction } from './createEvmToStardustTransaction'
Expand All @@ -12,9 +11,8 @@ export async function createEvmTransactionFromSendFlowParameters(
originEvmNetwork: IEvmNetwork,
account: IAccountState
): Promise<EvmTransactionData | undefined> {
const originNetworkId = getNetworkIdFromSendFlowParameters(sendFlowParameters)
const { destinationNetworkId } = sendFlowParameters
if (originNetworkId && destinationNetworkId) {
const { destinationNetworkId, sourceNetworkId } = sendFlowParameters
if (sourceNetworkId && destinationNetworkId) {
// L2 -> L2 transfer (same evmNetwork)
if (isEvmNetwork(destinationNetworkId)) {
return await createEvmToEvmTransaction(sendFlowParameters, originEvmNetwork, account)
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion packages/shared/src/lib/core/wallet/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export * from './getBech32AddressFromAddressTypes'
export * from './getHexAddressFromAddressTypes'
export * from './getIrc30MetadataFromFoundryOutput'
export * from './getMetadataFromFoundryOutput'
export * from './getNetworkIdFromSendFlowParameters'
export * from './getOutputParameters'
export * from './getSenderAddressFromUnlockCondition'
export * from './getSubjectFromAddress'
Expand Down
Loading