Skip to content

Commit

Permalink
Add assertSendTransaction to testnet helpers (#670)
Browse files Browse the repository at this point in the history
* Add assertSendTransaction to testnet helpers

* Make chain param optional
  • Loading branch information
nezouse authored Feb 28, 2025
1 parent 05921c6 commit 74e71f2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
20 changes: 3 additions & 17 deletions packages/app/src/test/e2e/setSparkReward.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { sparkRewardsConfig } from '@/config/contracts-generated'
import { TestnetClient } from '@marsfoundation/common-testnets'
import { assert, CheckedAddress, NormalizedUnitNumber } from '@marsfoundation/common-universal'
import { CheckedAddress, NormalizedUnitNumber } from '@marsfoundation/common-universal'
import { encodeAbiParameters, encodeFunctionData, erc20Abi, keccak256, parseUnits } from 'viem'
import { mainnet } from 'viem/chains'
import { TOKENS_ON_FORK } from './constants'
Expand Down Expand Up @@ -39,15 +38,14 @@ export async function setSparkReward({
),
)

await assertSendTransaction(testnetController.client, {
await testnetController.client.assertSendTransaction({
account: '0x4b340357aadd38403e5c8e64368fd502ed38df6a',
data: encodeFunctionData({
abi: sparkRewardsConfig.abi,
functionName: 'setMerkleRoot',
args: [merkleRoot],
}),
to: sparkRewardsConfig.address[mainnet.id],
chain: null,
})
await testnetController.progressSimulation(5)

Expand All @@ -60,15 +58,14 @@ export async function setSparkReward({
await testnetController.client.setErc20Balance(tokenAddress, wallet, cumulativeAmountBigInt)
await testnetController.progressSimulation(5)

await assertSendTransaction(testnetController.client, {
await testnetController.client.assertSendTransaction({
account: '0x4b340357aadd38403e5c8e64368fd502ed38df6a',
data: encodeFunctionData({
abi: erc20Abi,
functionName: 'approve',
args: [sparkRewardsConfig.address[mainnet.id], cumulativeAmountBigInt],
}),
to: tokenAddress,
chain: null,
})
await testnetController.progressSimulation(5)

Expand Down Expand Up @@ -96,14 +93,3 @@ export async function setSparkReward({
},
)
}

async function assertSendTransaction(
testnetClient: TestnetClient,
params: Parameters<TestnetClient['sendTransaction']>[0],
): Promise<void> {
const hash = await testnetClient.sendTransaction(params)
const { status } = await testnetClient.waitForTransactionReceipt({
hash,
})
assert(status === 'success', 'Transaction failed')
}
8 changes: 6 additions & 2 deletions packages/common-testnets/src/TestnetClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
Hex,
PartialBy,
PublicActions,
WaitForTransactionReceiptReturnType,
WalletClient,
WriteContractParameters,
WriteContractReturnType,
} from 'viem'

export interface TestnetClient extends WalletClient, PublicActions, TestnetClientHelperActions {
Expand All @@ -34,5 +34,9 @@ export type TestnetClientHelperActions = {
args extends ContractFunctionArgs<abi, 'payable' | 'nonpayable', functionName>,
>(
args: PartialBy<WriteContractParameters<abi, functionName, args, undefined>, 'chain'>, // avoids requiring chain parameter
) => Promise<WriteContractReturnType>
) => Promise<WaitForTransactionReceiptReturnType>

assertSendTransaction: (
args: PartialBy<Parameters<WalletClient['sendTransaction']>[0], 'chain'>,
) => Promise<WaitForTransactionReceiptReturnType>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CheckedAddress } from '@marsfoundation/common-universal'
import { expect } from 'earl'
import { erc20Abi, parseEther } from 'viem'
import { encodeFunctionData, erc20Abi, parseEther } from 'viem'
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import { TestnetClient } from '../TestnetClient.js'
Expand Down Expand Up @@ -31,7 +31,7 @@ describe(extendWithTestnetHelpers.name, () => {
await testnetClient.revertToBaseline()
})

it('asserts that transaction was mined successfully', async () => {
it('asserts that contract write was mined, but rejected', async () => {
const usds = CheckedAddress('0xdc035d45d973e3ec169d2276ddab16f1e407384f')
const account = privateKeyToAccount(generatePrivateKey())

Expand All @@ -48,6 +48,26 @@ describe(extendWithTestnetHelpers.name, () => {
}),
).toBeRejectedWith('Transaction failed: transfer')
})

it('asserts that transaction was mined, but rejected', async () => {
const usds = CheckedAddress('0xdc035d45d973e3ec169d2276ddab16f1e407384f')
const account = privateKeyToAccount(generatePrivateKey())

await testnetClient.setBalance(account.address, parseEther('1'))

await expect(() =>
testnetClient.assertSendTransaction({
account,
to: usds,
data: encodeFunctionData({
abi: erc20Abi,
functionName: 'transfer',
args: [CheckedAddress.random('alice'), 100n],
}),
gas: 100_000n,
}),
).toBeRejectedWith('Transaction failed:')
})
})
}
})
15 changes: 13 additions & 2 deletions packages/common-testnets/src/nodes/extendWithTestnetHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert } from '@marsfoundation/common-universal'
import { TestnetClient, TestnetClientHelperActions } from '../TestnetClient.js'

export function extendWithTestnetHelpers(
c: Pick<TestnetClient, 'snapshot' | 'revert' | 'writeContract' | 'waitForTransactionReceipt'>,
c: Pick<TestnetClient, 'snapshot' | 'revert' | 'writeContract' | 'waitForTransactionReceipt' | 'sendTransaction'>,
): TestnetClientHelperActions {
let baselineSnapshotId: string | undefined

Expand All @@ -28,7 +28,18 @@ export function extendWithTestnetHelpers(

assert(receipt.status === 'success', `Transaction failed: ${summaryText}`)

return txHash
return receipt
},
async assertSendTransaction(args) {
const txHash = await c.sendTransaction(args as any)

const receipt = await c.waitForTransactionReceipt({
hash: txHash,
})

assert(receipt.status === 'success', `Transaction failed: ${txHash}`)

return receipt
},
}
}

0 comments on commit 74e71f2

Please sign in to comment.