Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

fix: Crash if spending limit module is not deployed on a chain #3890

Merged
merged 2 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 3 additions & 7 deletions src/logic/contracts/spendingLimitContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ import { AllowanceModule } from 'src/types/contracts/allowance-module.d'
import { ChainId } from 'src/config/chain.d'

/**
* Returns an address of the deployed AllowanceModule contract. Throws if not found.
* Returns an address of the deployed AllowanceModule contract. Returns undefined if no address found.
* @param {ChainId} chainId - The chainId of the network
* @returns {string|undefined}
*/
const getSpendingLimitModuleAddress = (chainId: ChainId): string => {
const getSpendingLimitModuleAddress = (chainId: ChainId): string | undefined => {
const deployment = getAllowanceModuleDeployment({ network: chainId })

if (!deployment) {
throw new Error(`Could not find AllowanceModule deployment for chainId: ${chainId}`)
}

return deployment.networkAddresses[chainId]
return deployment?.networkAddresses[chainId]
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/logic/safe/utils/spendingLimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ export const getSpendingLimits = async (
chainId: ChainId,
): Promise<SpendingLimit[] | null> => {
const spendingLimitModuleAddress = getSpendingLimitModuleAddress(chainId)
const isSpendingLimitEnabled = isModuleEnabled(modules, spendingLimitModuleAddress)
if (!spendingLimitModuleAddress) return null

const isSpendingLimitEnabled = isModuleEnabled(modules, spendingLimitModuleAddress)
if (!isSpendingLimitEnabled) return null

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ const ReviewSendFundsTx = ({ onClose, onPrev, tx }: ReviewTxProps): React.ReactE
if (isSpendingLimitTx && txToken && tx.tokenSpendingLimit) {
const spendingLimitTokenAddress = isSendingNativeToken ? ZERO_ADDRESS : txToken.address
const spendingLimitModuleAddress = getSpendingLimitModuleAddress(chainId)
if (!spendingLimitModuleAddress) return

const spendingLimit = getSpendingLimitContract(spendingLimitModuleAddress)
try {
trackEvent(MODALS_EVENTS.USE_SPENDING_LIMIT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ const calculateSpendingLimitsTxData = async (
txToken: Token,
values: Record<string, string>,
modules: string[],
moduleAddress: string,
txParameters?: TxParameters,
): Promise<SpendingLimitsTxData> => {
const chainId = _getChainId()
const moduleAddress = getSpendingLimitModuleAddress(chainId)
const isSpendingLimitEnabled = isModuleEnabled(modules, moduleAddress)
const transactions: MultiSendTx[] = []

Expand Down Expand Up @@ -186,6 +185,9 @@ export const ReviewSpendingLimits = ({ onBack, onClose, txToken, values }: Revie

useEffect(() => {
const calculateSpendingLimit = async () => {
const moduleAddress = getSpendingLimitModuleAddress(_getChainId())
if (!moduleAddress) return

const { spendingLimitTxData } = await calculateSpendingLimitsTxData(
safeAddress,
safeVersion,
Expand All @@ -195,6 +197,7 @@ export const ReviewSpendingLimits = ({ onBack, onClose, txToken, values }: Revie
txToken,
values,
safeModules,
moduleAddress,
)
setEstimateGasArgs(spendingLimitTxData)
}
Expand All @@ -211,6 +214,9 @@ export const ReviewSpendingLimits = ({ onBack, onClose, txToken, values }: Revie
])

const handleSubmit = async (txParameters: TxParameters, delayExecution: boolean): Promise<void> => {
const moduleAddress = getSpendingLimitModuleAddress(_getChainId())
if (!moduleAddress) return

const { ethGasPrice, ethGasLimit, ethGasPriceInGWei } = txParameters
const advancedOptionsTxParameters = {
...txParameters,
Expand All @@ -229,6 +235,7 @@ export const ReviewSpendingLimits = ({ onBack, onClose, txToken, values }: Revie
txToken,
values,
safeModules,
moduleAddress,
advancedOptionsTxParameters,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const RemoveLimitModal = ({ onClose, spendingLimit, open }: RemoveSpendin
}, [spendingLimit])

const removeSelectedSpendingLimit = (txParameters: TxParameters, delayExecution: boolean) => {
if (!spendingLimitAddress) return
try {
dispatch(
createTransaction({
Expand Down