Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions cmd/web3/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gochain/gochain/v4/accounts/abi"
"github.com/gochain/web3"
)

func ListContract(contractFile string) {
myabi, err := web3.GetABI(contractFile)
if err != nil {
Expand All @@ -28,7 +29,7 @@ func ListContract(contractFile string) {
func GetContractConst(ctx context.Context, rpcURL, contractAddress, contractFile, functionName string, parameters ...interface{}) ([]interface{}, error) {
client, err := web3.Dial(rpcURL)
if err != nil {
return nil, fmt.Errorf("Failed to connect to %q: %v", rpcURL, err)
return nil, fmt.Errorf("failed to connect to %q: %v", rpcURL, err)
}
defer client.Close()
myabi, err := web3.GetABI(contractFile)
Expand All @@ -37,14 +38,14 @@ func GetContractConst(ctx context.Context, rpcURL, contractAddress, contractFile
}
fn, ok := myabi.Methods[functionName]
if !ok {
return nil, fmt.Errorf("There is no such function: %v", functionName)
return nil, fmt.Errorf("there is no such function: %v", functionName)
}
if !fn.IsConstant() {
return nil, err
}
res, err := web3.CallConstantFunction(ctx, client, *myabi, contractAddress, functionName, parameters...)
if err != nil {
return nil, fmt.Errorf("Error calling constant function: %v", err)
return nil, fmt.Errorf("error calling constant function: %v", err)
}
return res, nil
}
Expand Down Expand Up @@ -73,7 +74,7 @@ func callContract(ctx context.Context, client web3.Client, privateKey, contractA
if m.IsConstant() {
res, err := web3.CallConstantFunction(ctx, client, *myabi, contractAddress, functionName, parameters...)
if err != nil {
fatalExit(fmt.Errorf("Error calling constant function: %v", err))
fatalExit(fmt.Errorf("error calling constant function: %v", err))
}
switch format {
case "json":
Expand Down Expand Up @@ -104,7 +105,7 @@ func callContract(ctx context.Context, client web3.Client, privateKey, contractA
tx, err = web3.CallTransactFunction(ctx, client, *myabi, contractAddress, privateKey, functionName, amount, gasPrice, gasLimit, parameters...)
}
if err != nil {
fatalExit(fmt.Errorf("Error calling contract: %v", err))
fatalExit(fmt.Errorf("error calling contract: %v", err))
}
fmt.Println("Transaction hash:", tx.Hash.Hex())
if !waitForReceipt {
Expand Down
14 changes: 7 additions & 7 deletions cmd/web3/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,31 +360,31 @@ func VerifyClaim(ctx context.Context, rpcURL, privateKey, registryAddress, filen

func readDIDDocument(ctx context.Context, rpcURL, registryAddress, id string) (*did.Document, error) {
if registryAddress == "" {
return nil, fmt.Errorf("Registry contract address required")
return nil, fmt.Errorf("registry contract address required")
}

d, err := did.Parse(id)
if err != nil {
return nil, fmt.Errorf("Invalid DID: %s", id)
return nil, fmt.Errorf("invalid DID: %s", id)
}

client, err := web3.Dial(rpcURL)
if err != nil {
return nil, fmt.Errorf("Failed to connect to %q: %v", rpcURL, err)
return nil, fmt.Errorf("failed to connect to %q: %v", rpcURL, err)
}
defer client.Close()

myabi, err := abi.JSON(strings.NewReader(assets.DIDRegistryABI))
if err != nil {
return nil, fmt.Errorf("Cannot initialize DIDRegistry ABI: %v", err)
return nil, fmt.Errorf("cannot initialize DIDRegistry ABI: %v", err)
}

var idBytes32 [32]byte
copy(idBytes32[:], d.ID)

result, err := web3.CallConstantFunction(ctx, client, myabi, registryAddress, "hash", idBytes32)
if err != nil {
return nil, fmt.Errorf("Cannot call the contract: %v", err)
return nil, fmt.Errorf("cannot call the contract: %v", err)
}
if len(result) != 1 {
log.Fatalf("Expected single result but got: %v", result)
Expand All @@ -393,13 +393,13 @@ func readDIDDocument(ctx context.Context, rpcURL, registryAddress, id string) (*
hash := result[0].(string)
resp, err := http.Get(fmt.Sprintf("https://ipfs.infura.io:5001/api/v0/cat?arg=%s", hash))
if err != nil {
return nil, fmt.Errorf("Unable to fetch DID document from IPFS: %s", err)
return nil, fmt.Errorf("unable to fetch DID document from IPFS: %s", err)
}
defer resp.Body.Close()

var doc did.Document
if err := json.NewDecoder(resp.Body).Decode(&doc); err != nil {
return nil, fmt.Errorf("Unable to decode DID document: %s", err)
return nil, fmt.Errorf("unable to decode DID document: %s", err)
}
return &doc, nil
}
20 changes: 10 additions & 10 deletions cmd/web3/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ func GenerateCode(ctx context.Context, c *cli.Context) {
case "objc":
lang = bind.LangObjC
default:
fatalExit(fmt.Errorf("Unsupported destination language: %v", lang))
fatalExit(fmt.Errorf("unsupported destination language: %v", lang))
}

abiFile := c.String("abi")

if abiFile == "" {
fatalExit(errors.New("Please set the ABI file name"))
fatalExit(errors.New("please set the ABI file name"))
}

abi, err := ioutil.ReadFile(abiFile)
if err != nil {
fatalExit(fmt.Errorf("Failed to read file %q: %v", abiFile, err))
fatalExit(fmt.Errorf("failed to read file %q: %v", abiFile, err))
}

abis := []string{string(abi)}
Expand All @@ -51,12 +51,12 @@ func GenerateCode(ctx context.Context, c *cli.Context) {

code, err := bind.Bind(types, abis, bins, nil, c.String("pkg"), lang, nil, nil)
if err != nil {
fatalExit(fmt.Errorf("Failed to generate ABI binding %q: %v", abiFile, err))
fatalExit(fmt.Errorf("failed to generate ABI binding %q: %v", abiFile, err))
}
outFile := c.String("out")

if err := ioutil.WriteFile(outFile, []byte(code), 0600); err != nil {
fatalExit(fmt.Errorf("Failed to write ABI binding %q: %v", abiFile, err))
fatalExit(fmt.Errorf("failed to write ABI binding %q: %v", abiFile, err))
}
fmt.Println("The generated code has been successfully written to", outFile, "file")
}
Expand All @@ -72,11 +72,11 @@ func getOpenZeppelinLib(ctx context.Context, version string) error {
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fatalExit(fmt.Errorf("Cloning finished with error: %v", err))
fatalExit(fmt.Errorf("cloning finished with error: %v", err))
}
err = os.RemoveAll("lib/oz/.git")
if err != nil {
fatalExit(fmt.Errorf("Cannot cleanup .git dir in lib/oz: %v", err))
fatalExit(fmt.Errorf("cannot cleanup .git dir in lib/oz: %v", err))
}
}
return nil
Expand Down Expand Up @@ -164,12 +164,12 @@ func GenerateContract(ctx context.Context, contractType string, c *cli.Context)
func processTemplate(openZeppelinVersion string, params interface{}, fileName, contractTemplate string) {
tmpl, err := template.New("contract").Parse(contractTemplate)
if err != nil {
fatalExit(fmt.Errorf("Cannot parse the template: %v", err))
fatalExit(fmt.Errorf("cannot parse the template: %v", err))
}
var buff bytes.Buffer
err = tmpl.Execute(&buff, params)
if err != nil {
fatalExit(fmt.Errorf("Cannot execute the template: %v", err))
fatalExit(fmt.Errorf("cannot execute the template: %v", err))
}
s := fmt.Sprintf("// @openzeppelin v%v\n", openZeppelinVersion)
s += buff.String()
Expand All @@ -178,7 +178,7 @@ func processTemplate(openZeppelinVersion string, params interface{}, fileName, c
func writeStringToFile(s, fileName string) {
err := ioutil.WriteFile(fileName+".sol", []byte(s), 0666)
if err != nil {
fatalExit(fmt.Errorf("Cannot create the file: %v", err))
fatalExit(fmt.Errorf("cannot create the file: %v", err))
}
fmt.Println("The sample contract has been successfully written to", fileName+".sol", "file")
}
Loading