-
Notifications
You must be signed in to change notification settings - Fork 177
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
base: fs-viem-init
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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 | ||
} |
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, | ||
}) | ||
} |
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), | ||
} | ||
} | ||
} |
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 | ||
} |
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), | ||
} | ||
} | ||
} |
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 GitHub Actions / Lint on Node.js v20
Check failure on line 37 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Unit) on Node.js v18
Check failure on line 37 in src/experimental/createArbitrumClient.ts GitHub Actions / Lint on Node.js v18
Check failure on line 37 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Unit) on Node.js v20
Check failure on line 37 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v20
Check failure on line 37 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v18
Check failure on line 37 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v18 with L3
Check failure on line 37 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v18 with L3 with custom gas token (16 decimals)
Check failure on line 37 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v20 with L3 with custom gas token (20 decimals)
Check failure on line 37 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v18 with L3 with custom gas token (20 decimals)
Check failure on line 37 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v20 with L3
Check failure on line 37 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v18 with L3 with custom gas token (18 decimals)
Check failure on line 37 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v20 with L3 with custom gas token (18 decimals)
|
||
) as Address, | ||
}) | ||
const hash = await parentChainPublicClient.sendRawTransaction({ | ||
serializedTransaction: await parentChainWalletClient.signTransaction( | ||
tx | ||
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Lint on Node.js v20
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Unit) on Node.js v18
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Lint on Node.js v18
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Unit) on Node.js v20
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v20
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v18
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v18 with L3
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v18 with L3 with custom gas token (16 decimals)
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v20 with L3 with custom gas token (20 decimals)
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v18 with L3 with custom gas token (20 decimals)
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v20 with L3
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v18 with L3 with custom gas token (18 decimals)
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v20 with L3 with custom gas token (18 decimals)
Check failure on line 42 in src/experimental/createArbitrumClient.ts GitHub Actions / Test (Integration) on Node.js v20 with L3 with custom gas token (16 decimals)
|
||
), | ||
}) | ||
|
||
// Await on childChainPublicClient for the transaction | ||
return hash | ||
}, | ||
} | ||
} | ||
|
||
/** | ||
* const { depositEth } = createArbitrumClient({ | ||
* parentChainPublicClient, | ||
* parentChainWalletClient, | ||
* childChainPublicClient, | ||
* childChainWalletClient, | ||
* }) | ||
* | ||
* depositEth({ amount: 150000n }) | ||
* | ||
*/ |
There was a problem hiding this comment.
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)
orclient.depositEth(params)