Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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))
107 changes: 107 additions & 0 deletions packages/vm/examples/7702/uniswap-swap-transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
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.Cancun,
eips: [7702],
Copy link
Member

Choose a reason for hiding this comment

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

While this works, I think for the examples we should just run it on Prague and not on Cancun+7702.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, adjusted

})
const stateManager = new RPCStateManager({
provider: 'YourProviderURLHere',
blockTag: 22_000_000n,
})
const vm = await createVM({ common, stateManager })

Check warning on line 30 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-L30

Added lines #L21 - L30 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)}`
const BATCH_CONTRACT = '0xYourBatchContractAddressHere'

Check warning on line 37 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#L33-L37

Added lines #L33 - L37 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

A note should be placed here what the BATCH_CONTRACT does and the interface. It seems that there is a method which seems to have this type signature: executeBatch([calldata,address][]) (I'm not sure if this is a valid ABI signature)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That signature does not seem to be valid. Will add a note.

Copy link
Member

Choose a reason for hiding this comment

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

I think a signature which works could be executeBatch(bytes[],address[]). If you use MulticallV3 then use aggregate(Call[]) (not sure how solidity constructs/decontstructs these structures): https://github.com/EthereumClassicDAO/multicall3/blob/743c0015fac7f9331c24ca8bd8075e49f19f2ddd/src/Multicall3.sol#L41

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just seeing this now, but yes I've adjusted to bytes[] address[], fixes the next steps too


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)']
const batchAbi = ['function executeBatch(bytes[] calldata) external']

Check warning on line 44 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#L39-L44

Added lines #L39 - L44 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

Oh nvm this interface is here. But it seems that the encoding done in this example is done differently 🤔


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

Check warning on line 48 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#L46-L48

Added lines #L46 - L48 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 52 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#L51-L52

Added lines #L51 - L52 were not covered by tests

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

Check warning on line 55 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#L55

Added line #L55 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 62 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#L59-L62

Added lines #L59 - L62 were not covered by tests

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

Check warning on line 67 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#L65-L67

Added lines #L65 - L67 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 73 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#L70-L73

Added lines #L70 - L73 were not covered by tests

const calls = [callApprove, callSwap, callTransfer].map(hexToBytes)

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#L75

Added line #L75 was not covered by tests

// ─── sign authorization for each ──────
const targets: PrefixedHexString[] = [DAI, UNISWAP_V3_ROUTER, WETH]
const auths = targets.map((address, i) =>
eoaCode7702SignAuthorization({ chainId: '0x1', address, nonce: `0x${i}` }, privateKey),
Copy link
Member

Choose a reason for hiding this comment

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

This is incorrect. This will delegate the user EOA first to DAI, then to UNISWAP_V3_ROUTER, and then to WETH (in the same transaction). The net result is that the EOA will point to WETH. We want to point it to the BATCH_CONTRACT, such that if we call into our EOA it will batch-call the targets (as implied by the encoding)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah you are so right! That's an oversight on my part, adjusting.

)

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#L78-L81

Added lines #L78 - L81 were not covered by tests

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

Check warning on line 84 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

Added line #L84 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

The calldata is mapped to an array (which is fine). How does the executeBatch know which target it should call?


const txData: EOACode7702TxData = {
nonce: 0n,
gasLimit: 1_000_000n,
maxFeePerGas: parseUnits('10', 9), // 10 gwei
maxPriorityFeePerGas: parseUnits('5', 9), // 5 gwei
to: BATCH_CONTRACT,
Copy link
Member

Choose a reason for hiding this comment

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

This will call the batch contract. It will therfore interact with WETH/DAI/Uniswap from the batch contract. We want to interact with it from our EOA (which is why EOA should be delegated to BATCH_CONTRACT)

value: 0n,
data: hexToBytes(batchData),
accessList: [],
authorizationList: auths,
}

Check warning on line 96 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#L86-L96

Added lines #L86 - L96 were not covered by tests

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

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#L98-L99

Added lines #L98 - L99 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 105 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-L105

Added lines #L101 - L105 were not covered by tests

run().catch(console.error)

Check warning on line 107 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#L107

Added line #L107 was not covered by tests
Loading