|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/big" |
| 6 | + |
| 7 | + "github.com/eipcodelab/eip7702-go/pkg/batching" |
| 8 | + "github.com/eipcodelab/eip7702-go/pkg/eip7702" |
| 9 | + "github.com/ethereum/go-ethereum/common" |
| 10 | + "github.com/ethereum/go-ethereum/crypto" |
| 11 | +) |
| 12 | + |
| 13 | +const erc20ABI = `[ |
| 14 | + { |
| 15 | + "type": "function", |
| 16 | + "name": "transfer", |
| 17 | + "stateMutability": "nonpayable", |
| 18 | + "inputs": [ |
| 19 | + {"name": "to", "type": "address"}, |
| 20 | + {"name": "value", "type": "uint256"} |
| 21 | + ], |
| 22 | + "outputs": [{"name": "", "type": "bool"}] |
| 23 | + } |
| 24 | +]` |
| 25 | + |
| 26 | +func main() { |
| 27 | + key, err := crypto.HexToECDSA("4f3edf983ac63f7f8b7d0c4f76f2a5a70fadb53fcbf65f45d6fd5d77f07683ab") |
| 28 | + if err != nil { |
| 29 | + panic(err) |
| 30 | + } |
| 31 | + authority := crypto.PubkeyToAddress(key.PublicKey) |
| 32 | + |
| 33 | + chainID := big.NewInt(1) |
| 34 | + delegate := common.HexToAddress("0x1111111111111111111111111111111111111111") |
| 35 | + |
| 36 | + transferA, err := batching.EncodeFunctionCall( |
| 37 | + erc20ABI, |
| 38 | + "transfer", |
| 39 | + common.HexToAddress("0x2000000000000000000000000000000000000002"), |
| 40 | + big.NewInt(1_000_000_000_000_000_000), // 1 token with 18 decimals |
| 41 | + ) |
| 42 | + if err != nil { |
| 43 | + panic(err) |
| 44 | + } |
| 45 | + transferB, err := batching.EncodeFunctionCall( |
| 46 | + erc20ABI, |
| 47 | + "transfer", |
| 48 | + common.HexToAddress("0x3000000000000000000000000000000000000003"), |
| 49 | + big.NewInt(250_000_000_000_000_000), |
| 50 | + ) |
| 51 | + if err != nil { |
| 52 | + panic(err) |
| 53 | + } |
| 54 | + |
| 55 | + batchCalldata, err := batching.EncodeExecuteBatch([]batching.Call{ |
| 56 | + { |
| 57 | + Target: common.HexToAddress("0xA0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"), // example ERC20 |
| 58 | + Value: big.NewInt(0), |
| 59 | + Data: transferA, |
| 60 | + }, |
| 61 | + { |
| 62 | + Target: common.HexToAddress("0xA0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"), |
| 63 | + Value: big.NewInt(0), |
| 64 | + Data: transferB, |
| 65 | + }, |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + panic(err) |
| 69 | + } |
| 70 | + |
| 71 | + auth, err := eip7702.SignAuthorization(key, chainID, delegate, 0) |
| 72 | + if err != nil { |
| 73 | + panic(err) |
| 74 | + } |
| 75 | + |
| 76 | + setCodeTx := &eip7702.SetCodeTx{ |
| 77 | + ChainID: chainID, |
| 78 | + Nonce: 0, |
| 79 | + MaxPriorityFeePerGas: big.NewInt(2_000_000_000), |
| 80 | + MaxFeePerGas: big.NewInt(40_000_000_000), |
| 81 | + GasLimit: 300_000, |
| 82 | + Destination: authority, |
| 83 | + Value: big.NewInt(0), |
| 84 | + Data: batchCalldata, |
| 85 | + AuthorizationList: []eip7702.Authorization{auth}, |
| 86 | + // Outer tx signature is placeholder in this example. |
| 87 | + SignatureYParity: 0, |
| 88 | + SignatureR: big.NewInt(1), |
| 89 | + SignatureS: big.NewInt(1), |
| 90 | + } |
| 91 | + |
| 92 | + raw, err := setCodeTx.EncodeTypedTransaction() |
| 93 | + if err != nil { |
| 94 | + panic(err) |
| 95 | + } |
| 96 | + |
| 97 | + fmt.Println("== EIP-7702 Transaction Batching ==") |
| 98 | + fmt.Printf("Authority: %s\n", authority.Hex()) |
| 99 | + fmt.Printf("Delegation target: %s\n", delegate.Hex()) |
| 100 | + fmt.Printf("Batch calldata: 0x%x\n", batchCalldata) |
| 101 | + fmt.Printf("Typed tx (0x04...): 0x%x\n", raw) |
| 102 | + fmt.Println("Note: sign the outer set-code transaction before broadcasting.") |
| 103 | +} |
0 commit comments