Skip to content
Open
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 @@ -6,7 +6,7 @@

# Interface: PreparedTransactionsNeedDecreaseSpendAmountForGas

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:50](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L50)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:40](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L40)

## Properties

Expand All @@ -16,7 +16,7 @@ Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:50](https://
decreasedSpendAmount: BigNumber
```

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:55](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L55)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:45](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L45)

---

Expand All @@ -26,7 +26,7 @@ Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:55](https://
estimatedGasFeeInDecimal: BigNumber
```

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:54](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L54)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:44](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L44)

---

Expand All @@ -36,7 +36,7 @@ Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:54](https://
feeCurrency: TokenBalance
```

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:52](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L52)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:42](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L42)

---

Expand All @@ -46,7 +46,7 @@ Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:52](https://
maxGasFeeInDecimal: BigNumber
```

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:53](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L53)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:43](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L43)

---

Expand All @@ -56,4 +56,4 @@ Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:53](https://
type: 'need-decrease-spend-amount-for-gas'
```

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:51](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L51)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:41](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L41)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Interface: PreparedTransactionsNotEnoughBalanceForGas

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:58](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L58)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:48](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L48)

## Properties

Expand All @@ -16,7 +16,7 @@ Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:58](https://
feeCurrencies: TokenBalance[];
```

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:60](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L60)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:50](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L50)

---

Expand All @@ -26,4 +26,4 @@ Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:60](https://
type: 'not-enough-balance-for-gas'
```

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:59](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L59)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:49](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L49)
8 changes: 4 additions & 4 deletions docs/reference/interfaces/PreparedTransactionsPossible.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Interface: PreparedTransactionsPossible

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:44](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L44)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:34](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L34)

## Properties

Expand All @@ -16,7 +16,7 @@ Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:44](https://
feeCurrency: TokenBalance
```

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:47](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L47)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:37](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L37)

---

Expand All @@ -26,7 +26,7 @@ Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:47](https://
transactions: TransactionRequest[];
```

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:46](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L46)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:36](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L36)

---

Expand All @@ -36,4 +36,4 @@ Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:46](https://
type: 'possible'
```

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:45](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L45)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:35](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L35)
2 changes: 1 addition & 1 deletion docs/reference/type-aliases/PreparedTransactionsResult.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ type PreparedTransactionsResult =
| PreparedTransactionsNotEnoughBalanceForGas
```

Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:63](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L63)
Defined in: [packages/@divvi/mobile/src/viem/prepareTransactions.ts:53](https://github.com/divvi-xyz/divvi-mobile/blob/main/packages/@divvi/mobile/src/viem/prepareTransactions.ts#L53)
47 changes: 0 additions & 47 deletions packages/@divvi/mobile/src/viem/prepareTransactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ describe('prepareTransactions module', () => {
}),
{}
)
const mockExceededAllowanceError = new EstimateGasExecutionError(
new ExecutionRevertedError({
cause: new BaseError("transfer value exceeded sender's allowance for spender"),
}),
{}
)
const mockInvalidInputRpcError = new EstimateGasExecutionError(
new InvalidInputRpcError(
new BaseError('test mock', { details: 'gas required exceeds allowance' })
Expand Down Expand Up @@ -380,32 +374,6 @@ describe('prepareTransactions module', () => {
}
)
})
it('throws if gas estimation throws error for some other reason besides insufficient funds', async () => {
mocked(estimateFeesPerGas).mockResolvedValue({
maxFeePerGas: BigInt(100),
maxPriorityFeePerGas: BigInt(2),
baseFeePerGas: BigInt(50),
})
mocked(estimateGas).mockRejectedValue(mockExceededAllowanceError)

await expect(() =>
prepareTransactions({
feeCurrencies: mockFeeCurrencies,
spendToken: mockSpendToken,
spendTokenAmount: new BigNumber(20),
decreasedAmountGasFeeMultiplier: 1,
baseTransactions: [
{
from: '0xfrom' as Address,
to: '0xto' as Address,
data: '0xdata',
},
],
origin: 'send',
})
).rejects.toThrowError(EstimateGasExecutionError)
expect(AppAnalytics.track).not.toHaveBeenCalled()
})
it("returns a 'need-decrease-spend-amount-for-gas' result when spending the exact max amount of a feeCurrency, and no other feeCurrency has enough balance to pay for the fee", async () => {
mocked(estimateFeesPerGas).mockResolvedValue({
maxFeePerGas: BigInt(1),
Expand Down Expand Up @@ -909,21 +877,6 @@ describe('prepareTransactions module', () => {
})
expect(estimateTransactionOutput).toEqual(null)
})
it('throws if estimateGas throws error for some other reason besides insufficient funds', async () => {
mocked(estimateGas).mockRejectedValue(mockExceededAllowanceError)
const baseTransaction: TransactionRequest = { from: '0x123' }
await expect(() =>
tryEstimateTransaction({
client: mockPublicClient,
baseTransaction,
maxFeePerGas: BigInt(456),
feeCurrencySymbol: 'FEE',
feeCurrencyAddress: '0xabc',
maxPriorityFeePerGas: BigInt(789),
baseFeePerGas: BigInt(200),
})
).rejects.toThrowError(EstimateGasExecutionError)
})
})
describe('tryEstimateTransactions', () => {
it('returns null if estimateGas throws error due to insufficient funds', async () => {
Expand Down
30 changes: 3 additions & 27 deletions packages/@divvi/mobile/src/viem/prepareTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@ import Logger from 'src/utils/Logger'
import { appPublicClient, publicClient } from 'src/viem'
import { estimateFeesPerGas } from 'src/viem/estimateFeesPerGas'
import { networkIdToNetwork } from 'src/web3/networkConfig'
import {
Address,
Client,
EstimateGasExecutionError,
ExecutionRevertedError,
InsufficientFundsError,
InvalidInputRpcError,
TransactionRequestEIP1559,
encodeFunctionData,
erc20Abi,
} from 'viem'
import { Address, Client, TransactionRequestEIP1559, encodeFunctionData, erc20Abi } from 'viem'
import { estimateGas } from 'viem/actions'
import { TransactionRequestCIP64 } from 'viem/chains'

Expand Down Expand Up @@ -127,8 +117,6 @@ export function getFeeCurrencyAddress(feeCurrency: TokenBalance): Address | unde
* checks comparing the user's balance to send/swap amounts need to be done somewhere else to be able to give
* coherent error messages to the user when they lack the funds to perform a transaction.
*
* Throws other kinds of errors (e.g. if execution is reverted for some other reason)
*
* @param client
* @param baseTransaction
* @param maxFeePerGas
Expand Down Expand Up @@ -177,20 +165,8 @@ export async function tryEstimateTransaction({
baseFeePerGas,
})
} catch (e) {
if (
e instanceof EstimateGasExecutionError &&
(e.cause instanceof InsufficientFundsError ||
(e.cause instanceof ExecutionRevertedError && // viem does not reliably label node errors as InsufficientFundsError when the user has enough to pay for the transfer, but not for the transfer + gas
(/transfer value exceeded balance of sender/.test(e.cause.details) ||
/transfer amount exceeds balance/.test(e.cause.details))) ||
(e.cause instanceof InvalidInputRpcError &&
/gas required exceeds allowance/.test(e.cause.details)))
Comment on lines -181 to -187
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the change is to get rid of this guessing

) {
// too much gas was needed
Logger.warn(TAG, `Couldn't estimate gas with feeCurrency ${feeCurrencySymbol}`, e)
return null
}
throw e
Logger.warn(TAG, `Couldn't estimate gas with feeCurrency ${feeCurrencySymbol}`, e)
return null
}

return tx
Expand Down
Loading