Skip to content

Commit

Permalink
Merge pull request #83 from pimlicolabs/fix/type
Browse files Browse the repository at this point in the history
fix type
  • Loading branch information
plusminushalf authored Jan 15, 2024
2 parents 4f49845 + ee5e795 commit c5fa2eb
Show file tree
Hide file tree
Showing 41 changed files with 377 additions and 347 deletions.
7 changes: 7 additions & 0 deletions .changeset/light-ravens-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"permissionless": patch
---

1. Account type for smartAccountCLient will be inferred automatically, so you will not have to pass `account` everywhere in functions like `sendTransaction`
2. Switching tests to vitest to enable type testing for better types in future
3. Better types for all the actions, we prettify all the args & return types
2 changes: 1 addition & 1 deletion .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
run: bun run build

- name: Run tests & coverage
run: bun test --coverage
run: bun run test:ci
env:
ENTRYPOINT_ADDRESS: ${{ vars.ENTRYPOINT_ADDRESS }}
FACTORY_ADDRESS: ${{ vars.FACTORY_ADDRESS }}
Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"changeset:version": "changeset version && bun install --lockfile-only",
"format": "biome format . --write",
"lint": "biome check .",
"lint:fix": "bun run lint --apply"
"lint:fix": "bun run lint --apply",
"test": "vitest -c ./test/vitest.config.ts",
"test:ci": "CI=true vitest -c ./test/vitest.config.ts --coverage"
},
"simple-git-hooks": {
"pre-commit": "bun run format && bun run lint:fix"
Expand Down
7 changes: 4 additions & 3 deletions src/accounts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class SignTransactionNotSupportedBySmartAccount extends BaseError {
export type SmartAccount<
Name extends string = string,
transport extends Transport = Transport,
chain extends Chain | undefined = Chain | undefined
chain extends Chain | undefined = Chain | undefined,
TAbi extends Abi | readonly unknown[] = Abi
> = LocalAccount<Name> & {
client: Client<transport, chain>
entryPoint: Address
Expand All @@ -48,7 +49,7 @@ export type SmartAccount<
}[]
) => Promise<Hex>
getDummySignature(userOperation: UserOperation): Promise<Hex>
encodeDeployCallData: <TAbi extends Abi | readonly unknown[] = Abi>({
encodeDeployCallData: ({
abi,
args,
bytecode
Expand All @@ -57,6 +58,6 @@ export type SmartAccount<
}

export type SmartAccountSigner<
TSource extends string,
TSource extends string = string,
TAddress extends Address = Address
> = Omit<LocalAccount<TSource, TAddress>, "signTransaction">
5 changes: 3 additions & 2 deletions src/actions/bundler/estimateUserOperationGas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Account, Address, Chain, Client, Transport } from "viem"
import type { PartialBy } from "viem/types/utils"
import type { BundlerClient } from "../../clients/createBundlerClient.js"
import type { BundlerRpcSchema } from "../../types/bundler.js"
import type { Prettify } from "../../types/index.js"
import type { UserOperation } from "../../types/userOperation.js"
import type { UserOperationWithBigIntAsHex } from "../../types/userOperation.js"
import { deepHexlify } from "../../utils/deepHexlify.js"
Expand Down Expand Up @@ -53,8 +54,8 @@ export const estimateUserOperationGas = async <
TAccount extends Account | undefined = Account | undefined
>(
client: Client<TTransport, TChain, TAccount, BundlerRpcSchema>,
args: EstimateUserOperationGasParameters
): Promise<EstimateUserOperationGasReturnType> => {
args: Prettify<EstimateUserOperationGasParameters>
): Promise<Prettify<EstimateUserOperationGasReturnType>> => {
const { userOperation, entryPoint } = args

const response = await client.request({
Expand Down
7 changes: 4 additions & 3 deletions src/actions/bundler/getUserOperationByHash.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Account, Address, Chain, Client, Hash, Transport } from "viem"
import type { BundlerClient } from "../../clients/createBundlerClient.js"
import type { BundlerRpcSchema } from "../../types/bundler.js"
import type { Prettify } from "../../types/index.js"
import type { UserOperation } from "../../types/userOperation.js"

export type GetUserOperationByHashParameters = {
Expand All @@ -13,7 +14,7 @@ export type GetUserOperationByHashReturnType = {
transactionHash: Hash
blockHash: Hash
blockNumber: bigint
} | null
}

/**
* Returns the user operation from userOpHash
Expand Down Expand Up @@ -43,8 +44,8 @@ export const getUserOperationByHash = async <
TAccount extends Account | undefined = Account | undefined
>(
client: Client<TTransport, TChain, TAccount, BundlerRpcSchema>,
{ hash }: GetUserOperationByHashParameters
): Promise<GetUserOperationByHashReturnType> => {
{ hash }: Prettify<GetUserOperationByHashParameters>
): Promise<Prettify<GetUserOperationByHashReturnType> | null> => {
const params: [Hash] = [hash]

const response = await client.request({
Expand Down
5 changes: 3 additions & 2 deletions src/actions/bundler/getUserOperationReceipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
} from "viem"
import type { BundlerClient } from "../../clients/createBundlerClient.js"
import type { BundlerRpcSchema } from "../../types/bundler.js"
import type { Prettify } from "../../types/index.js"
import type { TStatus } from "../../types/userOperation.js"
import { transactionReceiptStatus } from "../../utils/deepHexlify.js"

Expand Down Expand Up @@ -77,8 +78,8 @@ export const getUserOperationReceipt = async <
TAccount extends Account | undefined = Account | undefined
>(
client: Client<TTransport, TChain, TAccount, BundlerRpcSchema>,
{ hash }: GetUserOperationReceiptParameters
): Promise<GetUserOperationReceiptReturnType | null> => {
{ hash }: Prettify<GetUserOperationReceiptParameters>
): Promise<Prettify<GetUserOperationReceiptReturnType> | null> => {
const params: [Hash] = [hash]

const response = await client.request({
Expand Down
3 changes: 2 additions & 1 deletion src/actions/bundler/sendUserOperation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Account, Address, Chain, Client, Hash, Transport } from "viem"
import type { BundlerClient } from "../../clients/createBundlerClient.js"
import type { BundlerRpcSchema } from "../../types/bundler.js"
import type { Prettify } from "../../types/index.js"
import type {
UserOperation,
UserOperationWithBigIntAsHex
Expand Down Expand Up @@ -43,7 +44,7 @@ export const sendUserOperation = async <
TAccount extends Account | undefined = Account | undefined
>(
client: Client<TTransport, TChain, TAccount, BundlerRpcSchema>,
args: SendUserOperationParameters
args: Prettify<SendUserOperationParameters>
): Promise<Hash> => {
const { userOperation, entryPoint } = args

Expand Down
5 changes: 3 additions & 2 deletions src/actions/bundler/waitForUserOperationReceipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type Transport,
stringify
} from "viem"
import type { Prettify } from "../../types/index.js"
import { getAction } from "../../utils/getAction.js"
import { observe } from "../../utils/observe.js"
import {
Expand Down Expand Up @@ -66,8 +67,8 @@ export const waitForUserOperationReceipt = <
hash,
pollingInterval = bundlerClient.pollingInterval,
timeout
}: WaitForUserOperationReceiptParameters
): Promise<GetUserOperationReceiptReturnType> => {
}: Prettify<WaitForUserOperationReceiptParameters>
): Promise<Prettify<GetUserOperationReceiptReturnType>> => {
const observerId = stringify([
"waitForUserOperationReceipt",
bundlerClient.uid,
Expand Down
4 changes: 2 additions & 2 deletions src/actions/pimlico.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {
} from "../clients/decorators/pimlico.js"

import {
type ValidateSponsorshipPolicies,
type ValidateSponsorshipPoliciesParameters,
type ValidateSponsorshipPoliciesReturnType,
validateSponsorshipPolicies
} from "./pimlico/validateSponsorshipPolicies.js"

Expand All @@ -37,7 +37,7 @@ export type {
PimlicoBundlerActions,
PimlicoPaymasterClientActions,
ValidateSponsorshipPoliciesParameters,
ValidateSponsorshipPoliciesReturnType
ValidateSponsorshipPolicies
}

export {
Expand Down
3 changes: 2 additions & 1 deletion src/actions/pimlico/getUserOperationGasPrice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Account, Chain, Client, Transport } from "viem"
import type { Prettify } from "../../types/index.js"
import type { PimlicoBundlerRpcSchema } from "../../types/pimlico.js"

export type GetUserOperationGasPriceReturnType = {
Expand Down Expand Up @@ -43,7 +44,7 @@ export const getUserOperationGasPrice = async <
TAccount extends Account | undefined = Account | undefined
>(
client: Client<TTransport, TChain, TAccount, PimlicoBundlerRpcSchema>
): Promise<GetUserOperationGasPriceReturnType> => {
): Promise<Prettify<GetUserOperationGasPriceReturnType>> => {
const gasPrices = await client.request({
method: "pimlico_getUserOperationGasPrice",
params: []
Expand Down
5 changes: 3 additions & 2 deletions src/actions/pimlico/getUserOperationStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Account, Chain, Client, Hash, Transport } from "viem"
import type { PimlicoBundlerClient } from "../../clients/pimlico.js"
import type { Prettify } from "../../types/index.js"
import type {
PimlicoBundlerRpcSchema,
PimlicoUserOperationStatus
Expand Down Expand Up @@ -39,8 +40,8 @@ export const getUserOperationStatus = async <
TAccount extends Account | undefined = Account | undefined
>(
client: Client<TTransport, TChain, TAccount, PimlicoBundlerRpcSchema>,
{ hash }: GetUserOperationStatusParameters
): Promise<GetUserOperationStatusReturnType> => {
{ hash }: Prettify<GetUserOperationStatusParameters>
): Promise<Prettify<GetUserOperationStatusReturnType>> => {
return client.request({
method: "pimlico_getUserOperationStatus",
params: [hash]
Expand Down
5 changes: 3 additions & 2 deletions src/actions/pimlico/sponsorUserOperation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Account, Address, Chain, Client, Transport } from "viem"
import type { PartialBy } from "viem/types/utils"
import type { Prettify } from "../../types/index.js"
import type { PimlicoPaymasterRpcSchema } from "../../types/pimlico.js"
import type {
UserOperation,
Expand Down Expand Up @@ -52,8 +53,8 @@ export const sponsorUserOperation = async <
TAccount extends Account | undefined = Account | undefined
>(
client: Client<TTransport, TChain, TAccount, PimlicoPaymasterRpcSchema>,
args: PimlicoSponsorUserOperationParameters
): Promise<SponsorUserOperationReturnType> => {
args: Prettify<PimlicoSponsorUserOperationParameters>
): Promise<Prettify<SponsorUserOperationReturnType>> => {
const response = await client.request({
method: "pm_sponsorUserOperation",
params: args.sponsorshipPolicyId
Expand Down
11 changes: 6 additions & 5 deletions src/actions/pimlico/validateSponsorshipPolicies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Account, Address, Chain, Client, Transport } from "viem"
import type { Prettify } from "../../types/index.js"
import type { PimlicoPaymasterRpcSchema } from "../../types/pimlico.js"
import type {
UserOperation,
Expand All @@ -12,23 +13,23 @@ export type ValidateSponsorshipPoliciesParameters = {
sponsorshipPolicyIds: string[]
}

export type ValidateSponsorshipPoliciesReturnType = {
export type ValidateSponsorshipPolicies = {
sponsorshipPolicyId: string
data: {
name: string | null
author: string | null
icon: string | null
description: string | null
}
}[]
}

/**
* Returns valid sponsorship policies for a userOperation from the list of ids passed
* - Docs: https://docs.pimlico.io/permissionless/reference/pimlico-paymaster-actions/ValidateSponsorshipPolicies
*
* @param client {@link PimlicoBundlerClient} that you created using viem's createClient whose transport url is pointing to the Pimlico's bundler.
* @param args {@link ValidateSponsorshipPoliciesParameters} UserOperation you want to sponsor & entryPoint.
* @returns valid sponsorship policies, see {@link ValidateSponsorshipPoliciesReturnType}
* @returns valid sponsorship policies, see {@link ValidateSponsorshipPolicies}
*
* @example
* import { createClient } from "viem"
Expand Down Expand Up @@ -63,8 +64,8 @@ export const validateSponsorshipPolicies = async <
TAccount extends Account | undefined = Account | undefined
>(
client: Client<TTransport, TChain, TAccount, PimlicoPaymasterRpcSchema>,
args: ValidateSponsorshipPoliciesParameters
): Promise<ValidateSponsorshipPoliciesReturnType> => {
args: Prettify<ValidateSponsorshipPoliciesParameters>
): Promise<Prettify<ValidateSponsorshipPolicies>[]> => {
return await client.request({
method: "pm_validateSponsorshipPolicies",
params: [
Expand Down
5 changes: 4 additions & 1 deletion src/actions/public/getAccountNonce.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Address, Chain, Client, Transport } from "viem"
import { readContract } from "viem/actions"
import type { Prettify } from "../../types/index.js"
import { getAction } from "../../utils/getAction.js"

export type GetAccountNonceParams = {
Expand Down Expand Up @@ -39,8 +40,10 @@ export const getAccountNonce = async <
TChain extends Chain | undefined = Chain | undefined
>(
client: Client<TTransport, TChain>,
{ sender, entryPoint, key = BigInt(0) }: GetAccountNonceParams
args: Prettify<GetAccountNonceParams>
): Promise<bigint> => {
const { sender, entryPoint, key = BigInt(0) } = args

return await getAction(
client,
readContract
Expand Down
5 changes: 4 additions & 1 deletion src/actions/public/getSenderAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "viem"

import { simulateContract } from "viem/actions"
import type { Prettify } from "../../types/index.js"
import { getAction } from "../../utils/getAction.js"

export type GetSenderAddressParams = { initCode: Hex; entryPoint: Address }
Expand Down Expand Up @@ -62,8 +63,10 @@ export const getSenderAddress = async <
TChain extends Chain | undefined = Chain | undefined
>(
client: Client<TTransport, TChain>,
{ initCode, entryPoint }: GetSenderAddressParams
args: Prettify<GetSenderAddressParams>
): Promise<Address> => {
const { initCode, entryPoint } = args

try {
await getAction(
client,
Expand Down
2 changes: 0 additions & 2 deletions src/actions/smartAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {

import {
type SendUserOperationParameters,
type SendUserOperationReturnType,
sendUserOperation
} from "./smartAccount/sendUserOperation.js"

Expand All @@ -44,7 +43,6 @@ export {
sendTransaction,
sendUserOperation,
type SendUserOperationParameters,
type SendUserOperationReturnType,
signMessage,
signTypedData,
type SendTransactionWithPaymasterParameters,
Expand Down
34 changes: 19 additions & 15 deletions src/actions/smartAccount/deployContract.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {
type Abi,
type Chain,
type Client,
type DeployContractParameters,
type DeployContractReturnType,
type Transport
import type {
Abi,
Chain,
Client,
DeployContractParameters,
EncodeDeployDataParameters,
Hash,
Transport
} from "viem"
import type { SmartAccount } from "../../accounts/types.js"
import type { Prettify } from "../../types/index.js"
import { getAction } from "../../utils/getAction.js"
import { parseAccount } from "../../utils/index.js"
import { AccountOrClientNotFoundError } from "../../utils/signUserOperationHashWithECDSA.js"
Expand All @@ -31,7 +33,7 @@ export type DeployContractParametersWithPaymaster<
*
* @param client - Client to use
* @param parameters - {@link DeployContractParameters}
* @returns The [Transaction](https://viem.sh/docs/glossary/terms.html#transaction) hash. {@link DeployContractReturnType}
* @returns The [Transaction](https://viem.sh/docs/glossary/terms.html#transaction) hash.
*
* @example
* import { createWalletClient, http } from 'viem'
Expand All @@ -55,14 +57,16 @@ export async function deployContract<
TAccount extends SmartAccount | undefined
>(
client: Client<Transport, TChain, TAccount>,
{
args: Prettify<DeployContractParametersWithPaymaster>
): Promise<Hash> {
const {
abi,
args,
args: constructorArgs,
bytecode,
sponsorUserOperation,
...request
}: DeployContractParametersWithPaymaster
): Promise<DeployContractReturnType> {
} = args

const { account: account_ = client.account } = request

if (!account_) {
Expand All @@ -84,9 +88,9 @@ export async function deployContract<
maxPriorityFeePerGas: request.maxPriorityFeePerGas || 0n,
callData: await account.encodeDeployCallData({
abi,
args,
bytecode
})
bytecode,
args: constructorArgs
} as EncodeDeployDataParameters)
},
account: account,
sponsorUserOperation
Expand Down
Loading

0 comments on commit c5fa2eb

Please sign in to comment.