Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit for viem api #552

Draft
wants to merge 3 commits into
base: fs-viem-init
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/experimental/arbitrumDeposit/abis/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const inboxAbi = [
name: 'depositEth',
outputs: [{ type: 'uint256' }],
stateMutability: 'payable',
type: 'function'
type: 'function',
},
{
inputs: [
Expand All @@ -15,28 +15,26 @@ export const inboxAbi = [
{ name: 'callValueRefundAddress', type: 'address' },
{ name: 'gasLimit', type: 'uint256' },
{ name: 'maxFeePerGas', type: 'uint256' },
{ name: 'data', type: 'bytes' }
{ name: 'data', type: 'bytes' },
],
name: 'createRetryableTicket',
outputs: [{ type: 'uint256' }],
stateMutability: 'payable',
type: 'function'
type: 'function',
},
{
anonymous: false,
inputs: [
{ indexed: false, name: 'messageNum', type: 'uint256' },
{ indexed: false, name: 'data', type: 'bytes' }
{ indexed: false, name: 'data', type: 'bytes' },
],
name: 'InboxMessageDelivered',
type: 'event'
type: 'event',
},
{
anonymous: false,
inputs: [
{ indexed: false, name: 'messageNum', type: 'uint256' }
],
inputs: [{ indexed: false, name: 'messageNum', type: 'uint256' }],
name: 'InboxMessageDeliveredFromOrigin',
type: 'event'
}
] as const
type: 'event',
},
] as const
90 changes: 0 additions & 90 deletions src/experimental/arbitrumDeposit/actions.ts

This file was deleted.

32 changes: 32 additions & 0 deletions src/experimental/arbitrumDeposit/depositEth.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracting action in their own function, means user can either call it with depositEth(client, params) or client.depositEth(params)

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Account, Address, encodeFunctionData, WalletClient } from 'viem'
import { inboxAbi } from './abis/inbox'
import { localEthChain } from '../chains'
import { ArbitrumNetwork } from '../../lib/dataEntities/networks'

export type DepositEthParams = {
amount: bigint
account: Account | Address
gasPrice: bigint
to: ArbitrumNetwork
}
export async function depositEth(
walletClient: WalletClient,
{ amount, account, gasPrice, to }: DepositEthParams
) {
const signedTx = await walletClient.signTransaction({
to: to.ethBridge.inbox as Address,
value: amount,
data: encodeFunctionData({
abi: inboxAbi,
functionName: 'depositEth',
}),
from: typeof account === 'string' ? account : account.address,
account,
chain: localEthChain,
gas: BigInt('130000'),
maxFeePerGas: gasPrice,
maxPriorityFeePerGas: gasPrice,
})

return signedTx
}
23 changes: 23 additions & 0 deletions src/experimental/arbitrumDeposit/prepareDepositEthTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Account, Address, encodeFunctionData, PublicClient } from 'viem'
import { inboxAbi } from './abis/inbox'

export type PrepareDepositEthTransaction = {
amount: bigint
account: Account
inbox: Address
}
export async function prepareDepositEthTransaction(
publicClient: PublicClient,
{ amount, account, inbox }: PrepareDepositEthTransaction
) {
return publicClient.prepareTransactionRequest({
chain: publicClient.chain,
to: inbox,
data: encodeFunctionData({
abi: inboxAbi,
functionName: 'depositEth',
}),
value: amount,
account,
})
}
16 changes: 16 additions & 0 deletions src/experimental/arbitrumDeposit/publicActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { PublicClient } from 'viem'
import { sendDepositEth, SendDepositEthParams } from './sendDepositEth'

export type ArbitrumPublicActions = {
sendDepositEth: (args: SendDepositEthParams) => Promise<`0x${string}`>
}

export function arbitrumPublicActions() {
return <TClient extends PublicClient>(
publicClient: TClient
): ArbitrumPublicActions => {
return {
sendDepositEth: args => sendDepositEth(publicClient, args),
}
}
}
22 changes: 22 additions & 0 deletions src/experimental/arbitrumDeposit/sendDepositEth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { parseTransaction, PublicClient, serializeTransaction } from 'viem'

export type SendDepositEthParams = {
signedTx: any
}
export async function sendDepositEth(
publicClient: PublicClient,
{ signedTx }: SendDepositEthParams
) {
// Parse and serialize with L2 chain ID
const parsedTx = parseTransaction(signedTx.raw)
const serializedTx = serializeTransaction({
...parsedTx,
})

// Send to L2
const hash = await publicClient.sendRawTransaction({
serializedTransaction: serializedTx,
})

return hash
}
16 changes: 16 additions & 0 deletions src/experimental/arbitrumDeposit/walletActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { type WalletClient } from 'viem'
import { depositEth, DepositEthParams } from './depositEth'

export type ArbitrumWalletActions = {
depositEth: (args: DepositEthParams) => Promise<`0x${string}`>
}

export function arbitrumWalletActions() {
return <TClient extends WalletClient>(
walletClient: TClient
): ArbitrumWalletActions => {
return {
depositEth: args => depositEth(walletClient, args),
}
}
}
62 changes: 62 additions & 0 deletions src/experimental/createArbitrumClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Address, PublicClient, WalletClient } from 'viem'
import { arbitrum, arbitrumNova, arbitrumSepolia } from 'viem/chains'
import { prepareDepositEthTransaction } from './arbitrumDeposit/prepareDepositEthTransaction'

function getInboxFromChainId(
chainId:
| typeof arbitrum.id
| typeof arbitrumNova.id
| typeof arbitrumSepolia.id
) {
return {
[arbitrum.id]: '0x...',
[arbitrumNova.id]: '0x...',
[arbitrumSepolia.id]: '0x...',
}[chainId]
}

export async function createArbitrumClient({
parentChainPublicClient,
parentChainWalletClient,
childChainPublicClient,
childChainWalletClient,
}: {
parentChainPublicClient: PublicClient
parentChainWalletClient: WalletClient
childChainPublicClient: PublicClient
childChainWalletClient: WalletClient
}): Promise<{
depositEth: (amount: bigint) => void
}> {
return {
async depositEth(amount: bigint) {
const tx = await prepareDepositEthTransaction(parentChainPublicClient, {
amount,
account: parentChainWalletClient.account!,
inbox: getInboxFromChainId(
childChainWalletClient.chain?.id!

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Lint on Node.js v20

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Unit) on Node.js v18

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Lint on Node.js v18

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Unit) on Node.js v20

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v20

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v18

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v18 with L3

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v18 with L3 with custom gas token (16 decimals)

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v20 with L3 with custom gas token (20 decimals)

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v18 with L3 with custom gas token (20 decimals)

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v20 with L3

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v18 with L3 with custom gas token (18 decimals)

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v20 with L3 with custom gas token (18 decimals)

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.

Check failure on line 37 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v20 with L3 with custom gas token (16 decimals)

Argument of type 'number' is not assignable to parameter of type '42161 | 42170 | 421614'.
) as Address,
})
const hash = await parentChainPublicClient.sendRawTransaction({
serializedTransaction: await parentChainWalletClient.signTransaction(
tx

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Lint on Node.js v20

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Unit) on Node.js v18

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Lint on Node.js v18

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Unit) on Node.js v20

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v20

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v18

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v18 with L3

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v18 with L3 with custom gas token (16 decimals)

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v20 with L3 with custom gas token (20 decimals)

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v18 with L3 with custom gas token (20 decimals)

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v20 with L3

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v18 with L3 with custom gas token (18 decimals)

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v20 with L3 with custom gas token (18 decimals)

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.

Check failure on line 42 in src/experimental/createArbitrumClient.ts

View workflow job for this annotation

GitHub Actions / Test (Integration) on Node.js v20 with L3 with custom gas token (16 decimals)

Argument of type '{ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; blo...' is not assignable to parameter of type '({ data?: `0x${string}` | undefined; value?: bigint | undefined; from: `0x${string}`; to?: `0x${string}` | null | undefined; accessList?: undefined; account: { client: Client; entryPoint: { abi: Abi; address: `0x${string}`; version: EntryPointVersion; }; ... 20 more ...; publicKey?: undefined; }; ... 13 more ...; bl...'.
),
})

// Await on childChainPublicClient for the transaction
return hash
},
}
}

/**
* const { depositEth } = createArbitrumClient({
* parentChainPublicClient,
* parentChainWalletClient,
* childChainPublicClient,
* childChainWalletClient,
* })
*
* depositEth({ amount: 150000n })
*
*/
Loading