Skip to content

Commit

Permalink
parse writeContract errors
Browse files Browse the repository at this point in the history
  • Loading branch information
frolic committed Aug 16, 2024
1 parent 5f350ec commit 4530187
Showing 1 changed file with 54 additions and 12 deletions.
66 changes: 54 additions & 12 deletions src/actions/wallet/writeContract.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import type { Abi } from 'abitype'

import type { Account } from '../../accounts/types.js'
import {
type ParseAccountErrorType,
parseAccount,
} from '../../accounts/utils/parseAccount.js'
import type { Client } from '../../clients/createClient.js'
import type { Transport } from '../../clients/transports/createTransport.js'
import {
AccountNotFoundError,
type AccountNotFoundErrorType,
} from '../../errors/account.js'
import type { BaseError } from '../../errors/base.js'
import type { ErrorType } from '../../errors/utils.js'
import type { GetAccountParameter } from '../../types/account.js'
import type {
Expand All @@ -22,6 +31,10 @@ import {
type EncodeFunctionDataParameters,
encodeFunctionData,
} from '../../utils/abi/encodeFunctionData.js'
import {
type GetContractErrorReturnType,
getContractError,
} from '../../utils/errors/getContractError.js'
import type { FormattedTransactionRequest } from '../../utils/formatters/transactionRequest.js'
import { getAction } from '../../utils/getAction.js'
import type { GetMutabilityAwareValue } from '../public/simulateContract.js'
Expand Down Expand Up @@ -81,7 +94,9 @@ export type WriteContractReturnType = SendTransactionReturnType

export type WriteContractErrorType =
| EncodeFunctionDataErrorType
| SendTransactionErrorType
| AccountNotFoundErrorType
| ParseAccountErrorType
| GetContractErrorReturnType<SendTransactionErrorType>
| ErrorType

/**
Expand Down Expand Up @@ -156,20 +171,47 @@ export async function writeContract<
chainOverride
>,
): Promise<WriteContractReturnType> {
const { abi, address, args, dataSuffix, functionName, ...request } =
parameters as WriteContractParameters
const {
abi,
account: account_ = client.account,
address,
args,
dataSuffix,
functionName,
...request
} = parameters as WriteContractParameters

if (!account_)
throw new AccountNotFoundError({
docsPath: '/docs/actions/wallet/writeContract',
})
const account = parseAccount(account_)

const data = encodeFunctionData({
abi,
args,
functionName,
} as EncodeFunctionDataParameters)
return getAction(
client,
sendTransaction,
'sendTransaction',
)({
data: `${data}${dataSuffix ? dataSuffix.replace('0x', '') : ''}`,
to: address,
...request,
})

try {
return getAction(
client,
sendTransaction,
'sendTransaction',
)({
data: `${data}${dataSuffix ? dataSuffix.replace('0x', '') : ''}`,
to: address,
account,
...request,
})
} catch (error) {
throw getContractError(error as BaseError, {
abi,
address,
args,
docsPath: '/docs/contract/writeContract',
functionName,
sender: account.address,
})
}
}

0 comments on commit 4530187

Please sign in to comment.