-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #455 from 0xPolygon/cmd/wrapcontract
Add storage initialization + read bytecode from file to wrap-contract
- Loading branch information
Showing
5 changed files
with
133 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,96 @@ | ||
package wrapcontract | ||
|
||
import ( | ||
"fmt" | ||
_ "embed" | ||
"github.com/spf13/cobra" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
"encoding/json" | ||
|
||
"github.com/0xPolygon/polygon-cli/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
//go:embed usage.md | ||
usage string | ||
usage string | ||
jsonStorage *string | ||
) | ||
|
||
var WrapContractCmd = &cobra.Command{ | ||
Use: "wrap-contract bytecode", | ||
Use: "wrap-contract bytecode|file", | ||
Aliases: []string{"wrapcontract", "wrapContract"}, | ||
Short: "Wrap deployed bytecode into create bytecode.", | ||
Long: usage, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
deployed_bytecode := args[0] | ||
storage_bytecode := "" | ||
create_bytecode := util.WrapDeployedCode(deployed_bytecode, storage_bytecode) | ||
fmt.Println(create_bytecode) | ||
deployedBytecode, err := getInputData(args) | ||
if err != nil { | ||
cmd.PrintErrf("There was an error reading input for wrapping contract: %s", err.Error()) | ||
return err | ||
} | ||
storageBytecode, err := getStorageBytecode() | ||
if err != nil { | ||
cmd.PrintErrf("There was an error reading storage map: %s", err.Error()) | ||
} | ||
createBytecode := util.WrapDeployedCode(deployedBytecode, storageBytecode) | ||
fmt.Println(createBytecode) | ||
return nil | ||
}, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return fmt.Errorf("expected exactly one argument: bytecode") | ||
if len(args) > 1 { | ||
return fmt.Errorf("expected at most one argument: bytecode") | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
flagSet := WrapContractCmd.PersistentFlags() | ||
jsonStorage = flagSet.String("storage", "", "Provide storage slots in json format k:v") | ||
} | ||
|
||
func getInputData(args []string) (string, error) { | ||
var deployedBytecode string | ||
if len(args) == 0 { | ||
deployedBytecodeBytes, err := io.ReadAll(os.Stdin) | ||
if err != nil { | ||
return "", err | ||
} | ||
deployedBytecode = string(deployedBytecodeBytes) | ||
} else { | ||
deployedBytecodeOrFile := args[0] | ||
// Try to open the param as a file, otherwise treat it as bytecode | ||
deployedBytecodeBytes, err := os.ReadFile(deployedBytecodeOrFile) | ||
if err != nil { | ||
deployedBytecode = deployedBytecodeOrFile | ||
} else { | ||
deployedBytecode = string(deployedBytecodeBytes) | ||
} | ||
} | ||
|
||
return strings.TrimSpace(deployedBytecode), nil | ||
} | ||
|
||
func getStorageBytecode() (string, error) { | ||
var storageBytecode string = "" | ||
|
||
if jsonStorage != nil && *jsonStorage != "" { | ||
var storage map[string]string | ||
err := json.Unmarshal([]byte(*jsonStorage), &storage) | ||
if err != nil { | ||
return storageBytecode, err | ||
} | ||
for k, v := range storage { | ||
slot := util.GetHexString(k) | ||
value := util.GetHexString(v) | ||
sLen := len(slot) / 2 | ||
vLen := len(value) / 2 | ||
sPushCode := 0x5f + sLen | ||
vPushCode := 0x5f + vLen | ||
storageBytecode += fmt.Sprintf("%02x%s%02x%s55", vPushCode, value, sPushCode, slot) | ||
} | ||
} | ||
|
||
return storageBytecode, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters