Skip to content

Commit

Permalink
wip: send transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Jun 30, 2023
1 parent e79852c commit 4d1bd3a
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 70 deletions.
59 changes: 8 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,53 +38,17 @@

<br>

## Features

- 🚀 20+ hooks for working with wallets, ENS, contracts, transactions, signing, etc.
- 💼 Built-in wallet connectors for MetaMask, WalletConnect, Coinbase Wallet, Injected, and more
- 👟 Caching, request deduplication, multicall, batching, and persistence
- 🌀 Auto-refresh data on wallet, block, and network changes
- 🦄 TypeScript ready (infer types from ABIs and EIP-712 Typed Data)
- 📦 Command-line interface for managing ABIs and code generation
- 🌳 Test suite running against forked Ethereum network

...and a lot more.

## Documentation

For full documentation and examples, visit [wagmi.sh](https://wagmi.sh).

## Installation

Install wagmi and its [viem](https://viem.sh) peer dependency.

```bash
pnpm add wagmi viem
```

```bash
npm install wagmi viem
```

```bash
yarn add wagmi viem
```

## Community

Check out the following places for more wagmi-related content:
## Contributing

- Join the [discussions on GitHub](https://github.com/wagmi-dev/wagmi/discussions)
- Follow [@wagmi_sh](https://twitter.com/wagmi_sh) on Twitter for project updates
- Share [your project/organization](https://github.com/wagmi-dev/wagmi/discussions/201) using wagmi
- Browse the [awesome-wagmi](https://github.com/wagmi-dev/awesome-wagmi) list of awesome projects and resources
Check out the [contributing docs][contributing] to get started.

## Support

If you find wagmi useful, please consider supporting development. Thank you 🙏

- [GitHub Sponsors](https://github.com/sponsors/wagmi-dev?metadata_campaign=gh_readme_support)
- [wagmi-dev.eth](https://etherscan.io/enslookup-search?search=wagmi-dev.eth)
If you find wagmi useful, please consider [supporting development](https://github.com/sponsors/wagmi-dev?metadata_campaign=gh_readme_support). Thank you 🙏

## Sponsors

Expand Down Expand Up @@ -182,23 +146,16 @@ If you find wagmi useful, please consider supporting development. Thank you 🙏
</picture>
</a>

## Contributing

If you're interested in contributing, please read the [contributing docs](/.github/CONTRIBUTING.md) **before submitting a pull request**.

## Authors

- [@tmm](https://github.com/tmm) (awkweb.eth, [Twitter](https://twitter.com/awkweb))
- [@jxom](https://github.com/jxom) (moxey.eth, [Twitter](https://twitter.com/jakemoxey))

Thanks to julianhutton.eth ([@julianjhutton](https://twitter.com/julianjhutton)) for providing the awesome logo!

## License

[MIT](/LICENSE) License
[MIT][license]

<br />

<a href="https://vercel.com/?utm_source=wagmi-dev&utm_campaign=oss">
<img src="https://www.datocms-assets.com/31049/1618983297-powered-by-vercel.svg" alt="Powered by Vercel" height="35">
</a>

[docs]: https://wagmi.sh
[contributing]: https://github.com/wagmi-dev/wagmi/blob/main/.github/CONTRIBUTING.md
[license]: https://github.com/wagmi-dev/wagmi/blob/main/LICENSE
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"rome": "^12.1.0",
"simple-git-hooks": "^2.8.1",
"typescript": "^5.0.4",
"viem": "0.0.0-w-20230630153145",
"viem": "1.2.7",
"vite": "^4.3.2",
"vitest": "^0.31.0"
},
Expand All @@ -55,7 +55,7 @@
"@wagmi/core": "workspace:*",
"remark-shiki-twoslash>shiki": "^0.14.1",
"shiki-twoslash>shiki": "^0.14.1",
"viem": "0.0.0-w-20230630153145"
"viem": "1.2.7"
},
"patchedDependencies": {
"@coinbase/wallet-sdk@3.7.1": "patches/@coinbase__wallet-sdk@3.7.1.patch",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('behavior: user rejected request', async () => {
[UserRejectedRequestError: User rejected the request.
Details: Failed to connect.
Version: viem@1.2.6]
Version: viem@1.2.7]
`)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/getToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ test('behavior: bogus token', async () => {
function: decimals()
Docs: https://viem.sh/docs/contract/multicall.html
Version: viem@1.2.6"
Version: viem@1.2.7"
`)
})
2 changes: 1 addition & 1 deletion packages/core/src/actions/prepareSendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function prepareSendTransaction<
config extends Config,
chainId extends config['chains'][number]['id'] | undefined,
>(
config: Config,
config: config,
parameters: PrepareSendTransactionParameters<config, chainId>,
): Promise<PrepareSendTransactionReturnType<config, chainId>> {
const { chainId, ...rest } = parameters
Expand Down
35 changes: 35 additions & 0 deletions packages/core/src/actions/sendTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { config } from '@wagmi/test'
import { parseEther } from 'viem'
import { expect, test } from 'vitest'

import { connect } from './connect.js'
import { disconnect } from './disconnect.js'
import { prepareSendTransaction } from './prepareSendTransaction.js'
import { sendTransaction } from './sendTransaction.js'

const connector = config.connectors[0]!

test('default', async () => {
await connect(config, { connector })
await expect(
sendTransaction(config, {
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
value: parseEther('0.01'),
}),
).resolves.toMatchObject({
hash: expect.any(String),
})
await disconnect(config, { connector })
})

test('default', async () => {
await connect(config, { connector })
const result = await prepareSendTransaction(config, {
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
value: parseEther('0.01'),
})
await expect(sendTransaction(config, result)).resolves.toMatchObject({
hash: expect.any(String),
})
await disconnect(config, { connector })
})
70 changes: 70 additions & 0 deletions packages/core/src/actions/sendTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type {
Account,
Chain,
SendTransactionParameters as viem_SendTransactionParameters,
SendTransactionReturnType as viem_SendTransactionReturnType,
} from 'viem'
import { sendTransaction as viem_sendTransaction } from 'viem/actions'

import type { Config } from '../config.js'
import { ConnectorNotFoundError } from '../errors/config.js'
import type { ChainId } from '../types/properties.js'
import type { Evaluate } from '../types/utils.js'
import { assertActiveChain } from '../utils/assertActiveChain.js'
import { getConnectorClient } from './getConnectorClient.js'

export type SendTransactionParameters<
config extends Config = Config,
chainId extends
| config['chains'][number]['id']
| undefined = config['chains'][number]['id'],
> = Evaluate<
Omit<
viem_SendTransactionParameters<
Extract<
config['chains'][number],
{ id: chainId }
> extends infer chain extends Chain
? chain
: config['chains'][number],
Account
>,
'account' | 'chain'
> &
ChainId<config, chainId> & {
mode?: 'prepared'
}
>

export type SendTransactionReturnType = {
hash: viem_SendTransactionReturnType
}

export type SendTransactionError = Error

export async function sendTransaction<
config extends Config,
chainId extends config['chains'][number]['id'] | undefined,
>(
config: config,
parameters: SendTransactionParameters<config, chainId>,
): Promise<SendTransactionReturnType>

/** https://wagmi.sh/core/actions/sendTransaction */
export async function sendTransaction(
config: Config,
parameters: SendTransactionParameters,
): Promise<SendTransactionReturnType> {
const { chainId, ...rest } = parameters

const client = await getConnectorClient(config, { chainId })
if (!client) throw new ConnectorNotFoundError()
if (chainId) assertActiveChain(config, { chainId })

const hash = await viem_sendTransaction(client, {
...(rest as viem_SendTransactionParameters),
chain: null,
})

return { hash }
}
2 changes: 1 addition & 1 deletion packages/core/src/actions/signMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test('behavior: user rejected request', async () => {
[UserRejectedRequestError: User rejected the request.
Details: Failed to sign message.
Version: viem@1.2.6]
Version: viem@1.2.7]
`)
await disconnect(config, { connector: connector_ })
})
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/switchChain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test('behavior: user rejected request', async () => {
[UserRejectedRequestError: User rejected the request.
Details: Failed to switch chain.
Version: viem@1.2.6]
Version: viem@1.2.7]
`)
await disconnect(config, { connector: connector_ })
})
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/query/prepareSendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function prepareSendTransactionQueryOptions<
config extends Config,
chainId extends config['chains'][number]['id'] | undefined,
>(
config: Config,
config: config,
options: PrepareSendTransactionOptions<
config,
chainId
Expand Down
25 changes: 14 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 4d1bd3a

@vercel
Copy link

@vercel vercel bot commented on 4d1bd3a Jun 30, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

wagmi-v2 – ./docs

wagmi-v2.vercel.app
wagmi-v2-wagmi-dev.vercel.app
wagmi-v2-git-tmm-v2-wagmi-dev.vercel.app

Please sign in to comment.