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: sending transactions on evm network #2443

Merged
merged 15 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
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 @@ -227,6 +227,7 @@
<TransactionAssetSection {baseCoinTransfer} {tokenTransfer} {nft} />
{/if}
<EvmTransactionDetails
sourceNetwork={evmNetwork}
destinationNetworkId={evmNetwork.id}
estimatedGasFee={calculateEstimatedGasFeeFromTransactionData(preparedTransaction)}
maxGasFee={calculateMaxGasFeeFromTransactionData(preparedTransaction)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
import { Alert, Table } from '@bloomwalletio/ui'
import { localize } from '@core/i18n'
import { resetShowInternalVerificationPopup, showInternalVerificationPopup } from '@core/ledger'
import { getBaseToken } from '@core/profile/actions'
import { LedgerStatusIllustration, LedgerIllustrationVariant } from '@ui'
import { formatTokenAmountBestMatch } from '@core/token/utils'
import { formatHexString } from '@core/utils'
import { onDestroy } from 'svelte'
import PopupTemplate from '../PopupTemplate.svelte'
import { IEvmNetwork } from '@core/network/interfaces'

export let isEvmTransaction: boolean = false
export let useBlindSigning: boolean = false

// Regular transaction
export let toAddress: string | undefined = undefined
export let toAmount: string | undefined = undefined
export let chainId: string | undefined = undefined
export let toAmount: bigint | undefined = undefined
export let network: IEvmNetwork | undefined = undefined
export let maxGasFee: bigint | undefined = undefined

// Blindly signed transaction
Expand Down Expand Up @@ -64,7 +64,10 @@
},
{
key: localize('general.amount'),
value: !useBlindSigning && isEvmTransaction ? toAmount : undefined,
value:
!useBlindSigning && isEvmTransaction
? formatTokenAmountBestMatch(toAmount, network?.baseToken)
: undefined,
},
{
key: localize('general.address'),
Expand All @@ -73,14 +76,14 @@
},
{
key: localize('general.network'),
value: !useBlindSigning && isEvmTransaction ? chainId : undefined,
value: !useBlindSigning && isEvmTransaction ? network?.chainId : undefined,
copyable: true,
},
{
key: localize('general.maxFees'),
value:
!useBlindSigning && isEvmTransaction
? formatTokenAmountBestMatch(maxGasFee, getBaseToken())
? formatTokenAmountBestMatch(maxGasFee, network?.baseToken)
: undefined,
},
{
Expand All @@ -90,7 +93,10 @@
},
{
key: localize('general.amount'),
value: !useBlindSigning && !isEvmTransaction ? toAmount : undefined,
value:
!useBlindSigning && !isEvmTransaction
? formatTokenAmountBestMatch(toAmount, network?.baseToken)
: undefined,
},
{
key: localize('general.message'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { localize } from '@core/i18n'
import { IEvmNetwork, IIscChain, NetworkType } from '@core/network'
import { IEvmNetwork, isIscNetwork } from '@core/network'
import { Table } from '@bloomwalletio/ui'
import { DrawerTemplate } from '@components/drawers'
import { NetworkConfigRoute } from '../../network-config-route.enum'
Expand All @@ -10,10 +10,6 @@
export let network: IEvmNetwork

const localeKey = 'views.dashboard.drawers.networkConfig.chain'

function isIscChain(network: IEvmNetwork): network is IIscChain {
return network.type === NetworkType.Isc
}
</script>

<DrawerTemplate title={network.name} {drawerRouter}>
Expand All @@ -36,7 +32,7 @@
value: network.explorerUrl ?? undefined,
copyable: true,
},
...(isIscChain(network)
...(isIscNetwork(network)
? [
{
key: localize(`${localeKey}.aliasAddress`),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
IEvmNetwork,
NetworkId,
NetworkType,
getEvmNetwork,
getIscChains,
getL1Network,
getNetwork,
Expand Down Expand Up @@ -43,7 +44,8 @@

const { sourceNetworkId, type } = $sendFlowParameters
if (sourceNetworkId && isEvmNetwork(sourceNetworkId)) {
hasInsufficientFunds = !(await canAccountMakeEvmTransaction($selectedAccountIndex, sourceNetworkId, type))
const network = getEvmNetwork(sourceNetworkId) as IEvmNetwork
hasInsufficientFunds = !(await canAccountMakeEvmTransaction($selectedAccountIndex, network, type))
} else {
hasInsufficientFunds = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import { localize } from '@core/i18n'
import { canAccountMakeEvmTransaction } from '@core/layer-2/actions'
import {
IEvmNetwork,
canAccountMakeStardustTransaction,
getActiveNetworkId,
getEvmNetwork,
isEvmNetwork,
isStardustNetwork,
networks,
Expand Down Expand Up @@ -36,9 +38,10 @@
async function setTokenError(): Promise<void> {
let hasEnoughFunds = true
if (selectedToken && isEvmNetwork(selectedToken.networkId)) {
const network = getEvmNetwork(selectedToken.networkId) as IEvmNetwork
hasEnoughFunds = await canAccountMakeEvmTransaction(
$selectedAccountIndex,
selectedToken.networkId,
network,
$sendFlowParameters?.type
)
} else if (selectedToken && isStardustNetwork(selectedToken.networkId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,13 @@
}}
{busy}
>
{#if isSourceNetworkLayer2 && preparedTransaction}
<EvmTransactionSummary transaction={preparedTransaction} sendFlowParameters={$sendFlowParameters} />
{:else if !isSourceNetworkLayer2 && preparedOutput}
{#if isSourceNetworkLayer2 && preparedTransaction && $sendFlowParameters && evmNetwork}
<EvmTransactionSummary
transaction={preparedTransaction}
sendFlowParameters={$sendFlowParameters}
network={evmNetwork}
/>
{:else if !isSourceNetworkLayer2 && preparedOutput && $sendFlowParameters}
{#if isDestinationNetworkLayer2}
<StardustToEvmTransactionSummary output={preparedOutput} sendFlowParameters={$sendFlowParameters} />
{:else}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script lang="ts">
import { Table } from '@bloomwalletio/ui'
import { localize } from '@core/i18n'
import { NetworkId } from '@core/network'
import { getBaseToken } from '@core/profile/actions'
import { IEvmNetwork, NetworkId } from '@core/network'
import { formatTokenAmountBestMatch } from '@core/token'
import { NetworkLabel } from '@ui'
import { Table } from '@bloomwalletio/ui'

export let sourceNetwork: IEvmNetwork
export let destinationNetworkId: NetworkId | undefined = undefined
export let estimatedGasFee: bigint | undefined = undefined
export let maxGasFee: bigint | undefined = undefined
Expand All @@ -24,11 +24,11 @@
},
{
key: localize('general.estimatedFee'),
value: estimatedGasFee ? formatTokenAmountBestMatch(estimatedGasFee, getBaseToken()) : undefined,
value: estimatedGasFee ? formatTokenAmountBestMatch(estimatedGasFee, sourceNetwork.baseToken) : undefined,
},
{
key: localize('general.maxFees'),
value: maxGasFee ? formatTokenAmountBestMatch(maxGasFee, getBaseToken()) : undefined,
value: maxGasFee ? formatTokenAmountBestMatch(maxGasFee, sourceNetwork.baseToken) : undefined,
},
]}
/>
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import { SendFlowParameters, SendFlowType, TokenTransferData } from '@core/wallet'
import { TransactionAssetSection } from '@ui'
import EvmTransactionDetails from './EvmTransactionDetails.svelte'
import { IEvmNetwork } from '@core/network'
import { handleError } from '@core/error/handlers'
import { StardustNetworkId } from '@core/network'

export let transaction: EvmTransactionData
export let sendFlowParameters: SendFlowParameters
export let network: IEvmNetwork

$: transactionAsset = getTransactionAsset(sendFlowParameters)
function getTransactionAsset(_sendFlowParameters: SendFlowParameters): {
Expand Down Expand Up @@ -65,6 +67,7 @@
<TransactionAssetSection baseCoinTransfer={sendFlowParameters.baseCoinTransfer} {...transactionAsset} />

<EvmTransactionDetails
sourceNetwork={network}
destinationNetworkId={sendFlowParameters?.destinationNetworkId}
estimatedGasFee={calculateEstimatedGasFeeFromTransactionData(transaction) + storageDeposit}
maxGasFee={calculateMaxGasFeeFromTransactionData(transaction) + storageDeposit}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { IAccountState } from '@core/account/interfaces'
import { getAddressFromAccountForNetwork } from '@core/account/utils'
import { calculateGasFeeInGlow } from '@core/layer-2/helpers'
import { IEvmNetwork, NetworkNamespace } from '@core/network'
import { IEvmNetwork, NetworkNamespace, calculateGasFee } from '@core/network'
import { MILLISECONDS_PER_SECOND } from '@core/utils/constants'
import { getSubjectFromAddress, isSubjectInternal } from '@core/wallet'
import { ActivityAction, ActivityDirection, InclusionState } from '../../enums'
Expand Down Expand Up @@ -38,7 +37,7 @@ export async function generateBaseEvmActivity(
// For native token transfers on L2, gasUsed is 0. Therefor we fallback to the estimatedGas
// https://discord.com/channels/397872799483428865/930642258427019354/1168854453005332490
const gasUsed = transaction.gasUsed || transaction.estimatedGas
const transactionFee = transaction.gasPrice ? calculateGasFeeInGlow(gasUsed ?? 0, transaction.gasPrice) : undefined
const transactionFee = transaction.gasPrice ? calculateGasFee(gasUsed, transaction.gasPrice) : undefined

return {
namespace: NetworkNamespace.Evm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { Converter, HEX_PREFIX } from '@core/utils'
import { EvmContractCallActivity } from '@core/activity/types/evm/evm-contract-call-activity.type'
import { SubjectType } from '@core/wallet'
import { ActivityDirection } from '@core/activity/enums'
import { WEI_PER_GLOW } from '@core/layer-2/constants'
import { getMethodForEvmTransaction } from '@core/layer-2'
import { addMethodToRegistry, getMethodFromRegistry } from '@core/layer-2/stores/method-registry.store'

Expand Down Expand Up @@ -109,7 +108,7 @@ async function generateEvmCoinTransferActivityFromBlockscoutTransaction(
type: EvmActivityType.CoinTransfer,
baseTokenTransfer: {
tokenId: BASE_TOKEN_ID,
rawAmount: Converter.bigIntLikeToBigInt(blockscoutTransaction.value) / WEI_PER_GLOW,
rawAmount: Converter.bigIntLikeToBigInt(blockscoutTransaction.value),
},
} as EvmCoinTransferActivity
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import {
EvmContractCallActivity,
EvmTokenTransferActivity,
} from '@core/activity/types'
import { WEI_PER_GLOW } from '@core/layer-2/constants'
import { getMethodForEvmTransaction } from '@core/layer-2/utils'
import { getTransferInfoFromTransactionData } from '@core/layer-2/utils/getTransferInfoFromTransactionData'
import { IEvmNetwork } from '@core/network'
import { NftStandard } from '@core/nfts'
import { BASE_TOKEN_ID, TokenStandard } from '@core/token'
import { LocalEvmTransaction } from '@core/transactions'
import { Converter } from '@core/utils/convert'
import { generateBaseEvmActivity } from './generateBaseEvmActivity'
import { SubjectType } from '@core/wallet'
import { generateBaseEvmActivity } from './generateBaseEvmActivity'
import { Converter } from '@core/utils'

export async function generateEvmActivityFromLocalEvmTransaction(
transaction: LocalEvmTransaction,
Expand Down Expand Up @@ -110,7 +109,7 @@ export async function generateEvmActivityFromLocalEvmTransaction(
type: EvmActivityType.CoinTransfer,
baseTokenTransfer: {
tokenId: BASE_TOKEN_ID,
rawAmount: Converter.bigIntLikeToBigInt(transaction.value) / WEI_PER_GLOW,
rawAmount: Converter.bigIntLikeToBigInt(transaction.value ?? 0),
},
} as EvmCoinTransferActivity
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IAccountState } from '@core/account/interfaces'
import { IEvmNetwork, NetworkNamespace } from '@core/network'
import { IEvmNetwork, NetworkNamespace, calculateGasFee } from '@core/network'
import {
BaseEvmActivity,
EvmCoinTransferActivity,
Expand All @@ -13,7 +13,6 @@ import { ISmartContractSubject, SubjectType, getSubjectFromAddress, isSubjectInt
import { EvmActivityType } from '@core/activity/enums/evm'
import { BASE_TOKEN_ID, TokenStandard } from '@core/token'
import { NftStandard } from '@core/nfts'
import { calculateGasFeeInGlow } from '@core/layer-2/helpers'
import {
BlockscoutTokenTransfer,
isBlockscoutErc20Transfer,
Expand Down Expand Up @@ -55,7 +54,7 @@ export async function generateEvmTokenTransferActivityFromBlockscoutTokenTransfe
const isInternal = isSubjectInternal(recipient)

const transactionFee = blockscoutTransaction
? calculateGasFeeInGlow(blockscoutTransaction.gas_used ?? 0, blockscoutTransaction.gas_price)
? calculateGasFee(blockscoutTransaction.gas_used, blockscoutTransaction.gas_price)
: undefined

let tokenId: string | undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { EvmNetworkId } from '@core/network/types'
import { SendFlowType } from '@core/wallet/enums'
import { getLayer2AccountBalanceForToken } from '../stores'
import { FALLBACK_ESTIMATED_GAS, GAS_LIMIT_MULTIPLIER } from '../constants'
import { calculateGasFeeInGlow } from '../helpers'
import { getEvmNetwork } from '@core/network'
import { IEvmNetwork, calculateGasFee } from '@core/network'

export async function canAccountMakeEvmTransaction(
accountIndex: number,
networkId: EvmNetworkId,
network: IEvmNetwork,
sendFlowType: SendFlowType | undefined
): Promise<boolean> {
const baseTokenAccountBalance = getLayer2AccountBalanceForToken(accountIndex, networkId)
const baseTokenAccountBalance = getLayer2AccountBalanceForToken(accountIndex, network.id)
const gasLimit = Math.floor(
FALLBACK_ESTIMATED_GAS[sendFlowType ?? SendFlowType.BaseCoinTransfer] * GAS_LIMIT_MULTIPLIER
)
const gasPrice = await getEvmNetwork(networkId)?.getGasPrice()
const gasPrice = await network.getGasPrice()
if (gasPrice === undefined) {
return false
}
const minimumGasFee = calculateGasFeeInGlow(gasLimit, gasPrice)

const minimumGasFee = calculateGasFee(gasLimit, gasPrice)
return baseTokenAccountBalance > minimumGasFee
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function getIscTransferSmartContractData(

const agentId = evmAddressToAgentId(recipientAddress, iscChain.aliasAddress)
const parameters = getAgentBalanceParameters(agentId)
const allowance = buildAssetAllowance(transferredAsset)
const allowance = buildAssetAllowance(iscChain, transferredAsset)

const contract = iscChain.getContract(ContractType.IscMagic, ISC_MAGIC_CONTRACT_ADDRESS)
const method = contract.methods.call(accountsCoreContract, transferAllowanceTo, parameters, allowance)
Expand Down
1 change: 0 additions & 1 deletion packages/shared/src/lib/core/layer-2/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ export * from './isc-magic-contract-address.constant'
export * from './layer2-tokens-poll-interval.constant'
export * from './target-contracts.constant'
export * from './transfer-allowance.constant'
export * from './wei.constants'
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
export const WEI_PER_GLOW = BigInt(1_000_000_000_000)

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions packages/shared/src/lib/core/layer-2/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
export * from './calculateGasFeeInGlow'
export * from './encodeAddress'
export * from './encodeAssetAllowance'
export * from './encodeSmartContractParameters'
export * from './evmAddressToAgentId'
export * from './getAgentBalanceParameters'
export * from './getEvmTransactionValueFromAmount'
export * from './getSmartContractHexName'
export * from './specialNativeTokenAmountEncoding'
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { IIscChain } from '@core/network/interfaces'
import { AssetType } from '../enums'
import { ILayer2AssetAllowance } from '../interfaces'
import { TransferredAsset } from '../types'

export function buildAssetAllowance(
iscChain: IIscChain,
transferredAsset: TransferredAsset,
storageDepositRequired?: bigint
): ILayer2AssetAllowance {
const baseTokens = String(storageDepositRequired ?? 0)
if (transferredAsset.type === AssetType.BaseCoin) {
return {
baseTokens: transferredAsset.amount.toString(),
baseTokens: iscChain.normaliseAmount(transferredAsset.amount).toString(),
nativeTokens: [],
nfts: [],
}
Expand Down
Loading
Loading