-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgetcode.go
45 lines (37 loc) · 1.12 KB
/
getcode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package cmd
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/spf13/cobra"
"log"
)
var getCodeCmd = &cobra.Command{
Use: "code <contract-address>",
Short: "Get runtime bytecode of a contract on the blockchain",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("requires contract-address")
}
if len(args) > 1 {
return fmt.Errorf("multiple contract-address is not supported")
}
if !isValidEthAddress(args[0]) {
return fmt.Errorf("%v is not a valid eth address", args[0])
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
contractAddress := args[0]
InitGlobalClient(globalOptNodeUrl)
ctx := context.Background()
byteCode, err := globalClient.EthClient.CodeAt(ctx, common.HexToAddress(contractAddress), nil)
checkErr(err)
if len(byteCode) == 0 {
log.Printf("no runtime bytecode found for %v, it is not a deployed contract", contractAddress)
return
}
fmt.Printf("runtime bytecode of contract %v is %v\n", contractAddress, hexutil.Encode(byteCode))
},
}