Skip to content

Commit

Permalink
refactor: organise imports and exports in layer-2 module (#522)
Browse files Browse the repository at this point in the history
Co-authored-by: Tuditi <[email protected]>
  • Loading branch information
nicole-obrien and Tuditi authored Aug 29, 2023
1 parent ce8e44f commit 33d5e13
Show file tree
Hide file tree
Showing 31 changed files with 52 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseLayer2Metadata, getDestinationNetworkFromAddress } from '@core/layer-2/utils'
import { parseLayer2Metadata } from '@core/layer-2/utils'
import { getDestinationNetworkFromAddress } from '@core/layer-2/actions'
import { Layer2Metadata } from '@core/layer-2/types'
import { SenderInfo } from '../../types'
import { SubjectType } from '@core/wallet/enums'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ActivityType } from '@core/activity/enums'
import { Activity } from '@core/activity/types'
import { localize } from '@core/i18n'
import { getLayer2NetworkFromAddress } from '@core/layer-2/utils'
import { getLayer2NetworkFromAddress } from '@core/layer-2/actions'
import { truncateString } from '@core/utils'
import { SubjectType } from '@core/wallet/enums'
import type { Subject } from '@core/wallet/types'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Converter } from '@core/utils'
import Web3 from 'web3'
import { getEvmTransactionValueFromAmount } from './getEvmTransactionValueFromAmount'
import { getEvmTransactionValueFromAmount } from '../helpers/getEvmTransactionValueFromAmount'
import { GAS_LIMIT_MULTIPLIER } from '../constants'
import { EvmTransactionData } from '../types'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SendFlowParameters, SendFlowType } from '@core/wallet'
import { FALLBACK_ESTIMATED_GAS, ISC_MAGIC_CONTRACT_ADDRESS } from '../constants'
import { AssetType } from '../enums'
import { TransferredAsset } from '../types'
import { getIscpTransferSmartContractData } from '../utils'
import { getIscpTransferSmartContractData } from './getIscpTransferSmartContractData'

export async function estimateGasForLayer1ToLayer2Transaction(sendFlowParameters: SendFlowParameters): Promise<number> {
const { recipient, destinationNetworkId } = sendFlowParameters ?? {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ContractType, evmAddressToAgentID, getAgentBalanceParameters, getSmartContractHexName } from '@core/layer-2'
import { ContractType } from '@core/layer-2'
import { IChain, getNetwork } from '@core/network'
import { ISC_MAGIC_CONTRACT_ADDRESS } from '../constants'
import { Converter } from '@core/utils/convert'
import { TOKEN_ID_BYTE_LENGTH } from '@core/token/constants'
import { setLayer2AccountBalanceForChain } from '../stores'
import { evmAddressToAgentId, getAgentBalanceParameters, getSmartContractHexName } from '../helpers'
import { getActiveProfile } from '@core/profile/stores'
import { IAccountState } from '@core/account'
import { calculateAndAddPersistedBalanceChange } from '@core/activity/actions'
Expand Down Expand Up @@ -58,7 +59,7 @@ async function getLayer2NativeTokenBalancesForAddress(
): Promise<{ balance: number; tokenId: string }[]> {
const accountsCoreContract = getSmartContractHexName('accounts')
const getBalanceFunc = getSmartContractHexName('balance')
const agentID = evmAddressToAgentID(evmAddress)
const agentID = evmAddressToAgentId(evmAddress)
const parameters = getAgentBalanceParameters(agentID)
try {
const contract = chain.getContract(ContractType.IscMagic, ISC_MAGIC_CONTRACT_ADDRESS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SendFlowParameters } from '@core/wallet'
import { BigIntLike } from '@ethereumjs/util'
import { GAS_LIMIT_MULTIPLIER } from '../constants'
import { calculateGasFeeInGlow } from '../utils'
import { calculateGasFeeInGlow } from '../helpers'
import { estimateGasForLayer1ToLayer2Transaction } from './estimateGasForLayer1ToLayer2Transaction'
import { getGasPriceInWei } from './getGasPriceInWei'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { IChain } from '@core/network'
import {
evmAddressToAgentID,
getAgentBalanceParameters,
buildAssetAllowance,
getSmartContractHexName,
} from '@core/layer-2/utils'
import { getSelectedAccount } from '@core/account/stores'
import { ContractType } from '@core/layer-2/enums'
import { ISC_MAGIC_CONTRACT_ADDRESS } from '@core/layer-2/constants'
import { handleError } from '@core/error/handlers'
import { IError } from '@core/error/interfaces'
import { TransferredAsset } from '../types'
import {
buildAssetAllowance,
evmAddressToAgentId,
getAgentBalanceParameters,
getSmartContractHexName,
} from '../helpers'

export function getIscpTransferSmartContractData(
recipientAddress: string,
Expand All @@ -31,7 +31,7 @@ export function getIscpTransferSmartContractData(
const accountsCoreContract = getSmartContractHexName('accounts')
const transferAllowanceTo = getSmartContractHexName('transferAllowanceTo')

const agentId = evmAddressToAgentID(recipientAddress)
const agentId = evmAddressToAgentId(recipientAddress)
const parameters = getAgentBalanceParameters(agentId)
const allowance = buildAssetAllowance(transferredAsset)

Expand Down
7 changes: 7 additions & 0 deletions packages/shared/src/lib/core/layer-2/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
export * from './buildEvmTransactionData'
export * from './buildUnwrapAssetParameters'
export * from './buildUnwrapAssetTargetAddress'
export * from './estimateGasForLayer1ToLayer2Transaction'
export * from './fetchSelectedAccountLayer2Balance'
export * from './generateAndStoreEvmAddressForAccount'
export * from './getDestinationNetworkFromAddress'
export * from './getGasFeesForLayer1ToLayer2Transaction'
export * from './getGasPriceInWei'
export * from './getIscpTransferSmartContractData'
export * from './getLayer2MetadataForTransfer'
export * from './getLayer2NetworkFromAddress'
export * from './pollLayer2Tokens'
export * from './signEvmTransactionWithStronghold'
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { INft } from '@core/nfts/interfaces'
import { IPersistedToken } from '@core/token/interfaces'
import { SendFlowParameters, SendFlowType, TokenTransferData } from '@core/wallet'
import { Converter } from '@iota/util.js'
import BigInteger from 'big-integer'
import { SendFlowType, SendFlowParameters, TokenTransferData } from '@core/wallet'
import { SpecialStream } from '../classes'
import { Allowance } from '../enums'
import { specialNativeTokenAmountEncoding } from '../utils'
import { INft } from '@core/nfts/interfaces'
import { IPersistedToken } from '@core/token/interfaces'
import { specialNativeTokenAmountEncoding } from './'

export function encodeAssetAllowance(sendFlowParameters: SendFlowParameters): Uint8Array {
const allowance = new SpecialStream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Converter } from '@iota/util.js'

export function evmAddressToAgentID(evmStoreAccount: string): Uint8Array {
export function evmAddressToAgentId(evmStoreAccount: string): Uint8Array {
// This function constructs an AgentID that is required to be used with contracts
// Wasp understands different AgentID types and each AgentID needs to provide a certain ID that describes it's address type.
// In the case of EVM addresses it's ID 3.
Expand Down
7 changes: 7 additions & 0 deletions packages/shared/src/lib/core/layer-2/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export * from './buildAssetAllowance'
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'
1 change: 0 additions & 1 deletion packages/shared/src/lib/core/layer-2/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './abis'
export * from './actions'
export * from './constants'
export * from './classes'
export * from './constants'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigIntLike } from '@ethereumjs/util'
import { EvmTransactionData } from '../types'
import { calculateGasFeeInGlow } from './calculateGasFeeInGlow'
import { calculateGasFeeInGlow } from '../helpers'

export function calculateEstimatedGasFeeFromTransactionData(
transactionData: EvmTransactionData
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigIntLike } from '@ethereumjs/util'
import { EvmTransactionData } from '../types'
import { calculateGasFeeInGlow } from './calculateGasFeeInGlow'
import { calculateGasFeeInGlow } from '../helpers'

export function calculateMaxGasFeeFromTransactionData(transactionData: EvmTransactionData): BigIntLike | undefined {
const { gasLimit, gasPrice } = transactionData
Expand Down
14 changes: 0 additions & 14 deletions packages/shared/src/lib/core/layer-2/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
export * from './buildAssetAllowance'
export * from './buildEvmTransactionData'
export * from './buildUnwrapAssetParameters'
export * from './buildUnwrapAssetTargetAddress'
export * from './calculateEstimatedGasFeeFromTransactionData'
export * from './calculateGasFeeInGlow'
export * from './calculateMaxGasFeeFromTransactionData'
export * from './evmAddressToAgentID'
export * from './getAbiForContractType'
export * from './getAgentBalanceParameters'
export * from './getAmountFromEvmTransactionValue'
export * from './getDestinationNetworkFromAddress'
export * from './getErc20TokenMetadata'
export * from './getErc20TransferSmartContractData'
export * from './getEvmTransactionValueFromAmount'
export * from './getIscpTransferSmartContractData'
export * from './getLayer2NetworkFromAddress'
export * from './getSmartContractHexName'
export * from './parseLayer2Metadata'
export * from './parseLayer2MetadataForTransfer'
export * from './prepareEvmTransaction'
export * from './signEvmTransactionWithStronghold'
export * from './specialNativeTokenAmountEncoding'
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Transaction } from '@ethereumjs/tx'
import { EvmChainId, getEvmTransactionOptions } from '@core/network'
import { RLP } from '@ethereumjs/rlp'
import { Transaction } from '@ethereumjs/tx'
import { bufArrToArr } from '@ethereumjs/util'

import { IEvmTransactionSignature } from '../interfaces'
import { EvmTransactionData } from '../types'
import { EvmChainId, getEvmTransactionOptions } from '@core/network'

export function prepareEvmTransaction(
transactionData: EvmTransactionData,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { Contract, ContractType, getAbiForContractType } from '@core/layer-2'
import { get } from 'svelte/store'

import Web3 from 'web3'

import { ContractType } from '@core/layer-2/enums'
import { getAbiForContractType } from '@core/layer-2/utils'
import { Contract } from '@core/layer-2/types'

import { NetworkHealth } from '../enums'
import { IBlock, IChain, IChainStatus, IIscpChainConfiguration, IIscpChainMetadata } from '../interfaces'
import { chainStatuses } from '../stores'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { IAccountState } from '@core/account'
import { AssetType } from '@core/layer-2'
import { ISC_MAGIC_CONTRACT_ADDRESS } from '@core/layer-2/constants'
import { EvmTransactionData, TransferredAsset } from '@core/layer-2/types'
import {
buildEvmTransactionData,
AssetType,
EvmTransactionData,
ISC_MAGIC_CONTRACT_ADDRESS,
TransferredAsset,
getErc20TransferSmartContractData,
getIscpTransferSmartContractData,
} from '@core/layer-2/utils'
} from '@core/layer-2'
import { buildEvmTransactionData, getIscpTransferSmartContractData } from '@core/layer-2/actions'
import { IChain } from '@core/network/interfaces'
import { IToken } from '@core/token/interfaces'
import { TokenStandard } from '@core/token/enums'
import { IToken } from '@core/token/interfaces'
import { SendFlowType } from '@core/wallet/stores'
import { SendFlowParameters } from '@core/wallet/types'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IAccountState } from '@core/account'
import { updateSelectedAccount } from '@core/account/stores'
import { handleError } from '@core/error/handlers'
import { EvmTransactionData } from '@core/layer-2/types'
import { signEvmTransactionWithStronghold } from '@core/layer-2/utils'
import { signEvmTransactionWithStronghold } from '@core/layer-2/actions'
import { Ledger } from '@core/ledger/classes'
import { EvmChainId } from '@core/network/enums'
import { isActiveLedgerProfile, isSoftwareProfile } from '@core/profile/stores'
Expand Down
15 changes: 4 additions & 11 deletions packages/shared/src/lib/core/wallet/actions/unwrapStardustAsset.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import Web3 from 'web3'

import { getSelectedAccountIndex } from '@core/account/stores'
import { handleError } from '@core/error/handlers'
import {
ContractType,
ISC_MAGIC_CONTRACT_ADDRESS,
buildEvmTransactionData,
ILayer2AssetAllowance,
buildUnwrapAssetParameters,
} from '@core/layer-2'
import { ETHEREUM_COIN_TYPE, IChain, getNetwork, SupportedNetworkId, NetworkId } from '@core/network'
import { ContractType, ILayer2AssetAllowance, ISC_MAGIC_CONTRACT_ADDRESS } from '@core/layer-2'
import { buildEvmTransactionData, buildUnwrapAssetParameters } from '@core/layer-2/actions'
import { ETHEREUM_COIN_TYPE, IChain, NetworkId, SupportedNetworkId, getNetwork } from '@core/network'
import { getActiveProfilePersistedEvmAddressesByAccountIndex } from '@core/profile/stores'

import Web3 from 'web3'
import { sendTransactionFromEvm } from '../actions/send'

export async function unwrapStardustAsset(
Expand Down

0 comments on commit 33d5e13

Please sign in to comment.