Skip to content

Commit

Permalink
Merge pull request #10443 from filecoin-project/steb/evm-bytecode-cmd
Browse files Browse the repository at this point in the history
feat: cli: Add an EVM command to fetch a contract's bytecode
  • Loading branch information
arajasek authored Mar 10, 2023
2 parents 58900a7 + 7a2eb86 commit 816ca59
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
49 changes: 49 additions & 0 deletions cli/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var EvmCmd = &cli.Command{
EvmGetInfoCmd,
EvmCallSimulateCmd,
EvmGetContractAddress,
EvmGetBytecode,
},
}

Expand Down Expand Up @@ -486,3 +487,51 @@ func ethAddrFromFilecoinAddress(ctx context.Context, addr address.Address, fnapi

return ethAddr, faddr, nil
}

var EvmGetBytecode = &cli.Command{
Name: "bytecode",
Usage: "Write the bytecode of a smart contract to a file",
ArgsUsage: "[contract-address] [file-name]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "bin",
Usage: "write the bytecode as raw binary and don't hex-encode",
},
},
Action: func(cctx *cli.Context) error {

if cctx.NArg() != 2 {
return IncorrectNumArgs(cctx)
}

contractAddr, err := ethtypes.ParseEthAddress(cctx.Args().Get(0))
if err != nil {
return err
}

fileName := cctx.Args().Get(1)

api, closer, err := GetFullNodeAPIV1(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)

code, err := api.EthGetCode(ctx, contractAddr, "latest")
if err != nil {
return err
}
if !cctx.Bool("bin") {
newCode := make([]byte, hex.EncodedLen(len(code)))
hex.Encode(newCode, code)
code = newCode
}
if err := os.WriteFile(fileName, code, 0o666); err != nil {
return xerrors.Errorf("failed to write bytecode to file %s: %w", fileName, err)
}

fmt.Printf("Code for %s written to %s\n", contractAddr, fileName)
return nil
},
}
14 changes: 14 additions & 0 deletions documentation/en/cli-lotus.md
Original file line number Diff line number Diff line change
Expand Up @@ -2647,6 +2647,7 @@ COMMANDS:
stat Print eth/filecoin addrs and code cid
call Simulate an eth contract call
contract-address Generate contract address from smart contract code
bytecode Write the bytecode of a smart contract to a file
help, h Shows a list of commands or help for one command
OPTIONS:
Expand Down Expand Up @@ -2721,6 +2722,19 @@ OPTIONS:
```

### lotus evm bytecode
```
NAME:
lotus evm bytecode - Write the bytecode of a smart contract to a file
USAGE:
lotus evm bytecode [command options] [contract-address] [file-name]
OPTIONS:
--bin write the bytecode as raw binary and don't hex-encode (default: false)
```

## lotus net
```
NAME:
Expand Down

0 comments on commit 816ca59

Please sign in to comment.