fabric-samples/asset-transfer-basic/rest-api-go/web/invoke.go
Basil K Y b2de360e1c
REST api added for asset transfer in Golang (#836)
* REST api added for asset transfer in Golang

Signed-off-by: Basil K Y <techiebasil@gmail.com>

* add go.sum to git

Signed-off-by: Basil K Y <techiebasil@gmail.com>

* fix golint error

Signed-off-by: Basil K Y <techiebasil@gmail.com>

Signed-off-by: Basil K Y <techiebasil@gmail.com>
2022-12-14 09:16:09 +01:00

40 lines
1.2 KiB
Go

package web
import (
"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"]
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())
}