mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 07:25:10 +00:00
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package web
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/hyperledger/fabric-gateway/pkg/client"
|
|
)
|
|
|
|
// Invoke handles chaincode invoke requests.
|
|
func (setup *OrgSetup) Invoke(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Println("Received Invoke request")
|
|
if err := r.ParseForm(); err != nil {
|
|
fmt.Fprintf(w, "ParseForm() err: %s", err)
|
|
return
|
|
}
|
|
chainCodeName := r.FormValue("chaincodeid")
|
|
channelID := r.FormValue("channelid")
|
|
function := r.FormValue("function")
|
|
args := r.Form["args"]
|
|
|
|
if len(args) == 1 {
|
|
var parsed []string
|
|
err := json.Unmarshal([]byte(args[0]), &parsed)
|
|
if err == nil {
|
|
args = parsed
|
|
} else {
|
|
fmt.Printf("Warning: failed to parse args as JSON: %s\n", err)
|
|
}
|
|
}
|
|
|
|
if len(args) == 0 {
|
|
fmt.Fprintf(w, "Error: args is empty")
|
|
return
|
|
}
|
|
|
|
fmt.Printf("channel: %s, chaincode: %s, function: %s, args: %s\n", channelID, chainCodeName, function, args)
|
|
network := setup.Gateway.GetNetwork(channelID)
|
|
contract := network.GetContract(chainCodeName)
|
|
txn_proposal, err := contract.NewProposal(function, client.WithArguments(args...))
|
|
if err != nil {
|
|
fmt.Fprintf(w, "Error creating txn proposal: %s", err)
|
|
return
|
|
}
|
|
txn_endorsed, err := txn_proposal.Endorse()
|
|
if err != nil {
|
|
fmt.Fprintf(w, "Error endorsing txn: %s", err)
|
|
return
|
|
}
|
|
txn_committed, err := txn_endorsed.Submit()
|
|
if err != nil {
|
|
fmt.Fprintf(w, "Error submitting transaction: %s", err)
|
|
return
|
|
}
|
|
fmt.Fprintf(w, "Transaction ID : %s Response: %s", txn_committed.TransactionID(), txn_endorsed.Result())
|
|
}
|