Skip to content

Commit

Permalink
Merge branch 'main' into jophish/knip-diff
Browse files Browse the repository at this point in the history
  • Loading branch information
jophish authored Nov 7, 2023
2 parents ecfa755 + 41eae07 commit 8d70c87
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/swap/useSwapQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export async function prepareSwapTransactions(
spendTokenAmount: new BigNumber(amountToApprove.toString()),
decreasedAmountGasFeeMultiplier: DECREASED_SWAP_AMOUNT_GAS_FEE_MULTIPLIER,
baseTransactions,
// We still want to prepare the transactions even if the user doesn't have enough balance
throwOnSpendTokenAmountExceedsBalance: false,
})
}

Expand Down
25 changes: 25 additions & 0 deletions src/viem/prepareTransactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,31 @@ describe('prepareTransactions module', () => {
})
).rejects.toThrowError(/Cannot prepareTransactions for amount greater than balance./)
})
it('does not throw if trying to sendAmount > sendToken balance when throwOnSpendTokenAmountExceedsBalance is false', async () => {
mocked(estimateFeesPerGas).mockResolvedValue({
maxFeePerGas: BigInt(100),
maxPriorityFeePerGas: BigInt(2),
})
mockPublicClient.estimateGas.mockResolvedValue(BigInt(1_000))

await expect(
prepareTransactions({
feeCurrencies: mockFeeCurrencies,
spendToken: mockSpendToken,
spendTokenAmount: new BigNumber(51_000),
decreasedAmountGasFeeMultiplier: 1,
baseTransactions: [
{
from: '0xfrom' as Address,
to: '0xto' as Address,
data: '0xdata',
type: 'cip42',
},
],
throwOnSpendTokenAmountExceedsBalance: false,
})
).resolves.toEqual(expect.anything())
})
it("returns a 'not-enough-balance-for-gas' result when the balances for feeCurrencies are too low to cover the fee", async () => {
mocked(estimateFeesPerGas).mockResolvedValue({
maxFeePerGas: BigInt(100),
Expand Down
10 changes: 8 additions & 2 deletions src/viem/prepareTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,28 +172,34 @@ export async function tryEstimateTransactions(
* Adds "maxFeePerGas" and "maxPriorityFeePerGas" fields to base transactions. Adds "gas" field to base
* transactions if they do not already include them.
*
* NOTE: throws if spendTokenAmount exceeds the user's balance of that token.
* NOTE: throws if spendTokenAmount exceeds the user's balance of that token, unless throwOnSpendTokenAmountExceedsBalance is false
*
* @param feeCurrencies
* @param spendToken
* @param spendTokenAmount
* @param decreasedAmountGasFeeMultiplier
* @param baseTransactions
* @param throwOnSpendTokenAmountExceedsBalance
*/
export async function prepareTransactions({
feeCurrencies,
spendToken,
spendTokenAmount,
decreasedAmountGasFeeMultiplier,
baseTransactions,
throwOnSpendTokenAmountExceedsBalance = true,
}: {
feeCurrencies: TokenBalance[]
spendToken: TokenBalanceWithAddress
spendTokenAmount: BigNumber
decreasedAmountGasFeeMultiplier: number
baseTransactions: (TransactionRequestCIP42 & { gas?: bigint })[]
throwOnSpendTokenAmountExceedsBalance?: boolean
}): Promise<PreparedTransactionsResult> {
if (spendTokenAmount.isGreaterThan(spendToken.balance.shiftedBy(spendToken.decimals))) {
if (
throwOnSpendTokenAmountExceedsBalance &&
spendTokenAmount.isGreaterThan(spendToken.balance.shiftedBy(spendToken.decimals))
) {
throw new Error(
`Cannot prepareTransactions for amount greater than balance. Amount: ${spendTokenAmount}, Balance: ${spendToken.balance}, Decimals: ${spendToken.decimals}`
)
Expand Down

0 comments on commit 8d70c87

Please sign in to comment.