Skip to content

Commit

Permalink
Merge pull request #115 from G7DAO/feat/calldata
Browse files Browse the repository at this point in the history
Feat: Add calldata flag to seer evm generator
  • Loading branch information
karacurt authored Nov 25, 2024
2 parents d7c2ef4 + 74e8e18 commit ced5f98
Show file tree
Hide file tree
Showing 3 changed files with 449 additions and 337 deletions.
127 changes: 74 additions & 53 deletions evm/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,8 @@ func {{.DeployHandler.HandlerName}}() *cobra.Command {
var salt [32]byte
var predictAddress bool
var safeNonce *big.Int
var calldata bool
{{range .DeployHandler.MethodArgs}}
var {{.CLIVar}} {{.CLIType}}
{{if (ne .CLIRawVar .CLIVar)}}var {{.CLIRawVar}} {{.CLIRawType}}{{end}}
Expand All @@ -1318,12 +1320,15 @@ func {{.DeployHandler.HandlerName}}() *cobra.Command {
Use: "deploy",
Short: "Deploy a new {{.StructName}} contract",
PreRunE: func(cmd *cobra.Command, args []string) error {
if keyfile == "" {
return fmt.Errorf("--keystore not specified (this should be a path to an Ethereum account keystore file)")
}
if rpc == "" {
return fmt.Errorf("--rpc not specified (this should be a URL to an Ethereum JSONRPC API)")
if !calldata {
if keyfile == "" {
return fmt.Errorf("--keystore not specified (this should be a path to an Ethereum account keystore file)")
}
if rpc == "" {
return fmt.Errorf("--rpc not specified (this should be a URL to an Ethereum JSONRPC API)")
}
}
if safeAddress != "" {
Expand Down Expand Up @@ -1393,6 +1398,22 @@ func {{.DeployHandler.HandlerName}}() *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// Generate deploy bytecode with constructor arguments
deployCalldata, err := generate{{.StructName}}DeployBytecode(
{{- range .DeployHandler.MethodArgs}}
{{.CLIVar}},
{{- end}}
)
if err != nil {
return fmt.Errorf("failed to generate deploy bytecode: %v", err)
}
if calldata {
deployCalldataHex := hex.EncodeToString(deployCalldata)
cmd.Println(deployCalldataHex)
return nil
}
client, clientErr := NewClient(rpc)
if clientErr != nil {
return clientErr
Expand All @@ -1418,16 +1439,6 @@ func {{.DeployHandler.HandlerName}}() *cobra.Command {
SetTransactionParametersFromArgs(transactionOpts, nonce, value, gasPrice, maxFeePerGas, maxPriorityFeePerGas, gasLimit, simulate)
if safeAddress != "" {
// Generate deploy bytecode with constructor arguments
deployBytecode, err := generate{{.StructName}}DeployBytecode(
{{- range .DeployHandler.MethodArgs}}
{{.CLIVar}},
{{- end}}
)
if err != nil {
return fmt.Errorf("failed to generate deploy bytecode: %v", err)
}
// Create Safe proposal for deployment
value := transactionOpts.Value
if value == nil {
Expand All @@ -1440,15 +1451,15 @@ func {{.DeployHandler.HandlerName}}() *cobra.Command {
if safeOperationType == 0 {
from = common.HexToAddress(safeCreateCall)
}
deploymentAddress, err := PredictDeploymentAddressSafe(from, salt, deployBytecode)
deploymentAddress, err := PredictDeploymentAddressSafe(from, salt, deployCalldata)
if err != nil {
return fmt.Errorf("failed to predict deployment address: %v", err)
}
fmt.Println("Predicted deployment address:", deploymentAddress.Hex())
return nil
} else {
fmt.Println("Creating Safe proposal...")
err = DeployWithSafe(client, key, common.HexToAddress(safeAddress), common.HexToAddress(safeCreateCall), value, safeApi, deployBytecode, SafeOperationType(safeOperationType), salt, safeNonce)
err = DeployWithSafe(client, key, common.HexToAddress(safeAddress), common.HexToAddress(safeCreateCall), value, safeApi, deployCalldata, SafeOperationType(safeOperationType), salt, safeNonce)
if err != nil {
return fmt.Errorf("failed to create Safe proposal: %v", err)
}
Expand All @@ -1468,7 +1479,6 @@ func {{.DeployHandler.HandlerName}}() *cobra.Command {
return deploymentErr
}
cmd.Printf("Transaction hash: %s\nContract address: %s\n", deploymentTransaction.Hash().Hex(), address.Hex())
if transactionOpts.NoSend {
estimationMessage := ethereum.CallMsg{
Expand Down Expand Up @@ -1517,6 +1527,7 @@ func {{.DeployHandler.HandlerName}}() *cobra.Command {
cmd.Flags().StringVar(&safeSaltRaw, "safe-salt", "", "Salt to use for the Safe transaction")
cmd.Flags().BoolVar(&predictAddress, "safe-predict-address", false, "Predict the deployment address (only works for Safe transactions)")
cmd.Flags().StringVar(&safeNonceRaw, "safe-nonce", "", "Safe nonce overrider for the transaction (optional)")
cmd.Flags().BoolVar(&calldata, "calldata", false, "Set this flag if want to return the calldata instead of sending the transaction")
{{range .DeployHandler.MethodArgs}}
cmd.Flags().{{.Flag}}
Expand Down Expand Up @@ -1654,6 +1665,7 @@ func {{.HandlerName}}() *cobra.Command {
var safeAddress, safeApi string
var safeOperationType uint8
var safeNonce *big.Int
var calldata bool
{{range .MethodArgs}}
var {{.CLIVar}} {{.CLIType}}
Expand All @@ -1664,19 +1676,21 @@ func {{.HandlerName}}() *cobra.Command {
Use: "{{(KebabCase .MethodName)}}",
Short: "Execute the {{.MethodName}} method on a {{$structName}} contract",
PreRunE: func(cmd *cobra.Command, args []string) error {
if contractAddressRaw == "" {
return fmt.Errorf("--contract not specified")
} else if !common.IsHexAddress(contractAddressRaw) {
return fmt.Errorf("--contract is not a valid Ethereum address")
}
contractAddress = common.HexToAddress(contractAddressRaw)
if !calldata {
if contractAddressRaw == "" {
return fmt.Errorf("--contract not specified")
} else if !common.IsHexAddress(contractAddressRaw) {
return fmt.Errorf("--contract is not a valid Ethereum address")
}
contractAddress = common.HexToAddress(contractAddressRaw)
if keyfile == "" {
return fmt.Errorf("--keystore not specified (this should be a path to an Ethereum account keystore file)")
}
if keyfile == "" {
return fmt.Errorf("--keystore not specified (this should be a path to an Ethereum account keystore file)")
}
if rpc == "" {
return fmt.Errorf("--rpc not specified (this should be a URL to an Ethereum JSONRPC API)")
if rpc == "" {
return fmt.Errorf("--rpc not specified (this should be a URL to an Ethereum JSONRPC API)")
}
}
if safeAddress != "" {
Expand Down Expand Up @@ -1720,6 +1734,34 @@ func {{.HandlerName}}() *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
abi, err := {{$structName}}MetaData.GetAbi()
if err != nil {
return fmt.Errorf("failed to get ABI: %v", err)
}
// Generate transaction data (override method name if safe function is specified)
methodName := "{{ToLowerCamel .MethodName}}"
if safeFunction != "" {
methodName = safeFunction
}
txCalldata, err := abi.Pack(
methodName,
{{- range .MethodArgs}}
{{.CLIVar}},
{{- end}}
)
if err != nil {
return err
}
if calldata {
txCalldataHex := hex.EncodeToString(txCalldata)
cmd.Println(txCalldataHex)
return nil
}
client, clientErr := NewClient(rpc)
if clientErr != nil {
return clientErr
Expand Down Expand Up @@ -1755,35 +1797,13 @@ func {{.HandlerName}}() *cobra.Command {
}
if safeAddress != "" {
abi, err := {{$structName}}MetaData.GetAbi()
if err != nil {
return fmt.Errorf("failed to get ABI: %v", err)
}
// Generate transaction data (override method name if safe function is specified)
methodName := "{{ToLowerCamel .MethodName}}"
if safeFunction != "" {
methodName = safeFunction
}
transaction, err := abi.Pack(
methodName,
{{- range .MethodArgs}}
{{.CLIVar}},
{{- end}}
)
if err != nil {
return err
}
// Create Safe proposal for transaction
value := transactionOpts.Value
if value == nil {
value = big.NewInt(0)
}
err = CreateSafeProposal(client, key, common.HexToAddress(safeAddress), contractAddress, transaction, value, safeApi, SafeOperationType(safeOperationType), safeNonce)
err = CreateSafeProposal(client, key, common.HexToAddress(safeAddress), contractAddress, txCalldata, value, safeApi, SafeOperationType(safeOperationType), safeNonce)
if err != nil {
return fmt.Errorf("failed to create Safe proposal: %v", err)
}
Expand All @@ -1800,7 +1820,7 @@ func {{.HandlerName}}() *cobra.Command {
return err
}
cmd.Printf("Transaction hash: %s\n", transaction.Hash().Hex())
cmd.Printf("Transaction hash: %s\n", transaction.Hash().Hex())
if transactionOpts.NoSend {
estimationMessage := ethereum.CallMsg{
From: transactionOpts.From,
Expand Down Expand Up @@ -1848,6 +1868,7 @@ func {{.HandlerName}}() *cobra.Command {
cmd.Flags().Uint8Var(&safeOperationType, "safe-operation", 0, "Safe operation type: 0 (Call) or 1 (DelegateCall)")
cmd.Flags().StringVar(&safeFunction, "safe-function", "", "Safe function overrider to use for the transaction (optional)")
cmd.Flags().StringVar(&safeNonceRaw, "safe-nonce", "", "Safe nonce overrider for the transaction (optional)")
cmd.Flags().BoolVar(&calldata, "calldata", false, "Set this flag if want to return the calldata instead of sending the transaction")
{{range .MethodArgs}}
cmd.Flags().{{.Flag}}
Expand Down
Loading

0 comments on commit ced5f98

Please sign in to comment.