@rocketh/read-execute 0.17.8 → 0.17.9
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/README.md +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -49
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
- package/src/index.ts +387 -0
package/README.md
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
1
|
# @rocketh/read-execute
|
|
2
|
+
|
|
3
|
+
Contract read and write utilities for rocketh, providing type-safe functions for reading from and executing transactions on deployed contracts.
|
|
4
|
+
|
|
5
|
+
For full documentation, visit [rocketh.dev](https://rocketh.dev).
|
|
6
|
+
|
|
7
|
+
For hardhat-deploy documentation, see [rocketh.dev/hardhat-deploy/](https://rocketh.dev/hardhat-deploy/).
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAC,MAAM,SAAS,CAAC;AAC5B,OAAO,EAAC,WAAW,EAA0B,yBAAyB,EAAC,MAAM,UAAU,CAAC;AACxF,OAAO,KAAK,EAAW,WAAW,EAAE,iBAAiB,EAAmB,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAC,MAAM,SAAS,CAAC;AAC5B,OAAO,EAAC,WAAW,EAA0B,yBAAyB,EAAC,MAAM,UAAU,CAAC;AACxF,OAAO,KAAK,EAAW,WAAW,EAAE,iBAAiB,EAAmB,MAAM,qBAAqB,CAAC;AAEpG,OAAO,KAAK,EACX,oBAAoB,EACpB,oBAAoB,EACpB,8BAA8B,EAC9B,sBAAsB,EACtB,yBAAyB,EACzB,uBAAuB,EACvB,MAAM,MAAM,CAAC;AAEd,YAAY,EACX,oBAAoB,EACpB,oB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AAC,CAoDpH"}
|
package/dist/index.js
CHANGED
|
@@ -1,24 +1,11 @@
|
|
|
1
|
+
import { resolveAccount, resolveAccountOrUndefined } from '@rocketh/core';
|
|
1
2
|
import { decodeFunctionResult, encodeFunctionData } from 'viem';
|
|
2
3
|
import { logs } from 'named-logs';
|
|
3
4
|
const logger = logs('@rocketh/read-execute');
|
|
4
5
|
export function execute(env) {
|
|
5
6
|
return async (deployment, args) => {
|
|
6
7
|
const { account, ...viemArgs } = args;
|
|
7
|
-
|
|
8
|
-
if (account.startsWith('0x')) {
|
|
9
|
-
address = account;
|
|
10
|
-
}
|
|
11
|
-
else {
|
|
12
|
-
if (env.namedAccounts) {
|
|
13
|
-
address = env.namedAccounts[account];
|
|
14
|
-
if (!address) {
|
|
15
|
-
throw new Error(`no address for ${account}`);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
throw new Error(`no accounts setup, cannot get address for ${account}`);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
8
|
+
const address = resolveAccount(account, env);
|
|
22
9
|
const artifactToUse = deployment;
|
|
23
10
|
const abi = artifactToUse.abi;
|
|
24
11
|
const calldata = encodeFunctionData({
|
|
@@ -72,7 +59,7 @@ export function execute(env) {
|
|
|
72
59
|
type: 'execution',
|
|
73
60
|
transaction: { hash: txHash, origin: address },
|
|
74
61
|
// description, // TODO
|
|
75
|
-
// TODO we should have the nonce, except for wallet like metamask where it is not
|
|
62
|
+
// TODO we should have the nonce, except for wallet like metamask where it is not sure you get the nonce you start with
|
|
76
63
|
};
|
|
77
64
|
const receipt = await env.savePendingExecution(pendingExecution, args.message);
|
|
78
65
|
return receipt;
|
|
@@ -90,23 +77,7 @@ export function executeByName(env) {
|
|
|
90
77
|
export function read(env) {
|
|
91
78
|
return async (deployment, args) => {
|
|
92
79
|
const { account, ...viemArgs } = args;
|
|
93
|
-
|
|
94
|
-
if (account) {
|
|
95
|
-
if (account.startsWith('0x')) {
|
|
96
|
-
address = account;
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
if (env.namedAccounts) {
|
|
100
|
-
address = env.namedAccounts[account];
|
|
101
|
-
if (!address) {
|
|
102
|
-
throw new Error(`no address for ${account}`);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
throw new Error(`no accounts setup, cannot get address for ${account}`);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
80
|
+
const address = account ? resolveAccountOrUndefined(account, env) : undefined;
|
|
110
81
|
const artifactToUse = deployment;
|
|
111
82
|
const abi = artifactToUse.abi;
|
|
112
83
|
const calldata = encodeFunctionData({
|
|
@@ -148,21 +119,7 @@ export function readByName(env) {
|
|
|
148
119
|
export function tx(env) {
|
|
149
120
|
return async (txData, options) => {
|
|
150
121
|
const { account, ...viemArgs } = txData;
|
|
151
|
-
|
|
152
|
-
if (account.startsWith('0x')) {
|
|
153
|
-
address = account;
|
|
154
|
-
}
|
|
155
|
-
else {
|
|
156
|
-
if (env.namedAccounts) {
|
|
157
|
-
address = env.namedAccounts[account];
|
|
158
|
-
if (!address) {
|
|
159
|
-
throw new Error(`no address for ${account}`);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
throw new Error(`no accounts setup, cannot get address for ${account}`);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
122
|
+
const address = resolveAccount(account, env);
|
|
166
123
|
const signer = env.addressSigners[address];
|
|
167
124
|
const txParam = {
|
|
168
125
|
type: '0x2',
|
|
@@ -202,7 +159,7 @@ export function tx(env) {
|
|
|
202
159
|
type: 'execution',
|
|
203
160
|
transaction: { hash: txHash, origin: address },
|
|
204
161
|
// description, // TODO
|
|
205
|
-
// TODO we should have the nonce, except for wallet like metamask where it is not
|
|
162
|
+
// TODO we should have the nonce, except for wallet like metamask where it is not sure you get the nonce you start with
|
|
206
163
|
};
|
|
207
164
|
await env.savePendingExecution(pendingExecution, options?.message);
|
|
208
165
|
return txHash;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAC,cAAc,EAAE,yBAAyB,EAAC,MAAM,eAAe,CAAC;AAkBxE,OAAO,EAAC,oBAAoB,EAAE,kBAAkB,EAAC,MAAM,MAAM,CAAC;AAC9D,OAAO,EAAC,IAAI,EAAC,MAAM,YAAY,CAAC;AAEhC,MAAM,MAAM,GAAG,IAAI,CAAC,uBAAuB,CAAC,CAAC;AAmF7C,MAAM,UAAU,OAAO,CACtB,GAAgB;IAahB,OAAO,KAAK,EASX,UAAmC,EACnC,IAA+C,EAC9C,EAAE;QACH,MAAM,EAAC,OAAO,EAAE,GAAG,QAAQ,EAAC,GAAG,IAAI,CAAC;QACpC,MAAM,OAAO,GAAG,cAAc,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QAE7C,MAAM,aAAa,GAAG,UAAuC,CAAC;QAC9D,MAAM,GAAG,GAAG,aAAa,CAAC,GAAG,CAAC;QAC9B,MAAM,QAAQ,GAAG,kBAAkB,CAAsB;YACxD,GAAG;YACH,YAAY,EAAE,QAAQ,CAAC,YAAY;YACnC,IAAI,EAAE,QAAQ,CAAC,IAAI;SACZ,CAAC,CAAC;QAEV,MAAM,MAAM,GAAG,GAAG,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QAE3C,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,yBAAyB,OAAO,EAAE,CAAC,CAAC;QACrD,CAAC;QAED,MAAM,OAAO,GAA2B;YACvC,EAAE,EAAE,UAAU,CAAC,OAAO;YACtB,IAAI,EAAE,KAAK;YACX,IAAI,EAAE,OAAO;YACb,OAAO,EAAE,KAAK,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAA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C,EAAE,CAAC,EAAmB,CAAC;QACtE,CAAC;QAED,IAAI,MAAqB,CAAC;QAC1B,IAAI,MAAM,CAAC,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC1D,MAAM,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC;gBACpC,MAAM,EAAE,qBAAqB;gBAC7B,MAAM,EAAE,CAAC,OAAO,CAAQ,EAAE,wBAAwB;aAClD,CAAC,CAAC;QACJ,CAAC;aAAM,CAAC;YACP,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC;gBACzC,MAAM,EAAE,qBAAqB;gBAC7B,MAAM,EAAE,CAAC,OAAO,CAAC;aACjB,CAAC,CAAC;YAEH,MAAM,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;gBAC3C,MAAM,EAAE,wBAAwB;gBAChC,MAAM,EAAE,CAAC,KAAK,CAAC;aACf,CAAC,CAAC;QACJ,CAAC;QAED,MAAM,gBAAgB,GAAqB;YAC1C,IAAI,EAAE,WAAW;YACjB,WAAW,EAAE,EAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAC;YAC5C,uBAAuB;YACvB,uHAAuH;SACvH,CAAC;QACF,MAAM,GAAG,CAAC,oBAAoB,CAAC,gBAAgB,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QACnE,OAAO,MAAM,CAAC;IACf,CAAC,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rocketh/read-execute",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.9",
|
|
4
4
|
"description": "provide read abd execute functions for rocketh",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -18,17 +18,18 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
|
21
|
-
"dist"
|
|
21
|
+
"dist",
|
|
22
|
+
"src"
|
|
22
23
|
],
|
|
23
24
|
"dependencies": {
|
|
24
25
|
"abitype": "^1.2.3",
|
|
25
26
|
"eip-1193": "^0.6.5",
|
|
26
27
|
"named-logs": "^0.4.1",
|
|
27
|
-
"viem": "^2.44.
|
|
28
|
-
"@rocketh/core": "0.17.
|
|
28
|
+
"viem": "^2.44.4",
|
|
29
|
+
"@rocketh/core": "0.17.9"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
|
-
"as-soon": "^0.
|
|
32
|
+
"as-soon": "^0.1.4",
|
|
32
33
|
"rimraf": "^6.1.2",
|
|
33
34
|
"typescript": "^5.9.3"
|
|
34
35
|
},
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
import {Abi} from 'abitype';
|
|
2
|
+
import {EIP1193DATA, EIP1193TransactionData, EIP1193TransactionReceipt} from 'eip-1193';
|
|
3
|
+
import type {Artifact, Environment, MinimalDeployment, PendingExecution} from '@rocketh/core/types';
|
|
4
|
+
import {resolveAccount, resolveAccountOrUndefined} from '@rocketh/core';
|
|
5
|
+
import type {
|
|
6
|
+
ContractFunctionArgs,
|
|
7
|
+
ContractFunctionName,
|
|
8
|
+
DecodeFunctionResultReturnType,
|
|
9
|
+
ReadContractParameters,
|
|
10
|
+
TransactionRequestEIP1559,
|
|
11
|
+
WriteContractParameters,
|
|
12
|
+
} from 'viem';
|
|
13
|
+
// export type so viem is not needed for inference
|
|
14
|
+
export type {
|
|
15
|
+
ContractFunctionArgs,
|
|
16
|
+
ContractFunctionName,
|
|
17
|
+
DecodeFunctionResultReturnType,
|
|
18
|
+
ReadContractParameters,
|
|
19
|
+
TransactionRequestEIP1559,
|
|
20
|
+
WriteContractParameters,
|
|
21
|
+
};
|
|
22
|
+
import {decodeFunctionResult, encodeFunctionData} from 'viem';
|
|
23
|
+
import {logs} from 'named-logs';
|
|
24
|
+
|
|
25
|
+
const logger = logs('@rocketh/read-execute');
|
|
26
|
+
|
|
27
|
+
type TransactionData = Omit<TransactionRequestEIP1559, 'from' | 'nonce'> & {account: string};
|
|
28
|
+
|
|
29
|
+
export type ExecuteFunction = <
|
|
30
|
+
TAbi extends Abi,
|
|
31
|
+
TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
|
|
32
|
+
TArgs extends ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName> = ContractFunctionArgs<
|
|
33
|
+
TAbi,
|
|
34
|
+
'nonpayable' | 'payable',
|
|
35
|
+
TFunctionName
|
|
36
|
+
>,
|
|
37
|
+
>(
|
|
38
|
+
deployment: MinimalDeployment<TAbi>,
|
|
39
|
+
args: ExecutionArgs<TAbi, TFunctionName, TArgs>,
|
|
40
|
+
) => Promise<EIP1193DATA>;
|
|
41
|
+
|
|
42
|
+
export type ExecuteFunctionByName = <
|
|
43
|
+
TAbi extends Abi,
|
|
44
|
+
TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
|
|
45
|
+
TArgs extends ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName> = ContractFunctionArgs<
|
|
46
|
+
TAbi,
|
|
47
|
+
'nonpayable' | 'payable',
|
|
48
|
+
TFunctionName
|
|
49
|
+
>,
|
|
50
|
+
>(
|
|
51
|
+
name: string,
|
|
52
|
+
args: ExecutionArgs<TAbi, TFunctionName, TArgs>,
|
|
53
|
+
) => Promise<EIP1193DATA>;
|
|
54
|
+
|
|
55
|
+
export type ReadFunction = <
|
|
56
|
+
TAbi extends Abi,
|
|
57
|
+
TFunctionName extends ContractFunctionName<TAbi, 'pure' | 'view'>,
|
|
58
|
+
TArgs extends ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName> = ContractFunctionArgs<
|
|
59
|
+
TAbi,
|
|
60
|
+
'pure' | 'view',
|
|
61
|
+
TFunctionName
|
|
62
|
+
>,
|
|
63
|
+
>(
|
|
64
|
+
deployment: MinimalDeployment<TAbi>,
|
|
65
|
+
args: ReadingArgs<TAbi, TFunctionName, TArgs>,
|
|
66
|
+
) => Promise<DecodeFunctionResultReturnType<TAbi, TFunctionName>>;
|
|
67
|
+
|
|
68
|
+
export type ReadFunctionByName = <
|
|
69
|
+
TAbi extends Abi,
|
|
70
|
+
TFunctionName extends ContractFunctionName<TAbi, 'pure' | 'view'>,
|
|
71
|
+
TArgs extends ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName> = ContractFunctionArgs<
|
|
72
|
+
TAbi,
|
|
73
|
+
'pure' | 'view',
|
|
74
|
+
TFunctionName
|
|
75
|
+
>,
|
|
76
|
+
>(
|
|
77
|
+
name: string,
|
|
78
|
+
args: ReadingArgs<TAbi, TFunctionName, TArgs>,
|
|
79
|
+
) => Promise<DecodeFunctionResultReturnType<TAbi, TFunctionName>>;
|
|
80
|
+
|
|
81
|
+
export type TxFunction = (tx: TransactionData, options?: {message?: string}) => Promise<EIP1193DATA>;
|
|
82
|
+
|
|
83
|
+
export type ExecutionArgs<
|
|
84
|
+
TAbi extends Abi,
|
|
85
|
+
TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
|
|
86
|
+
TArgs extends ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName> = ContractFunctionArgs<
|
|
87
|
+
TAbi,
|
|
88
|
+
'nonpayable' | 'payable',
|
|
89
|
+
TFunctionName
|
|
90
|
+
>,
|
|
91
|
+
> = Omit<WriteContractParameters<TAbi, TFunctionName, TArgs>, 'address' | 'abi' | 'account' | 'nonce' | 'chain'> & {
|
|
92
|
+
account: string;
|
|
93
|
+
message?: string;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export type ReadingArgs<
|
|
97
|
+
TAbi extends Abi,
|
|
98
|
+
TFunctionName extends ContractFunctionName<TAbi, 'pure' | 'view'>,
|
|
99
|
+
TArgs extends ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName> = ContractFunctionArgs<
|
|
100
|
+
TAbi,
|
|
101
|
+
'pure' | 'view',
|
|
102
|
+
TFunctionName
|
|
103
|
+
>,
|
|
104
|
+
> = Omit<ReadContractParameters<TAbi, TFunctionName, TArgs>, 'address' | 'abi' | 'account' | 'nonce'> & {
|
|
105
|
+
account?: string;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export function execute(
|
|
109
|
+
env: Environment,
|
|
110
|
+
): <
|
|
111
|
+
TAbi extends Abi,
|
|
112
|
+
TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
|
|
113
|
+
TArgs extends ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName> = ContractFunctionArgs<
|
|
114
|
+
TAbi,
|
|
115
|
+
'nonpayable' | 'payable',
|
|
116
|
+
TFunctionName
|
|
117
|
+
>,
|
|
118
|
+
>(
|
|
119
|
+
deployment: MinimalDeployment<TAbi>,
|
|
120
|
+
args: ExecutionArgs<TAbi, TFunctionName, TArgs>,
|
|
121
|
+
) => Promise<EIP1193TransactionReceipt> {
|
|
122
|
+
return async <
|
|
123
|
+
TAbi extends Abi,
|
|
124
|
+
TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
|
|
125
|
+
TArgs extends ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName> = ContractFunctionArgs<
|
|
126
|
+
TAbi,
|
|
127
|
+
'nonpayable' | 'payable',
|
|
128
|
+
TFunctionName
|
|
129
|
+
>,
|
|
130
|
+
>(
|
|
131
|
+
deployment: MinimalDeployment<TAbi>,
|
|
132
|
+
args: ExecutionArgs<TAbi, TFunctionName, TArgs>,
|
|
133
|
+
) => {
|
|
134
|
+
const {account, ...viemArgs} = args;
|
|
135
|
+
const address = resolveAccount(account, env);
|
|
136
|
+
|
|
137
|
+
const artifactToUse = deployment as unknown as Artifact<TAbi>;
|
|
138
|
+
const abi = artifactToUse.abi;
|
|
139
|
+
const calldata = encodeFunctionData<TAbi, TFunctionName>({
|
|
140
|
+
abi,
|
|
141
|
+
functionName: viemArgs.functionName,
|
|
142
|
+
args: viemArgs.args,
|
|
143
|
+
} as any);
|
|
144
|
+
|
|
145
|
+
const signer = env.addressSigners[address];
|
|
146
|
+
|
|
147
|
+
if (!signer) {
|
|
148
|
+
throw new Error(`cannot get signer for ${address}`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const txParam: EIP1193TransactionData = {
|
|
152
|
+
to: deployment.address,
|
|
153
|
+
type: '0x2',
|
|
154
|
+
from: address,
|
|
155
|
+
chainId: `0x${env.network.chain.id.toString(16)}` as `0x${string}`,
|
|
156
|
+
data: calldata,
|
|
157
|
+
gas: viemArgs.gas && (`0x${viemArgs.gas.toString(16)}` as `0x${string}`),
|
|
158
|
+
// gasPrice: viemArgs.gasPrice && `0x${viemArgs.gasPrice.toString(16)}` as `0x${string}`,
|
|
159
|
+
maxFeePerGas: viemArgs.maxFeePerGas ? (`0x${viemArgs.maxFeePerGas.toString(16)}` as `0x${string}`) : undefined,
|
|
160
|
+
maxPriorityFeePerGas: viemArgs.maxPriorityFeePerGas
|
|
161
|
+
? (`0x${viemArgs.maxPriorityFeePerGas.toString(16)}` as `0x${string}`)
|
|
162
|
+
: undefined,
|
|
163
|
+
accessList: viemArgs.accessList as any, // TODO
|
|
164
|
+
// nonce: viemArgs.nonce ? (`0x${viemArgs.nonce.toString(16)}` as `0x${string}`) : undefined,
|
|
165
|
+
};
|
|
166
|
+
if (viemArgs.value) {
|
|
167
|
+
txParam.value = `0x${viemArgs.value?.toString(16)}` as `0x${string}`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
let txHash: `0x${string}`;
|
|
171
|
+
if (signer.type === 'wallet' || signer.type === 'remote') {
|
|
172
|
+
txHash = await signer.signer.request({
|
|
173
|
+
method: 'eth_sendTransaction',
|
|
174
|
+
params: [txParam] as any, // TODO fix eip-1193 ?,,
|
|
175
|
+
});
|
|
176
|
+
} else {
|
|
177
|
+
const rawTx = await signer.signer.request({
|
|
178
|
+
method: 'eth_signTransaction',
|
|
179
|
+
params: [txParam],
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
txHash = await env.network.provider.request({
|
|
183
|
+
method: 'eth_sendRawTransaction',
|
|
184
|
+
params: [rawTx],
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (env.tags['auto-mine']) {
|
|
189
|
+
await (env.network.provider as any).request({method: 'evm_mine', params: []});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const pendingExecution: PendingExecution = {
|
|
193
|
+
type: 'execution',
|
|
194
|
+
transaction: {hash: txHash, origin: address},
|
|
195
|
+
// description, // TODO
|
|
196
|
+
// TODO we should have the nonce, except for wallet like metamask where it is not sure you get the nonce you start with
|
|
197
|
+
};
|
|
198
|
+
const receipt = await env.savePendingExecution(pendingExecution, args.message);
|
|
199
|
+
return receipt;
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function executeByName(
|
|
204
|
+
env: Environment,
|
|
205
|
+
): <
|
|
206
|
+
TAbi extends Abi,
|
|
207
|
+
TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
|
|
208
|
+
TArgs extends ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName> = ContractFunctionArgs<
|
|
209
|
+
TAbi,
|
|
210
|
+
'nonpayable' | 'payable',
|
|
211
|
+
TFunctionName
|
|
212
|
+
>,
|
|
213
|
+
>(
|
|
214
|
+
name: string,
|
|
215
|
+
args: ExecutionArgs<TAbi, TFunctionName, TArgs>,
|
|
216
|
+
) => Promise<EIP1193TransactionReceipt> {
|
|
217
|
+
return async <
|
|
218
|
+
TAbi extends Abi,
|
|
219
|
+
TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
|
|
220
|
+
TArgs extends ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName> = ContractFunctionArgs<
|
|
221
|
+
TAbi,
|
|
222
|
+
'nonpayable' | 'payable',
|
|
223
|
+
TFunctionName
|
|
224
|
+
>,
|
|
225
|
+
>(
|
|
226
|
+
name: string,
|
|
227
|
+
args: ExecutionArgs<TAbi, TFunctionName, TArgs>,
|
|
228
|
+
) => {
|
|
229
|
+
const deployment = env.getOrNull<TAbi>(name);
|
|
230
|
+
if (!deployment) {
|
|
231
|
+
throw new Error(`no deployment named ${name}`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return execute(env)(deployment, args);
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function read(
|
|
239
|
+
env: Environment,
|
|
240
|
+
): <
|
|
241
|
+
TAbi extends Abi,
|
|
242
|
+
TFunctionName extends ContractFunctionName<TAbi, 'pure' | 'view'>,
|
|
243
|
+
TArgs extends ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName> = ContractFunctionArgs<
|
|
244
|
+
TAbi,
|
|
245
|
+
'pure' | 'view',
|
|
246
|
+
TFunctionName
|
|
247
|
+
>,
|
|
248
|
+
>(
|
|
249
|
+
deployment: MinimalDeployment<TAbi>,
|
|
250
|
+
args: ReadingArgs<TAbi, TFunctionName, TArgs>,
|
|
251
|
+
) => Promise<DecodeFunctionResultReturnType<TAbi, TFunctionName>> {
|
|
252
|
+
return async <
|
|
253
|
+
TAbi extends Abi,
|
|
254
|
+
TFunctionName extends ContractFunctionName<TAbi, 'pure' | 'view'>,
|
|
255
|
+
TArgs extends ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName> = ContractFunctionArgs<
|
|
256
|
+
TAbi,
|
|
257
|
+
'pure' | 'view',
|
|
258
|
+
TFunctionName
|
|
259
|
+
>,
|
|
260
|
+
>(
|
|
261
|
+
deployment: MinimalDeployment<TAbi>,
|
|
262
|
+
args: ReadingArgs<TAbi, TFunctionName, TArgs>,
|
|
263
|
+
) => {
|
|
264
|
+
const {account, ...viemArgs} = args;
|
|
265
|
+
const address = account ? resolveAccountOrUndefined(account, env) : undefined;
|
|
266
|
+
|
|
267
|
+
const artifactToUse = deployment as unknown as Artifact<TAbi>;
|
|
268
|
+
const abi = artifactToUse.abi;
|
|
269
|
+
const calldata = encodeFunctionData<TAbi, TFunctionName>({
|
|
270
|
+
abi,
|
|
271
|
+
functionName: viemArgs.functionName,
|
|
272
|
+
args: viemArgs.args,
|
|
273
|
+
} as any);
|
|
274
|
+
|
|
275
|
+
const result: `0x${string}` = (await env.network.provider.request({
|
|
276
|
+
method: 'eth_call',
|
|
277
|
+
params: [
|
|
278
|
+
{
|
|
279
|
+
to: deployment.address,
|
|
280
|
+
type: '0x2',
|
|
281
|
+
from: address,
|
|
282
|
+
chainId: `0x${env.network.chain.id.toString(16)}` as `0x${string}`,
|
|
283
|
+
data: calldata,
|
|
284
|
+
// value: `0x${viemArgs.value?.toString(16)}` as `0x${string}`,
|
|
285
|
+
},
|
|
286
|
+
] as any, // TODO fix eip-1193 ?,,
|
|
287
|
+
})) as `0x${string}`;
|
|
288
|
+
|
|
289
|
+
const parsed = decodeFunctionResult<TAbi, TFunctionName>({
|
|
290
|
+
abi,
|
|
291
|
+
functionName: viemArgs.functionName,
|
|
292
|
+
data: result,
|
|
293
|
+
args: viemArgs.args,
|
|
294
|
+
} as any);
|
|
295
|
+
|
|
296
|
+
return parsed as DecodeFunctionResultReturnType<TAbi, TFunctionName>;
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export function readByName(
|
|
301
|
+
env: Environment,
|
|
302
|
+
): <
|
|
303
|
+
TAbi extends Abi,
|
|
304
|
+
TFunctionName extends ContractFunctionName<TAbi, 'pure' | 'view'>,
|
|
305
|
+
TArgs extends ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName> = ContractFunctionArgs<
|
|
306
|
+
TAbi,
|
|
307
|
+
'pure' | 'view',
|
|
308
|
+
TFunctionName
|
|
309
|
+
>,
|
|
310
|
+
>(
|
|
311
|
+
name: string,
|
|
312
|
+
args: ReadingArgs<TAbi, TFunctionName, TArgs>,
|
|
313
|
+
) => Promise<DecodeFunctionResultReturnType<TAbi, TFunctionName>> {
|
|
314
|
+
return async <
|
|
315
|
+
TAbi extends Abi,
|
|
316
|
+
TFunctionName extends ContractFunctionName<TAbi, 'pure' | 'view'>,
|
|
317
|
+
TArgs extends ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName> = ContractFunctionArgs<
|
|
318
|
+
TAbi,
|
|
319
|
+
'pure' | 'view',
|
|
320
|
+
TFunctionName
|
|
321
|
+
>,
|
|
322
|
+
>(
|
|
323
|
+
name: string,
|
|
324
|
+
args: ReadingArgs<TAbi, TFunctionName, TArgs>,
|
|
325
|
+
) => {
|
|
326
|
+
const deployment = env.getOrNull<TAbi>(name);
|
|
327
|
+
if (!deployment) {
|
|
328
|
+
throw new Error(`no deployment named ${name}`);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return read(env)(deployment, args);
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function tx(env: Environment): (txData: TransactionData, options?: {message?: string}) => Promise<EIP1193DATA> {
|
|
336
|
+
return async (txData: TransactionData, options?: {message?: string}) => {
|
|
337
|
+
const {account, ...viemArgs} = txData;
|
|
338
|
+
const address = resolveAccount(account, env);
|
|
339
|
+
|
|
340
|
+
const signer = env.addressSigners[address];
|
|
341
|
+
|
|
342
|
+
const txParam: EIP1193TransactionData = {
|
|
343
|
+
type: '0x2',
|
|
344
|
+
to: txData.to || undefined,
|
|
345
|
+
from: address,
|
|
346
|
+
chainId: `0x${env.network.chain.id.toString(16)}` as `0x${string}`,
|
|
347
|
+
data: txData.data,
|
|
348
|
+
gas: viemArgs.gas ? (`0x${viemArgs.gas.toString(16)}` as `0x${string}`) : undefined,
|
|
349
|
+
maxFeePerGas: viemArgs.maxFeePerGas ? (`0x${viemArgs.maxFeePerGas.toString(16)}` as `0x${string}`) : undefined,
|
|
350
|
+
maxPriorityFeePerGas: viemArgs.maxPriorityFeePerGas
|
|
351
|
+
? (`0x${viemArgs.maxPriorityFeePerGas.toString(16)}` as `0x${string}`)
|
|
352
|
+
: undefined,
|
|
353
|
+
// nonce: viemArgs.nonce ? (`0x${viemArgs.nonce.toString(16)}` as `0x${string}`) : undefined,
|
|
354
|
+
accessList: viemArgs.accessList as any, // TODO check
|
|
355
|
+
};
|
|
356
|
+
if (viemArgs.value) {
|
|
357
|
+
txParam.value = `0x${viemArgs.value?.toString(16)}` as `0x${string}`;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
let txHash: `0x${string}`;
|
|
361
|
+
if (signer.type === 'wallet' || signer.type === 'remote') {
|
|
362
|
+
txHash = await signer.signer.request({
|
|
363
|
+
method: 'eth_sendTransaction',
|
|
364
|
+
params: [txParam] as any, // TODO fix eip-1193 ?,,
|
|
365
|
+
});
|
|
366
|
+
} else {
|
|
367
|
+
const rawTx = await signer.signer.request({
|
|
368
|
+
method: 'eth_signTransaction',
|
|
369
|
+
params: [txParam],
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
txHash = await env.network.provider.request({
|
|
373
|
+
method: 'eth_sendRawTransaction',
|
|
374
|
+
params: [rawTx],
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const pendingExecution: PendingExecution = {
|
|
379
|
+
type: 'execution',
|
|
380
|
+
transaction: {hash: txHash, origin: address},
|
|
381
|
+
// description, // TODO
|
|
382
|
+
// TODO we should have the nonce, except for wallet like metamask where it is not sure you get the nonce you start with
|
|
383
|
+
};
|
|
384
|
+
await env.savePendingExecution(pendingExecution, options?.message);
|
|
385
|
+
return txHash;
|
|
386
|
+
};
|
|
387
|
+
}
|