Skip to content

Commit 4d8ed14

Browse files
committed
refactor: move abi def and typed Permit type to other file
1 parent 2b24459 commit 4d8ed14

File tree

2 files changed

+54
-66
lines changed

2 files changed

+54
-66
lines changed

account-kit/infra/src/gas-manager.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Address, Chain } from "viem";
1+
import type { Address, Chain, Hex } from "viem";
22
import {
33
arbitrum,
44
arbitrumSepolia,
@@ -66,3 +66,38 @@ export const getAlchemyPaymasterAddress = (chain: Chain): Address => {
6666
throw new Error(`Unsupported chain: ${chain}`);
6767
}
6868
};
69+
70+
export const PermitTypes = {
71+
EIP712Domain: [
72+
{ name: "name", type: "string" },
73+
{ name: "version", type: "string" },
74+
{ name: "chainId", type: "uint256" },
75+
{ name: "verifyingContract", type: "address" },
76+
],
77+
Permit: [
78+
{ name: "owner", type: "address" },
79+
{ name: "spender", type: "address" },
80+
{ name: "value", type: "uint256" },
81+
{ name: "nonce", type: "uint256" },
82+
{ name: "deadline", type: "uint256" },
83+
],
84+
} as const;
85+
86+
export const EIP712NoncesAbi = [
87+
"function nonces(address owner) external view returns (uint)",
88+
] as const;
89+
90+
export type PermitMessage = {
91+
owner: Hex;
92+
spender: Hex;
93+
value: bigint;
94+
nonce: bigint;
95+
deadline: bigint;
96+
};
97+
98+
export type PermitDomain = {
99+
name: string;
100+
version: string;
101+
chainId: bigint;
102+
verifyingContract: Hex;
103+
};

account-kit/infra/src/middleware/gasManager.ts

Lines changed: 18 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ import {
3030
encodeFunctionData,
3131
parseAbi,
3232
maxUint256,
33+
sliceHex,
3334
} from "viem";
3435
import type { AlchemySmartAccountClient } from "../client/smartAccountClient.js";
3536
import type { AlchemyTransport } from "../alchemyTransport.js";
3637
import { alchemyFeeEstimator } from "./feeEstimator.js";
38+
import type { RequestGasAndPaymasterAndDataRequest } from "../actions/types.js";
39+
import { PermitTypes, EIP712NoncesAbi } from "../gas-manager.js";
40+
import type { PermitMessage, PermitDomain } from "../gas-manager.js";
3741

3842
/**
3943
* Paymaster middleware factory that uses Alchemy's Gas Manager for sponsoring
@@ -212,22 +216,15 @@ export function alchemyGasAndPaymasterAndDataMiddleware(
212216
: {}),
213217
});
214218

215-
let erc20Context:
216-
| {
217-
tokenAddress: Address;
218-
maxTokenAmount?: bigint;
219-
permit?: Hex;
220-
}
221-
| undefined = undefined;
219+
let erc20Context: RequestGasAndPaymasterAndDataRequest[0]["erc20Context"] =
220+
undefined;
222221
if (policyToken !== undefined) {
223-
let maxAmountToken = policyToken.maxTokenAmount
224-
? policyToken.maxTokenAmount
225-
: maxUint256;
222+
const maxAmountToken = policyToken.maxTokenAmount || maxUint256;
226223

227224
erc20Context = {
228225
tokenAddress: policyToken.address,
229226
...(policyToken.maxTokenAmount
230-
? { maxTokenAmount: maxAmountToken }
227+
? { maxTokenAmount: policyToken.maxTokenAmount }
231228
: {}),
232229
};
233230
if (policyToken.approvalMode === "PERMIT") {
@@ -250,85 +247,41 @@ export function alchemyGasAndPaymasterAndDataMiddleware(
250247
paymasterAddress = paymasterData.paymaster
251248
? paymasterData.paymaster
252249
: paymasterData.paymasterAndData
253-
? (paymasterData.paymasterAndData.slice(0, 42) as Address)
250+
? sliceHex(paymasterData.paymasterAndData, 0, 20)
254251
: undefined;
255252

256-
if (paymasterAddress === undefined) {
253+
if (paymasterAddress === undefined || paymasterAddress === "0x") {
257254
throw new Error("no paymaster contract address available");
258255
}
259256
const deadline = maxUint256;
260-
const eip712Abi = [
261-
"function nonces(address owner) external view returns (uint)",
262-
];
263257
const { data } = await client.call({
264258
to: policyToken.address,
265259
data: encodeFunctionData({
266-
abi: parseAbi(eip712Abi),
260+
abi: parseAbi(EIP712NoncesAbi),
267261
functionName: "nonces",
268-
args: [client.account?.address],
262+
args: [account.address],
269263
}),
270264
});
271265
if (!data) {
272266
throw new Error("No nonces returned from erc20 contract call");
273267
}
274268
console.log(data);
275269
const typedPermitData = {
276-
types: {
277-
EIP712Domain: [
278-
{
279-
name: "name",
280-
type: "string",
281-
},
282-
{
283-
name: "version",
284-
type: "string",
285-
},
286-
{
287-
name: "chainId",
288-
type: "uint256",
289-
},
290-
{
291-
name: "verifyingContract",
292-
type: "address",
293-
},
294-
],
295-
Permit: [
296-
{
297-
name: "owner",
298-
type: "address",
299-
},
300-
{
301-
name: "spender",
302-
type: "address",
303-
},
304-
{
305-
name: "value",
306-
type: "uint256",
307-
},
308-
{
309-
name: "nonce",
310-
type: "uint256",
311-
},
312-
{
313-
name: "deadline",
314-
type: "uint256",
315-
},
316-
],
317-
},
318-
primaryType: "Permit",
270+
types: PermitTypes,
271+
primaryType: "Permit" as const,
319272
domain: {
320273
name: policyToken.erc20Name ?? "",
321274
version: policyToken.version ?? "",
322275
chainId: BigInt(client.chain.id),
323276
verifyingContract: policyToken.address,
324-
},
277+
} as PermitDomain,
325278
message: {
326-
owner: account.address as Hex,
327-
spender: paymasterAddress as Hex,
279+
owner: account.address,
280+
spender: paymasterAddress,
328281
value: maxAmountToken,
329282
nonce: BigInt(data),
330283
deadline,
331-
},
284+
} as PermitMessage,
332285
} as const;
333286

334287
const signedPermit = await account.signTypedData(typedPermitData);

0 commit comments

Comments
 (0)