Skip to content

Commit

Permalink
Gas revert (#55)
Browse files Browse the repository at this point in the history
* adding revert check in receipt of fulfill, and test

* fix error msg

* lint
  • Loading branch information
StoyanD authored Oct 30, 2024
1 parent fd85f9a commit e4c823a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/common/errors/eco-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Network } from 'alchemy-sdk'
import { Logger } from '@nestjs/common'
import * as _ from 'lodash'
import { EcoLogMessage } from '../logging/eco-log-message'
import { Chain } from 'viem'
import { Chain, TransactionReceipt } from 'viem'

export class EcoError extends Error {
// Alchemy Service
Expand Down Expand Up @@ -42,6 +42,9 @@ export class EcoError extends Error {
static FeasableIntentNoTransactionError = new Error('No transaction data found')
static FulfillIntentNoTransactionError = new Error('No transaction data found')
static FulfillIntentBatchError = new Error('Could not fulfill batch transaction')
static FulfillIntentRevertError(receipt: TransactionReceipt) {
return new Error(JSON.stringify(receipt))
}

// ValidateIntent Service
static ValidateIntentDescructureFailed(err?: Error) {
Expand Down
5 changes: 4 additions & 1 deletion src/intent/fulfill-intent.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ export class FulfillIntentService {
const receipt = await kernelAccountClient.waitForTransactionReceipt({ hash: transactionHash })

// set the status and receipt for the model
model.status = 'SOLVED'
model.receipt = receipt as any
if (receipt.status === 'reverted') {
throw EcoError.FulfillIntentRevertError(receipt)
}
model.status = 'SOLVED'

this.logger.debug(
EcoLogMessage.fromDefault({
Expand Down
30 changes: 30 additions & 0 deletions src/intent/tests/fulfill-intent.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,36 @@ describe('FulfillIntentService', () => {
await expect(() => fulfillIntentService.executeFulfillIntent(hash)).rejects.toThrow(error)
})

it('should fail on receipt status reverted', async () => {
const receipt = { status: 'reverted' }
const error = EcoError.FulfillIntentRevertError(receipt as any)
utilsIntentService.getIntentProcessData = jest.fn().mockResolvedValue({ model, solver })
const mockGetFulfillIntentData = jest.fn()
fulfillIntentService['getFulfillIntentData'] = mockGetFulfillIntentData
fulfillIntentService['getTransactionsForTargets'] = jest.fn().mockReturnValue([])
jest.spyOn(ecoConfigService, 'getEth').mockReturnValue({ claimant } as any)
jest.spyOn(accountClientService, 'getClient').mockImplementation(async (id) => {
return {
execute: () => {
return '0x33'
},
waitForTransactionReceipt: () => {
return receipt
},
} as any
})
await expect(() => fulfillIntentService.executeFulfillIntent(hash)).rejects.toThrow(error)
expect(mockLogError).toHaveBeenCalledTimes(1)
expect((model as any).status).toBe('FAILED')
expect(mockLogError).toHaveBeenCalledWith({
msg: `fulfillIntent: Invalid transaction`,
error: EcoError.FulfillIntentBatchError.toString(),
model,
errorPassed: error,
flatExecuteData: emptyTxs,
})
})

it('should log error', async () => {
const error = new Error('stuff went bad')
utilsIntentService.getIntentProcessData = jest.fn().mockResolvedValue({ model, solver })
Expand Down

0 comments on commit e4c823a

Please sign in to comment.