|
| 1 | +package batching |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + |
| 10 | + "github.com/ethereum/go-ethereum/accounts/abi" |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | +) |
| 13 | + |
| 14 | +const executeBatchABIJSON = `[ |
| 15 | + { |
| 16 | + "type": "function", |
| 17 | + "name": "executeBatch", |
| 18 | + "stateMutability": "payable", |
| 19 | + "inputs": [ |
| 20 | + { |
| 21 | + "name": "calls", |
| 22 | + "type": "tuple[]", |
| 23 | + "components": [ |
| 24 | + {"name": "target", "type": "address"}, |
| 25 | + {"name": "value", "type": "uint256"}, |
| 26 | + {"name": "data", "type": "bytes"} |
| 27 | + ] |
| 28 | + } |
| 29 | + ], |
| 30 | + "outputs": [] |
| 31 | + } |
| 32 | +]` |
| 33 | + |
| 34 | +type executeCall struct { |
| 35 | + Target common.Address `abi:"target"` |
| 36 | + Value *big.Int `abi:"value"` |
| 37 | + Data []byte `abi:"data"` |
| 38 | +} |
| 39 | + |
| 40 | +// Call represents one low-level call executed by a batch delegate contract. |
| 41 | +type Call struct { |
| 42 | + Target common.Address |
| 43 | + Value *big.Int |
| 44 | + Data []byte |
| 45 | +} |
| 46 | + |
| 47 | +var ( |
| 48 | + onceBatchABI sync.Once |
| 49 | + batchABI abi.ABI |
| 50 | + batchABIErr error |
| 51 | +) |
| 52 | + |
| 53 | +func getExecuteBatchABI() (abi.ABI, error) { |
| 54 | + onceBatchABI.Do(func() { |
| 55 | + batchABI, batchABIErr = abi.JSON(strings.NewReader(executeBatchABIJSON)) |
| 56 | + }) |
| 57 | + return batchABI, batchABIErr |
| 58 | +} |
| 59 | + |
| 60 | +// EncodeExecuteBatch encodes calldata for executeBatch((address,uint256,bytes)[] calls). |
| 61 | +func EncodeExecuteBatch(calls []Call) ([]byte, error) { |
| 62 | + if len(calls) == 0 { |
| 63 | + return nil, errors.New("calls must not be empty") |
| 64 | + } |
| 65 | + execCalls := make([]executeCall, len(calls)) |
| 66 | + for i, c := range calls { |
| 67 | + value := c.Value |
| 68 | + if value == nil { |
| 69 | + value = big.NewInt(0) |
| 70 | + } |
| 71 | + execCalls[i] = executeCall{ |
| 72 | + Target: c.Target, |
| 73 | + Value: value, |
| 74 | + Data: c.Data, |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + parsedABI, err := getExecuteBatchABI() |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("parse executeBatch ABI: %w", err) |
| 81 | + } |
| 82 | + data, err := parsedABI.Pack("executeBatch", execCalls) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("pack executeBatch calldata: %w", err) |
| 85 | + } |
| 86 | + return data, nil |
| 87 | +} |
| 88 | + |
| 89 | +// EncodeFunctionCall is a small helper for example programs. |
| 90 | +func EncodeFunctionCall(abiJSON string, method string, args ...any) ([]byte, error) { |
| 91 | + parsedABI, err := abi.JSON(strings.NewReader(abiJSON)) |
| 92 | + if err != nil { |
| 93 | + return nil, fmt.Errorf("parse ABI: %w", err) |
| 94 | + } |
| 95 | + out, err := parsedABI.Pack(method, args...) |
| 96 | + if err != nil { |
| 97 | + return nil, fmt.Errorf("pack %s: %w", method, err) |
| 98 | + } |
| 99 | + return out, nil |
| 100 | +} |
0 commit comments