viem 0.0.1-alpha.16 → 0.0.1-alpha.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chains.d.ts +1 -1
- package/dist/chains.js +5 -5
- package/dist/chains.mjs +1 -1
- package/dist/{chunk-LQXQPPTU.js → chunk-BL2ZZ7UN.js} +14 -14
- package/dist/{chunk-LQXQPPTU.js.map → chunk-BL2ZZ7UN.js.map} +0 -0
- package/dist/{chunk-4XREGFHD.js → chunk-EOY65E5E.js} +36 -13
- package/dist/chunk-EOY65E5E.js.map +1 -0
- package/dist/{chunk-WTXKCAG7.js → chunk-EVOCIEHK.js} +109 -53
- package/dist/chunk-EVOCIEHK.js.map +1 -0
- package/dist/{chunk-TSJ3OOJW.mjs → chunk-HF44N2P3.mjs} +2 -2
- package/dist/{chunk-TSJ3OOJW.mjs.map → chunk-HF44N2P3.mjs.map} +0 -0
- package/dist/{chunk-KCMYVU3Z.mjs → chunk-JNAHFK7Z.mjs} +26 -3
- package/dist/chunk-JNAHFK7Z.mjs.map +1 -0
- package/dist/{chunk-XJKOJIX3.js → chunk-KCXMY4FZ.js} +12 -12
- package/dist/{chunk-XJKOJIX3.js.map → chunk-KCXMY4FZ.js.map} +0 -0
- package/dist/{chunk-KM6AFT2K.mjs → chunk-KQI5IRRL.mjs} +2 -2
- package/dist/{chunk-KM6AFT2K.mjs.map → chunk-KQI5IRRL.mjs.map} +0 -0
- package/dist/{chunk-B7A2CAHU.mjs → chunk-KWHCOWNW.mjs} +63 -7
- package/dist/chunk-KWHCOWNW.mjs.map +1 -0
- package/dist/{chunk-U7QDLGQL.mjs → chunk-WQFS6KDQ.mjs} +2 -2
- package/dist/{chunk-U7QDLGQL.mjs.map → chunk-WQFS6KDQ.mjs.map} +0 -0
- package/dist/{chunk-EWTLCB3N.js → chunk-XEGTPY6M.js} +2 -2
- package/dist/{chunk-EWTLCB3N.js.map → chunk-XEGTPY6M.js.map} +0 -0
- package/dist/clients/index.d.ts +7 -7
- package/dist/clients/index.js +3 -3
- package/dist/clients/index.mjs +2 -2
- package/dist/{createClient-cd948138.d.ts → createClient-aadeff37.d.ts} +1 -1
- package/dist/{createPublicClient-989a0556.d.ts → createPublicClient-88f35518.d.ts} +2 -2
- package/dist/{createTestClient-81507f58.d.ts → createTestClient-fbf66ec2.d.ts} +2 -2
- package/dist/{createWalletClient-43f801b9.d.ts → createWalletClient-b13dabd6.d.ts} +2 -2
- package/dist/{eip1193-4330b722.d.ts → eip1193-3a40c941.d.ts} +9 -6
- package/dist/index.d.ts +47 -16
- package/dist/index.js +10 -6
- package/dist/index.mjs +13 -9
- package/dist/{parseGwei-f2d23de6.d.ts → parseGwei-e6f49667.d.ts} +2 -2
- package/dist/public.d.ts +9 -9
- package/dist/public.js +4 -4
- package/dist/public.mjs +5 -5
- package/dist/{rpc-b77c5aee.d.ts → rpc-858670f1.d.ts} +12 -1
- package/dist/{sendTransaction-7a9d241a.d.ts → sendTransaction-bd109cd4.d.ts} +3 -3
- package/dist/{stopImpersonatingAccount-8113150e.d.ts → stopImpersonatingAccount-6603ebdd.d.ts} +2 -2
- package/dist/test.d.ts +5 -5
- package/dist/test.js +3 -3
- package/dist/test.mjs +2 -2
- package/dist/{transactionReceipt-5d332aab.d.ts → transactionReceipt-86c1094c.d.ts} +30 -4
- package/dist/{transactionRequest-327eb7c2.d.ts → transactionRequest-8e970b0e.d.ts} +1 -1
- package/dist/utils/index.d.ts +5 -5
- package/dist/utils/index.js +2 -2
- package/dist/utils/index.mjs +1 -1
- package/dist/wallet.d.ts +7 -7
- package/dist/wallet.js +3 -3
- package/dist/wallet.mjs +2 -2
- package/dist/{watchAsset-0088384c.d.ts → watchAsset-7ef25553.d.ts} +3 -3
- package/dist/{watchPendingTransactions-670a7ca3.d.ts → watchPendingTransactions-2563071e.d.ts} +26 -11
- package/dist/{webSocket-9a3b0b26.d.ts → webSocket-2a77cdb3.d.ts} +2 -2
- package/dist/window.d.ts +2 -2
- package/package.json +1 -1
- package/src/actions/index.test.ts +3 -1
- package/src/actions/index.ts +9 -3
- package/src/actions/public/createEventFilter.ts +2 -4
- package/src/actions/public/deployContract.test.ts +8 -8
- package/src/actions/public/getLogs.test.ts +105 -0
- package/src/actions/public/getLogs.ts +83 -0
- package/src/actions/public/index.test.ts +2 -1
- package/src/actions/public/index.ts +8 -5
- package/src/actions/public/simulateContract.bench.ts +31 -0
- package/src/actions/public/simulateContract.test.ts +238 -0
- package/src/actions/public/{callContract.ts → simulateContract.ts} +34 -12
- package/src/actions/wallet/index.test.ts +1 -0
- package/src/actions/wallet/index.ts +3 -0
- package/src/actions/wallet/writeContract.test.ts +54 -0
- package/src/actions/wallet/writeContract.ts +61 -0
- package/src/index.test.ts +3 -1
- package/src/index.ts +9 -3
- package/src/public.ts +3 -3
- package/src/types/eip1193.ts +9 -6
- package/src/types/index.ts +1 -1
- package/src/types/misc.ts +1 -0
- package/dist/chunk-4XREGFHD.js.map +0 -1
- package/dist/chunk-B7A2CAHU.mjs.map +0 -1
- package/dist/chunk-KCMYVU3Z.mjs.map +0 -1
- package/dist/chunk-WTXKCAG7.js.map +0 -1
- package/src/actions/public/callContract.bench.ts +0 -24
- package/src/actions/public/callContract.test.ts +0 -285
@@ -0,0 +1,238 @@
|
|
1
|
+
/**
|
2
|
+
* TODO: Heaps more test cases :D
|
3
|
+
* - Complex calldata types
|
4
|
+
* - Complex return types (tuple/structs)
|
5
|
+
* - EIP-1559
|
6
|
+
* - Calls against blocks
|
7
|
+
* - Custom chain types
|
8
|
+
* - Custom nonce
|
9
|
+
*/
|
10
|
+
|
11
|
+
import { describe, expect, test } from 'vitest'
|
12
|
+
import {
|
13
|
+
accounts,
|
14
|
+
publicClient,
|
15
|
+
testClient,
|
16
|
+
wagmiContractConfig,
|
17
|
+
walletClient,
|
18
|
+
} from '../../_test'
|
19
|
+
import { baycContractConfig } from '../../_test/abis'
|
20
|
+
import { encodeFunctionData } from '../../utils'
|
21
|
+
import { mine } from '../test'
|
22
|
+
import { sendTransaction } from '../wallet'
|
23
|
+
|
24
|
+
import { deployContract } from './deployContract'
|
25
|
+
import { getTransactionReceipt } from './getTransactionReceipt'
|
26
|
+
import { simulateContract } from './simulateContract'
|
27
|
+
|
28
|
+
describe('wagmi', () => {
|
29
|
+
test('default', async () => {
|
30
|
+
expect(
|
31
|
+
(
|
32
|
+
await simulateContract(publicClient, {
|
33
|
+
...wagmiContractConfig,
|
34
|
+
from: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
|
35
|
+
functionName: 'mint',
|
36
|
+
args: [69420n],
|
37
|
+
})
|
38
|
+
).result,
|
39
|
+
).toEqual(undefined)
|
40
|
+
expect(
|
41
|
+
(
|
42
|
+
await simulateContract(publicClient, {
|
43
|
+
...wagmiContractConfig,
|
44
|
+
functionName: 'safeTransferFrom',
|
45
|
+
from: '0x1a1E021A302C237453D3D45c7B82B19cEEB7E2e6',
|
46
|
+
args: [
|
47
|
+
'0x1a1E021A302C237453D3D45c7B82B19cEEB7E2e6',
|
48
|
+
'0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
|
49
|
+
1n,
|
50
|
+
],
|
51
|
+
})
|
52
|
+
).result,
|
53
|
+
).toEqual(undefined)
|
54
|
+
})
|
55
|
+
|
56
|
+
test('revert', async () => {
|
57
|
+
await expect(() =>
|
58
|
+
simulateContract(publicClient, {
|
59
|
+
...wagmiContractConfig,
|
60
|
+
functionName: 'approve',
|
61
|
+
args: ['0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', 420n],
|
62
|
+
from: accounts[0].address,
|
63
|
+
}),
|
64
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(`
|
65
|
+
"ERC721: approval to current owner
|
66
|
+
|
67
|
+
Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
|
68
|
+
Contract: 0x0000000000000000000000000000000000000000
|
69
|
+
Function: approve(address to, uint256 tokenId)
|
70
|
+
Arguments: (0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC, 420)
|
71
|
+
|
72
|
+
Details: execution reverted: ERC721: approval to current owner
|
73
|
+
Version: viem@1.0.2"
|
74
|
+
`)
|
75
|
+
await expect(() =>
|
76
|
+
simulateContract(publicClient, {
|
77
|
+
...wagmiContractConfig,
|
78
|
+
functionName: 'mint',
|
79
|
+
args: [1n],
|
80
|
+
from: accounts[0].address,
|
81
|
+
}),
|
82
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(`
|
83
|
+
"Token ID is taken
|
84
|
+
|
85
|
+
Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
|
86
|
+
Contract: 0x0000000000000000000000000000000000000000
|
87
|
+
Function: mint(uint256 tokenId)
|
88
|
+
Arguments: (1)
|
89
|
+
|
90
|
+
Details: execution reverted: Token ID is taken
|
91
|
+
Version: viem@1.0.2"
|
92
|
+
`)
|
93
|
+
await expect(() =>
|
94
|
+
simulateContract(publicClient, {
|
95
|
+
...wagmiContractConfig,
|
96
|
+
functionName: 'safeTransferFrom',
|
97
|
+
from: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
|
98
|
+
args: [
|
99
|
+
'0x1a1E021A302C237453D3D45c7B82B19cEEB7E2e6',
|
100
|
+
'0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
|
101
|
+
1n,
|
102
|
+
],
|
103
|
+
}),
|
104
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(`
|
105
|
+
"ERC721: transfer caller is not owner nor approved
|
106
|
+
|
107
|
+
Sender: 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC
|
108
|
+
Contract: 0x0000000000000000000000000000000000000000
|
109
|
+
Function: safeTransferFrom(address from, address to, uint256 tokenId)
|
110
|
+
Arguments: (0x1a1E021A302C237453D3D45c7B82B19cEEB7E2e6, 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC, 1)
|
111
|
+
|
112
|
+
Details: execution reverted: ERC721: transfer caller is not owner nor approved
|
113
|
+
Version: viem@1.0.2"
|
114
|
+
`)
|
115
|
+
})
|
116
|
+
})
|
117
|
+
|
118
|
+
describe('BAYC', () => {
|
119
|
+
describe('default', () => {
|
120
|
+
test('mintApe', async () => {
|
121
|
+
const { contractAddress } = await deployBAYC()
|
122
|
+
|
123
|
+
// Set sale state to active
|
124
|
+
// TODO: replace w/ writeContract
|
125
|
+
await sendTransaction(walletClient, {
|
126
|
+
data: encodeFunctionData({
|
127
|
+
abi: baycContractConfig.abi,
|
128
|
+
functionName: 'flipSaleState',
|
129
|
+
}),
|
130
|
+
from: accounts[0].address,
|
131
|
+
to: contractAddress!,
|
132
|
+
})
|
133
|
+
await mine(testClient, { blocks: 1 })
|
134
|
+
|
135
|
+
// Mint an Ape!
|
136
|
+
expect(
|
137
|
+
(
|
138
|
+
await simulateContract(publicClient, {
|
139
|
+
abi: baycContractConfig.abi,
|
140
|
+
address: contractAddress!,
|
141
|
+
functionName: 'mintApe',
|
142
|
+
from: accounts[0].address,
|
143
|
+
args: [1n],
|
144
|
+
value: 1000000000000000000n,
|
145
|
+
})
|
146
|
+
).result,
|
147
|
+
).toBe(undefined)
|
148
|
+
})
|
149
|
+
|
150
|
+
test('get a free $100k', async () => {
|
151
|
+
const { contractAddress } = await deployBAYC()
|
152
|
+
|
153
|
+
// Reserve apes
|
154
|
+
expect(
|
155
|
+
(
|
156
|
+
await simulateContract(publicClient, {
|
157
|
+
abi: baycContractConfig.abi,
|
158
|
+
address: contractAddress!,
|
159
|
+
functionName: 'reserveApes',
|
160
|
+
from: accounts[0].address,
|
161
|
+
})
|
162
|
+
).result,
|
163
|
+
).toBe(undefined)
|
164
|
+
})
|
165
|
+
})
|
166
|
+
|
167
|
+
describe('revert', () => {
|
168
|
+
test('sale inactive', async () => {
|
169
|
+
const { contractAddress } = await deployBAYC()
|
170
|
+
|
171
|
+
// Expect mint to fail.
|
172
|
+
await expect(() =>
|
173
|
+
simulateContract(publicClient, {
|
174
|
+
abi: baycContractConfig.abi,
|
175
|
+
address: contractAddress!,
|
176
|
+
functionName: 'mintApe',
|
177
|
+
from: accounts[0].address,
|
178
|
+
args: [1n],
|
179
|
+
}),
|
180
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(`
|
181
|
+
"Sale must be active to mint Ape
|
182
|
+
|
183
|
+
Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
|
184
|
+
Contract: 0x0000000000000000000000000000000000000000
|
185
|
+
Function: mintApe(uint256 numberOfTokens)
|
186
|
+
Arguments: (1)
|
187
|
+
|
188
|
+
Details: execution reverted: Sale must be active to mint Ape
|
189
|
+
Version: viem@1.0.2"
|
190
|
+
`)
|
191
|
+
})
|
192
|
+
})
|
193
|
+
})
|
194
|
+
|
195
|
+
test('fake contract address', async () => {
|
196
|
+
await expect(() =>
|
197
|
+
simulateContract(publicClient, {
|
198
|
+
abi: [
|
199
|
+
{
|
200
|
+
name: 'mint',
|
201
|
+
type: 'function',
|
202
|
+
stateMutability: 'nonpayable',
|
203
|
+
inputs: [],
|
204
|
+
outputs: [{ type: 'uint256' }],
|
205
|
+
},
|
206
|
+
],
|
207
|
+
address: '0x0000000000000000000000000000000000000069',
|
208
|
+
functionName: 'mint',
|
209
|
+
from: accounts[0].address,
|
210
|
+
args: [],
|
211
|
+
}),
|
212
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(`
|
213
|
+
"The contract method \\"mint\\" returned no data (\\"0x\\"). This could be due to any of the following:
|
214
|
+
- The contract does not have the function \\"mint\\",
|
215
|
+
- The parameters passed to the contract function may be invalid, or
|
216
|
+
- The address is not a contract.
|
217
|
+
|
218
|
+
Contract: 0x0000000000000000000000000000000000000000
|
219
|
+
Function: mint()
|
220
|
+
> \\"0x\\"
|
221
|
+
|
222
|
+
Version: viem@1.0.2"
|
223
|
+
`)
|
224
|
+
})
|
225
|
+
|
226
|
+
// Deploy BAYC Contract
|
227
|
+
async function deployBAYC() {
|
228
|
+
const hash = await deployContract(walletClient, {
|
229
|
+
...baycContractConfig,
|
230
|
+
args: ['Bored Ape Wagmi Club', 'BAYC', 69420n, 0n],
|
231
|
+
from: accounts[0].address,
|
232
|
+
})
|
233
|
+
await mine(testClient, { blocks: 1 })
|
234
|
+
const { contractAddress } = await getTransactionReceipt(publicClient, {
|
235
|
+
hash,
|
236
|
+
})
|
237
|
+
return { contractAddress }
|
238
|
+
}
|
@@ -15,30 +15,42 @@ import {
|
|
15
15
|
encodeFunctionData,
|
16
16
|
getContractError,
|
17
17
|
} from '../../utils'
|
18
|
+
import { WriteContractArgs } from '../wallet'
|
18
19
|
import { call, CallArgs, FormattedCall } from './call'
|
19
20
|
|
20
|
-
export type
|
21
|
+
export type FormattedSimulateContract<
|
21
22
|
TFormatter extends Formatter | undefined = Formatter,
|
22
23
|
> = FormattedCall<TFormatter>
|
23
24
|
|
24
|
-
export type
|
25
|
+
export type SimulateContractArgs<
|
25
26
|
TChain extends Chain = Chain,
|
26
27
|
TAbi extends Abi | readonly unknown[] = Abi,
|
27
28
|
TFunctionName extends string = any,
|
28
|
-
> = Omit<CallArgs<TChain>, '
|
29
|
+
> = Omit<CallArgs<TChain>, 'to' | 'data' | 'value'> & {
|
29
30
|
address: Address
|
30
31
|
abi: TAbi
|
31
|
-
|
32
|
-
|
32
|
+
functionName: ExtractFunctionNameFromAbi<
|
33
|
+
TAbi,
|
34
|
+
TFunctionName,
|
35
|
+
'payable' | 'nonpayable'
|
36
|
+
>
|
33
37
|
value?: GetValue<TAbi, TFunctionName, CallArgs<TChain>['value']>
|
34
38
|
} & ExtractArgsFromAbi<TAbi, TFunctionName>
|
35
39
|
|
36
|
-
export type
|
40
|
+
export type SimulateContractResponse<
|
41
|
+
TChain extends Chain = Chain,
|
37
42
|
TAbi extends Abi | readonly unknown[] = Abi,
|
38
43
|
TFunctionName extends string = string,
|
39
|
-
> =
|
44
|
+
> = {
|
45
|
+
result: ExtractResultFromAbi<TAbi, TFunctionName>
|
46
|
+
request: WriteContractArgs<TChain, TAbi, TFunctionName> & {
|
47
|
+
address: Address
|
48
|
+
abi: TAbi
|
49
|
+
functionName: ExtractFunctionNameFromAbi<TAbi, TFunctionName>
|
50
|
+
} & ExtractArgsFromAbi<TAbi, TFunctionName>
|
51
|
+
}
|
40
52
|
|
41
|
-
export async function
|
53
|
+
export async function simulateContract<
|
42
54
|
TChain extends Chain,
|
43
55
|
TAbi extends Abi = Abi,
|
44
56
|
TFunctionName extends string = any,
|
@@ -50,8 +62,8 @@ export async function callContract<
|
|
50
62
|
args,
|
51
63
|
functionName,
|
52
64
|
...callRequest
|
53
|
-
}:
|
54
|
-
): Promise<
|
65
|
+
}: SimulateContractArgs<TChain, TAbi, TFunctionName>,
|
66
|
+
): Promise<SimulateContractResponse<TChain, TAbi, TFunctionName>> {
|
55
67
|
const calldata = encodeFunctionData({
|
56
68
|
abi,
|
57
69
|
args,
|
@@ -63,11 +75,21 @@ export async function callContract<
|
|
63
75
|
to: address,
|
64
76
|
...callRequest,
|
65
77
|
} as unknown as CallArgs<TChain>)
|
66
|
-
|
78
|
+
const result = decodeFunctionResult({
|
67
79
|
abi,
|
68
80
|
functionName,
|
69
81
|
data: data || '0x',
|
70
|
-
})
|
82
|
+
})
|
83
|
+
return {
|
84
|
+
result,
|
85
|
+
request: {
|
86
|
+
abi,
|
87
|
+
address,
|
88
|
+
args,
|
89
|
+
functionName,
|
90
|
+
...callRequest,
|
91
|
+
},
|
92
|
+
} as unknown as SimulateContractResponse<TChain, TAbi, TFunctionName>
|
71
93
|
} catch (err) {
|
72
94
|
throw getContractError(err, {
|
73
95
|
abi,
|
@@ -25,3 +25,6 @@ export type { SwitchChainArgs } from './switchChain'
|
|
25
25
|
|
26
26
|
export { watchAsset } from './watchAsset'
|
27
27
|
export type { WatchAssetArgs, WatchAssetResponse } from './watchAsset'
|
28
|
+
|
29
|
+
export { writeContract } from './writeContract'
|
30
|
+
export type { WriteContractArgs, WriteContractResponse } from './writeContract'
|
@@ -0,0 +1,54 @@
|
|
1
|
+
import { expect, test } from 'vitest'
|
2
|
+
import {
|
3
|
+
accounts,
|
4
|
+
publicClient,
|
5
|
+
testClient,
|
6
|
+
wagmiContractConfig,
|
7
|
+
walletClient,
|
8
|
+
} from '../../_test'
|
9
|
+
import { simulateContract } from '../public'
|
10
|
+
import { mine } from '../test'
|
11
|
+
|
12
|
+
import { writeContract } from './writeContract'
|
13
|
+
|
14
|
+
test('default', async () => {
|
15
|
+
expect(
|
16
|
+
await writeContract(walletClient, {
|
17
|
+
...wagmiContractConfig,
|
18
|
+
from: accounts[0].address,
|
19
|
+
functionName: 'mint',
|
20
|
+
args: [69420n],
|
21
|
+
}),
|
22
|
+
).toBeDefined()
|
23
|
+
})
|
24
|
+
|
25
|
+
test('w/ simulateContract', async () => {
|
26
|
+
const { request } = await simulateContract(publicClient, {
|
27
|
+
...wagmiContractConfig,
|
28
|
+
from: accounts[0].address,
|
29
|
+
functionName: 'mint',
|
30
|
+
args: [69420n],
|
31
|
+
})
|
32
|
+
expect(await writeContract(walletClient, request)).toBeDefined()
|
33
|
+
|
34
|
+
await mine(testClient, { blocks: 1 })
|
35
|
+
|
36
|
+
await expect(() =>
|
37
|
+
simulateContract(publicClient, {
|
38
|
+
...wagmiContractConfig,
|
39
|
+
from: accounts[0].address,
|
40
|
+
functionName: 'mint',
|
41
|
+
args: [69420n],
|
42
|
+
}),
|
43
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(`
|
44
|
+
"Token ID is taken
|
45
|
+
|
46
|
+
Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
|
47
|
+
Contract: 0x0000000000000000000000000000000000000000
|
48
|
+
Function: mint(uint256 tokenId)
|
49
|
+
Arguments: (69420)
|
50
|
+
|
51
|
+
Details: execution reverted: Token ID is taken
|
52
|
+
Version: viem@1.0.2"
|
53
|
+
`)
|
54
|
+
})
|
@@ -0,0 +1,61 @@
|
|
1
|
+
import { Abi } from 'abitype'
|
2
|
+
|
3
|
+
import type { Chain, Formatter } from '../../chains'
|
4
|
+
import type { WalletClient } from '../../clients'
|
5
|
+
import type {
|
6
|
+
Address,
|
7
|
+
ExtractArgsFromAbi,
|
8
|
+
ExtractFunctionNameFromAbi,
|
9
|
+
GetValue,
|
10
|
+
} from '../../types'
|
11
|
+
import { EncodeFunctionDataArgs, encodeFunctionData } from '../../utils'
|
12
|
+
import {
|
13
|
+
FormattedTransactionRequest,
|
14
|
+
sendTransaction,
|
15
|
+
SendTransactionArgs,
|
16
|
+
SendTransactionResponse,
|
17
|
+
} from './sendTransaction'
|
18
|
+
|
19
|
+
export type FormattedWriteContract<
|
20
|
+
TFormatter extends Formatter | undefined = Formatter,
|
21
|
+
> = FormattedTransactionRequest<TFormatter>
|
22
|
+
|
23
|
+
export type WriteContractArgs<
|
24
|
+
TChain extends Chain = Chain,
|
25
|
+
TAbi extends Abi | readonly unknown[] = Abi,
|
26
|
+
TFunctionName extends string = any,
|
27
|
+
> = Omit<SendTransactionArgs<TChain>, 'to' | 'data' | 'value'> & {
|
28
|
+
address: Address
|
29
|
+
abi: TAbi
|
30
|
+
functionName: ExtractFunctionNameFromAbi<TAbi, TFunctionName>
|
31
|
+
value?: GetValue<TAbi, TFunctionName, SendTransactionArgs<TChain>['value']>
|
32
|
+
} & ExtractArgsFromAbi<TAbi, TFunctionName>
|
33
|
+
|
34
|
+
export type WriteContractResponse = SendTransactionResponse
|
35
|
+
|
36
|
+
export async function writeContract<
|
37
|
+
TChain extends Chain,
|
38
|
+
TAbi extends Abi = Abi,
|
39
|
+
TFunctionName extends string = any,
|
40
|
+
>(
|
41
|
+
client: WalletClient,
|
42
|
+
{
|
43
|
+
abi,
|
44
|
+
address,
|
45
|
+
args,
|
46
|
+
functionName,
|
47
|
+
...request
|
48
|
+
}: WriteContractArgs<TChain, TAbi, TFunctionName>,
|
49
|
+
): Promise<WriteContractResponse> {
|
50
|
+
const data = encodeFunctionData({
|
51
|
+
abi,
|
52
|
+
args,
|
53
|
+
functionName,
|
54
|
+
} as unknown as EncodeFunctionDataArgs<TAbi, TFunctionName>)
|
55
|
+
const hash = await sendTransaction(client, {
|
56
|
+
data,
|
57
|
+
to: address,
|
58
|
+
...request,
|
59
|
+
} as unknown as SendTransactionArgs<TChain>)
|
60
|
+
return hash
|
61
|
+
}
|
package/src/index.test.ts
CHANGED
@@ -65,7 +65,6 @@ test('exports actions', () => {
|
|
65
65
|
"bytesToNumber": [Function],
|
66
66
|
"bytesToString": [Function],
|
67
67
|
"call": [Function],
|
68
|
-
"callContract": [Function],
|
69
68
|
"createBlockFilter": [Function],
|
70
69
|
"createClient": [Function],
|
71
70
|
"createPendingTransactionFilter": [Function],
|
@@ -122,6 +121,7 @@ test('exports actions', () => {
|
|
122
121
|
"getFilterLogs": [Function],
|
123
122
|
"getFunctionSignature": [Function],
|
124
123
|
"getGasPrice": [Function],
|
124
|
+
"getLogs": [Function],
|
125
125
|
"getPermissions": [Function],
|
126
126
|
"getTransaction": [Function],
|
127
127
|
"getTransactionConfirmations": [Function],
|
@@ -177,6 +177,7 @@ test('exports actions', () => {
|
|
177
177
|
"setNonce": [Function],
|
178
178
|
"setStorageAt": [Function],
|
179
179
|
"signMessage": [Function],
|
180
|
+
"simulateContract": [Function],
|
180
181
|
"size": [Function],
|
181
182
|
"slice": [Function],
|
182
183
|
"sliceBytes": [Function],
|
@@ -203,6 +204,7 @@ test('exports actions', () => {
|
|
203
204
|
"ether": -18,
|
204
205
|
"gwei": -9,
|
205
206
|
},
|
207
|
+
"writeContract": [Function],
|
206
208
|
}
|
207
209
|
`)
|
208
210
|
})
|
package/src/index.ts
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
export type {
|
2
2
|
CallArgs,
|
3
|
-
CallContractArgs,
|
4
|
-
CallContractResponse,
|
5
3
|
CallResponse,
|
6
4
|
CreateBlockFilterResponse,
|
7
5
|
CreatePendingTransactionFilterResponse,
|
@@ -27,6 +25,8 @@ export type {
|
|
27
25
|
GetFilterLogsArgs,
|
28
26
|
GetFilterLogsResponse,
|
29
27
|
GetGasPriceResponse,
|
28
|
+
GetLogsArgs,
|
29
|
+
GetLogsResponse,
|
30
30
|
GetPermissionsResponse,
|
31
31
|
GetTransactionArgs,
|
32
32
|
GetTransactionConfirmationsArgs,
|
@@ -65,6 +65,8 @@ export type {
|
|
65
65
|
SetStorageAtArgs,
|
66
66
|
SignMessageArgs,
|
67
67
|
SignMessageResponse,
|
68
|
+
SimulateContractArgs,
|
69
|
+
SimulateContractResponse,
|
68
70
|
StopImpersonatingAccountArgs,
|
69
71
|
SwitchChainArgs,
|
70
72
|
UninstallFilterArgs,
|
@@ -76,11 +78,12 @@ export type {
|
|
76
78
|
WatchBlockNumberArgs,
|
77
79
|
WatchBlocksArgs,
|
78
80
|
WatchPendingTransactionsArgs,
|
81
|
+
WriteContractArgs,
|
82
|
+
WriteContractResponse,
|
79
83
|
} from './actions'
|
80
84
|
export {
|
81
85
|
addChain,
|
82
86
|
call,
|
83
|
-
callContract,
|
84
87
|
createBlockFilter,
|
85
88
|
createPendingTransactionFilter,
|
86
89
|
deployContract,
|
@@ -98,6 +101,7 @@ export {
|
|
98
101
|
getFilterChanges,
|
99
102
|
getFilterLogs,
|
100
103
|
getGasPrice,
|
104
|
+
getLogs,
|
101
105
|
getPermissions,
|
102
106
|
getTransaction,
|
103
107
|
getTransactionConfirmations,
|
@@ -130,6 +134,7 @@ export {
|
|
130
134
|
setNonce,
|
131
135
|
setStorageAt,
|
132
136
|
signMessage,
|
137
|
+
simulateContract,
|
133
138
|
snapshot,
|
134
139
|
stopImpersonatingAccount,
|
135
140
|
switchChain,
|
@@ -139,6 +144,7 @@ export {
|
|
139
144
|
watchBlockNumber,
|
140
145
|
watchBlocks,
|
141
146
|
watchPendingTransactions,
|
147
|
+
writeContract,
|
142
148
|
} from './actions'
|
143
149
|
|
144
150
|
export type {
|
package/src/public.ts
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
export {
|
2
2
|
call,
|
3
|
-
callContract,
|
4
3
|
createBlockFilter,
|
5
4
|
createPendingTransactionFilter,
|
6
5
|
deployContract,
|
@@ -20,6 +19,7 @@ export {
|
|
20
19
|
getTransactionConfirmations,
|
21
20
|
getTransactionCount,
|
22
21
|
getTransactionReceipt,
|
22
|
+
simulateContract,
|
23
23
|
uninstallFilter,
|
24
24
|
waitForTransactionReceipt,
|
25
25
|
watchBlockNumber,
|
@@ -28,8 +28,6 @@ export {
|
|
28
28
|
} from './actions/public'
|
29
29
|
export type {
|
30
30
|
CallArgs,
|
31
|
-
CallContractArgs,
|
32
|
-
CallContractResponse,
|
33
31
|
CallResponse,
|
34
32
|
CreateBlockFilterResponse,
|
35
33
|
CreatePendingTransactionFilterResponse,
|
@@ -70,6 +68,8 @@ export type {
|
|
70
68
|
OnTransactionsResponse,
|
71
69
|
ReplacementReason,
|
72
70
|
ReplacementResponse,
|
71
|
+
SimulateContractArgs,
|
72
|
+
SimulateContractResponse,
|
73
73
|
UninstallFilterArgs,
|
74
74
|
UninstallFilterResponse,
|
75
75
|
WaitForTransactionReceiptArgs,
|
package/src/types/eip1193.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import type { BlockTag } from './block'
|
2
|
-
import type { Address, Hash, Hex } from './misc'
|
2
|
+
import type { Address, Hash, Hex, LogTopic } from './misc'
|
3
3
|
import type {
|
4
4
|
RpcBlock as Block,
|
5
5
|
RpcBlockIdentifier as BlockIdentifier,
|
@@ -381,25 +381,28 @@ export type PublicRequests = {
|
|
381
381
|
* @description Returns a list of all logs based on a filter object
|
382
382
|
* @link https://eips.ethereum.org/EIPS/eip-1474
|
383
383
|
* @example
|
384
|
-
* provider.request({ method: 'eth_getLogs', params: [{ ... }] })
|
384
|
+
* provider.request({ method: 'eth_getLogs', params: [{ fromBlock: '0x...', toBlock: '0x...', address: '0x...', topics: ['0x...'] }] })
|
385
385
|
* // => [{ ... }, { ... }]
|
386
386
|
* */
|
387
387
|
method: 'eth_getLogs'
|
388
388
|
params: [
|
389
|
-
|
389
|
+
parameters: {
|
390
390
|
address?: Address | Address[]
|
391
|
-
topics?:
|
391
|
+
topics?: LogTopic[]
|
392
392
|
} & (
|
393
393
|
| {
|
394
394
|
fromBlock?: BlockNumber | BlockTag
|
395
395
|
toBlock?: BlockNumber | BlockTag
|
396
|
+
blockHash?: never
|
396
397
|
}
|
397
398
|
| {
|
399
|
+
fromBlock?: never
|
400
|
+
toBlock?: never
|
398
401
|
blockHash?: Hash
|
399
402
|
}
|
400
403
|
),
|
401
404
|
]
|
402
|
-
}): Promise<Log>
|
405
|
+
}): Promise<Log[]>
|
403
406
|
request(args: {
|
404
407
|
/**
|
405
408
|
* @description Returns the value from a storage position at an address
|
@@ -539,7 +542,7 @@ export type PublicRequests = {
|
|
539
542
|
fromBlock?: BlockNumber | BlockTag
|
540
543
|
toBlock?: BlockNumber | BlockTag
|
541
544
|
address?: Address | Address[]
|
542
|
-
topics?:
|
545
|
+
topics?: LogTopic[]
|
543
546
|
},
|
544
547
|
]
|
545
548
|
}): Promise<Quantity>
|
package/src/types/index.ts
CHANGED
package/src/types/misc.ts
CHANGED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"sources":["../src/actions/wallet/addChain.ts","../src/actions/wallet/getAccounts.ts","../src/actions/wallet/getPermissions.ts","../src/actions/wallet/requestAccounts.ts","../src/actions/wallet/requestPermissions.ts","../src/actions/wallet/sendTransaction.ts","../src/actions/wallet/signMessage.ts","../src/actions/wallet/switchChain.ts","../src/actions/wallet/watchAsset.ts"],"names":[],"mappings":"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","sourcesContent":["import type { Chain } from '../../chains'\nimport type { WalletClient } from '../../clients'\nimport { numberToHex } from '../../utils'\n\nexport async function addChain(client: WalletClient, chain: Chain) {\n const { id, name, nativeCurrency, rpcUrls, blockExplorers } = chain\n await client.request({\n method: 'wallet_addEthereumChain',\n params: [\n {\n chainId: numberToHex(id),\n chainName: name,\n nativeCurrency,\n rpcUrls: rpcUrls.default.http,\n blockExplorerUrls: blockExplorers\n ? Object.values(blockExplorers).map(({ url }) => url)\n : undefined,\n },\n ],\n })\n}\n","import type { WalletClient } from '../../clients'\nimport { checksumAddress } from '../../utils/address'\n\nexport async function getAccounts(client: WalletClient) {\n const addresses = await client.request({ method: 'eth_accounts' })\n return addresses.map((address) => checksumAddress(address))\n}\n","import type { WalletClient } from '../../clients'\nimport type { WalletPermission } from '../../types/eip1193'\n\nexport type GetPermissionsResponse = WalletPermission[]\n\nexport async function getPermissions(client: WalletClient) {\n const permissions = await client.request({ method: 'wallet_getPermissions' })\n return permissions\n}\n","import type { WalletClient } from '../../clients'\nimport { getAddress } from '../../utils'\n\nexport async function requestAccounts(client: WalletClient) {\n const addresses = await client.request({ method: 'eth_requestAccounts' })\n return addresses.map((address) => getAddress(address))\n}\n","import type { WalletClient } from '../../clients'\nimport type { WalletPermission } from '../../types/eip1193'\n\nexport type RequestPermissionsArgs = {\n eth_accounts: Record<string, any>\n} & {\n [key: string]: Record<string, any>\n}\nexport type RequestPermissionsResponse = WalletPermission[]\n\nexport async function requestPermissions(\n client: WalletClient,\n permissions: RequestPermissionsArgs,\n) {\n return client.request({\n method: 'wallet_requestPermissions',\n params: [permissions],\n })\n}\n","import type { Chain, Formatter } from '../../chains'\nimport type { WalletClient } from '../../clients'\nimport { InvalidGasArgumentsError } from '../../errors'\nimport type {\n Hash,\n MergeIntersectionProperties,\n TransactionRequest,\n} from '../../types'\nimport type { Formatted, TransactionRequestFormatter } from '../../utils'\nimport { extract, format, formatTransactionRequest } from '../../utils'\n\nexport type FormattedTransactionRequest<\n TFormatter extends Formatter | undefined = Formatter,\n> = MergeIntersectionProperties<\n Formatted<TFormatter, TransactionRequest, true>,\n TransactionRequest\n>\n\nexport type SendTransactionArgs<TChain extends Chain = Chain> =\n FormattedTransactionRequest<TransactionRequestFormatter<TChain>> & {\n chain?: TChain\n }\n\nexport type SendTransactionResponse = Hash\n\nexport async function sendTransaction<TChain extends Chain>(\n client: WalletClient,\n {\n chain,\n from,\n accessList,\n data,\n gas,\n gasPrice,\n maxFeePerGas,\n maxPriorityFeePerGas,\n nonce,\n to,\n value,\n ...rest\n }: SendTransactionArgs<TChain>,\n): Promise<SendTransactionResponse> {\n if (\n maxFeePerGas !== undefined &&\n maxPriorityFeePerGas !== undefined &&\n maxFeePerGas < maxPriorityFeePerGas\n )\n throw new InvalidGasArgumentsError()\n\n const formatter = chain?.formatters?.transactionRequest\n const request_ = format(\n {\n from,\n accessList,\n data,\n gas,\n gasPrice,\n maxFeePerGas,\n maxPriorityFeePerGas,\n nonce,\n to,\n value,\n // Pick out extra data that might exist on the chain's transaction request type.\n ...extract(rest, { formatter }),\n } as TransactionRequest,\n {\n formatter: formatter || formatTransactionRequest,\n },\n )\n\n const hash = await client.request({\n method: 'eth_sendTransaction',\n params: [request_],\n })\n return hash\n}\n","import type { WalletClient } from '../../clients'\nimport { BaseError } from '../../errors'\nimport type { Address, ByteArray, Hex } from '../../types'\nimport { encodeHex } from '../../utils'\n\nexport type SignMessageArgs = {\n from: Address\n data: Hex | ByteArray\n}\n\nexport type SignMessageResponse = Hex\n\nexport async function signMessage(\n client: WalletClient,\n { from, data: data_ }: SignMessageArgs,\n): Promise<SignMessageResponse> {\n let data\n if (typeof data_ === 'string') {\n if (!data_.startsWith('0x'))\n throw new BaseError(\n `data (\"${data_}\") must be a hex value. Encode it first to a hex with the \\`encodeHex\\` util.`,\n {\n docsPath: '/TODO',\n },\n )\n data = data_\n } else {\n data = encodeHex(data_)\n }\n const signed = await client.request({\n method: 'personal_sign',\n params: [data, from],\n })\n return signed\n}\n","import type { Chain } from '../../chains'\nimport type { WalletClient } from '../../clients'\nimport { numberToHex } from '../../utils'\n\nexport type SwitchChainArgs = { id: Chain['id'] }\n\nexport async function switchChain(\n client: WalletClient,\n { id }: SwitchChainArgs,\n) {\n await client.request({\n method: 'wallet_switchEthereumChain',\n params: [\n {\n chainId: numberToHex(id),\n },\n ],\n })\n}\n","import type { WalletClient } from '../../clients'\nimport type { WatchAssetParams } from '../../types/eip1193'\n\nexport type WatchAssetArgs = WatchAssetParams\nexport type WatchAssetResponse = boolean\n\nexport async function watchAsset(\n client: WalletClient,\n params: WatchAssetParams,\n): Promise<WatchAssetResponse> {\n const added = await client.request({\n method: 'wallet_watchAsset',\n params: [params],\n })\n return added\n}\n"]}
|