Skip to content

Commit

Permalink
chore: up exports
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Sep 6, 2023
1 parent 6938c15 commit a32d973
Show file tree
Hide file tree
Showing 68 changed files with 890 additions and 238 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"rimraf": "^4.4.1",
"simple-git-hooks": "^2.8.1",
"typescript": "5.0.4",
"viem": "0.0.0-alpha-20230904204032",
"viem": "0.0.0-alpha-20230906150254",
"vite": "^4.3.9",
"vitest": "^0.34.1"
},
Expand All @@ -61,7 +61,7 @@
"overrides": {
"@wagmi/connectors": "workspace:*",
"@wagmi/core": "workspace:*",
"viem": "0.0.0-alpha-20230904204032"
"viem": "0.0.0-alpha-20230906150254"
},
"peerDependencyRules": {
"ignoreMissing": [
Expand Down
10 changes: 1 addition & 9 deletions packages/connectors/src/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export function injected(parameters: InjectedParameters = {}) {
const windowProvider = getWindowProvider()
if (typeof windowProvider.provider === 'function')
return windowProvider.provider(window as Window | undefined)
return findProvider(window as Window | undefined, 'isCoinbaseWallet')
return findProvider(window as Window | undefined, () => true)
},
async isAuthorized() {
try {
Expand Down Expand Up @@ -391,14 +391,6 @@ export function injected(parameters: InjectedParameters = {}) {
config.emitter.emit('change', { chainId })
},
async onConnect(connectInfo) {
// If `connect` event fires and wallet is explicitly disconnected, ignore.
if (
shimDisconnect &&
// If shim does not exist in storage, wallet is disconnected
!config.storage?.getItem(this.shimDisconnectStorageKey)
)
return

const accounts = await this.getAccounts()
if (accounts.length === 0) return

Expand Down
2 changes: 1 addition & 1 deletion packages/connectors/src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function ledger(parameters: LedgerParameters = {}) {
async disconnect() {
const provider = await this.getProvider()
try {
await provider?.disconnect?.()
await provider?.disconnect?.().catch(() => {})
} catch (error) {
console.log({ error })
if (!/No matching key/i.test((error as Error).message)) throw error
Expand Down
23 changes: 14 additions & 9 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,30 @@
"types": "./dist/types/index.d.ts",
"default": "./dist/esm/index.js"
},
"./actions": {
"types": "./dist/types/exports/actions.d.ts",
"default": "./dist/esm/exports/actions.js"
},
"./chains": {
"types": "./dist/types/chains.d.ts",
"default": "./dist/esm/chains.js"
"types": "./dist/types/exports/chains.d.ts",
"default": "./dist/esm/exports/chains.js"
},
"./internal": {
"types": "./dist/types/internal.d.ts",
"default": "./dist/esm/internal.js"
"types": "./dist/types/exports/internal.d.ts",
"default": "./dist/esm/exports/internal.js"
},
"./query": {
"types": "./dist/types/query.d.ts",
"default": "./dist/esm/query.js"
"types": "./dist/types/exports/query.d.ts",
"default": "./dist/esm/exports/query.js"
},
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"chains": ["./dist/types/chains.d.ts"],
"internal": ["./dist/types/internal.d.ts"],
"query": ["./dist/types/query.d.ts"]
"actions": ["./dist/types/exports/actions.d.ts"],
"chains": ["./dist/types/exports/chains.d.ts"],
"internal": ["./dist/types/exports/internal.d.ts"],
"query": ["./dist/types/exports/query.d.ts"]
}
},
"peerDependencies": {
Expand Down
59 changes: 59 additions & 0 deletions packages/core/src/actions/estimateGas.test-d.ts
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',
})
})
46 changes: 46 additions & 0 deletions packages/core/src/actions/estimateGas.test.ts
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"
`)
})
69 changes: 69 additions & 0 deletions packages/core/src/actions/estimateGas.ts
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,
})
}
13 changes: 6 additions & 7 deletions packages/core/src/actions/sendTransaction.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ test('chain formatters', () => {
})

type Result = SendTransactionParameters<typeof config>
expectTypeOf<Result>().toMatchTypeOf<
{ chainId?: typeof celo.id | typeof mainnet.id | undefined } & {
feeCurrency?: `0x${string}` | undefined
gatewayFee?: bigint | undefined
gatewayFeeRecipient?: `0x${string}` | undefined
}
>()
expectTypeOf<Result>().toMatchTypeOf<{
chainId?: typeof celo.id | typeof mainnet.id | undefined
feeCurrency?: `0x${string}` | undefined
gatewayFee?: bigint | undefined
gatewayFeeRecipient?: `0x${string}` | undefined
}>()
sendTransaction(config, {
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
value: parseEther('0.01'),
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/actions/simulateContract.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ test('chain formatters', () => {
gatewayFeeRecipient?: `0x${string}` | undefined
}>()
simulateContract(config, {
account: '0x',
address: '0x',
abi: abi.erc20,
functionName: 'transferFrom',
Expand All @@ -87,6 +88,7 @@ test('chain formatters', () => {
}>()
simulateContract(config, {
chainId: celo.id,
account: '0x',
address: '0x',
abi: abi.erc20,
functionName: 'transferFrom',
Expand Down Expand Up @@ -114,6 +116,7 @@ test('chain formatters', () => {
}>()
simulateContract(config, {
chainId: mainnet.id,
account: '0x',
address: '0x',
abi: abi.erc20,
functionName: 'transferFrom',
Expand Down
40 changes: 38 additions & 2 deletions packages/core/src/actions/simulateContract.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { abi, address, config } from '@wagmi/test'
import { abi, accounts, address, config } from '@wagmi/test'
import { expect, test } from 'vitest'

import { connect } from './connect.js'
Expand All @@ -7,13 +7,48 @@ import { simulateContract } from './simulateContract.js'

const connector = config.connectors[0]!

test('default', async () => {
test('parameters: account', async () => {
await expect(
simulateContract(config, {
account: accounts[0],
address: address.wagmiMintExample,
abi: abi.wagmiMintExample,
functionName: 'mint',
}),
).resolves.toMatchInlineSnapshot(`
{
"request": {
"__mode": "prepared",
"abi": [
{
"inputs": [],
"name": "mint",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function",
},
],
"account": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"address": "0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2",
"args": undefined,
"chainId": undefined,
"dataSuffix": undefined,
"functionName": "mint",
},
"result": undefined,
}
`)
})

test('parameters: connector', async () => {
await connect(config, { connector })

await expect(
simulateContract(config, {
address: address.wagmiMintExample,
abi: abi.wagmiMintExample,
functionName: 'mint',
connector,
}),
).resolves.toMatchInlineSnapshot(`
{
Expand Down Expand Up @@ -41,5 +76,6 @@ test('default', async () => {
"result": undefined,
}
`)

await disconnect(config, { connector })
})
Loading

0 comments on commit a32d973

Please sign in to comment.