-
Notifications
You must be signed in to change notification settings - Fork 75
Add storage initialization + read bytecode from file to wrap-contract #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.