Skip to content

Commit

Permalink
Fix TypeScript Lints
Browse files Browse the repository at this point in the history
  • Loading branch information
nlordell committed Aug 8, 2024
1 parent 4e697bd commit 9af48c1
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 161 deletions.
2 changes: 1 addition & 1 deletion examples/4337-gas-metering/pimlico/pimlico.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ if (transactionType === UserOperationType.VerifyingPaymaster) {

if (senderUSDCBalance < usdcAmount) {
console.log(`\nTransferring ${usdcAmount / usdcDenomination} USDC Token for paying the Paymaster from Sender to Safe.`)
await transferERC20Token(usdcTokenAddress, publicClient, signer, senderAddress, usdcAmount, chain, paymaster)
await transferERC20Token(usdcTokenAddress, publicClient, signer, senderAddress, usdcAmount, paymaster)
while (senderUSDCBalance < usdcAmount) {
await setTimeout(15000)
senderUSDCBalance = await getERC20Balance(usdcTokenAddress, publicClient, senderAddress)
Expand Down
30 changes: 15 additions & 15 deletions examples/4337-gas-metering/utils/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const ERC20_TOKEN_APPROVE_ABI = [
stateMutability: 'nonpayable',
type: 'function',
},
]
] as const

export const ERC20_TOKEN_TRANSFER_ABI = [
{
Expand All @@ -24,7 +24,7 @@ export const ERC20_TOKEN_TRANSFER_ABI = [
stateMutability: 'nonpayable',
type: 'function',
},
]
] as const

export const ERC20_TOKEN_DECIMALS_ABI = [
{
Expand All @@ -34,7 +34,7 @@ export const ERC20_TOKEN_DECIMALS_ABI = [
type: 'function',
stateMutability: 'view',
},
]
] as const

export const ERC20_TOKEN_BALANCE_OF_ABI = [
{
Expand All @@ -44,7 +44,7 @@ export const ERC20_TOKEN_BALANCE_OF_ABI = [
type: 'function',
stateMutability: 'view',
},
]
] as const

export const ERC20_TOKEN_MINT_ABI = [
{
Expand All @@ -57,7 +57,7 @@ export const ERC20_TOKEN_MINT_ABI = [
type: 'function',
stateMutability: 'public',
},
]
] as const

export const ERC721_TOKEN_SAFEMINT_ABI = [
{
Expand All @@ -68,7 +68,7 @@ export const ERC721_TOKEN_SAFEMINT_ABI = [
stateMutability: 'nonpayable',
type: 'function',
},
]
] as const

export const MULTISEND_ABI = [
{
Expand All @@ -78,7 +78,7 @@ export const MULTISEND_ABI = [
stateMutability: 'payable',
type: 'function',
},
]
] as const

export const SAFE_NONCE_ABI = [
{
Expand All @@ -88,7 +88,7 @@ export const SAFE_NONCE_ABI = [
type: 'function',
stateMutability: 'view',
},
]
] as const

export const SAFE_EXECTRANSACTION_ABI = [
{
Expand All @@ -110,7 +110,7 @@ export const SAFE_EXECTRANSACTION_ABI = [
stateMutability: 'external',
type: 'function',
},
]
] as const

export const SAFE_SETUP_ABI = [
{
Expand Down Expand Up @@ -161,7 +161,7 @@ export const SAFE_SETUP_ABI = [
stateMutability: 'nonpayable',
type: 'function',
},
]
] as const

export const SAFE_ENABLE_MODULES_ABI = [
{
Expand All @@ -177,7 +177,7 @@ export const SAFE_ENABLE_MODULES_ABI = [
stateMutability: 'nonpayable',
type: 'function',
},
]
] as const

export const SAFE_FACTORY_CREATE_PROXY_WITH_NONCE_ABI = [
{
Expand Down Expand Up @@ -209,7 +209,7 @@ export const SAFE_FACTORY_CREATE_PROXY_WITH_NONCE_ABI = [
stateMutability: 'nonpayable',
type: 'function',
},
]
] as const

export const SAFE_4337_EXECUTE_USEROP_ABI = [
{
Expand Down Expand Up @@ -240,7 +240,7 @@ export const SAFE_4337_EXECUTE_USEROP_ABI = [
stateMutability: 'nonpayable',
type: 'function',
},
]
] as const

export const SAFE_FACTORY_PROXY_CREATION_CODE_ABI = [
{
Expand All @@ -256,7 +256,7 @@ export const SAFE_FACTORY_PROXY_CREATION_CODE_ABI = [
stateMutability: 'pure',
type: 'function',
},
]
] as const

export const SAFE_4337_MODULE_ABI = [
{ inputs: [{ internalType: 'address', name: 'entryPoint', type: 'address' }], stateMutability: 'nonpayable', type: 'constructor' },
Expand Down Expand Up @@ -476,4 +476,4 @@ export const SAFE_4337_MODULE_ABI = [
stateMutability: 'nonpayable',
type: 'function',
},
]
] as const
172 changes: 49 additions & 123 deletions examples/4337-gas-metering/utils/erc20.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import dotenv from 'dotenv'
import { HttpTransport, http, Address, encodeFunctionData, createWalletClient, PrivateKeyAccount, PublicClient } from 'viem'
import { baseSepolia, goerli, sepolia } from 'viem/chains'
import { Address, Chain, PrivateKeyAccount, PublicClient, Transport, createWalletClient, encodeFunctionData, http } from 'viem'
import {
ERC20_TOKEN_APPROVE_ABI,
ERC20_TOKEN_BALANCE_OF_ABI,
Expand Down Expand Up @@ -32,166 +31,76 @@ export const generateTransferCallData = (to: Address, value: bigint) => {
return transferData
}

export const getERC20Decimals = async (
export const getERC20Decimals = async <C extends Chain>(
erc20TokenAddress: Address,
publicClient: PublicClient<HttpTransport, typeof baseSepolia | typeof sepolia>,
): Promise<bigint> => {
const erc20Decimals = (await publicClient.readContract({
publicClient: PublicClient<Transport<'http'>, C>,
): Promise<number> => {
const erc20Decimals = await publicClient.readContract({
abi: ERC20_TOKEN_DECIMALS_ABI,
address: erc20TokenAddress,
functionName: 'decimals',
})) as bigint
})

return erc20Decimals
}

export const getERC20Balance = async (
export const getERC20Balance = async <C extends Chain>(
erc20TokenAddress: Address,
publicClient: PublicClient<HttpTransport, typeof baseSepolia | typeof sepolia>,
publicClient: PublicClient<Transport<'http'>, C>,
owner: Address,
): Promise<bigint> => {
const senderERC20Balance = (await publicClient.readContract({
const senderERC20Balance = await publicClient.readContract({
abi: ERC20_TOKEN_BALANCE_OF_ABI,
address: erc20TokenAddress,
functionName: 'balanceOf',
args: [owner],
})) as bigint
})

return senderERC20Balance
}

export const mintERC20Token = async (
export const mintERC20Token = async <C extends Chain>(
erc20TokenAddress: Address,
publicClient: PublicClient<HttpTransport, typeof baseSepolia | typeof sepolia>,
publicClient: PublicClient<Transport<'http'>, C>,
signer: PrivateKeyAccount,
to: Address,
amount: bigint,
chain: string,
paymaster: string,
) => {
let walletClient
if (paymaster == 'pimlico') {
if (chain == 'sepolia') {
walletClient = createWalletClient({
account: signer,
chain: sepolia,
transport: http(pimlicoRPCURL),
})
} else if (chain == 'base-sepolia') {
walletClient = createWalletClient({
account: signer,
chain: baseSepolia,
transport: http(pimlicoRPCURL),
})
} else {
throw new Error('Current code only support limited networks. Please make required changes if you want to use custom network.')
}
} else if (paymaster == 'alchemy') {
if (chain == 'sepolia') {
walletClient = createWalletClient({
account: signer,
chain: sepolia,
transport: http(alchemyRPCURL),
})
} else if (chain == 'goerli') {
walletClient = createWalletClient({
account: signer,
chain: goerli,
transport: http(alchemyRPCURL),
})
} else {
throw new Error('Current code only support limited networks. Please make required changes if you want to use custom network.')
}
} else if (paymaster == 'gelato') {
if (chain == 'sepolia') {
walletClient = createWalletClient({
account: signer,
chain: sepolia,
transport: http(gelatoRPCURL),
})
} else if (chain == 'base-sepolia') {
walletClient = createWalletClient({
account: signer,
chain: baseSepolia,
transport: http(gelatoRPCURL),
})
} else {
throw new Error('Current code only support limited networks. Please make required changes if you want to use custom network.')
}
} else {
throw new Error('Current code only support Pimlico and Alchemy. Please make required changes if you want to use a different Paymaster.')
}
const walletClient = createWalletClient({
account: signer,
chain: publicClient.chain,
transport: getTransport(paymaster),
})

const { request } = await publicClient.simulateContract({
address: erc20TokenAddress,
abi: ERC20_TOKEN_MINT_ABI,
functionName: 'mint',
args: [to, amount],
account: signer,
})
await walletClient.writeContract(request)

// I cannot get Viem to accept the `request` type here, and it seems to be related to this
// function being generic on the chain type. Using concrete chain types helps, but doesn't
// completely solve the issue either.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await walletClient.writeContract(request as any)
}

export const transferERC20Token = async (
export const transferERC20Token = async <C extends Chain>(
erc20TokenAddress: Address,
publicClient: PublicClient<HttpTransport, typeof baseSepolia | typeof sepolia>,
publicClient: PublicClient<Transport<'http'>, C>,
signer: PrivateKeyAccount,
to: Address,
amount: bigint,
chain: string,
paymaster: string,
) => {
let walletClient
if (paymaster == 'pimlico') {
if (chain == 'sepolia') {
walletClient = createWalletClient({
account: signer,
chain: sepolia,
transport: http(pimlicoRPCURL),
})
} else if (chain == 'base-sepolia') {
walletClient = createWalletClient({
account: signer,
chain: baseSepolia,
transport: http(pimlicoRPCURL),
})
} else {
throw new Error('Current code only support limited networks. Please make required changes if you want to use custom network.')
}
} else if (paymaster == 'alchemy') {
if (chain == 'sepolia') {
walletClient = createWalletClient({
account: signer,
chain: sepolia,
transport: http(alchemyRPCURL),
})
} else if (chain == 'goerli') {
walletClient = createWalletClient({
account: signer,
chain: goerli,
transport: http(alchemyRPCURL),
})
} else {
throw new Error('Current code only support limited networks. Please make required changes if you want to use custom network.')
}
} else if (paymaster == 'gelato') {
if (chain == 'sepolia') {
walletClient = createWalletClient({
account: signer,
chain: sepolia,
transport: http(gelatoRPCURL),
})
} else if (chain == 'base-sepolia') {
walletClient = createWalletClient({
account: signer,
chain: baseSepolia,
transport: http(gelatoRPCURL),
})
} else {
throw new Error('Current code only support limited networks. Please make required changes if you want to use custom network.')
}
} else {
throw new Error('Current code only support Pimlico and Alchemy. Please make required changes if you want to use a different Paymaster.')
}
const walletClient = createWalletClient({
account: signer,
chain: publicClient.chain,
transport: getTransport(paymaster),
})

const signerERC20Bal = await getERC20Balance(erc20TokenAddress, publicClient, signer.address)
if (signerERC20Bal < amount) {
Expand All @@ -206,5 +115,22 @@ export const transferERC20Token = async (
args: [to, amount],
account: signer,
})
await walletClient.writeContract(request)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
await walletClient.writeContract(request as any)
}

const getTransport = (paymaster: string): Transport<'http'> => {
switch (paymaster) {
case 'pimlico':
return http(pimlicoRPCURL)
case 'alchemy':
return http(alchemyRPCURL)
case 'gelato':
return http(gelatoRPCURL)
default:
throw new Error(
'Current code only support Alchemy, Pimlico and Gelato. Please make required changes if you want to use a different Paymaster.',
)
}
}
6 changes: 3 additions & 3 deletions examples/4337-gas-metering/utils/nativeTransfer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dotenv from 'dotenv'
import { http, createWalletClient, PrivateKeyAccount, Address, WalletClient, HttpTransport, Chain, Account, PublicClient } from 'viem'
import { Account, Address, Chain, HttpTransport, PrivateKeyAccount, PublicClient, WalletClient, createWalletClient, http } from 'viem'
import { baseSepolia, goerli, sepolia } from 'viem/chains'
import { setTimeout } from 'timers/promises'

Expand All @@ -8,8 +8,8 @@ const pimlicoRPCURL = process.env.PIMLICO_RPC_URL
const alchemyRPCURL = process.env.ALCHEMY_RPC_URL
const gelatoRPCURL = process.env.GELATO_RPC_URL

export const transferETH = async (
publicClient: PublicClient<HttpTransport, typeof baseSepolia | typeof sepolia | typeof goerli>,
export const transferETH = async <C extends Chain>(
publicClient: PublicClient<HttpTransport, C>,
signer: PrivateKeyAccount,
receiver: Address,
amount: bigint,
Expand Down
Loading

0 comments on commit 9af48c1

Please sign in to comment.