Skip to content

Commit

Permalink
feat: dataSuffix (#431)
Browse files Browse the repository at this point in the history
* feat: dataSuffix

* Update src/actions/wallet/writeContract.ts

Co-authored-by: awkweb <[email protected]>

* Update src/actions/public/simulateContract.ts

Co-authored-by: awkweb <[email protected]>

---------

Co-authored-by: awkweb <[email protected]>
  • Loading branch information
jxom and tmm authored Apr 30, 2023
1 parent 41bc9e3 commit 31aafb3
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-parrots-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Added a `dataSuffix` argument to `writeContract` and `simulateContract`
16 changes: 16 additions & 0 deletions site/docs/contract/simulateContract.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,22 @@ const { result } = await publicClient.simulateContract({
})
```

### dataSuffix

- **Type:** `Hex`

Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f).

```ts
const { result } = await publicClient.simulateContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
dataSuffix: '0xdeadbeef' // [!code focus]
})
```

### gasPrice (optional)

- **Type:** `bigint`
Expand Down
16 changes: 16 additions & 0 deletions site/docs/contract/writeContract.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,22 @@ await walletClient.writeContract({
})
```

### dataSuffix

- **Type:** `Hex`

Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f).

```ts
await walletClient.writeContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
dataSuffix: '0xdeadbeef' // [!code focus]
})
```

### gasPrice (optional)

- **Type:** `bigint`
Expand Down
19 changes: 18 additions & 1 deletion src/actions/public/simulateContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* - Custom nonce
*/

import { describe, expect, test } from 'vitest'
import { describe, expect, test, vi } from 'vitest'
import {
accounts,
deployBAYC,
Expand All @@ -21,6 +21,7 @@ import { baycContractConfig } from '../../_test/abis.js'
import { encodeFunctionData, parseEther, parseGwei } from '../../utils/index.js'
import { mine } from '../test/index.js'
import { sendTransaction } from '../wallet/index.js'
import * as call from './call.js'

import { simulateContract } from './simulateContract.js'
import { deployErrorExample } from '../../_test/utils.js'
Expand Down Expand Up @@ -155,6 +156,22 @@ describe('wagmi', () => {
})
})

test('args: dataSuffix', async () => {
const spy = vi.spyOn(call, 'call')
await simulateContract(publicClient, {
...wagmiContractConfig,
account: accounts[0].address,
functionName: 'mint',
dataSuffix: '0x12345678',
})
expect(spy).toHaveBeenCalledWith(publicClient, {
account: accounts[0].address,
batch: false,
data: '0x1249c58b12345678',
to: wagmiContractConfig.address,
})
})

describe('BAYC', () => {
describe('default', () => {
test('mintApe', async () => {
Expand Down
6 changes: 5 additions & 1 deletion src/actions/public/simulateContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
ContractFunctionConfig,
ContractFunctionResult,
GetValue,
Hex,
} from '../../types/index.js'
import {
decodeFunctionResult,
Expand All @@ -29,6 +30,8 @@ export type SimulateContractParameters<
TChainOverride extends Chain | undefined = undefined,
> = {
chain?: TChainOverride
/** Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f). */
dataSuffix?: Hex
} & ContractFunctionConfig<TAbi, TFunctionName, 'payable' | 'nonpayable'> &
Omit<
CallParameters<TChainOverride extends Chain ? TChainOverride : TChain>,
Expand Down Expand Up @@ -99,6 +102,7 @@ export async function simulateContract<
abi,
address,
args,
dataSuffix,
functionName,
...callRequest
}: SimulateContractParameters<TAbi, TFunctionName, TChain, TChainOverride>,
Expand All @@ -116,7 +120,7 @@ export async function simulateContract<
try {
const { data } = await call(client, {
batch: false,
data: calldata,
data: `${calldata}${dataSuffix ? dataSuffix.replace('0x', '') : ''}`,
to: address,
...callRequest,
} as unknown as CallParameters<TChain>)
Expand Down
18 changes: 17 additions & 1 deletion src/actions/wallet/writeContract.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, test } from 'vitest'
import { describe, expect, test, vi } from 'vitest'
import { optimism } from '../../chains.js'
import { createWalletClient, http } from '../../clients/index.js'
import {
Expand All @@ -15,6 +15,7 @@ import { simulateContract } from '../public/index.js'
import { mine } from '../test/index.js'

import { writeContract } from './writeContract.js'
import * as sendTransaction from './sendTransaction.js'

test('default', async () => {
expect(
Expand Down Expand Up @@ -126,6 +127,21 @@ describe('args: chain', () => {
})
})

test('args: dataSuffix', async () => {
const spy = vi.spyOn(sendTransaction, 'sendTransaction')
await writeContract(walletClient, {
...wagmiContractConfig,
account: accounts[0].address,
functionName: 'mint',
dataSuffix: '0x12345678',
})
expect(spy).toHaveBeenCalledWith(walletClient, {
account: accounts[0].address,
data: '0x1249c58b12345678',
to: wagmiContractConfig.address,
})
})

test('overloaded function', async () => {
expect(
await writeContract(walletClient, {
Expand Down
9 changes: 7 additions & 2 deletions src/actions/wallet/writeContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
ContractFunctionConfig,
GetChain,
GetValue,
Hex,
} from '../../types/index.js'
import { encodeFunctionData } from '../../utils/index.js'
import type { EncodeFunctionDataParameters } from '../../utils/index.js'
Expand All @@ -28,7 +29,10 @@ export type WriteContractParameters<
'chain' | 'to' | 'data' | 'value'
> &
GetChain<TChain, TChainOverride> &
GetValue<TAbi, TFunctionName, SendTransactionParameters<TChain>['value']>
GetValue<TAbi, TFunctionName, SendTransactionParameters<TChain>['value']> & {
/** Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f). */
dataSuffix?: Hex
}

export type WriteContractReturnType = SendTransactionReturnType

Expand Down Expand Up @@ -94,6 +98,7 @@ export async function writeContract<
abi,
address,
args,
dataSuffix,
functionName,
...request
}: WriteContractParameters<
Expand All @@ -110,7 +115,7 @@ export async function writeContract<
functionName,
} as unknown as EncodeFunctionDataParameters<TAbi, TFunctionName>)
const hash = await sendTransaction(client, {
data,
data: `${data}${dataSuffix ? dataSuffix.replace('0x', '') : ''}`,
to: address,
...request,
} as unknown as SendTransactionParameters<TChain, TAccount, TChainOverride>)
Expand Down

1 comment on commit 31aafb3

@vercel
Copy link

@vercel vercel bot commented on 31aafb3 Apr 30, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.