-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
68 changed files
with
890 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { http, parseEther } from 'viem' | ||
import { celo, mainnet } from 'viem/chains' | ||
import { expectTypeOf, test } from 'vitest' | ||
|
||
import { createConfig } from '../createConfig.js' | ||
import { type EstimateGasParameters, estimateGas } from './estimateGas.js' | ||
|
||
test('chain formatters', () => { | ||
const config = createConfig({ | ||
chains: [mainnet, celo], | ||
transports: { [celo.id]: http(), [mainnet.id]: http() }, | ||
}) | ||
|
||
type Result = EstimateGasParameters<typeof config> | ||
expectTypeOf<Result>().toMatchTypeOf<{ | ||
chainId?: typeof celo.id | typeof mainnet.id | undefined | ||
feeCurrency?: `0x${string}` | undefined | ||
gatewayFee?: bigint | undefined | ||
gatewayFeeRecipient?: `0x${string}` | undefined | ||
}>() | ||
estimateGas(config, { | ||
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B', | ||
value: parseEther('0.01'), | ||
feeCurrency: '0x', | ||
gatewayFee: 100n, | ||
gatewayFeeRecipient: '0x', | ||
}) | ||
|
||
type Result2 = EstimateGasParameters<typeof config, typeof celo.id> | ||
expectTypeOf<Result2>().toMatchTypeOf<{ | ||
feeCurrency?: `0x${string}` | undefined | ||
gatewayFee?: bigint | undefined | ||
gatewayFeeRecipient?: `0x${string}` | undefined | ||
}>() | ||
estimateGas(config, { | ||
chainId: celo.id, | ||
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B', | ||
value: parseEther('0.01'), | ||
feeCurrency: '0x', | ||
gatewayFee: 100n, | ||
gatewayFeeRecipient: '0x', | ||
}) | ||
|
||
type Result3 = EstimateGasParameters<typeof config, typeof mainnet.id> | ||
expectTypeOf<Result3>().not.toMatchTypeOf<{ | ||
feeCurrency?: `0x${string}` | undefined | ||
gatewayFee?: bigint | undefined | ||
gatewayFeeRecipient?: `0x${string}` | undefined | ||
}>() | ||
estimateGas(config, { | ||
chainId: mainnet.id, | ||
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B', | ||
value: parseEther('0.01'), | ||
// @ts-expect-error | ||
feeCurrency: '0x', | ||
gatewayFee: 100n, | ||
gatewayFeeRecipient: '0x', | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { accounts, config, testConnector } from '@wagmi/test' | ||
import { parseEther } from 'viem' | ||
import { expect, test } from 'vitest' | ||
|
||
import { connect } from './connect.js' | ||
import { disconnect } from './disconnect.js' | ||
import { estimateGas } from './estimateGas.js' | ||
|
||
const connector = config._internal.setup(testConnector({ accounts })) | ||
|
||
test('parameters: account', async () => { | ||
await expect( | ||
estimateGas(config, { | ||
account: accounts[0], | ||
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B', | ||
value: parseEther('0.01'), | ||
}), | ||
).resolves.toMatchInlineSnapshot('21000n') | ||
}) | ||
|
||
test('parameters: connector', async () => { | ||
await connect(config, { connector }) | ||
|
||
await expect( | ||
estimateGas(config, { | ||
connector, | ||
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B', | ||
value: parseEther('0.01'), | ||
}), | ||
).resolves.toMatchInlineSnapshot('21000n') | ||
|
||
await disconnect(config, { connector }) | ||
}) | ||
|
||
test('behavior: no account and not connected', async () => { | ||
await expect( | ||
estimateGas(config, { | ||
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B', | ||
value: parseEther('0.01'), | ||
}), | ||
).rejects.toThrowErrorMatchingInlineSnapshot(` | ||
"Connector not found. | ||
Version: @wagmi/core@x.y.z" | ||
`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type { Account, Address, Chain } from 'viem' | ||
import { | ||
type EstimateGasParameters as viem_EstimateGasParameters, | ||
type EstimateGasReturnType as viem_EstimateGasReturnType, | ||
estimateGas as viem_estimateGas, | ||
} from 'viem/actions' | ||
|
||
import { type Config } from '../createConfig.js' | ||
import type { SelectChains } from '../types/chain.js' | ||
import type { | ||
AccountParameter, | ||
ChainIdParameter, | ||
ConnectorParameter, | ||
} from '../types/properties.js' | ||
import { | ||
type OneOf, | ||
type UnionEvaluate, | ||
type UnionLooseOmit, | ||
} from '../types/utils.js' | ||
import { getConnectorClient } from './getConnectorClient.js' | ||
|
||
export type EstimateGasParameters< | ||
config extends Config = Config, | ||
chainId extends | ||
| config['chains'][number]['id'] | ||
| undefined = config['chains'][number]['id'], | ||
/// | ||
chains extends readonly Chain[] = SelectChains<config, chainId>, | ||
> = { | ||
[key in keyof chains]: UnionEvaluate< | ||
UnionLooseOmit< | ||
viem_EstimateGasParameters<chains[key]>, | ||
'account' | 'chain' | ||
> & | ||
ChainIdParameter<config, chainId> & | ||
OneOf<AccountParameter | ConnectorParameter> | ||
> | ||
}[number] | ||
|
||
export type EstimateGasReturnType = viem_EstimateGasReturnType | ||
|
||
export type EstimateGasError = Error | ||
|
||
/** https://alpha.wagmi.sh/core/actions/estimateGas */ | ||
export async function estimateGas< | ||
config extends Config, | ||
chainId extends config['chains'][number]['id'] | undefined = undefined, | ||
>( | ||
config: config, | ||
parameters: EstimateGasParameters<config, chainId>, | ||
): Promise<EstimateGasReturnType> { | ||
const { chainId, connector, ...rest } = parameters | ||
|
||
let account: Address | Account | ||
if (parameters.account) account = parameters.account | ||
else { | ||
const connectorClient = await getConnectorClient(config, { | ||
chainId, | ||
connector, | ||
}) | ||
account = connectorClient.account | ||
} | ||
|
||
const client = config.getClient({ chainId }) | ||
return viem_estimateGas(client, { | ||
...(rest as viem_EstimateGasParameters), | ||
account, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.