Skip to content
Open
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
33 changes: 19 additions & 14 deletions packages/tx/examples/EOACodeTx.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { createEOACode7702Tx } from '@ethereumjs/tx'
import { type PrefixedHexString, createAddressFromPrivateKey, randomBytes } from '@ethereumjs/util'
import { Capability, createEOACode7702Tx } from '@ethereumjs/tx'
import {
type EOACode7702AuthorizationListItem,
type PrefixedHexString,
createAddressFromPrivateKey,
randomBytes,
} from '@ethereumjs/util'

const ones32 = `0x${'01'.repeat(32)}` as PrefixedHexString
const ones32: PrefixedHexString = `0x${'01'.repeat(32)}`
const authorizationListItem: EOACode7702AuthorizationListItem = {
chainId: '0x2',
address: `0x${'20'.repeat(20)}`,
nonce: '0x1',
yParity: '0x1',
r: ones32,
s: ones32,
Copy link
Member

Choose a reason for hiding this comment

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

This does not show how to sign the authorization list item, which is necessary, because the authorization signer does not necessarily have to be the tx signer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, I just refactored this one initially (didn't actually work on it) so that it had at least correct types. We can certainly improve it.

}

const common = new Common({ chain: Mainnet, hardfork: Hardfork.Cancun, eips: [7702] })
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Prague })
const tx = createEOACode7702Tx(
{
authorizationList: [
{
chainId: '0x2',
address: `0x${'20'.repeat(20)}`,
nonce: '0x1',
yParity: '0x1',
r: ones32,
s: ones32,
},
],
authorizationList: [authorizationListItem],
to: createAddressFromPrivateKey(randomBytes(32)),
},
{ common },
Expand All @@ -25,3 +29,4 @@ const tx = createEOACode7702Tx(
console.log(
`EIP-7702 EOA code tx created with ${tx.authorizationList.length} authorization list item(s).`,
)
console.log('Tx supports EIP-7702? ', tx.supports(Capability.EIP7702EOACode))
110 changes: 110 additions & 0 deletions packages/vm/examples/7702/uniswap-swap-transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { RPCStateManager } from '@ethereumjs/statemanager'
import { EOACode7702Tx, type EOACode7702TxData } from '@ethereumjs/tx'

Check warning on line 3 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L1-L3

Added lines #L1 - L3 were not covered by tests
import type { PrefixedHexString } from '@ethereumjs/util'
import {

Check warning on line 5 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L5

Added line #L5 was not covered by tests
createAddressFromPrivateKey,
eoaCode7702SignAuthorization,
hexToBytes,
} from '@ethereumjs/util'
import { createVM, runTx } from '@ethereumjs/vm'
import { Interface, parseEther, parseUnits } from 'ethers'

Check warning on line 11 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L10-L11

Added lines #L10 - L11 were not covered by tests

async function run() {

Check warning on line 13 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L13

Added line #L13 was not covered by tests
// ─── your EOA key & address ───────────────────────────────────────────
const privateKeyHex = '0x1122334455667788112233445566778811223344556677881122334455667788'
const privateKey = hexToBytes(privateKeyHex)
const userAddress = createAddressFromPrivateKey(privateKey)
console.log('EOA:', userAddress.toString())

Check warning on line 18 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L15-L18

Added lines #L15 - L18 were not covered by tests

// ─── set up EthereumJS VM with EIP-7702 enabled ───────────────────────
const common = new Common({
chain: Mainnet,
hardfork: Hardfork.Prague,
})
const stateManager = new RPCStateManager({
provider: 'YourProviderURLHere',
blockTag: 22_000_000n,
})
const vm = await createVM({ common, stateManager })

Check warning on line 29 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L21-L29

Added lines #L21 - L29 were not covered by tests

// ─── constants & ABIs ────────────────────────────────────────────────
const DAI = '0x6B175474E89094C44Da98b954EedeAC495271d0F'
const UNISWAP_V3_ROUTER = '0xE592427A0AEce92De3Edee1F18E0157C05861564'
const WETH = '0xC02aaa39b223FE8D0A0e5c4F27EaD9083C756Cc2'
const COLD_WALLET = `0x${'42'.repeat(20)}`

Check warning on line 35 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L32-L35

Added lines #L32 - L35 were not covered by tests

const erc20Abi = [
'function approve(address _spender, uint256 _amount) external returns (bool success)',
'function transfer(address _to, uint256 _value) public returns (bool success)',
]
const routerAbi = ['function exactInput(bytes)']

Check warning on line 41 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L37-L41

Added lines #L37 - L41 were not covered by tests

// This Batch contract is a placeholder. Replace with your actual batch contract address.
// This contract is responsible for executing multiple calls in a single TX.
const BATCH_CONTRACT = '0xYourBatchContractAddressHere'
const batchAbi = ['function executeBatch(bytes[] calldata, address[] targets) external']

Check warning on line 46 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L45-L46

Added lines #L45 - L46 were not covered by tests

const erc20 = new Interface(erc20Abi)
const router = new Interface(routerAbi)
const batch = new Interface(batchAbi)

Check warning on line 50 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L48-L50

Added lines #L48 - L50 were not covered by tests

// ─── trade parameters ────────────────────────────────────────────────
const amountIn = parseUnits('10000', 18) // 10000 DAI
const amountOut = parseEther('4') // expect at least 4 WETH

Check warning on line 54 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L53-L54

Added lines #L53 - L54 were not covered by tests

// ─── encode your underlying swap data ───────────────────────────────────
const uniswapV3SwapPayload = `0xYourSwapCallDataHere`

Check warning on line 57 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L57

Added line #L57 was not covered by tests

// ─── encode your three sub-calls ───────────────────────────────────────
// 1) DAI approve
const callApprove = erc20.encodeFunctionData('approve', [
UNISWAP_V3_ROUTER,
amountIn,
]) as PrefixedHexString

Check warning on line 64 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L61-L64

Added lines #L61 - L64 were not covered by tests

// 2) Uniswap V3 swapExactInput
const callSwap = router.encodeFunctionData('exactInput', [
uniswapV3SwapPayload,
]) as PrefixedHexString

Check warning on line 69 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L67-L69

Added lines #L67 - L69 were not covered by tests

// 3) sweep WETH to cold wallet
const callTransfer = erc20.encodeFunctionData('transfer', [
COLD_WALLET,
amountOut,
]) as PrefixedHexString

Check warning on line 75 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L72-L75

Added lines #L72 - L75 were not covered by tests

const targets: PrefixedHexString[] = [DAI, UNISWAP_V3_ROUTER, WETH]
const calls = [callApprove, callSwap, callTransfer].map(hexToBytes)

Check warning on line 78 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L77-L78

Added lines #L77 - L78 were not covered by tests

// ─── build & send your single 7702 tx ───────────────────────────────
const batchData = batch.encodeFunctionData('executeBatch', [calls, targets]) as `0x${string}`

Check warning on line 81 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L81

Added line #L81 was not covered by tests

// ─── sign authorization for Batch Contract ──────
const authorizationListItem = eoaCode7702SignAuthorization(
{ chainId: '0x1', address: BATCH_CONTRACT, nonce: `0x1` },
privateKey,
)

Check warning on line 87 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L84-L87

Added lines #L84 - L87 were not covered by tests

const txData: EOACode7702TxData = {
nonce: 0n,
gasLimit: 1_000_000n,
maxFeePerGas: parseUnits('10', 9), // 10 gwei
maxPriorityFeePerGas: parseUnits('5', 9), // 5 gwei
to: userAddress, // Using our own wallet, which will be acting as the BATCH_CONTRACT smart contract
value: 0n,
data: hexToBytes(batchData),
accessList: [],
authorizationList: [authorizationListItem],
}

Check warning on line 99 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L89-L99

Added lines #L89 - L99 were not covered by tests

const tx = new EOACode7702Tx(txData, { common }).sign(privateKey)
const { execResult } = await runTx(vm, { tx })

Check warning on line 102 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L101-L102

Added lines #L101 - L102 were not covered by tests

console.log(
'🔀 Batch swap DAI→WETH → your wallet:',
execResult.exceptionError ? '❌ Failed' : '✅ Success',
)
Copy link
Member

Choose a reason for hiding this comment

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

We could also query the VM here to see the changes (deposit to wallet for instance)

}

Check warning on line 108 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L104-L108

Added lines #L104 - L108 were not covered by tests

run().catch(console.error)

Check warning on line 110 in packages/vm/examples/7702/uniswap-swap-transfer.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/7702/uniswap-swap-transfer.ts#L110

Added line #L110 was not covered by tests
Loading