Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove simulation #4417

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions .changeset/swift-terms-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@wagmi/connectors": minor
"wagmi": minor
"@wagmi/core": minor
"@wagmi/vue": minor
---

Removed simulation in `writeContract` & `sendTransaction`.
2 changes: 0 additions & 2 deletions packages/connectors/src/coinbaseWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ function version4(parameters: Version4Parameters) {
id: 'coinbaseWalletSDK',
name: 'Coinbase Wallet',
rdns: 'com.coinbase.wallet',
supportsSimulation: true,
type: coinbaseWallet.type,
async connect({ chainId } = {}) {
try {
Expand Down Expand Up @@ -327,7 +326,6 @@ function version3(parameters: Version3Parameters) {
return createConnector<Provider>((config) => ({
id: 'coinbaseWalletSDK',
name: 'Coinbase Wallet',
supportsSimulation: true,
type: coinbaseWallet.type,
async connect({ chainId } = {}) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ test('default', async () => {
expectTypeOf(result).toMatchTypeOf<{
result: boolean
request: {
__mode: 'prepared'
chainId: 1
abi: readonly [
{
Expand Down Expand Up @@ -126,7 +125,6 @@ test('functionName', async () => {
expectTypeOf(result).toMatchTypeOf<{
result: boolean
request: {
__mode: 'prepared'
chainId: 1
abi: readonly [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ test('default', async () => {
{
"chainId": 1,
"request": {
"__mode": "prepared",
"abi": [
{
"inputs": [],
Expand Down Expand Up @@ -70,7 +69,6 @@ test('multichain', async () => {
{
"chainId": 456,
"request": {
"__mode": "prepared",
"abi": [
{
"inputs": [],
Expand Down Expand Up @@ -112,7 +110,6 @@ test('functionName', async () => {
{
"chainId": 1,
"request": {
"__mode": "prepared",
"abi": [
{
"inputs": [],
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/codegen/createWriteContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export type CreateWriteContractReturnType<
| undefined
}
: Compute<ChainIdParameter<config, chainId>>) &
ConnectorParameter & { __mode?: 'prepared' }
Copy link
Member

Choose a reason for hiding this comment

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

Might be good to leave __mode here and just add a @deprecated comment so people that upgrade don't experience unexpected type errors if they were passing this in directly (to skip simulation).

ConnectorParameter
>,
) => Promise<WriteContractReturnType>

Expand Down
35 changes: 2 additions & 33 deletions packages/core/src/actions/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import type {
SendTransactionParameters as viem_SendTransactionParameters,
SendTransactionReturnType as viem_SendTransactionReturnType,
} from 'viem'
import {
estimateGas as viem_estimateGas,
sendTransaction as viem_sendTransaction,
} from 'viem/actions'
import { sendTransaction as viem_sendTransaction } from 'viem/actions'

import type { Config } from '../createConfig.js'
import type { BaseErrorType, ErrorType } from '../errors/base.js'
Expand All @@ -21,7 +18,6 @@ import type {
} from '../types/properties.js'
import type { Compute } from '../types/utils.js'
import { getAction } from '../utils/getAction.js'
import { getAccount } from './getAccount.js'
import {
type GetConnectorClientErrorType,
getConnectorClient,
Expand Down Expand Up @@ -66,7 +62,7 @@ export async function sendTransaction<
config: config,
parameters: SendTransactionParameters<config, chainId>,
): Promise<SendTransactionReturnType> {
const { account, chainId, connector, gas: gas_, ...rest } = parameters
const { account, chainId, connector, ...rest } = parameters

let client: Client
if (typeof account === 'object' && account?.type === 'local')
Expand All @@ -78,37 +74,10 @@ export async function sendTransaction<
connector,
})

const { connector: activeConnector } = getAccount(config)

const gas = await (async () => {
// Skip gas estimation if `data` doesn't exist (not a contract interaction).
if (!('data' in parameters) || !parameters.data) return undefined

// Skip gas estimation if connector supports simulation.
if ((connector ?? activeConnector)?.supportsSimulation) return undefined

// Skip gas estimation if `null` is provided.
if (gas_ === null) return undefined

// Run gas estimation if no value is provided.
if (gas_ === undefined) {
const action = getAction(client, viem_estimateGas, 'estimateGas')
return action({
...(rest as any),
account,
chain: chainId ? { id: chainId } : null,
})
}

// Use provided gas value.
return gas_
})()

const action = getAction(client, viem_sendTransaction, 'sendTransaction')
const hash = await action({
...(rest as any),
...(account ? { account } : {}),
gas,
chain: chainId ? { id: chainId } : null,
})

Expand Down
1 change: 0 additions & 1 deletion packages/core/src/actions/simulateContract.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ test('default', async () => {
expectTypeOf(response).toMatchTypeOf<{
result: boolean
request: {
__mode: 'prepared'
chainId: 1
abi: readonly [
{
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/actions/simulateContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ test('parameters: account', async () => {
{
"chainId": 1,
"request": {
"__mode": "prepared",
"abi": [
{
"inputs": [],
Expand Down Expand Up @@ -58,7 +57,6 @@ test('parameters: connector', async () => {
{
"chainId": 1,
"request": {
"__mode": "prepared",
"abi": [
{
"inputs": [],
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/actions/simulateContract.ts
Copy link
Member

Choose a reason for hiding this comment

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

Probably good to keep and deprecate __mode here too.

Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export type SimulateContractReturnType<
chainId: chains[key]['id']
request: Compute<
PartialBy<
{ __mode: 'prepared'; chainId: chainId; chain: chains[key] },
{ chainId: chainId; chain: chains[key] },
chainId extends config['chains'][number]['id'] ? never : 'chainId'
>
>
Expand Down Expand Up @@ -155,7 +155,7 @@ export async function simulateContract<
return {
chainId: client.chain.id,
result,
request: { __mode: 'prepared', ...request, chainId },
request: { ...request, chainId },
} as unknown as SimulateContractReturnType<
abi,
functionName,
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/actions/writeContract.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ test('simulateContract', async () => {
})
await writeContract(config, request)
await writeContract(config, {
__mode: 'prepared',
address: '0x',
abi: abi.erc20,
functionName: 'transferFrom',
Expand Down
27 changes: 3 additions & 24 deletions packages/core/src/actions/writeContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ import type {
} from '../types/properties.js'
import type { Compute, UnionCompute } from '../types/utils.js'
import { getAction } from '../utils/getAction.js'
import { getAccount } from './getAccount.js'
import {
type GetConnectorClientErrorType,
getConnectorClient,
} from './getConnectorClient.js'
import {
type SimulateContractErrorType,
simulateContract,
} from './simulateContract.js'

export type WriteContractParameters<
abi extends Abi | readonly unknown[] = Abi,
Expand Down Expand Up @@ -64,16 +59,14 @@ export type WriteContractParameters<
>
}[number] &
Compute<ChainIdParameter<config, chainId>> &
ConnectorParameter & { __mode?: 'prepared' }
ConnectorParameter
>

export type WriteContractReturnType = viem_WriteContractReturnType

export type WriteContractErrorType =
// getConnectorClient()
| GetConnectorClientErrorType
// simulateContract()
| SimulateContractErrorType
// base
| BaseErrorType
| ErrorType
Expand All @@ -95,7 +88,7 @@ export async function writeContract<
config: config,
parameters: WriteContractParameters<abi, functionName, args, config, chainId>,
): Promise<WriteContractReturnType> {
const { account, chainId, connector, __mode, ...rest } = parameters
const { account, chainId, connector, ...request } = parameters

let client: Client
if (typeof account === 'object' && account?.type === 'local')
Expand All @@ -107,23 +100,9 @@ export async function writeContract<
connector,
})

const { connector: activeConnector } = getAccount(config)

let request: any
if (__mode === 'prepared' || activeConnector?.supportsSimulation)
request = rest
else {
const { request: simulateRequest } = await simulateContract(config, {
...rest,
account,
chainId,
} as any)
request = simulateRequest
}

const action = getAction(client, viem_writeContract, 'writeContract')
const hash = await action({
...request,
...(request as any),
...(account ? { account } : {}),
chain: chainId ? { id: chainId } : null,
})
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/connectors/createConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type CreateConnectorFn<
readonly id: string
readonly name: string
readonly rdns?: string | undefined
/** @deprecated */
readonly supportsSimulation?: boolean | undefined
readonly type: string

Expand Down
6 changes: 0 additions & 6 deletions packages/core/src/connectors/injected.ts
Copy link
Member

Choose a reason for hiding this comment

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

I'd keep this around too

Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ export type InjectedParameters = {
unstable_shimAsyncInject?: boolean | number | undefined
}

// Regex of wallets/providers that can accurately simulate contract calls & display contract revert reasons.
const supportsSimulationIdRegex = /(rabby|trustwallet)/

injected.type = 'injected' as const
export function injected(parameters: InjectedParameters = {}) {
const { shimDisconnect = true, unstable_shimAsyncInject } = parameters
Expand Down Expand Up @@ -91,9 +88,6 @@ export function injected(parameters: InjectedParameters = {}) {
get name() {
return getTarget().name
},
get supportsSimulation() {
return supportsSimulationIdRegex.test(this.id.toLowerCase())
},
type: injected.type,
async setup() {
const provider = await this.getProvider()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ test('default', () => {
| {
result: boolean
request: {
__mode: 'prepared'
chainId: 123
abi: readonly [
{
Expand Down Expand Up @@ -144,7 +143,6 @@ test('functionName', () => {
| {
result: boolean
request: {
__mode: 'prepared'
chainId: 123
abi: readonly [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ test('default', async () => {
"data": {
"chainId": 1,
"request": {
"__mode": "prepared",
"abi": [
{
"inputs": [],
Expand Down Expand Up @@ -116,7 +115,6 @@ test('multichain', async () => {
"data": {
"chainId": 456,
"request": {
"__mode": "prepared",
"abi": [
{
"inputs": [],
Expand Down Expand Up @@ -197,7 +195,6 @@ test('functionName', async () => {
"data": {
"chainId": 1,
"request": {
"__mode": "prepared",
"abi": [
{
"inputs": [],
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/codegen/createUseWriteContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,5 @@ type Variables<
| undefined
}
: Compute<ChainIdParameter<config, chainId>>) &
ConnectorParameter & { __mode?: 'prepared' }
ConnectorParameter
>
2 changes: 0 additions & 2 deletions packages/react/src/hooks/useSimulateContract.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ test('default', () => {
| {
result: boolean
request: {
__mode: 'prepared'
chainId?: undefined
abi: readonly [
{
Expand Down Expand Up @@ -82,7 +81,6 @@ test('UseSimulateContractReturnType', () => {
| {
result: boolean
request: {
__mode: 'prepared'
chainId: number
abi: readonly [
{
Expand Down
1 change: 0 additions & 1 deletion packages/react/src/hooks/useSimulateContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ test('default', async () => {
"data": {
"chainId": 1,
"request": {
"__mode": "prepared",
"abi": [
{
"inputs": [],
Expand Down
Loading